Skip to content

Commit dbb01fc

Browse files
committed
merge support --what-if
1 parent b9aac2d commit dbb01fc

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

src/azure-cli-core/azure/cli/core/what_if.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
55

6-
76
import threading
87
import time
98
import sys
@@ -22,11 +21,11 @@ def read_script_file(script_path):
2221
raise CLIError(f"Error reading script file: {ex}")
2322

2423

25-
def get_auth_headers(cmd, subscription_id):
24+
def _get_auth_headers(cli_ctx, subscription_id):
2625
from azure.cli.core._profile import Profile
2726

28-
resource = cmd.cli_ctx.cloud.endpoints.active_directory_resource_id
29-
profile = Profile(cli_ctx=cmd.cli_ctx)
27+
resource = cli_ctx.cloud.endpoints.active_directory_resource_id
28+
profile = Profile(cli_ctx=cli_ctx)
3029

3130
try:
3231
token_result = profile.get_raw_token(resource, subscription=subscription_id)
@@ -41,7 +40,7 @@ def get_auth_headers(cmd, subscription_id):
4140
}
4241

4342

44-
def make_what_if_request(payload, headers_dict):
43+
def _make_what_if_request(payload, headers_dict):
4544
request_completed = threading.Event()
4645

4746
def _rotating_progress():
@@ -166,3 +165,35 @@ def _create_resource_change(change_data):
166165
potential_changes.append(_create_resource_change(change_data))
167166

168167
return WhatIfOperationResult(changes, potential_changes, [])
168+
169+
def show_what_if(cli_ctx, azcli_script: str, subscription_id: str = None, no_pretty_print=False):
170+
from azure.cli.core.commands.client_factory import get_subscription_id
171+
from azure.cli.command_modules.resource._formatters import format_what_if_operation_result
172+
173+
if not subscription_id:
174+
subscription_id = get_subscription_id(cli_ctx)
175+
176+
payload = {
177+
"azcli_script": azcli_script,
178+
"subscription_id": subscription_id
179+
}
180+
181+
headers_dict = _get_auth_headers(cli_ctx, subscription_id)
182+
response = _make_what_if_request(payload, headers_dict)
183+
184+
try:
185+
raw_results = response.json()
186+
except ValueError as ex:
187+
raise CLIError(f"Failed to parse response from what-if service: {ex}")
188+
189+
success = raw_results.get('success')
190+
if success is False:
191+
return raw_results
192+
if success is True:
193+
what_if_result = raw_results.get('what_if_result', {})
194+
what_if_operation_result = convert_json_to_what_if_result(what_if_result)
195+
if no_pretty_print:
196+
return what_if_result
197+
print(format_what_if_operation_result(what_if_operation_result, cli_ctx.enable_color))
198+
return what_if_result
199+
raise CLIError(f"Unexpected response from what-if service, got: {raw_results}")

src/azure-cli/azure/cli/command_modules/util/custom.py

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -373,35 +373,9 @@ def get_token(self, *scopes, **kwargs): # pylint: disable=unused-argument
373373

374374

375375
def show_what_if(cmd, script_path, no_pretty_print=False):
376+
from azure.cli.core.what_if import show_what_if as _show_what_if, read_script_file
376377
from azure.cli.core.commands.client_factory import get_subscription_id
377-
from azure.cli.command_modules.resource._formatters import format_what_if_operation_result
378-
from azure.cli.core.what_if import (read_script_file, get_auth_headers,
379-
make_what_if_request, convert_json_to_what_if_result)
380378

381-
script_content = read_script_file(script_path)
382379
subscription_id = get_subscription_id(cmd.cli_ctx)
383-
384-
payload = {
385-
"azcli_script": script_content,
386-
"subscription_id": subscription_id
387-
}
388-
389-
headers_dict = get_auth_headers(cmd, subscription_id)
390-
response = make_what_if_request(payload, headers_dict)
391-
392-
try:
393-
raw_results = response.json()
394-
except ValueError as ex:
395-
raise CLIError(f"Failed to parse response from what-if service: {ex}")
396-
397-
success = raw_results.get('success')
398-
if success is False:
399-
return raw_results
400-
if success is True:
401-
what_if_result = raw_results.get('what_if_result', {})
402-
what_if_operation_result = convert_json_to_what_if_result(what_if_result)
403-
if no_pretty_print:
404-
return what_if_result
405-
print(format_what_if_operation_result(what_if_operation_result, cmd.cli_ctx.enable_color))
406-
return what_if_result
407-
raise CLIError(f"Unexpected response from what-if service, got: {raw_results}")
380+
script_content = read_script_file(script_path)
381+
return _show_what_if(cmd.cli_ctx, script_content, subscription_id=subscription_id, no_pretty_print=no_pretty_print)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# Create a VM directly instead of using an ARM template
21
az vm create --resource-group myrg --name MyVM_01 --image UbuntuLTS --size Standard_D2s_v3 --admin-username azureuser --generate-ssh-keys
2+
3+
az functionapp update --name myfunctionapp --resource-group myrg --set tags.Environment=Test

0 commit comments

Comments
 (0)