-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[Resource] az bicep: Add snapshot and run subcommands
#33398
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: dev
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 |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ | |
| from azure.mgmt.resource.deployments.models import DeploymentMode | ||
| import azure.mgmt.resource.deploymentstacks.models as StackModels | ||
|
|
||
| from azure.cli.core.azclierror import ArgumentUsageError, InvalidArgumentValueError, ResourceNotFoundError | ||
| from azure.cli.core.azclierror import ArgumentUsageError, InvalidArgumentValueError, ResourceNotFoundError, ValidationError | ||
| from azure.cli.core.parser import IncorrectUsageError | ||
| from azure.cli.core.util import get_file_json, read_file_content, shell_safe_json_parse, sdk_no_wait | ||
| from azure.cli.core.commands import LongRunningOperation | ||
|
|
@@ -4580,6 +4580,63 @@ def lint_bicep_file(cmd, file, no_restore=None, diagnostics_format=None): | |
| logger.error("az bicep lint could not be executed with the current version of Bicep CLI. Please upgrade Bicep CLI to v%s or later.", minimum_supported_version) | ||
|
|
||
|
|
||
| def snapshot_bicep_file(cmd, file, mode=None, tenant_id=None, subscription_id=None, | ||
| management_group_id=None, location=None, resource_group=None, | ||
| deployment_name=None): | ||
| ensure_bicep_installation(cmd.cli_ctx, stdout=False) | ||
|
|
||
| minimum_supported_version = "0.41.2" | ||
| if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version): | ||
| args = ["snapshot", file] | ||
| if mode: | ||
| args += ["--mode", mode] | ||
| if tenant_id: | ||
| args += ["--tenant-id", tenant_id] | ||
| if subscription_id: | ||
| args += ["--subscription-id", subscription_id] | ||
| if management_group_id: | ||
| args += ["--management-group-id", management_group_id] | ||
| if location: | ||
| args += ["--location", location] | ||
| if resource_group: | ||
| args += ["--resource-group", resource_group] | ||
| if deployment_name: | ||
| args += ["--deployment-name", deployment_name] | ||
|
|
||
| output = run_bicep_command(cmd.cli_ctx, args) | ||
|
|
||
| if output: | ||
| print(output) | ||
| else: | ||
| raise ValidationError( | ||
| f"az bicep snapshot could not be executed with the current version of Bicep CLI. " | ||
| f"Please upgrade Bicep CLI to v{minimum_supported_version} or later." | ||
| ) | ||
|
|
||
|
|
||
| def run_bicep_cli_passthrough(cmd, command_string): | ||
| import shlex | ||
|
|
||
| ensure_bicep_installation(cmd.cli_ctx, stdout=False) | ||
|
|
||
| # Use non-POSIX mode so that backslashes in Windows paths are preserved. | ||
| # In non-POSIX mode, shlex retains the surrounding quotes on quoted tokens, | ||
| # so strip them so the values are passed through cleanly to the Bicep CLI. | ||
| args = [] | ||
| for token in shlex.split(command_string, posix=False): | ||
|
Comment on lines
+4617
to
+4626
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in d88079b — |
||
| if len(token) >= 2 and token[0] in ('"', "'") and token[0] == token[-1]: | ||
| token = token[1:-1] | ||
| args.append(token) | ||
|
|
||
| if not args: | ||
| raise InvalidArgumentValueError("--command must not be empty.") | ||
|
|
||
| output = run_bicep_command(cmd.cli_ctx, args) | ||
|
|
||
| if output: | ||
| print(output) | ||
|
|
||
|
|
||
| def create_resourcemanager_privatelink( | ||
| cmd, resource_group, name, location): | ||
| rcf = _resource_privatelinks_client_factory(cmd.cli_ctx) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5669,6 +5669,68 @@ def test_bicep_lint_diagnostics_format_sarif(self): | |
|
|
||
| self.cmd('az bicep lint -f {tf} --diagnostics-format sarif') | ||
|
|
||
|
|
||
| class BicepSnapshotTest(LiveScenarioTest): | ||
| def setUp(self): | ||
| super().setUp() | ||
| self.cmd('az bicep uninstall') | ||
|
Comment on lines
+5673
to
+5676
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in d88079b — renamed both the method and the super() call to setUp in BicepSnapshotTest. |
||
|
|
||
| def tearDown(self): | ||
| super().tearDown() | ||
| self.cmd('az bicep uninstall') | ||
|
|
||
| def test_bicep_snapshot(self): | ||
| curr_dir = os.path.dirname(os.path.realpath(__file__)) | ||
| params_file = os.path.join(curr_dir, 'sample_params.bicepparam').replace('\\', '\\\\') | ||
| snapshot_path = os.path.join(curr_dir, 'sample_params.snapshot.json') | ||
|
Comment on lines
+5683
to
+5685
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fixtures already exist in this directory and are reused by other Bicep tests (e.g. |
||
| self.kwargs.update({ | ||
| 'pf': params_file, | ||
| }) | ||
|
|
||
| try: | ||
| # Capture (default mode). | ||
| self.cmd('az bicep snapshot --file {pf}') | ||
| self.assertTrue(os.path.exists(snapshot_path)) | ||
|
|
||
| # Validate against the just-captured snapshot. | ||
| self.cmd('az bicep snapshot --file {pf} --mode Validate') | ||
| finally: | ||
| if os.path.exists(snapshot_path): | ||
| os.remove(snapshot_path) | ||
|
|
||
|
|
||
| class BicepRunTest(LiveScenarioTest): | ||
| def setUp(self): | ||
| super().setUp() | ||
| self.cmd('az bicep uninstall') | ||
|
Comment on lines
+5702
to
+5705
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in d88079b — same correction applied to BicepRunTest. |
||
|
|
||
| def tearDown(self): | ||
| super().tearDown() | ||
| self.cmd('az bicep uninstall') | ||
|
|
||
| def test_bicep_run_version(self): | ||
| # Ensure Bicep CLI is installed so the passthrough has something to call. | ||
| self.cmd('az bicep install') | ||
| # Use the --option=value form because the value itself starts with --, | ||
| # which argparse otherwise treats as another option flag. | ||
| self.cmd('az bicep run --command=--version') | ||
|
|
||
| def test_bicep_run_build(self): | ||
| curr_dir = os.path.dirname(os.path.realpath(__file__)) | ||
| bf = os.path.join(curr_dir, 'sample_params.bicep').replace('\\', '\\\\') | ||
| self.kwargs.update({ | ||
| 'bf': bf, | ||
| }) | ||
|
|
||
| self.cmd('az bicep install') | ||
| self.cmd('az bicep run --command "build {bf} --stdout"') | ||
|
|
||
| def test_bicep_run_empty_command_fails(self): | ||
| from azure.cli.core.azclierror import InvalidArgumentValueError | ||
| with self.assertRaises(InvalidArgumentValueError): | ||
| self.cmd('az bicep run --command " "') | ||
|
|
||
|
|
||
| class BicepInstallationTest(LiveScenarioTest): | ||
| def setup(self): | ||
| super().setup() | ||
|
|
||
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.
az bicep runis intentionally a thin escape hatch for users to consume new Bicep CLI features (or one-off subcommands) without waiting for anaz bicepwrapper to ship in azure-cli — so an allowlist of subcommands would defeat its purpose. The command runs the Bicep CLI as the same user, so it can't grant elevated privileges that the user wouldn't already have by invokingbicepdirectly, but the untrusted-input concern is fair: in d88079b the long-summary in--helpnow warns "Because the value is forwarded to the Bicep CLI without validation, do not pass strings derived from untrusted input." Wrappers for the most useful Bicep CLI subcommands continue to be added over time, andaz bicep runis meant to bridge the gap in the meantime.