Skip to content

Commit 6c8a793

Browse files
authored
Merge pull request #121 from lhhyung/master
Add customer_tenant_id support to get_benefit_data
2 parents e19dfc9 + 8901cbf commit 6c8a793

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

src/cloudforet/cost_analysis/connector/azure_cost_mgmt_connector.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ def query_usage_http(
151151
secret_data: dict,
152152
start: datetime,
153153
end: datetime,
154+
collect_scope: str,
154155
account_agreement_type: str,
155-
options=None,
156+
tenant_id: str,
156157
):
157158
try:
158159
billing_account_id = secret_data["billing_account_id"]
@@ -173,6 +174,9 @@ def query_usage_http(
173174
parameters["dataset"]["grouping"] = (
174175
BENEFIT_GROUPING + BENEFIT_GROUPING_MPA
175176
)
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+
176180
elif account_agreement_type == "EnterpriseAgreement":
177181
parameters["dataset"]["grouping"] = (
178182
BENEFIT_GROUPING + BENEFIT_GROUPING_EA

src/cloudforet/cost_analysis/manager/cost_manager.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ def get_benefit_data(
381381
domain_id: str,
382382
):
383383
self.azure_cm_connector.create_session(options, secret_data, schema)
384+
385+
collect_scope: str = task_options["collect_scope"]
386+
tenant_ids: list = self._get_tenant_ids(task_options, collect_scope)
384387
account_agreement_type = task_options.get("account_agreement_type")
385388
start: datetime = self._get_first_date_of_month(task_options["start"])
386389
end: datetime = datetime.utcnow()
@@ -390,17 +393,37 @@ def get_benefit_data(
390393
for time_period in monthly_time_period:
391394
_start = time_period["start"]
392395
_end = time_period["end"]
393-
response_stream = self.azure_cm_connector.query_usage_http(
394-
secret_data, _start, _end, account_agreement_type
396+
397+
start_time = time.time()
398+
_LOGGER.info(
399+
f"[get_benefit_data] {tenant_ids} start to collect data from {_start} to {_end}"
395400
)
401+
for idx, tenant_id in enumerate(tenant_ids):
402+
_LOGGER.info(
403+
f"[get_benefit_data] #{idx + 1} {tenant_id} tenant start to collect data from {_start} to {_end}, domain_id: {domain_id}"
404+
)
396405

397-
for results in response_stream:
398-
yield self._make_benefit_cost_data(
399-
results=results,
400-
end=_end,
401-
options=options,
406+
response_stream = self.azure_cm_connector.query_usage_http(
407+
secret_data,
408+
_start,
409+
_end,
410+
collect_scope,
411+
account_agreement_type,
412+
tenant_id,
402413
)
403414

415+
for results in response_stream:
416+
yield self._make_benefit_cost_data(
417+
results=results,
418+
end=_end,
419+
options=options,
420+
)
421+
422+
end_time = time.time()
423+
_LOGGER.info(
424+
f"[get_benefit_data] all collect is done in {int(end_time - start_time)} seconds"
425+
)
426+
404427
def _make_benefit_cost_data(
405428
self,
406429
results: dict,

src/cloudforet/cost_analysis/manager/job_manager.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,41 @@ def get_tasks(
171171

172172
# Benefit Job Task
173173
if options.get("cost_metric") == "AmortizedCost":
174-
tasks.append(
175-
{
176-
"task_options": {
177-
"start": start_month,
178-
"account_agreement_type": billing_account_agreement_type,
179-
"collect_scope": "billing_account_id",
180-
"billing_tenant_id": secret_data["tenant_id"],
181-
"is_benefit_job": True,
174+
if (
175+
billing_account_agreement_type == "MicrosoftPartnerAgreement"
176+
and options.get("collect_scope") == "customer_tenant_id"
177+
):
178+
customer_tenants, first_sync_tenants = self._get_customer_tenants(
179+
secret_data, linked_accounts
180+
)
181+
divided_customer_tenants = self._get_divided_customer_tenants(
182+
customer_tenants
183+
)
184+
for divided_customer_tenant_info in divided_customer_tenants:
185+
tasks.append(
186+
{
187+
"task_options": {
188+
"start": start_month,
189+
"account_agreement_type": billing_account_agreement_type,
190+
"collect_scope": "customer_tenant_id",
191+
"customer_tenants": divided_customer_tenant_info,
192+
"billing_tenant_id": secret_data["tenant_id"],
193+
"is_benefit_job": True,
194+
}
195+
}
196+
)
197+
else:
198+
tasks.append(
199+
{
200+
"task_options": {
201+
"start": start_month,
202+
"account_agreement_type": billing_account_agreement_type,
203+
"collect_scope": "billing_account_id",
204+
"billing_tenant_id": secret_data["tenant_id"],
205+
"is_benefit_job": True,
206+
}
182207
}
183-
}
184-
)
208+
)
185209

186210
elif secret_type == "USE_SERVICE_ACCOUNT_SECRET":
187211
task = self._get_task_scope_subscription(secret_data, options, start_month)

0 commit comments

Comments
 (0)