Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,23 @@ def query_usage_http(
tenant_id: str,
):
try:
billing_account_id = secret_data["billing_account_id"]
api_version = "2023-11-01"
self.next_link = f"https://management.azure.com/providers/Microsoft.Billing/billingAccounts/{billing_account_id}/providers/Microsoft.CostManagement/query?api-version={api_version}"
api_version = "2025-03-01"

# Set next_link for collect_scope
# if collect_scope == "subscription_id":
# subscription_id = secret_data.get("subscription_id")
# self.next_link = f"https://management.azure.com/subscriptions/{subscription_id}/providers/Microsoft.CostManagement/query?api-version={api_version}"
# else:
billing_account_id = secret_data.get("billing_account_id")
if (
account_agreement_type == "MicrosoftPartnerAgreement"
and collect_scope == "customer_tenant_id"
):
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}"
else:
self.next_link = f"https://management.azure.com/providers/Microsoft.Billing/billingAccounts/{billing_account_id}/providers/Microsoft.CostManagement/query?api-version={api_version}"

# Set parameters for the cost management query
parameters = {
"type": TYPE,
"timeframe": TIMEFRAME,
Expand All @@ -170,21 +183,21 @@ def query_usage_http(
"filter": BENEFIT_FILTER,
},
}

if account_agreement_type == "MicrosoftPartnerAgreement":
parameters["dataset"]["grouping"] = (
BENEFIT_GROUPING + BENEFIT_GROUPING_MPA
)
if collect_scope == "customer_tenant_id":
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}"

elif account_agreement_type == "EnterpriseAgreement":
parameters["dataset"]["grouping"] = (
BENEFIT_GROUPING + BENEFIT_GROUPING_EA
)
else:
elif account_agreement_type == "MicrosoftCustomerAgreement":
parameters["dataset"]["grouping"] = (
BENEFIT_GROUPING + BENEFIT_GROUPING_MCA
)
else:
parameters["dataset"]["grouping"] = BENEFIT_GROUPING

while self.next_link:
url = self.next_link
Expand Down
11 changes: 8 additions & 3 deletions src/cloudforet/cost_analysis/manager/cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ def get_benefit_data(

collect_scope: str = task_options["collect_scope"]
tenant_ids: list = self._get_tenant_ids(task_options, collect_scope)
billing_tenant_id = task_options["billing_tenant_id"]
account_agreement_type = task_options.get("account_agreement_type")
start: datetime = self._get_first_date_of_month(task_options["start"])
end: datetime = datetime.utcnow()
Expand Down Expand Up @@ -417,6 +418,7 @@ def get_benefit_data(
results=results,
end=_end,
options=options,
billing_tenant_id=billing_tenant_id,
)

end_time = time.time()
Expand All @@ -429,7 +431,7 @@ def _make_benefit_cost_data(
results: dict,
end: datetime,
options: dict,
tenant_id: str = None,
billing_tenant_id: str = None,
account_agreement_type: str = None,
) -> list:
benefit_costs_data = []
Expand All @@ -446,7 +448,9 @@ def _make_benefit_cost_data(
if not billed_at:
continue

data = self._make_benefit_cost_info(cb_result, options, billed_at)
data = self._make_benefit_cost_info(
cb_result, options, billing_tenant_id, billed_at
)
benefit_costs_data.append(data)

except Exception as e:
Expand All @@ -456,7 +460,7 @@ def _make_benefit_cost_data(
return benefit_costs_data

def _make_benefit_cost_info(
self, result: dict, options: dict, billed_at: str
self, result: dict, options: dict, billing_tenant_id: str, billed_at: str
) -> dict:
cost = 0

Expand All @@ -467,6 +471,7 @@ def _make_benefit_cost_info(
"Reservation Id": result.get("ReservationId"),
"Reservation Name": result.get("ReservationName"),
"Charge Type": result.get("ChargeType"),
"Billing Tenant Id": billing_tenant_id,
}

if subscription_id := result.get("SubscriptionId"):
Expand Down
15 changes: 15 additions & 0 deletions src/cloudforet/cost_analysis/manager/job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ def get_tasks(
}
}
)
elif options.get("collect_scope") == "subscription_id":
pass
# subscription_id = secret_data["subscription_id"]
# tenant_id = secret_data.get("tenant_id")
# tasks.append(
# {
# "task_options": {
# "collect_scope": "subscription_id",
# "start": start_month,
# "subscription_id": subscription_id,
# "tenant_id": tenant_id,
# "is_benefit_job": True,
# }
# }
# )
else:
tasks.append(
{
Expand Down
Loading