.net library (.net standard 1.4 and .net framework 4.5.2) that reads data from the azure rest billing apis. It can be used to:
- Read data from the Azure billing RateCard API
- Read data from the Azure billing Usage API
- Read data from both APIs and combine them to calculate the costs (without tax).
This library is also available as NuGet Package (https://www.nuget.org/packages/CodeHollow.AzureBillingApi/)
A detailed description can be found at my blog: https://codehollow.com/2017/02/using-the-azure-billing-api-to-calculate-the-costs/
- Add a new application to active directory:
- Open https://portal.azure.com/, navigate to the Azure Active Directory and App Registrations
- Add a new native application
- Add delegated permissions for Windows Azure Service Management
- App registrations - Settings - Required Permissions - Add - Windows Azure Service Management API
- Copy the Application ID and use it as the client id
- If you use application authentication, create a new key and use it as client secret
- Give the user/application at least "Reader" rights for the subscription
- Subscriptions - your subscriptions - Access control (IAM)
- Build and run the application
Client c = new Client("mytenant.onmicrosoft.com", "[CLIENTID]",
"[CLIENTSECRET]", "[SUBSCRIPTIONID]", "http://[REDIRECTURL]");
var resourceCosts = c.GetResourceCosts("MS-AZR-0003p", "EUR", "en-US", "AT",
new DateTime(2016, 11, 14, 0, 0, 0), new DateTime(2016, 12, 13, 23, 0, 0), AggregationGranularity.Hourly, true);
var costs = from x in resourceCosts.Costs select x.CalculatedCosts;
var totalCosts = costs.Sum();
Console.WriteLine(totalCosts + " " + resourceCosts.RateCardData.Currency);
Client c = new Client("mytenant.onmicrosoft.com", "[CLIENTID]",
"[CLIENTSECRET]", "[SUBSCRIPTIONID]", "http://[REDIRECTURL]");
var usageData = c.GetUsageData(new DateTime(2017, 1, 14, 0, 0, 0), DateTime.Now, Usage.AggregationGranularity.Daily, true);
foreach(var usageValue in usageData.Values)
{
Console.Write(usageValue.Name + ": " + usageValue.Properties.UsageStartTime + " - " + usageValue.Properties.UsageEndTime + " - " + usageValue.Properties.Quantity);
}
Client c = new Client("mytenant.onmicrosoft.com", "[CLIENTID]",
"[CLIENTSECRET]", "[SUBSCRIPTIONID]", "http://[REDIRECTURL]");
var ratecardData = c.GetRateCardData("MS-AZR-0003p", "EUR", "de-AT", "AT");
foreach(var meter in ratecardData.Meters)
{
Console.WriteLine(meter.MeterName + ": " + meter.MeterRates.First().Value);
}
Client c = new Client("mytenant.onmicrosoft.com", "[CLIENTID]",
"[CLIENTSECRET]", "[SUBSCRIPTIONID]", "http://[REDIRECTURL]");
var usageData = c.GetUsageData(new DateTime(2017, 1, 14, 0, 0, 0), DateTime.Now, Usage.AggregationGranularity.Daily, true);
var ratecardData = c.GetRateCardData("MS-AZR-0003p", "EUR", "de-AT", "AT");
var combined = Client.Combine(ratecardData, usageData);
var costs = from x in combined.Costs select x.CalculatedCosts;
var totalCosts = costs.Sum();
This can be achieved with the build in method GetResourceCostsForPeriod:
Client c = new Client("mytenant.onmicrosoft.com", "[CLIENTID]",
"[CLIENTSECRET]", "[SUBSCRIPTIONID]", "http://[REDIRECTURL]");
var resourceData = c.GetResourceCostsForPeriod("MS-AZR-0017P", "EUR", "en-US", "AT", 2017, 1);
This method could be slow, because the correct values for one billing cycle (so that it's the same as on the bill) are determined with the parameters:
- Start date: e.g. 14.01.2017 00:00
- End date: e.g. 13.02.2017 23:00
- Aggregation granularity: Hourly
That's the reason why GetResourceCostsForPeriod reads the usage value with hourly granularity which takes some time. You could use the GetResourceCosts with Daily aggregation to speed it up, but the resulting costs per meter/resource/totalcosts will not be the exact same as on the bill:
Client c = new Client("mytenant.onmicrosoft.com", "[CLIENTID]", "[CLIENTSECRET]", "[SUBSCRIPTIONID]", "http://[REDIRECTURL]");
var resourceCosts = c.GetResourceCosts("MS-AZR-0003p", "EUR", "en-US", "AT",
new DateTime(2017, 1, 14), new DateTime(2017, 2, 13), AggregationGranularity.Daily, true);
- Documentation
- Troubleshoot section in readme file
- Add tax parameter