Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
yutongzhang-microsoft committed Dec 13, 2024
1 parent 6c092bc commit 5200cd3
Showing 1 changed file with 7 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import argparse
import math
import json
from constant import PR_CHECKER_TOPOLOGY_NAME, MAX_INSTANCE_NUMBER, MAX_GET_TOKEN_RETRY_TIMES
from azure.kusto.data import KustoConnectionStringBuilder, KustoClient
from datetime import datetime, timezone
Expand Down Expand Up @@ -41,12 +40,11 @@ def get_access_token():

# 1. Run az login with re-try
az_login_cmd = f"az login --identity --username {managed_identity_id}"
print(az_login_cmd)
az_login_attempts = 0
while az_login_attempts < MAX_GET_TOKEN_RETRY_TIMES:
try:
os.popen(az_login_cmd)
# print(result.read())
result = os.popen(az_login_cmd)
print(result.read())
print(f"Az login successfully. Login time: {datetime.now(timezone.utc)}")
break
except Exception as exception:
Expand All @@ -61,25 +59,20 @@ def get_access_token():
raise Exception(f"Failed to az login after {MAX_GET_TOKEN_RETRY_TIMES} attempts.")

# 2. Get access token with re-try
get_token_cmd = f"az login --identity --username {managed_identity_id} " \
f"& az account get-access-token --resource https://api.kusto.windows.net --query accessToken -o tsv"
get_token_cmd = "az account get-access-token --resource https://api.kusto.windows.net --query accessToken -o tsv"
print(get_token_cmd)
get_token_attempts = 0
while get_token_attempts < MAX_GET_TOKEN_RETRY_TIMES:
try:
result = os.popen(get_token_cmd)
# token = os.popen(get_token_cmd)
print(result.read())
token = json.loads(result.read())
access_token = token.get("accessToken", None)
access_token = result.read()
print(access_token)
# token = json.loads(result.read())
# access_token = token.get("accessToken", None)
if not access_token:
raise Exception("Parse token from stdout failed, accessToken is None.")

# Parse token expires time from string
token_expires_on = token.get("expiresOn", "")
if token_expires_on:
print(f"Get token successfully. Token will expire on {token_expires_on}.")

return access_token

except Exception as exception:
Expand Down

0 comments on commit 5200cd3

Please sign in to comment.