-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[DO NOT MERGE] Fix #10354 #10389
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
Merged
Merged
[DO NOT MERGE] Fix #10354 #10389
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0b1370e
wip
yugangw-msft d32ee97
wip 2
yugangw-msft 9b822d8
resolve json comment and multiple lines
Juliehzl d6494eb
resolve error in Laruent code
Juliehzl cce3d81
fix errors in CI
Juliehzl c26d412
fix CI
Juliehzl 8e47055
update test
Juliehzl cfaf63b
escape None type
Juliehzl 8b42f65
update test
Juliehzl dc9637a
fix style
Juliehzl d00dc86
add --send-modified parameters for group deployment
Juliehzl b419b01
change parameter level
Juliehzl ff09de1
resolve comments
Juliehzl edefab6
fix style
Juliehzl af81e77
update test record
Juliehzl c624c14
update HISTORU.rst
Juliehzl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ | |
| from azure.mgmt.resource.resources.models import GenericResource | ||
|
|
||
| from azure.cli.core.parser import IncorrectUsageError | ||
| from azure.cli.core.util import get_file_json, shell_safe_json_parse, sdk_no_wait | ||
| from azure.cli.core.util import get_file_json, read_file_content, shell_safe_json_parse, sdk_no_wait | ||
| from azure.cli.core.commands.client_factory import get_mgmt_service_client | ||
| from azure.cli.core.profiles import ResourceType, get_sdk, get_api_version | ||
|
|
||
|
|
@@ -286,6 +286,120 @@ def _deploy_arm_template_core(cli_ctx, resource_group_name, | |
| return sdk_no_wait(no_wait, smc.deployments.create_or_update, resource_group_name, deployment_name, properties) | ||
|
|
||
|
|
||
| def _remove_comments_from_json(template): | ||
| from jsmin import jsmin | ||
|
|
||
| minified = jsmin(template) | ||
| # Get rid of multi-line strings. Note, we are not sending it on the wire rather just extract parameters to prompt for values | ||
| result = re.sub(r'"[^"]*?\n[^"]*?(?<!\\)"', '"#Azure Cli#"', minified, re.DOTALL) | ||
| return shell_safe_json_parse(result, preserve_order=True) | ||
|
|
||
|
|
||
| # pylint: disable=too-many-locals, too-many-statements, too-few-public-methods | ||
| def _deploy_arm_template_unmodified(cli_ctx, resource_group_name, template_file=None, | ||
| template_uri=None, deployment_name=None, parameters=None, | ||
| mode=None, rollback_on_error=None, validate_only=False, no_wait=False): | ||
| DeploymentProperties, TemplateLink, OnErrorDeployment = get_sdk(cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES, | ||
| 'DeploymentProperties', 'TemplateLink', | ||
| 'OnErrorDeployment', mod='models') | ||
| template_link = None | ||
| template_obj = None | ||
| on_error_deployment = None | ||
| template_content = None | ||
| if template_uri: | ||
| template_link = TemplateLink(uri=template_uri) | ||
| template_content = _urlretrieve(template_uri).decode('utf-8') | ||
| template_obj = _remove_comments_from_json(template_content) | ||
| else: | ||
| template_content = read_file_content(template_file) | ||
| template_obj = _remove_comments_from_json(template_content) | ||
|
|
||
| if rollback_on_error == '': | ||
| on_error_deployment = OnErrorDeployment(type='LastSuccessful') | ||
| elif rollback_on_error: | ||
| on_error_deployment = OnErrorDeployment(type='SpecificDeployment', deployment_name=rollback_on_error) | ||
|
|
||
| template_param_defs = template_obj.get('parameters', {}) | ||
| template_obj['resources'] = template_obj.get('resources', []) | ||
| parameters = _process_parameters(template_param_defs, parameters) or {} | ||
| parameters = _get_missing_parameters(parameters, template_obj, _prompt_for_parameters) | ||
|
|
||
| parameters = json.loads(json.dumps(parameters)) | ||
|
|
||
| properties = DeploymentProperties(template=template_content, template_link=template_link, | ||
| parameters=parameters, mode=mode, on_error_deployment=on_error_deployment) | ||
|
|
||
| smc = get_mgmt_service_client(cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES) | ||
|
|
||
| class JsonCTemplate(object): | ||
| def __init__(self, template_as_bytes): | ||
| self.template_as_bytes = template_as_bytes | ||
|
Member
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 name I debugged (I found this when reviewing #21220.) |
||
|
|
||
| from msrest.serialization import Serializer | ||
|
|
||
| class MySerializer(Serializer): | ||
| def body(self, data, data_type, **kwargs): | ||
| if data_type == 'Deployment': | ||
| # Be sure to pass a DeploymentProperties | ||
| template = data.properties.template | ||
| if template: | ||
| data.properties.template = None | ||
| data_as_dict = data.serialize() | ||
| data_as_dict["properties"]["template"] = JsonCTemplate(template) | ||
| return data_as_dict | ||
| return super(MySerializer, self).body(data, data_type, **kwargs) | ||
| deployments_operation_group = smc.deployments # This solves the multi-api for you | ||
|
|
||
| # pylint: disable=protected-access | ||
| deployments_operation_group._serialize = MySerializer( | ||
| deployments_operation_group._serialize.dependencies | ||
| ) | ||
|
|
||
| # Now, you have a serializer that keeps a weird class inside it, you need to explain to the HTTP pipeline how to translate that into bytes | ||
| from msrest.pipeline import SansIOHTTPPolicy | ||
|
|
||
| class JsonCTemplatePolicy(SansIOHTTPPolicy): | ||
| def on_request(self, request, **kwargs): | ||
| http_request = request.http_request | ||
| logger.info(http_request.data) | ||
| if (getattr(http_request, 'data', {}) or {}).get('properties', {}).get('template'): | ||
| 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"}}" | ||
|
|
||
| # Plug this as default HTTP pipeline | ||
| from msrest.pipeline import Pipeline | ||
| from msrest.pipeline.requests import ( | ||
| RequestsCredentialsPolicy, | ||
| RequestsPatchSession, | ||
| PipelineRequestsHTTPSender | ||
| ) | ||
| from msrest.universal_http.requests import RequestsHTTPSender | ||
|
|
||
| smc.config.pipeline = Pipeline( | ||
| policies=[ | ||
| JsonCTemplatePolicy(), | ||
| smc.config.user_agent_policy, | ||
| RequestsPatchSession(), | ||
| smc.config.http_logger_policy, | ||
| RequestsCredentialsPolicy(smc.config.credentials) | ||
| ], | ||
| sender=PipelineRequestsHTTPSender(RequestsHTTPSender(smc.config)) | ||
| ) | ||
|
|
||
| if validate_only: | ||
| return sdk_no_wait(no_wait, deployments_operation_group.validate, resource_group_name, deployment_name, properties) | ||
| return sdk_no_wait(no_wait, deployments_operation_group.create_or_update, resource_group_name, deployment_name, properties) | ||
|
|
||
|
|
||
| def _deploy_arm_template_subscription_scope(cli_ctx, | ||
| template_file=None, template_uri=None, | ||
| deployment_name=None, deployment_location=None, | ||
|
|
@@ -724,7 +838,11 @@ def list_applications(cmd, resource_group_name=None): | |
|
|
||
| def deploy_arm_template(cmd, resource_group_name, | ||
| template_file=None, template_uri=None, deployment_name=None, | ||
| parameters=None, mode=None, rollback_on_error=None, no_wait=False): | ||
| parameters=None, mode=None, rollback_on_error=None, no_wait=False, handle_extended_json_format=False): | ||
| if handle_extended_json_format: | ||
| return _deploy_arm_template_unmodified(cmd.cli_ctx, resource_group_name, template_file, template_uri, | ||
| deployment_name, parameters, mode, rollback_on_error, no_wait=no_wait) | ||
|
|
||
| return _deploy_arm_template_core(cmd.cli_ctx, resource_group_name, template_file, template_uri, | ||
| deployment_name, parameters, mode, rollback_on_error, no_wait=no_wait) | ||
|
|
||
|
|
@@ -739,6 +857,7 @@ def deploy_arm_template_at_subscription_scope(cmd, template_file=None, template_ | |
|
|
||
| def validate_arm_template(cmd, resource_group_name, template_file=None, template_uri=None, | ||
| parameters=None, mode=None, rollback_on_error=None): | ||
|
|
||
| return _deploy_arm_template_core(cmd.cli_ctx, resource_group_name, template_file, template_uri, | ||
| 'deployment_dry_run', parameters, mode, rollback_on_error, validate_only=True) | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

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.
re.DOTALLchanges the behavior of.:https://docs.python.org/3/library/re.html#re.DOTALL
But there is no
.in the regex at all.