Skip to content

Commit a433ce3

Browse files
authored
Merge pull request #113 from lhhyung/master
Add RI/SP cost display as list price in PAYG
2 parents 7d36b1a + cae660b commit a433ce3

File tree

4 files changed

+43
-31
lines changed

4 files changed

+43
-31
lines changed

src/cloudforet/cost_analysis/connector/azure_cost_mgmt_connector.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,23 @@
22
import os
33
import tempfile
44
import time
5-
import requests
6-
import pandas as pd
7-
import numpy as np
8-
import re
9-
105
from datetime import datetime
116
from functools import wraps
127
from io import BytesIO
13-
from typing import get_type_hints, Union, Any, Generator
8+
from typing import get_type_hints, Union, Any
149

10+
import numpy as np
11+
import pandas as pd
12+
import requests
13+
from azure.core.exceptions import ResourceNotFoundError, HttpResponseError
1514
from azure.identity import DefaultAzureCredential
1615
from azure.mgmt.billing import BillingManagementClient
17-
from azure.mgmt.costmanagement import CostManagementClient
1816
from azure.mgmt.consumption import ConsumptionManagementClient
19-
from azure.core.exceptions import (
20-
ResourceNotFoundError,
21-
HttpResponseError,
22-
ServiceResponseError,
23-
)
17+
from azure.mgmt.costmanagement import CostManagementClient
2418
from spaceone.core.connector import BaseConnector
2519

26-
from cloudforet.cost_analysis.error.cost import *
2720
from cloudforet.cost_analysis.conf.cost_conf import *
21+
from cloudforet.cost_analysis.error.cost import *
2822

2923
__all__ = ["AzureCostMgmtConnector"]
3024

@@ -252,7 +246,8 @@ def list_by_billing_account(self):
252246

253247
@staticmethod
254248
def get_retail_price(meter_id: str, currency: str = "USD"):
255-
url = f"https://prices.azure.com/api/retail/prices?currencyCode={currency}&$filter=priceType eq 'Consumption' and meterId eq '{meter_id}'"
249+
# url = f"https://prices.azure.com/api/retail/prices?currencyCode={currency}&$filter=priceType eq 'Consumption' and meterId eq '{meter_id}'"
250+
url = f"https://prices.azure.com/api/retail/prices?currencyCode={currency}&$filter=meterId eq '{meter_id}'"
256251
try:
257252
response = requests.get(url=url)
258253
return response.json()

src/cloudforet/cost_analysis/manager/cost_manager.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import calendar
2-
import logging
32
import json
3+
import logging
44
import time
5-
import pandas as pd
6-
from typing import Union, Tuple, Any, Generator
75
from datetime import datetime, timezone
6+
from typing import Union
87

8+
import pandas as pd
99
from spaceone.core.error import *
1010
from spaceone.core.manager import BaseManager
1111

12+
from cloudforet.cost_analysis.conf.cost_conf import *
1213
from cloudforet.cost_analysis.connector.azure_cost_mgmt_connector import (
1314
AzureCostMgmtConnector,
1415
)
15-
from cloudforet.cost_analysis.conf.cost_conf import *
1616

1717
_LOGGER = logging.getLogger("spaceone")
1818

@@ -462,6 +462,7 @@ def _make_benefit_cost_info(self, result: dict, billed_at: str) -> dict:
462462
result.get("UsageQuantity", 0.0)
463463
)
464464
actual_cost = self._convert_str_to_float_format(result.get("Cost", 0.0))
465+
465466
data = {
466467
"cost": 0,
467468
"usage_quantity": usage_quantity,
@@ -474,6 +475,7 @@ def _make_benefit_cost_info(self, result: dict, billed_at: str) -> dict:
474475
},
475476
"additional_info": additional_info,
476477
}
478+
477479
return data
478480

479481
@staticmethod
@@ -517,14 +519,22 @@ def _get_cost_from_result_with_options(self, result: dict, options: dict) -> flo
517519
cost_pay_as_you_go = 0.0
518520

519521
if options.get("include_reservation_cost_at_payg", False):
520-
pricing_model = result.get("pricingmodel")
521-
charge_type = result.get("chargetype")
522-
523-
if (
524-
pricing_model in ["Reservation", "SavingsPlan"]
525-
and charge_type == "Purchase"
526-
):
527-
cost_pay_as_you_go = result.get("costinbillingcurrency", 0.0)
522+
if options.get("cost_metric") == "AmortizedCost":
523+
pricing_model = result.get("pricingmodel")
524+
charge_type = result.get("chargetype")
525+
if (
526+
pricing_model in ["Reservation", "SavingsPlan"]
527+
and charge_type == "Usage"
528+
):
529+
cost_pay_as_you_go = self._get_retail_cost(result)
530+
elif options.get("cost_metric") == "ActualCost":
531+
pricing_model = result.get("pricingmodel")
532+
charge_type = result.get("chargetype")
533+
if (
534+
pricing_model in ["Reservation", "SavingsPlan"]
535+
and charge_type == "Purchase"
536+
):
537+
cost_pay_as_you_go = self._get_retail_cost(result)
528538

529539
return cost_pay_as_you_go
530540

@@ -562,12 +572,11 @@ def _get_aggregate_data(self, result: dict, options: dict) -> dict:
562572

563573
return aggregate_data
564574

565-
def _get_saved_cost(self, result: dict, cost: float) -> float:
575+
def _get_retail_cost(self, result: dict) -> float:
566576
exchange_rate = 1.0
567-
saved_cost = 0
568-
currency = result.get("billingcurrency", "USD")
569577
meter_id = result.get("meterid")
570578
product_id = result.get("productid")
579+
currency = result.get("billingcurrency", "USD")
571580
quantity = self._convert_str_to_float_format(result.get("quantity", 0.0))
572581

573582
if not self.retail_price_map.get(f"{meter_id}:{product_id}"):
@@ -578,6 +587,13 @@ def _get_saved_cost(self, result: dict, cost: float) -> float:
578587

579588
unit_price = self.retail_price_map[f"{meter_id}:{product_id}"]
580589

590+
retail_cost = exchange_rate * quantity * unit_price
591+
592+
return retail_cost
593+
594+
def _get_saved_cost(self, result: dict, cost: float) -> float:
595+
saved_cost = 0
596+
581597
# if currency != "USD" and quantity > 0:
582598
# cost_in_billing_currency = self._convert_str_to_float_format(
583599
# result.get("costinbillingcurrency", 0.0)
@@ -591,7 +607,7 @@ def _get_saved_cost(self, result: dict, cost: float) -> float:
591607
# else:
592608
# exchange_rate = cost_in_billing_currency / cost_in_pricing_currency
593609

594-
retail_cost = exchange_rate * quantity * unit_price
610+
retail_cost = self._get_retail_cost(result)
595611
if retail_cost:
596612
saved_cost = retail_cost - cost
597613

src/cloudforet/cost_analysis/manager/data_source_manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from spaceone.core.error import ERROR_INVALID_ARGUMENT
55
from spaceone.core.manager import BaseManager
6+
67
from cloudforet.cost_analysis.connector.azure_cost_mgmt_connector import (
78
AzureCostMgmtConnector,
89
)

src/cloudforet/cost_analysis/manager/job_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from typing import Tuple, Union
55

66
from dateutil.relativedelta import relativedelta
7-
87
from spaceone.core.manager import BaseManager
8+
99
from cloudforet.cost_analysis.conf.cost_conf import SECRET_TYPE_DEFAULT
1010
from cloudforet.cost_analysis.connector import AzureCostMgmtConnector
1111
from cloudforet.cost_analysis.error.cost import *

0 commit comments

Comments
 (0)