Skip to content

Commit 2dfeb1c

Browse files
committed
feat: add instance type for VirtualMachines at additional info
Signed-off-by: ImMin5 <[email protected]>
1 parent 2591bad commit 2dfeb1c

File tree

1 file changed

+56
-33
lines changed

1 file changed

+56
-33
lines changed

src/cloudforet/cost_analysis/manager/cost_manager.py

+56-33
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, *args, **kwargs):
2424
)
2525

2626
def get_linked_accounts(
27-
self, options: dict, secret_data: dict, domain_id: str, schema
27+
self, options: dict, secret_data: dict, domain_id: str, schema
2828
) -> list:
2929
self.azure_cm_connector.create_session(options, secret_data, schema)
3030
billing_account_info = self.azure_cm_connector.get_billing_account()
@@ -51,11 +51,12 @@ def get_linked_accounts(
5151
return accounts_info
5252

5353
def get_data(
54-
self, options: dict, secret_data: dict, schema, task_options: dict
54+
self, options: dict, secret_data: dict, schema, task_options: dict
5555
) -> list:
5656
self.azure_cm_connector.create_session(options, secret_data, schema)
5757
self._check_task_options(task_options)
5858

59+
agreement_type: str = task_options.get("agreement_type")
5960
collect_scope: str = task_options["collect_scope"]
6061
tenant_ids: list = self._get_tenant_ids(task_options, collect_scope)
6162
start: datetime = self._get_first_date_of_month(task_options["start"])
@@ -85,7 +86,11 @@ def get_data(
8586
response_stream = self.azure_cm_connector.get_cost_data(blobs, options)
8687
for results in response_stream:
8788
yield self._make_cost_data(
88-
results=results, end=_end, tenant_id=tenant_id, options=options
89+
results=results,
90+
end=_end,
91+
tenant_id=tenant_id,
92+
options=options,
93+
agreement_type=agreement_type,
8994
)
9095
_LOGGER.info(
9196
f"[get_data] #{idx + 1} {tenant_id} tenant collect is done"
@@ -97,7 +102,12 @@ def get_data(
97102
yield []
98103

99104
def _make_cost_data(
100-
self, results: list, end: datetime, options: dict, tenant_id: str = None
105+
self,
106+
results: list,
107+
end: datetime,
108+
options: dict,
109+
tenant_id: str = None,
110+
agreement_type: str = None,
101111
) -> list:
102112
"""Source Data Model"""
103113

@@ -113,7 +123,13 @@ def _make_cost_data(
113123
if self._exclude_cost_data_with_options(result, options):
114124
continue
115125

116-
data = self._make_data_info(result, billed_date, options, tenant_id)
126+
data = self._make_data_info(
127+
result,
128+
billed_date,
129+
options,
130+
tenant_id,
131+
agreement_type=agreement_type,
132+
)
117133
costs_data.append(data)
118134

119135
except Exception as e:
@@ -123,20 +139,27 @@ def _make_cost_data(
123139
return costs_data
124140

125141
def _make_data_info(
126-
self, result: dict, billed_date: str, options: dict, tenant_id: str = None
142+
self,
143+
result: dict,
144+
billed_date: str,
145+
options: dict,
146+
tenant_id: str = None,
147+
agreement_type: str = None,
127148
):
128-
additional_info = self._get_additional_info(result, options, tenant_id)
129-
cost = self._get_cost_from_result_with_options(result, options)
130-
usage_quantity = self._convert_str_to_float_format(result.get("quantity", 0.0))
131-
usage_type = result.get("metername", "")
132-
usage_unit = str(result.get("unitofmeasure", ""))
133-
region_code = self._get_region_code(result.get("resourcelocation", ""))
134-
product = result.get("metercategory", "")
135-
tags = self._convert_tags_str_to_dict(result.get("tags"))
149+
additional_info: dict = self._get_additional_info(result, options, tenant_id)
150+
cost: float = self._get_cost_from_result_with_options(result, options)
151+
usage_quantity: float = self._convert_str_to_float_format(
152+
result.get("quantity", 0.0)
153+
)
154+
usage_type: str = result.get("metername", "")
155+
usage_unit: str = str(result.get("unitofmeasure", ""))
156+
region_code: str = self._get_region_code(result.get("resourcelocation", ""))
157+
product: str = result.get("metercategory", "")
158+
tags: dict = self._convert_tags_str_to_dict(result.get("tags"))
136159

137160
aggregate_data = self._get_aggregate_data(result, options)
138161
# Set Network Traffic Cost at Additional Info
139-
additional_info = self._set_network_traffic_cost(
162+
additional_info: dict = self._set_network_traffic_cost(
140163
additional_info, product, usage_type
141164
)
142165

@@ -169,7 +192,7 @@ def _get_additional_info(self, result: dict, options: dict, tenant_id: str = Non
169192
additional_info["Tenant Id"] = tenant_id
170193
additional_info["Subscription Id"] = result.get("subscriptionid", "Shared")
171194

172-
if meter_category == "Virtual Machines" and "Meter" in result:
195+
if meter_category == "Virtual Machines":
173196
additional_info["Instance Type"] = result["metername"]
174197

175198
if result.get("resourcegroupname") != "" and result.get("resourcegroupname"):
@@ -188,8 +211,8 @@ def _get_additional_info(self, result: dict, options: dict, tenant_id: str = Non
188211
additional_info["Benefit Name"] = benefit_name
189212

190213
if (
191-
result.get("pricingmodel") == "Reservation"
192-
and result["metercategory"] == ""
214+
result.get("pricingmodel") == "Reservation"
215+
and result["metercategory"] == ""
193216
):
194217
result["metercategory"] = self._set_product_from_benefit_name(
195218
benefit_name
@@ -200,14 +223,14 @@ def _get_additional_info(self, result: dict, options: dict, tenant_id: str = Non
200223
if result.get("metersubcategory") != "" and result.get("metersubcategory"):
201224
additional_info["Meter SubCategory"] = result.get("metersubcategory")
202225
if (
203-
result.get("pricingmodel") == "OnDemand"
204-
and result.get("metercategory") == ""
226+
result.get("pricingmodel") == "OnDemand"
227+
and result.get("metercategory") == ""
205228
):
206229
result["metercategory"] = result.get("metercategory")
207230

208231
if result.get("customername") is None:
209232
if result.get("invoicesectionname") != "" and result.get(
210-
"invoicesectionname"
233+
"invoicesectionname"
211234
):
212235
additional_info["Department Name"] = result.get("invoicesectionname")
213236
elif result.get("departmentname") != "" and result.get("departmentname"):
@@ -216,15 +239,15 @@ def _get_additional_info(self, result: dict, options: dict, tenant_id: str = Non
216239
if result.get("accountname") != "" and result.get("accountname"):
217240
additional_info["Enrollment Account Name"] = result["accountname"]
218241
elif result.get("enrollmentaccountname") != "" and result.get(
219-
"enrollmentaccountname"
242+
"enrollmentaccountname"
220243
):
221244
additional_info["Enrollment Account Name"] = result["enrollmentaccountname"]
222245

223246
collect_resource_id = options.get("collect_resource_id", False)
224247
if (
225-
collect_resource_id
226-
and result.get("resourceid") != ""
227-
and result.get("resourceid")
248+
collect_resource_id
249+
and result.get("resourceid") != ""
250+
and result.get("resourceid")
228251
):
229252
additional_info["Resource Id"] = result["resourceid"]
230253
additional_info["Resource Name"] = result["resourceid"].split("/")[-1]
@@ -308,10 +331,10 @@ def _get_tenant_ids(task_options: dict, collect_scope: str) -> list:
308331

309332
@staticmethod
310333
def _make_scope(
311-
secret_data: dict,
312-
task_options: dict,
313-
collect_scope: str,
314-
customer_tenant_id: str = None,
334+
secret_data: dict,
335+
task_options: dict,
336+
collect_scope: str,
337+
customer_tenant_id: str = None,
315338
):
316339
if collect_scope == "subscription_id":
317340
subscription_id = task_options["subscription_id"]
@@ -412,7 +435,7 @@ def _convert_date_format_to_utc(date_format: str) -> datetime:
412435
return datetime.strptime(date_format, "%Y-%m-%d").replace(tzinfo=timezone.utc)
413436

414437
def _make_monthly_time_period(
415-
self, start_date: datetime, end_date: datetime
438+
self, start_date: datetime, end_date: datetime
416439
) -> list:
417440
monthly_time_period = []
418441
current_date = end_date
@@ -442,7 +465,7 @@ def _make_monthly_time_period(
442465

443466
@staticmethod
444467
def _get_linked_customer_tenants(
445-
secret_data: dict, billing_accounts_info: list
468+
secret_data: dict, billing_accounts_info: list
446469
) -> list:
447470
customer_tenants = secret_data.get("customer_tenants", [])
448471
if not customer_tenants:
@@ -455,7 +478,7 @@ def _get_linked_customer_tenants(
455478

456479
@staticmethod
457480
def _make_accounts_info_from_customer_tenants(
458-
billing_accounts_info: list, customer_tenants: list
481+
billing_accounts_info: list, customer_tenants: list
459482
) -> list:
460483
accounts_info = []
461484
for billing_account_info in billing_accounts_info:
@@ -493,7 +516,7 @@ def _exclude_cost_data_with_options(result: dict, options: dict) -> bool:
493516

494517
@staticmethod
495518
def _set_network_traffic_cost(
496-
additional_info: dict, product: str, usage_type: str
519+
additional_info: dict, product: str, usage_type: str
497520
) -> dict:
498521
if product in ["Bandwidth", "Content Delivery Network"]:
499522
additional_info["Usage Type Details"] = usage_type

0 commit comments

Comments
 (0)