33# Licensed under the MIT License. See License.txt in the project root for license information.
44# --------------------------------------------------------------------------------------------
55
6-
76import threading
87import time
98import 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 } " )
0 commit comments