Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/azure-cli/azure/cli/command_modules/resource/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines -392 to +393
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The template_as_bytes will be converted to bytes in the subsequent step of JsonCTemplatePolicy, but not in the step of JSONSerializer. So correct the variable name to avoid misleading

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data is ambiguous. It can mean object or string. Let's call it template_str.



class JSONSerializer(Serializer):
Expand All @@ -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 {}
Expand All @@ -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')


Expand Down