How can I programmatically retrieve all the hourly prices for a specific virtual machine SKU?

Bill Baer 20 Reputation points
2023-06-29T06:41:37.9433333+00:00

I'm attempting to write a python program to extract all the retail prices for a specific SKU using the Azure retail APIhttps://learn.microsoft.com/en-us/rest/api/cost-management/retail-prices/azure-retail-prices. The sample code provided in this link for which I copied below works quite well to retrieve all the SKUs matching a virtual machine critieria, but all of the retail prices are for 1 hourly unit. I want to retrieve all the retail prices for hourly unit ranges 1 - 300 hours in a month for a single SKU. I'm not seeing a search criteria to specify the hour and was curious if anyone else knows how to do this?

    main()/usr/bin/env python3
import requests
import json
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['unitOfMeasure'], item['armRegionName'], meter, item['productName']])
Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
3,577 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2023-07-05T16:22:25.47+00:00

    The retail API is more of a price sheet than a calculator. So, in order to determine retail prices based on an hourly range, you can adjust your code to include a computed column that multiplies the hourly rate by the unit hour.

    def build_pricing_table(json_data, df):
        for item in json_data['Items']:
            meter = item['meterName']
            for hours in range(1, 301):
                computed_value = hours * item['unitOfMeasure'] * item['retailPrice']
                df = df.append({
                    'SKU': item['armSkuName'],
                    'Retail Price': item['retailPrice'],
                    'Unit of Measure': item['unitOfMeasure'],
                    'Region': item['armRegionName'],
                    'Meter': meter,
                    'Product Name': item['productName'],
                    'Computed Value': computed_value
                }, ignore_index=True)
        return df
    
    

    The above code block utilizes a DataFrame rather than table_data but the principles are the same.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.