diff --git a/src/cloudforet/cost_analysis/connector/azure_cost_mgmt_connector.py b/src/cloudforet/cost_analysis/connector/azure_cost_mgmt_connector.py index 8d40e74..4413055 100644 --- a/src/cloudforet/cost_analysis/connector/azure_cost_mgmt_connector.py +++ b/src/cloudforet/cost_analysis/connector/azure_cost_mgmt_connector.py @@ -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, @@ -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 diff --git a/src/cloudforet/cost_analysis/manager/cost_manager.py b/src/cloudforet/cost_analysis/manager/cost_manager.py index 2c0f643..fb5b8b4 100644 --- a/src/cloudforet/cost_analysis/manager/cost_manager.py +++ b/src/cloudforet/cost_analysis/manager/cost_manager.py @@ -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() @@ -417,6 +418,7 @@ def get_benefit_data( results=results, end=_end, options=options, + billing_tenant_id=billing_tenant_id, ) end_time = time.time() @@ -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 = [] @@ -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: @@ -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 @@ -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"): diff --git a/src/cloudforet/cost_analysis/manager/job_manager.py b/src/cloudforet/cost_analysis/manager/job_manager.py index 8e79ac5..ee16257 100644 --- a/src/cloudforet/cost_analysis/manager/job_manager.py +++ b/src/cloudforet/cost_analysis/manager/job_manager.py @@ -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( {