diff --git a/Dockerfile b/Dockerfile index 3499b99..93b5088 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM cloudforet/python-core:2.0.90 +FROM cloudforet/python-core:2 ENV PYTHONUNBUFFERED 1 ENV SPACEONE_PORT 50051 diff --git a/src/cloudforet/cost_analysis/conf/cost_conf.py b/src/cloudforet/cost_analysis/conf/cost_conf.py index 90da05f..96f1670 100644 --- a/src/cloudforet/cost_analysis/conf/cost_conf.py +++ b/src/cloudforet/cost_analysis/conf/cost_conf.py @@ -28,8 +28,6 @@ ] } BENEFIT_GROUPING = [ - {"type": "Dimension", "name": "CustomerTenantId"}, - {"type": "Dimension", "name": "CustomerName"}, {"type": "Dimension", "name": "PricingModel"}, {"type": "Dimension", "name": "Frequency"}, {"type": "Dimension", "name": "BenefitId"}, @@ -40,6 +38,20 @@ {"type": "Dimension", "name": "MeterCategory"}, ] +BENEFIT_GROUPING_MPA = [ + {"type": "Dimension", "name": "CustomerTenantId"}, + {"type": "Dimension", "name": "CustomerName"}, +] + +BENEFIT_GROUPING_EA = [ + {"type": "Dimension", "name": "DepartmentName"}, + {"type": "Dimension", "name": "EnrollmentAccountName"}, +] + +BENEFIT_GROUPING_MCA = [ + {"type": "Dimension", "name": "TenantId"}, +] + GROUPING_EA_AGREEMENT_OPTION = [ {"type": "Dimension", "name": "DepartmentName"}, {"type": "Dimension", "name": "EnrollmentAccountName"}, 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 eef8425..9f73d03 100644 --- a/src/cloudforet/cost_analysis/connector/azure_cost_mgmt_connector.py +++ b/src/cloudforet/cost_analysis/connector/azure_cost_mgmt_connector.py @@ -165,7 +165,12 @@ def list_billing_accounts(self) -> list: return billing_accounts_info def query_usage_http( - self, secret_data: dict, start: datetime, end: datetime, options=None + self, + secret_data: dict, + start: datetime, + end: datetime, + account_agreement_type: str, + options=None, ): try: billing_account_id = secret_data["billing_account_id"] @@ -183,6 +188,12 @@ def query_usage_http( "filter": BENEFIT_FILTER, }, } + if account_agreement_type == "MicrosoftPartnerAgreement": + parameters["dataset"]["grouping"].extend(BENEFIT_GROUPING_MPA) + elif account_agreement_type == "EnterpriseAgreement": + parameters["dataset"]["grouping"].extend(BENEFIT_GROUPING_EA) + else: + parameters["dataset"]["grouping"].extend(BENEFIT_GROUPING_MCA) while self.next_link: url = self.next_link @@ -240,10 +251,12 @@ def get_cost_data(self, blobs: list, options: dict) -> list: total_cost_count = 0 for blob in blobs: - cost_csv = self._download_cost_data(blob) + response = self._download_cost_data(blob) df_chunk = pd.read_csv( - StringIO(cost_csv), low_memory=False, chunksize=_PAGE_SIZE + StringIO(response.text), + low_memory=False, + chunksize=_PAGE_SIZE, ) for df in df_chunk: @@ -253,7 +266,7 @@ def get_cost_data(self, blobs: list, options: dict) -> list: total_cost_count += len(costs_data) yield costs_data del df_chunk - del cost_csv + del response _LOGGER.debug(f"[get_cost_data] total_cost_count: {total_cost_count}") def list_by_billing_account(self): @@ -341,12 +354,13 @@ def _retry_request(self, response, url, headers, json, retry_count, method="post raise e @staticmethod - def _download_cost_data(blob: dict) -> str: + def _download_cost_data(blob: dict) -> requests.Response: try: response = requests.get(blob.get("blob_link")) if response.status_code != 200: raise ERROR_CONNECTOR_CALL_API(reason=f"{response.reason}") - return response.text + _LOGGER.debug(f"[_download_cost_data] response: {response}") + return response except Exception as e: _LOGGER.error(f"[_download_cost_data] download error: {e}", exc_info=True) raise e diff --git a/src/cloudforet/cost_analysis/manager/cost_manager.py b/src/cloudforet/cost_analysis/manager/cost_manager.py index cc674c4..52b2cd4 100644 --- a/src/cloudforet/cost_analysis/manager/cost_manager.py +++ b/src/cloudforet/cost_analysis/manager/cost_manager.py @@ -65,6 +65,7 @@ def get_benefit_data( domain_id: str, ): self.azure_cm_connector.create_session(options, secret_data, schema) + account_agreement_type = task_options.get("agreement_type") start: datetime = self._get_first_date_of_month(task_options["start"]) end: datetime = datetime.utcnow() @@ -74,7 +75,7 @@ def get_benefit_data( _start = time_period["start"] _end = time_period["end"] response_stream = self.azure_cm_connector.query_usage_http( - secret_data, _start, _end + secret_data, _start, _end, account_agreement_type ) for results in response_stream: @@ -452,7 +453,13 @@ def _get_additional_info(self, result: dict, options: dict, tenant_id: str = Non additional_info["Meter Name"] = result["metername"] if result.get("term") != "" and result.get("term"): - additional_info["Term"] = result["term"] + term = result.get("term").trim().lower() + if term in ["1year"]: + term = 12 + elif term in ["3year"]: + term = 36 + + additional_info["Term"] = term if options.get("cost_metric") == "AmortizedCost": if result.get("pricingmodel") in ["Reservation", "SavingsPlan"]: