AzureBillingApi:.net库,可从azure rest计费api读取数据

  • h6_747628
    了解作者
  • 44.1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-09 00:49
    上传日期
AzureBillingApi .net库(.net标准1.4和.net框架4.5.2),该文件从azure rest计费api读取数据。 它可以用于: 从Azure计费RateCard API读取数据 从Azure帐单使用情况API读取数据 从两个API读取数据,然后将它们合并以计算成本(不含税)。 该库也可以作为NuGet包( )提供。 可以在我的博客中找到详细的描述: : 先决条件:Azure Active Directory的配置 将新的应用程序添加到活动目录: 打开 ,导航到Azure Active Directory和应用程序注册 添加一个新的本机应用程序 添加Windows Azure服务管理的委派权限应用程序注册-设置-所需权限-添加-Windows Azure服务管理API 复制应用程序ID并将其用作客户端ID 如果使用应用程序身份验证,请创建一个新密钥并将其用作客户端密钥 给用户/应用程序至少订阅的“读者”权限订阅-您的订阅-访问控制(IAM) 生成并运行应用程序 用法 获取成本(使用数据和价目表数据的组合) Client c = new Cl
AzureBillingApi-master.zip
  • AzureBillingApi-master
  • AzureBillingApi.Tests
  • Properties
  • AssemblyInfo.cs
    1.4KB
  • CalculationTest.cs
    3.7KB
  • AzureBillingApi.Tests.csproj
    4.4KB
  • AzureBillingApi
  • RateCard
  • RateCardData.cs
    1.1KB
  • Meter.cs
    2.2KB
  • Usage
  • InfoFields.cs
    935B
  • MicrosoftResourcesDataType.cs
    1.7KB
  • AggregationGranularity.cs
    403B
  • UsageData.cs
    672B
  • UsageValue.cs
    1KB
  • InstanceDataType.cs
    450B
  • UsageProperties.cs
    3.1KB
  • Globals.cs
    248B
  • Extensions.cs
    11.7KB
  • UsageClient.cs
    5.7KB
  • ResourceCostData.cs
    959B
  • Client.cs
    16.3KB
  • icon.png
    8.2KB
  • packages.config
    249B
  • key.snk
    596B
  • AzureBillingApi.csproj
    1.7KB
  • RateCardClient.cs
    4.1KB
  • AzureBillingApi.nuspec
    799B
  • ResourceCosts.cs
    815B
  • AzureAuthenticationHelper.cs
    3.3KB
  • AzureBillingApi.ConsoleSample
  • Properties
  • AssemblyInfo.cs
    1.4KB
  • App.config
    180B
  • packages.config
    141B
  • AzureBillingApi.ConsoleSample.csproj
    3.2KB
  • Program.cs
    10.9KB
  • AzureBillingApi.sln
    2.7KB
  • README.md
    4.6KB
  • .gitignore
    4.1KB
  • LICENSE
    1KB
内容介绍
# AzureBillingApi .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/ ## Prerequisits: Configuration of Azure Active Directory 1. 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 2. Add delegated permissions for Windows Azure Service Management * App registrations - Settings - Required Permissions - Add - Windows Azure Service Management API 3. 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 4. Give the user/application at least "Reader" rights for the subscription * Subscriptions - your subscriptions - Access control (IAM) 5. Build and run the application ## Usage ### Getting the costs (combination of usage data and ratecard data) ```csharp 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); ``` ### Getting only the usage data ```csharp 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); } ``` ### Getting only the ratecard data ```csharp 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); } ``` ### Get usage and ratecard data and combine them: ```csharp 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(); ``` ### Get usage and costs for a period This can be achieved with the build in method *GetResourceCostsForPeriod*: ```csharp 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: ```csharp 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); ``` ## Improvements (todo) * Documentation * Troubleshoot section in readme file * Add tax parameter
评论
    相关推荐