-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_price.py
76 lines (60 loc) · 1.91 KB
/
get_price.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
import json
import sys
import requests
from tabulate import tabulate
def build_pricing_table(json_data, table_data):
for item in json_data["Items"]:
meter = item["meterName"]
table_data.append(
[
item["armSkuName"],
item["retailPrice"],
item["currencyCode"],
item["unitOfMeasure"],
item["armRegionName"],
meter,
item["productName"],
]
)
def get_user_input():
if len(sys.argv) < 3:
print("Usage: get_price.py <region> <sku>")
sys.exit(1)
selected_region = sys.argv[1]
selected_sku = sys.argv[2]
return selected_region, selected_sku
def main():
selected_region, selected_sku = get_user_input()
table_data = []
table_header = [
"SKU",
"Retail Price",
"Currency",
"Unit",
"Region",
"Meter",
"Product Name",
]
table_data.append(table_header)
api_url = (
"https://prices.azure.com/api/retail/prices?api-version=2021-10-01-preview"
)
query = f"armRegionName eq '{selected_region}' and armSkuName eq '{selected_sku}' and priceType eq 'Consumption' "
response = requests.get(api_url, params={"$filter": query})
json_data = json.loads(response.text)
if json_data["Items"] == []:
print(
f"No results found for region '{selected_region}' and sku '{selected_sku}'"
)
sys.exit(1)
build_pricing_table(json_data, table_data)
nextPage = json_data["NextPageLink"]
while nextPage:
response = requests.get(nextPage)
json_data = json.loads(response.text)
nextPage = json_data["NextPageLink"]
build_pricing_table(json_data, table_data)
print(tabulate(table_data, headers="firstrow", tablefmt="rounded_outline"))
if __name__ == "__main__":
main()