diff --git a/src/azure-cli/azure/cli/command_modules/resource/custom.py b/src/azure-cli/azure/cli/command_modules/resource/custom.py index 1ae23ddf37f..8ab6ed157a5 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/custom.py +++ b/src/azure-cli/azure/cli/command_modules/resource/custom.py @@ -62,6 +62,8 @@ logger = get_logger(__name__) +_CLI_GENERATED_DEPLOYMENT_NAME_PATTERN = re.compile(r'azurecli\d+\.\d+') + RPAAS_APIS = {'microsoft.datadog': '/subscriptions/{subscriptionId}/providers/Microsoft.Datadog/agreements/default?api-version=2020-02-01-preview', 'microsoft.confluent': '/subscriptions/{subscriptionId}/providers/Microsoft.Confluent/agreements/default?api-version=2020-03-01-preview'} @@ -4651,7 +4653,7 @@ def snapshot_bicep_file(cmd, file, mode=None, tenant_id=None, subscription_id=No args += ["--location", location] if resource_group: args += ["--resource-group", resource_group] - if deployment_name: + if deployment_name and not _CLI_GENERATED_DEPLOYMENT_NAME_PATTERN.fullmatch(deployment_name): args += ["--deployment-name", deployment_name] output = run_bicep_command(cmd.cli_ctx, args) diff --git a/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_bicep.py b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_bicep.py index 5ed92fac0ee..a6dfcb91a30 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_bicep.py +++ b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_bicep.py @@ -330,6 +330,34 @@ def test_snapshot_bicep_file_passes_minimum_args( bicep_version_check_mock.assert_called_once_with(self.cli_ctx, "0.41.2") run_bicep_command_mock.assert_called_once_with(self.cli_ctx, ["snapshot", "main.bicepparam"]) + @mock.patch("azure.cli.command_modules.resource.custom.run_bicep_command") + @mock.patch("azure.cli.command_modules.resource.custom.bicep_version_greater_than_or_equal_to") + @mock.patch("azure.cli.command_modules.resource.custom.ensure_bicep_installation") + def test_snapshot_bicep_file_ignores_generated_deployment_name( + self, ensure_bicep_installation_mock, bicep_version_check_mock, run_bicep_command_mock + ): + bicep_version_check_mock.return_value = True + run_bicep_command_mock.return_value = "" + + snapshot_bicep_file(self.cmd, "main.bicepparam", deployment_name="azurecli123.456789") + + run_bicep_command_mock.assert_called_once_with(self.cli_ctx, ["snapshot", "main.bicepparam"]) + + @mock.patch("azure.cli.command_modules.resource.custom.run_bicep_command") + @mock.patch("azure.cli.command_modules.resource.custom.bicep_version_greater_than_or_equal_to") + @mock.patch("azure.cli.command_modules.resource.custom.ensure_bicep_installation") + def test_snapshot_bicep_file_passes_similarly_named_deployment_name( + self, ensure_bicep_installation_mock, bicep_version_check_mock, run_bicep_command_mock + ): + bicep_version_check_mock.return_value = True + run_bicep_command_mock.return_value = "" + + snapshot_bicep_file(self.cmd, "main.bicepparam", deployment_name="azurecli123") + + run_bicep_command_mock.assert_called_once_with( + self.cli_ctx, ["snapshot", "main.bicepparam", "--deployment-name", "azurecli123"] + ) + @mock.patch("azure.cli.command_modules.resource.custom.run_bicep_command") @mock.patch("azure.cli.command_modules.resource.custom.bicep_version_greater_than_or_equal_to") @mock.patch("azure.cli.command_modules.resource.custom.ensure_bicep_installation")