-
Notifications
You must be signed in to change notification settings - Fork 7
feat: support .bicepparam parameter files for Bicep deployments #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -32,6 +32,7 @@ import ( | |||||||
|
|
||||||||
| "github.com/Azure/mpf/pkg/infrastructure/ARMTemplateShared" | ||||||||
| "github.com/Azure/mpf/pkg/infrastructure/authorizationCheckers/ARMTemplateDeployment" | ||||||||
| "github.com/Azure/mpf/pkg/infrastructure/bicepUtils" | ||||||||
| mpfSharedUtils "github.com/Azure/mpf/pkg/infrastructure/mpfSharedUtils" | ||||||||
| rgm "github.com/Azure/mpf/pkg/infrastructure/resourceGroupManager" | ||||||||
| spram "github.com/Azure/mpf/pkg/infrastructure/spRoleAssignmentManager" | ||||||||
|
|
@@ -187,6 +188,88 @@ func TestBicepAksFullDeployment(t *testing.T) { | |||||||
| assert.Equal(t, 9, len(mpfResult.RequiredPermissions[mpfConfig.SubscriptionID])) | ||||||||
| } | ||||||||
|
|
||||||||
| func TestBicepWithBicepparamFile(t *testing.T) { | ||||||||
| mpfArgs, err := getTestingMPFArgs() | ||||||||
| if err != nil { | ||||||||
| t.Skip("required environment variables not set, skipping end to end test") | ||||||||
| } | ||||||||
|
|
||||||||
| if checkBicepTestEnvVars() { | ||||||||
| t.Skip("required environment variables not set, skipping end to end test") | ||||||||
| } | ||||||||
|
|
||||||||
| bicepExecPath := os.Getenv("MPF_BICEPEXECPATH") | ||||||||
|
|
||||||||
| bicepFilePath, err := getAbsolutePath("../samples/bicep/storage-account-simple.bicep") | ||||||||
| if err != nil { | ||||||||
| t.Fatalf("failed to resolve absolute path for bicep file: %v", err) | ||||||||
| } | ||||||||
| parametersFilePath, err := getAbsolutePath("../samples/bicep/storage-account-simple-params.bicepparam") | ||||||||
| if err != nil { | ||||||||
| t.Fatalf("failed to resolve absolute path for parameters file: %v", err) | ||||||||
| } | ||||||||
|
|
||||||||
| // Exercise the same compile helper used by the bicep CLI command so this | ||||||||
| // test covers the auto-compile-on-.bicepparam code path. | ||||||||
| if !bicepUtils.IsBicepParamFile(parametersFilePath) { | ||||||||
| t.Fatalf("expected %q to be detected as a .bicepparam file", parametersFilePath) | ||||||||
| } | ||||||||
| compiledParamsPath, err := bicepUtils.CompileBicepParamsToTempFile(bicepExecPath, parametersFilePath) | ||||||||
| if err != nil { | ||||||||
| t.Fatalf("error compiling .bicepparam file: %v", err) | ||||||||
| } | ||||||||
| t.Cleanup(func() { _ = os.Remove(compiledParamsPath) }) | ||||||||
|
|
||||||||
| // Build bicep to ARM template | ||||||||
| armTemplatePath := strings.TrimSuffix(bicepFilePath, ".bicep") + ".json" | ||||||||
|
||||||||
| armTemplatePath := strings.TrimSuffix(bicepFilePath, ".bicep") + ".json" | |
| armTemplatePath := strings.TrimSuffix(bicepFilePath, ".bicep") + ".json" | |
| t.Cleanup(func() { _ = os.Remove(armTemplatePath) }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Added t.Cleanup(func() { _ = os.Remove(armTemplatePath) }) right after computing the path so the generated .json is removed even if the test fails or panics.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| module github.com/Azure/mpf | ||
|
|
||
| go 1.26.1 | ||
| go 1.26.2 | ||
|
|
||
| require ( | ||
| github.com/Azure/azure-sdk-for-go/sdk/azcore v1.21.0 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // MIT License | ||
| // | ||
| // Copyright (c) Microsoft Corporation. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in all | ||
| // copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| // SOFTWARE | ||
|
|
||
| // Package bicepUtils contains helpers for working with Bicep input files, | ||
| // such as compiling .bicepparam files to ARM JSON parameter files. | ||
| package bicepUtils | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // IsBicepParamFile reports whether the given path refers to a .bicepparam file. | ||
| func IsBicepParamFile(path string) bool { | ||
| return strings.HasSuffix(strings.ToLower(path), ".bicepparam") | ||
| } | ||
|
|
||
| // CompileBicepParamsToTempFile compiles the given .bicepparam file to an ARM | ||
| // JSON parameters file using `bicep build-params`. The output is written to a | ||
| // freshly-created temp file, so any pre-existing "<name>.parameters.json" next | ||
| // to the source file is left untouched. | ||
| // | ||
| // The caller is responsible for removing the returned path when no longer | ||
| // needed (typically via defer / t.Cleanup). If an error occurs, the temp file | ||
| // is removed before returning. | ||
| func CompileBicepParamsToTempFile(bicepExecPath, paramsFilePath string) (string, error) { | ||
| tempFile, err := os.CreateTemp("", "mpf-bicepparam-*.parameters.json") | ||
| if err != nil { | ||
| return "", fmt.Errorf("creating temporary ARM parameters file: %w", err) | ||
| } | ||
| compiledPath := tempFile.Name() | ||
| if err := tempFile.Close(); err != nil { | ||
| _ = os.Remove(compiledPath) | ||
| return "", fmt.Errorf("closing temporary ARM parameters file: %w", err) | ||
| } | ||
|
|
||
| cmd := exec.Command(bicepExecPath, "build-params", paramsFilePath, "--outfile", compiledPath) | ||
| cmd.Dir = filepath.Dir(paramsFilePath) | ||
|
|
||
| output, err := cmd.CombinedOutput() | ||
| if err != nil { | ||
| _ = os.Remove(compiledPath) | ||
| return "", fmt.Errorf("running bicep build-params: %w\n%s", err, string(output)) | ||
| } | ||
|
|
||
| return compiledPath, nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // MIT License | ||
| // | ||
| // Copyright (c) Microsoft Corporation. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in all | ||
| // copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| // SOFTWARE | ||
|
|
||
| package bicepUtils | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestIsBicepParamFile(t *testing.T) { | ||
| cases := []struct { | ||
| path string | ||
| want bool | ||
| }{ | ||
| {"params.bicepparam", true}, | ||
| {"PARAMS.BICEPPARAM", true}, | ||
| {"/some/path/Params.BicepParam", true}, | ||
| {"params.json", false}, | ||
| {"main.bicep", false}, | ||
| {"", false}, | ||
| {"params.bicepparam.bak", false}, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| if got := IsBicepParamFile(tc.path); got != tc.want { | ||
| t.Errorf("IsBicepParamFile(%q) = %v, want %v", tc.path, got, tc.want) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| using './storage-account-simple.bicep' | ||
|
|
||
| param storageAccountName = 'myazdemostg' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Markdown formatting is broken here: the PowerShell code block is not properly closed before the next heading (the closing fence and heading are on the same line). This will render incorrectly in the docs; put the closing ``` on its own line, then add the heading on the next line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Moved the closing fence to its own line and put a blank line before the next heading so the markdown renders correctly.