Skip to content

Commit c460d79

Browse files
authored
Merge pull request #111 from ImMin5/master
Refactor get access token logic
2 parents 0bf9d07 + 12ddd12 commit c460d79

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/cloudforet/cost_analysis/connector/azure_cost_mgmt_connector.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,9 @@ def query_usage_http(
173173
BENEFIT_GROUPING + BENEFIT_GROUPING_MCA
174174
)
175175

176-
_LOGGER.debug(f"[query_usage] parameters: {parameters}")
177-
178176
while self.next_link:
179177
url = self.next_link
180-
headers = self._make_request_headers()
178+
headers = self._make_request_headers(secret_data)
181179

182180
_LOGGER.debug(f"[query_usage] url:{url}, parameters: {parameters}")
183181
response = requests.post(url=url, headers=headers, json=parameters)
@@ -316,8 +314,8 @@ def convert_nested_dictionary(self, cloud_svc_object):
316314

317315
return cloud_svc_dict
318316

319-
def _make_request_headers(self, client_type=None):
320-
access_token = self._get_access_token()
317+
def _make_request_headers(self, secret_data: dict, client_type=None):
318+
access_token = self._get_access_token(secret_data)
321319
headers = {
322320
"Authorization": f"Bearer {access_token}",
323321
"Content-Type": "application/json",
@@ -329,7 +327,7 @@ def _make_request_headers(self, client_type=None):
329327

330328
def _retry_request(self, response, url, headers, json, retry_count, method="post"):
331329
try:
332-
_LOGGER.error(f"{datetime.utcnow()}[INFO] retry_request {response.headers}")
330+
_LOGGER.error(f"[INFO] retry_request {response.headers}")
333331
if retry_count == 0:
334332
raise ERROR_UNKNOWN(
335333
message=f"[ERROR] retry_request failed {response.json()}"
@@ -384,12 +382,26 @@ def _get_sleep_time(response_headers):
384382
return sleep_time + 1
385383

386384
@staticmethod
387-
def _get_access_token():
385+
def _get_access_token(secret_data: dict) -> str:
388386
try:
389-
credential = DefaultAzureCredential(logging_enable=True)
390-
scopes = ["https://management.azure.com/.default"]
391-
token_info = credential.get_token(*scopes)
392-
return token_info.token
387+
header = {
388+
"Content-Type": "application/x-www-form-urlencoded",
389+
}
390+
data = {
391+
"client_id": secret_data["client_id"],
392+
"client_secret": secret_data["client_secret"],
393+
"grant_type": "client_credentials",
394+
"resource": "https://management.azure.com",
395+
"scope": "https://management.azure.com/.default",
396+
}
397+
398+
response = requests.post(
399+
f"https://login.microsoftonline.com/{secret_data['tenant_id']}/oauth2/token",
400+
data=data,
401+
headers=header,
402+
)
403+
access_token = response.json().get("access_token")
404+
return access_token
393405
except Exception as e:
394406
_LOGGER.error(f"[ERROR] _get_access_token :{e}")
395407
raise ERROR_INVALID_TOKEN(token=e)

src/cloudforet/cost_analysis/manager/cost_manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def _get_additional_info(self, result: dict, options: dict, tenant_id: str = Non
342342
additional_info["Term"] = term
343343

344344
if azure_additional_info := result.get("additionalinfo"):
345-
azure_additional_info: dict = json.loads(azure_additional_info)
345+
azure_additional_info = json.loads(azure_additional_info)
346346
if ri_normalization_ratio := azure_additional_info.get(
347347
"RINormalizationRatio"
348348
):
@@ -426,7 +426,6 @@ def _make_benefit_cost_data(
426426
return benefit_costs_data
427427

428428
def _make_benefit_cost_info(self, result: dict, billed_at: str) -> dict:
429-
print(result)
430429
additional_info = {
431430
"Pricing Model": result.get("PricingModel"),
432431
"Benefit Id": result.get("BenefitId"),

0 commit comments

Comments
 (0)