Skip to content

Commit

Permalink
allow using none statements in bicepparam files at build-params c…
Browse files Browse the repository at this point in the history
…ommand (#15107)

When
+ there are multiple projects with a very similar infrastructure needs
(usually encountered in software shops)
+ there are multiple layers in the same project with very similar
infrastructure needs (usually encountered in multi-tenant softwares)

There should be multiple `.bicep` files (per project, per layer, etc.)
and multiple `.bicepparam` files pairing with `.bicep` files (with
`using {filename}.bicep` statements in `.bicepparam` files)

When `az deployment group create --resource-group $rgname --name $rgname
--template-file ./main.bicep --parameters ./main.bicepparam` command
executed, `az cli` downstreams the parameters to `bicep build-params`
command.

Since the `.bicep` and `.bicepparam` files are paired with the `using`
statement in `.bicepparam` file, it's not possible to re-use the same
`.bicepparam` file with multiple deployments.

This PR relaxes the check in the `build-params` command that requires
the `using` statement in the `.bicepparam` file points to the same
`.bicep` file as the `build-params` command arguments

So, if the `.bicepparam` file has a `using none` statement, it'll be
possible to use the same `.bicepparam` file in multiple deployments.

---

## Testing the PR

> git worktree add /tmp/bicep-allow-using-none
polatengin/allow-using-none-statements-build-params

```bicep
// main.bicep
param bar string
```

```bicep
// main.bicepparam
using none
param bar = 'foo'
```

When you run the following command;

```bash
dotnet run --project src/Bicep.Cli/Bicep.Cli.csproj -- build-params ./main.bicepparam --bicep-file ./main.bicep --stdout
```

It fails with the following error message; `Bicep file ./main.bicep
provided with --bicep-file can only be used if the Bicep parameters
"using" declaration refers to a Bicep file on disk.`

With the changes in this PR, same command runs successfully and builds
the `main.bicepparam` file

### BuildParamsCommandTests

I also added a test
(https://github.com/Azure/bicep/pull/15107/files#diff-30f70729cbd5bcbfb69d4edd8ff3fa9bee94690e92f5e3e86ac9a7eb98b3f550R431)
to ensure the `build-params` command successfully builds given
`.bicepparam` file when there is a `using none` statement instead of a
`using {filename}.bicep` statement in the `.bicepparam` file.

---

###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/15107)
  • Loading branch information
polatengin authored Nov 9, 2024
1 parent 99579a9 commit edae013
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
26 changes: 26 additions & 0 deletions src/Bicep.Cli.IntegrationTests/BuildParamsCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,32 @@ public async Task Build_params_returns_intuitive_error_if_invoked_with_bicep_fil
result.Should().Fail().And.HaveStderrMatch($"Bicep file * provided with --bicep-file can only be used if the Bicep parameters \"using\" declaration refers to a Bicep file on disk.*");
}

[TestMethod]
[TestCategory(BaselineHelper.BaselineTestCategory)]
public async Task Build_params_works_with_using_none()
{
var outputPath = FileHelper.GetUniqueTestOutputPath(TestContext);

var bicepFile = FileHelper.SaveResultFile(TestContext, "main.bicep", @"
param unusedParam int
", outputPath);

var inputFile = FileHelper.SaveResultFile(TestContext, "main.bicepparam", @"
using none
param unusedParam = 3
", outputPath);

var (output, error, result) = await Bicep(["build-params", inputFile, "--bicep-file", bicepFile]);

var expectedOutputFile = FileHelper.GetResultFilePath(TestContext, "main.json", outputPath);

File.Exists(expectedOutputFile).Should().BeTrue();
output.Should().BeEmpty();
error.Should().BeEmpty();
result.Should().Be(0);
}

[TestMethod]
[EmbeddedFilesTestData(@"Files/BuildParamsCommandTests/Registry/main\.bicepparam")]
[TestCategory(BaselineHelper.BaselineTestCategory)]
Expand Down
17 changes: 10 additions & 7 deletions src/Bicep.Cli/Commands/BuildParamsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,17 @@ public async Task<int> RunAsync(BuildParamsArguments args)
if (bicepFileUri is not null &&
compilation.GetEntrypointSemanticModel().Root.TryGetBicepFileSemanticModelViaUsing().IsSuccess(out var usingModel))
{
if (usingModel is not SemanticModel bicepSemanticModel)
if (usingModel is not EmptySemanticModel)
{
throw new CommandLineException($"Bicep file {bicepFileUri.LocalPath} provided with --bicep-file can only be used if the Bicep parameters \"using\" declaration refers to a Bicep file on disk.");
}

if (!bicepSemanticModel.Root.FileUri.Equals(bicepFileUri))
{
throw new CommandLineException($"Bicep file {bicepFileUri.LocalPath} provided with --bicep-file option doesn't match the Bicep file {bicepSemanticModel.Root.Name} referenced by the \"using\" declaration in the parameters file.");
if (usingModel is not SemanticModel bicepSemanticModel)
{
throw new CommandLineException($"Bicep file {bicepFileUri.LocalPath} provided with --bicep-file can only be used if the Bicep parameters \"using\" declaration refers to a Bicep file on disk.");
}

if (!bicepSemanticModel.Root.FileUri.Equals(bicepFileUri))
{
throw new CommandLineException($"Bicep file {bicepFileUri.LocalPath} provided with --bicep-file option doesn't match the Bicep file {bicepSemanticModel.Root.Name} referenced by the \"using\" declaration in the parameters file.");
}
}
}

Expand Down

0 comments on commit edae013

Please sign in to comment.