Skip to content

Commit

Permalink
Fix the getting subsciption_id error.
Browse files Browse the repository at this point in the history
  • Loading branch information
krtaiyang committed Feb 7, 2025
1 parent 8fac44f commit b237114
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import requests
import traceback
from sps_module import sps_shared_resources
from datetime import datetime, timedelta, timezone

Expand Down Expand Up @@ -77,55 +78,48 @@ def get_next_available_location():
clean_expired_over_limit_locations()
clean_expired_over_call_history_locations()

# Get the list of subscription_ids
subscription_ids = list(SS_Resources.locations_call_history_tmp.keys())

# Get the last used subscription_id and location
last_subscription_id = SS_Resources.last_subscription_id_and_location_tmp.get('last_subscription_id')
last_location = SS_Resources.last_subscription_id_and_location_tmp.get('last_location')

# Find the index of the last used subscription_id (use last_subscription_id directly)
start_subscription_index = 0
if last_subscription_id is not None and last_subscription_id in subscription_ids:
start_subscription_index = subscription_ids.index(last_subscription_id)

# Iterate through subscription_ids (allow reusing the last subscription_id)

for i in range(len(subscription_ids)):
subscription_index = (start_subscription_index + i) % len(subscription_ids)
subscription_id = subscription_ids[subscription_index]

current_history = SS_Resources.locations_call_history_tmp[subscription_id]
current_over_limit_locations = SS_Resources.locations_over_limit_tmp.get(subscription_id)

# Get the list of locations
locations = list(current_history.keys())

# Find the index of the last used location and start from the next one
start_location_index = 0
if last_location is not None and last_location in locations:
start_location_index = (locations.index(last_location) + 1) % len(locations) # Move to the next location
start_location_index = (locations.index(last_location) + 1) % len(locations)

# Iterate through locations (starting from the next one after last_location)
for j in range(len(locations)):
location_index = (start_location_index + j) % len(locations)
location = locations[location_index]

# Check if the location can be called
if validation_can_call(location, current_history, current_over_limit_locations):
# Update last used subscription_id (keep it) and location (move to next)

SS_Resources.last_subscription_id_and_location_tmp['last_subscription_id'] = subscription_id
SS_Resources.last_subscription_id_and_location_tmp['last_location'] = location
return subscription_id, location, current_history, current_over_limit_locations

return None # No valid location found
return None

except Exception as e:
print(f"Failed to get_next_available_location: {e}")
print("\n[ERROR] Exception occurred in get_next_available_location:")
print(traceback.format_exc())
print(f"\n[ERROR] Failed to get_next_available_location: {e}")
return None




def collect_available_locations():
"""
이 메서드는 잘못된 location 파라미터로 Azure API를 호출해 API에서 리턴한 지원되는 locations 을 리턴합니다.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time
from azure.identity import ClientSecretCredential
from utils.pub_service import db_AzureAuth
from azure.core.exceptions import ClientAuthenticationError

def get_token():
db = db_AzureAuth
Expand Down Expand Up @@ -38,6 +39,15 @@ def get_sps_token_and_subscriptions():
client_secret = os.environ.get('CLIENT_SECRET')
subscriptions = os.environ.get('SUBSCRIPTIONS')

sps_token = ClientSecretCredential(tenant_id, client_id, client_secret).get_token_info("https://management.azure.com/.default").token
if not all([tenant_id, client_id, client_secret, subscriptions]):
raise ValueError("Missing required environment variables: TENANT_ID, CLIENT_ID, CLIENT_SECRET, or SUBSCRIPTIONS")

return sps_token, list(subscriptions)
subscriptions = [sub.strip() for sub in subscriptions.split(",") if sub.strip()]

try:
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
sps_token = credential.get_token("https://management.azure.com/.default").token
except ClientAuthenticationError as e:
raise ValueError(f"Failed to authenticate with Azure: {e}")

return sps_token, subscriptions

0 comments on commit b237114

Please sign in to comment.