Skip to content

Commit

Permalink
Add resource types in outputs and parameters
Browse files Browse the repository at this point in the history
Fixes: #2246

This change implements functionality for declaring strongly type
parameters and outputs using resource types.

Example:

```
param storage resource 'Microsoft.Storage/storageAccounts@2020-01-01'
```

This declares a parameter that can be interacted with as-if it were an
'existing' resource type declaration of the provided type.

In addition you can do the same with outputs:

```
output out resource 'Microsoft.Storage/storageAccounts@2020-01-01' = foo
```

or using type inference (outputs):

```
output out resource = foo
```

These features together allow you to pass resources across module
boundaries, and as command line parameters (using the resource ID).

---

This PR implements #2246 with a few caveats that I discussed offline
with one of the maintainers.

1. It does not include the 'any resource type' that's outlined in the
   proposal. It's not clear how that part of the proposal will work
   in the future with extensibility.
2. It does not support extensibility resources currently. To do this we
   need to define the semantics of how an extensibility resource can
   be serialized (what's the equivalent of `.id`). This is explicitly
   blocked in the first cut and can be added as extensibility is
   designed.
3. Parameter resources cannot be used with the `.parent` or `.scope`
   properties because it would allow you to bypass scope validation
   easily. The set of cases that we could actually provide validation
   for these use cases are really limited.
  • Loading branch information
rynowak committed Feb 4, 2022
1 parent df694ef commit 9c41db6
Show file tree
Hide file tree
Showing 123 changed files with 2,492 additions and 799 deletions.
9 changes: 6 additions & 3 deletions docs/grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ targetScopeDecl -> "targetScope" "=" expression
importDecl -> decorator* "import" IDENTIFIER(providerName) "as" IDENTIFIER(aliasName) object? NL
parameterDecl -> decorator* "parameter" IDENTIFIER(name) IDENTIFIER(type) parameterDefaultValue? NL
parameterDecl ->
decorator* "parameter" IDENTIFIER(name) IDENTIFIER(type) parameterDefaultValue? NL |
decorator* "parameter" IDENTIFIER(name) "resource" interpString(type) parameterDefaultValue? NL |
parameterDefaultValue -> "=" expression
variableDecl -> decorator* "variable" IDENTIFIER(name) "=" expression NL
Expand All @@ -25,8 +27,9 @@ resourceDecl -> decorator* "resource" IDENTIFIER(name) interpString(type) "exist
moduleDecl -> decorator* "module" IDENTIFIER(name) interpString(type) "=" (ifCondition | object | forExpression) NL
outputDecl -> decorator* "output" IDENTIFIER(name) IDENTIFIER(type) "=" expression NL
outputDecl ->
decorator* "output" IDENTIFIER(name) IDENTIFIER(type) "=" expression NL
decorator* "output" IDENTIFIER(name) "resource" interpString(type) "=" expression NL
NL -> ("\n" | "\r")+
decorator -> "@" decoratorExpression NL
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Cli.IntegrationTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected static IEnumerable<string> GetAllDiagnostics(string bicepFilePath, ICo
var dispatcher = new ModuleDispatcher(new DefaultModuleRegistryProvider(BicepTestConstants.FileResolver, clientFactory, templateSpecRepositoryFactory, BicepTestConstants.Features));
var configuration = BicepTestConstants.BuiltInConfiguration;
var sourceFileGrouping = SourceFileGroupingBuilder.Build(BicepTestConstants.FileResolver, dispatcher, new Workspace(), PathHelper.FilePathToFileUrl(bicepFilePath), configuration);
var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), sourceFileGrouping, configuration, BicepTestConstants.LinterAnalyzer);
var compilation = new Compilation(BicepTestConstants.Features, TestTypeHelper.CreateEmptyProvider(), sourceFileGrouping, configuration, BicepTestConstants.LinterAnalyzer);

var output = new List<string>();
foreach (var (bicepFile, diagnostics) in compilation.GetAllDiagnosticsByBicepFile())
Expand Down
30 changes: 18 additions & 12 deletions src/Bicep.Cli/CliResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Bicep.Cli/CliResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ If you would like to report any issues or inaccurate conversions, please see htt
<data name="SymbolicNamesDisclaimerMessage" xml:space="preserve">
<value>WARNING: Symbolic name support in ARM is experimental, and should be enabled for testing purposes only. Do not enable this setting for any production usage, or you may be unexpectedly broken at any time!</value>
</data>
<data name="ResourceTypesDisclaimerMessage" xml:space="preserve">
<value>WARNING: Resource-typed parameters and outputs in ARM are experimental, and should be enabled for testing purposes only. Do not enable this setting for any production usage, or you may be unexpectedly broken at any time!</value>
</data>
<data name="DecompilationFailedFormat" xml:space="preserve">
<value>{0}: Decompilation failed with fatal error "{1}"</value>
<comment>{0} json path {1} message string</comment>
Expand Down
5 changes: 5 additions & 0 deletions src/Bicep.Cli/Commands/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public async Task<int> RunAsync(BuildArguments args)
logger.LogWarning(CliResources.SymbolicNamesDisclaimerMessage);
}

if (invocationContext.Features.ResourceTypedParamsAndOutputsEnabled)
{
logger.LogWarning(CliResources.ResourceTypesDisclaimerMessage);
}

var compilation = await compilationService.CompileAsync(inputPath, args.NoRestore);

if (diagnosticLogger.ErrorCount < 1)
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Cli/Services/CompilationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task<Compilation> CompileAsync(string inputPath, bool skipRestore)
}
}

var compilation = new Compilation(this.invocationContext.NamespaceProvider, sourceFileGrouping, configuration, new LinterAnalyzer(configuration));
var compilation = new Compilation(this.invocationContext.Features, this.invocationContext.NamespaceProvider, sourceFileGrouping, configuration, new LinterAnalyzer(configuration));
LogDiagnostics(compilation);

return compilation;
Expand Down
4 changes: 2 additions & 2 deletions src/Bicep.Core.IntegrationTests/DecoratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ param inputb string
",
};

var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SourceFileGroupingFactory.CreateForFiles(files, mainUri, BicepTestConstants.FileResolver, BicepTestConstants.BuiltInConfiguration), BicepTestConstants.BuiltInConfiguration, BicepTestConstants.LinterAnalyzer);
var compilation = new Compilation(BicepTestConstants.Features, TestTypeHelper.CreateEmptyProvider(), SourceFileGroupingFactory.CreateForFiles(files, mainUri, BicepTestConstants.FileResolver, BicepTestConstants.BuiltInConfiguration), BicepTestConstants.BuiltInConfiguration, BicepTestConstants.LinterAnalyzer);
var diagnosticsByFile = compilation.GetAllDiagnosticsByBicepFile().ToDictionary(kvp => kvp.Key.FileUri, kvp => kvp.Value);
var success = diagnosticsByFile.Values.SelectMany(x => x).All(d => d.Level != DiagnosticLevel.Error);

Expand Down Expand Up @@ -161,7 +161,7 @@ param inputb string
",
};

var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SourceFileGroupingFactory.CreateForFiles(files, mainUri, BicepTestConstants.FileResolver, BicepTestConstants.BuiltInConfiguration), BicepTestConstants.BuiltInConfiguration, BicepTestConstants.LinterAnalyzer);
var compilation = new Compilation(BicepTestConstants.Features, TestTypeHelper.CreateEmptyProvider(), SourceFileGroupingFactory.CreateForFiles(files, mainUri, BicepTestConstants.FileResolver, BicepTestConstants.BuiltInConfiguration), BicepTestConstants.BuiltInConfiguration, BicepTestConstants.LinterAnalyzer);
var diagnosticsByFile = compilation.GetAllDiagnosticsByBicepFile().ToDictionary(kvp => kvp.Key.FileUri, kvp => kvp.Value);
var success = diagnosticsByFile.Values.SelectMany(x => x).All(d => d.Level != DiagnosticLevel.Error);

Expand Down
4 changes: 2 additions & 2 deletions src/Bicep.Core.IntegrationTests/Emit/TemplateEmitterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public void TemplateEmitter_should_not_dispose_text_writer()

private EmitResult EmitTemplate(SourceFileGrouping sourceFileGrouping, EmitterSettings emitterSettings, string filePath)
{
var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), sourceFileGrouping, BicepTestConstants.BuiltInConfiguration, BicepTestConstants.LinterAnalyzer);
var compilation = new Compilation(BicepTestConstants.Features, TestTypeHelper.CreateEmptyProvider(), sourceFileGrouping, BicepTestConstants.BuiltInConfiguration, BicepTestConstants.LinterAnalyzer);
var emitter = new TemplateEmitter(compilation.GetEntrypointSemanticModel(), emitterSettings);

using var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
Expand All @@ -229,7 +229,7 @@ private EmitResult EmitTemplate(SourceFileGrouping sourceFileGrouping, EmitterSe

private EmitResult EmitTemplate(SourceFileGrouping sourceFileGrouping, EmitterSettings emitterSettings, MemoryStream memoryStream)
{
var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), sourceFileGrouping, BicepTestConstants.BuiltInConfiguration, BicepTestConstants.LinterAnalyzer);
var compilation = new Compilation(BicepTestConstants.Features, TestTypeHelper.CreateEmptyProvider(), sourceFileGrouping, BicepTestConstants.BuiltInConfiguration, BicepTestConstants.LinterAnalyzer);
var emitter = new TemplateEmitter(compilation.GetEntrypointSemanticModel(), emitterSettings);

TextWriter tw = new StreamWriter(memoryStream);
Expand Down
Loading

0 comments on commit 9c41db6

Please sign in to comment.