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 5012984f1f0..098f75df3e5 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/custom.py +++ b/src/azure-cli/azure/cli/command_modules/resource/custom.py @@ -389,8 +389,8 @@ def _deploy_arm_template_core_unmodified(cmd, resource_group_name, template_file class JsonCTemplate: - def __init__(self, template_as_bytes): - self.template_as_bytes = template_as_bytes + def __init__(self, template_string): + self.template_string = template_string class JSONSerializer(Serializer): @@ -408,6 +408,7 @@ def body(self, data, data_type, **kwargs): class JsonCTemplatePolicy(SansIOHTTPPolicy): + # Obtain the template data from JsonCTemplate object, and then splice it with other properties into the JSONC format def on_request(self, request): http_request = request.http_request request_data = getattr(http_request, 'data', {}) or {} @@ -422,14 +423,19 @@ def on_request(self, request): template = http_request.data["properties"]["template"] if not isinstance(template, JsonCTemplate): raise ValueError() - del http_request.data["properties"]["template"] + # templateLink nad template cannot exist at the same time in deployment_dry_run mode if "templateLink" in http_request.data["properties"].keys(): del http_request.data["properties"]["templateLink"] - partial_request = json.dumps(http_request.data) - http_request.data = partial_request[:-2] + ", template:" + template.template_as_bytes + r"}}" + # The 'template' and other properties (such as 'parameters','mode'...) are spliced and encoded into the UTF-8 bytes as the request data + # The format of the request data is: {"properties": {"parameters": {...}, "mode": "Incremental", template:{\r\n "$schema": "...",\r\n "contentVersion": "...",\r\n "parameters": {...}}} + # This is not an ordinary JSON format, but it is a JSONC format that service can deserialize + # If not do this splicing, the request data generated by default serialization cannot be deserialized on the service side. + # Because the service cannot deserialize the template element: "template": "{\r\n \"$schema\": \"...\",\r\n \"contentVersion\": \"...\",\r\n \"parameters\": {...}}" + partial_request = json.dumps(http_request.data) + http_request.data = partial_request[:-2] + ", template:" + template.template_string + r"}}" http_request.data = http_request.data.encode('utf-8')