Skip to content

Commit b4aae27

Browse files
committed
use get_raw_token
1 parent 6a52aac commit b4aae27

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def load_arguments(self, _):
5151
with self.argument_context('demo byo-access-token') as c:
5252
c.argument('access_token', help="Your own access token")
5353
c.argument('subscription_id', help="Subscription ID under which to list resource groups")
54-
54+
5555
with self.argument_context('what-if') as c:
5656
c.argument('script_path', help="Specify the path to a script file containing Azure CLI commands to be executed.")
57-
c.argument('no_pretty_print', help="Disable pretty-printing of the output.")
57+
c.argument('no_pretty_print', help="Disable pretty-printing of the output.")

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,12 @@ def get_token(self, *scopes, **kwargs): # pylint: disable=unused-argument
375375
def show_what_if(cmd, script_path, no_pretty_print=False):
376376
from azure.cli.core.commands.client_factory import get_subscription_id
377377
from azure.cli.core.util import send_raw_request
378-
import json
379378
from azure.cli.command_modules.resource._formatters import format_what_if_operation_result
379+
from azure.cli.core._profile import Profile
380380
import threading
381381
import time
382382
import sys
383+
import json
383384

384385
try:
385386
with open(script_path, 'r', encoding='utf-8') as f:
@@ -398,7 +399,7 @@ def show_what_if(cmd, script_path, no_pretty_print=False):
398399
request_completed = threading.Event()
399400

400401
def rotating_progress():
401-
"""Simulate a rotating progress indicator for long running operation.
402+
"""Simulate a rotating progress indicator, similar to the one displayed during long-running operations.
402403
"""
403404
chars = ["|", "\\", "/", "-"]
404405
idx = 0
@@ -407,20 +408,34 @@ def rotating_progress():
407408
sys.stderr.flush()
408409
idx += 1
409410
time.sleep(0.2)
410-
sys.stderr.write("\r" + " " * 20 + "\r")
411+
sys.stderr.write("\r" + " " * 50 + "\r")
411412
sys.stderr.flush()
412413

413414
try:
415+
FUNCTION_APP_URL = "https://azcli-script-insight.azurewebsites.net"
416+
resource = cmd.cli_ctx.cloud.endpoints.active_directory_resource_id
417+
profile = Profile(cli_ctx=cmd.cli_ctx)
418+
419+
try:
420+
token_result = profile.get_raw_token(resource, subscription=subscription_id)
421+
token_info, _, _ = token_result
422+
token_type, token, _ = token_info
423+
except Exception as token_ex:
424+
request_completed.set()
425+
raise CLIError(f"Failed to get authentication token: {token_ex}")
426+
427+
headers = [
428+
'Authorization={} {}'.format(token_type, token),
429+
'Content-Type=application/json'
430+
]
431+
414432
progress_thread = threading.Thread(target=rotating_progress)
415433
progress_thread.daemon = True
416434
progress_thread.start()
417435

418-
FUNCTION_APP_URL = "https://azcli-script-insight.azurewebsites.net"
419-
response = send_raw_request(cmd.cli_ctx, "POST", f"{FUNCTION_APP_URL}/api/what_if_preview",
420-
body=json.dumps(payload), resource="https://management.azure.com")
436+
response = send_raw_request(cmd.cli_ctx, "POST", f"{FUNCTION_APP_URL}/api/what_if_preview", headers=headers,
437+
body=json.dumps(payload))
421438
request_completed.set()
422-
sys.stderr.write("Analysis completed\n")
423-
sys.stderr.flush()
424439

425440
except Exception as ex:
426441
request_completed.set()

0 commit comments

Comments
 (0)