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 432c6400eb2..ab3c3901e86 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/custom.py +++ b/src/azure-cli/azure/cli/command_modules/resource/custom.py @@ -1844,9 +1844,15 @@ def create_template_spec(cmd, resource_group_name, name, template_file=None, loc if template_file: from azure.cli.command_modules.resource._packing_engine import (pack) - packed_template = pack(cmd, template_file) - input_template = getattr(packed_template, 'RootTemplate') - artifacts = getattr(packed_template, 'Artifacts') + if is_bicep_file(template_file): + template_content = run_bicep_command(["build", "--stdout", template_file]) + input_content = _remove_comments_from_json(template_content, file_path=template_file) + input_template = json.loads(json.dumps(input_content)) + artifacts = [] + else: + packed_template = pack(cmd, template_file) + input_template = getattr(packed_template, 'RootTemplate') + artifacts = getattr(packed_template, 'Artifacts') if ui_form_definition_file: ui_form_definition_content = _remove_comments_from_json(read_file_content(ui_form_definition_file)) @@ -1888,9 +1894,15 @@ def update_template_spec(cmd, resource_group_name=None, name=None, template_spec existing_template, artifacts, input_ui_form_definition = None, None, None if template_file: from azure.cli.command_modules.resource._packing_engine import (pack) - packed_template = pack(cmd, template_file) - input_template = getattr(packed_template, 'RootTemplate') - artifacts = getattr(packed_template, 'Artifacts') + if is_bicep_file(template_file): + template_content = run_bicep_command(["build", "--stdout", template_file]) + input_content = _remove_comments_from_json(template_content, file_path=template_file) + input_template = json.loads(json.dumps(input_content)) + artifacts = [] + else: + packed_template = pack(cmd, template_file) + input_template = getattr(packed_template, 'RootTemplate') + artifacts = getattr(packed_template, 'Artifacts') if ui_form_definition_file: ui_form_definition_content = _remove_comments_from_json(read_file_content(ui_form_definition_file)) diff --git a/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource.py b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource.py index 3421f2c35c2..3f1904ca8bc 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource.py +++ b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource.py @@ -3759,6 +3759,32 @@ def test_tenent_level_deployment_with_bicep(self): self.check('properties.provisioningState', 'Succeeded') ]) + @ResourceGroupPreparer(name_prefix='cli_test_template_specs', location='westus') + def test_create_template_specs_bicep(self, resource_group, resource_group_location): + curr_dir = os.path.dirname(os.path.realpath(__file__)) + template_spec_name = self.create_random_name('cli-test-create-template-spec', 60) + self.kwargs.update({ + 'template_spec_name': template_spec_name, + 'tf': os.path.join(curr_dir, 'storage_account_deploy.bicep').replace('\\', '\\\\'), + 'resource_group_location': resource_group_location, + 'description': '"AzCLI test root template spec from bicep"', + 'version_description': '"AzCLI test version of root template spec from bicep"', + }) + + result = self.cmd('ts create -g {rg} -n {template_spec_name} -v 1.0 -l {resource_group_location} -f "{tf}" --description {description} --version-description {version_description}', checks=[ + self.check('location', "westus"), + self.check('mainTemplate.functions', []), + self.check("name", "1.0") + ]).get_output_in_json() + + with self.assertRaises(IncorrectUsageError) as err: + self.cmd('ts create --name {template_spec_name} -g {rg} -l {resource_group_location} --template-file "{tf}"') + self.assertTrue("please provide --template-uri if --query-string is specified" in str(err.exception)) + + # clean up + self.kwargs['template_spec_id'] = result['id'].replace('/versions/1.0', '') + self.cmd('ts delete --template-spec {template_spec_id} --yes') + def _remove_bicep_cli(self): bicep_cli_path = self._get_bicep_cli_path() if os.path.isfile(bicep_cli_path):