Skip to content

Commit 5512a8a

Browse files
authored
Merge pull request #122 from lhhyung/master
Add billing_tenant_id to benefit_cost_data
2 parents 055efdb + 323cd8f commit 5512a8a

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

src/cloudforet/cost_analysis/connector/azure_cost_mgmt_connector.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,23 @@ def query_usage_http(
156156
tenant_id: str,
157157
):
158158
try:
159-
billing_account_id = secret_data["billing_account_id"]
160-
api_version = "2023-11-01"
161-
self.next_link = f"https://management.azure.com/providers/Microsoft.Billing/billingAccounts/{billing_account_id}/providers/Microsoft.CostManagement/query?api-version={api_version}"
159+
api_version = "2025-03-01"
160+
161+
# Set next_link for collect_scope
162+
# if collect_scope == "subscription_id":
163+
# subscription_id = secret_data.get("subscription_id")
164+
# self.next_link = f"https://management.azure.com/subscriptions/{subscription_id}/providers/Microsoft.CostManagement/query?api-version={api_version}"
165+
# else:
166+
billing_account_id = secret_data.get("billing_account_id")
167+
if (
168+
account_agreement_type == "MicrosoftPartnerAgreement"
169+
and collect_scope == "customer_tenant_id"
170+
):
171+
self.next_link = f"https://management.azure.com/providers/Microsoft.Billing/billingAccounts/{billing_account_id}/customers/{tenant_id}/providers/Microsoft.CostManagement/query?api-version={api_version}"
172+
else:
173+
self.next_link = f"https://management.azure.com/providers/Microsoft.Billing/billingAccounts/{billing_account_id}/providers/Microsoft.CostManagement/query?api-version={api_version}"
162174

175+
# Set parameters for the cost management query
163176
parameters = {
164177
"type": TYPE,
165178
"timeframe": TIMEFRAME,
@@ -170,21 +183,21 @@ def query_usage_http(
170183
"filter": BENEFIT_FILTER,
171184
},
172185
}
186+
173187
if account_agreement_type == "MicrosoftPartnerAgreement":
174188
parameters["dataset"]["grouping"] = (
175189
BENEFIT_GROUPING + BENEFIT_GROUPING_MPA
176190
)
177-
if collect_scope == "customer_tenant_id":
178-
self.next_link = f"https://management.azure.com/providers/Microsoft.Billing/billingAccounts/{billing_account_id}/customers/{tenant_id}/providers/Microsoft.CostManagement/query?api-version={api_version}"
179-
180191
elif account_agreement_type == "EnterpriseAgreement":
181192
parameters["dataset"]["grouping"] = (
182193
BENEFIT_GROUPING + BENEFIT_GROUPING_EA
183194
)
184-
else:
195+
elif account_agreement_type == "MicrosoftCustomerAgreement":
185196
parameters["dataset"]["grouping"] = (
186197
BENEFIT_GROUPING + BENEFIT_GROUPING_MCA
187198
)
199+
else:
200+
parameters["dataset"]["grouping"] = BENEFIT_GROUPING
188201

189202
while self.next_link:
190203
url = self.next_link

src/cloudforet/cost_analysis/manager/cost_manager.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ def get_benefit_data(
384384

385385
collect_scope: str = task_options["collect_scope"]
386386
tenant_ids: list = self._get_tenant_ids(task_options, collect_scope)
387+
billing_tenant_id = task_options["billing_tenant_id"]
387388
account_agreement_type = task_options.get("account_agreement_type")
388389
start: datetime = self._get_first_date_of_month(task_options["start"])
389390
end: datetime = datetime.utcnow()
@@ -417,6 +418,7 @@ def get_benefit_data(
417418
results=results,
418419
end=_end,
419420
options=options,
421+
billing_tenant_id=billing_tenant_id,
420422
)
421423

422424
end_time = time.time()
@@ -429,7 +431,7 @@ def _make_benefit_cost_data(
429431
results: dict,
430432
end: datetime,
431433
options: dict,
432-
tenant_id: str = None,
434+
billing_tenant_id: str = None,
433435
account_agreement_type: str = None,
434436
) -> list:
435437
benefit_costs_data = []
@@ -446,7 +448,9 @@ def _make_benefit_cost_data(
446448
if not billed_at:
447449
continue
448450

449-
data = self._make_benefit_cost_info(cb_result, options, billed_at)
451+
data = self._make_benefit_cost_info(
452+
cb_result, options, billing_tenant_id, billed_at
453+
)
450454
benefit_costs_data.append(data)
451455

452456
except Exception as e:
@@ -456,7 +460,7 @@ def _make_benefit_cost_data(
456460
return benefit_costs_data
457461

458462
def _make_benefit_cost_info(
459-
self, result: dict, options: dict, billed_at: str
463+
self, result: dict, options: dict, billing_tenant_id: str, billed_at: str
460464
) -> dict:
461465
cost = 0
462466

@@ -467,6 +471,7 @@ def _make_benefit_cost_info(
467471
"Reservation Id": result.get("ReservationId"),
468472
"Reservation Name": result.get("ReservationName"),
469473
"Charge Type": result.get("ChargeType"),
474+
"Billing Tenant Id": billing_tenant_id,
470475
}
471476

472477
if subscription_id := result.get("SubscriptionId"):

src/cloudforet/cost_analysis/manager/job_manager.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,21 @@ def get_tasks(
194194
}
195195
}
196196
)
197+
elif options.get("collect_scope") == "subscription_id":
198+
pass
199+
# subscription_id = secret_data["subscription_id"]
200+
# tenant_id = secret_data.get("tenant_id")
201+
# tasks.append(
202+
# {
203+
# "task_options": {
204+
# "collect_scope": "subscription_id",
205+
# "start": start_month,
206+
# "subscription_id": subscription_id,
207+
# "tenant_id": tenant_id,
208+
# "is_benefit_job": True,
209+
# }
210+
# }
211+
# )
197212
else:
198213
tasks.append(
199214
{

0 commit comments

Comments
 (0)