无法从Azure API管理检索成本详细信息

bf1o4zei  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(117)

我试图编写一段代码,使用AzureManagement库从Azure Cloud检索成本详细信息。然而,我被困在检索数据的部分。这是我的代码:

using Azure.Core;
using Azure.Identity;
using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Consumption;
using System;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;

namespace AzureCostReport
{
    class Program
    {
        // Replace the values below with your Azure subscription ID, tenant ID, client ID, and client secret.
        private const string subscriptionId = "*********";
        private const string tenantId = "*********";
        private const string clientId = "*********";
        private const string clientSecret = "**********";

        private static ServiceClientCredentials TokenCredentials { get; set; }

        static async Task Main(string[] args)
        {
            // Authenticate using a service principal.
            var clientCredential = new ClientCredential(clientId, clientSecret);
            var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
            var token = await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://management.azure.com/.default" }));

            // Create the Azure Resource Manager client.
            //var armClient = new ResourceManagementClient(TokenCredentials);

            // Set the date range for the cost report.
            var startDate = new DateTime(2023, 3, 1);
            var endDate = new DateTime(2023, 3, 1).AddDays(1).AddSeconds(-1);

            // Create the Azure Consumption client.
            var consumptionClient = new ConsumptionManagementClient(TokenCredentials);

            // Query the Azure Consumption API for the cost report.
            var result = await consumptionClient.UsageDetails.ListAsync(
                filter: $"properties/usageStart ge '{startDate}' and properties/usageEnd le '{endDate}'",
                expand: "properties/meterDetails");

            // Group the usage details by resource group and resource name.
            var usageByResource = result
                .GroupBy(r => new { r.ResourceGroupName, r.ResourceName })
                .Select(g => new {
                    ResourceGroupName = g.Key.ResourceGroupName,
                    ResourceName = g.Key.ResourceName,
                    Cost = g.Sum(r => r.Cost.Value),
                    UsageQuantity = g.Sum(r => r.UsageQuantity.Value),
                    MeterName = g.First().MeterDetails.MeterName,
                    MeterCategory = g.First().MeterDetails.MeterCategory,
                    MeterSubCategory = g.First().MeterDetails.MeterSubCategory,
                    Unit = g.First().Unit,
                    Currency = g.First().Currency
                });

            // Print the cost report.
            Console.WriteLine($"Cost Report for {startDate.ToShortDateString()}:");
            Console.WriteLine("Resource Group\tResource Name\tMeter Category\tMeter Sub-Category\tMeter Name\tUsage Quantity\tUnit\tCurrency\tCost");
            foreach (var usage in usageByResource)
            {
                Console.WriteLine($"{usage.ResourceGroupName}\t{usage.ResourceName}\t{usage.MeterCategory}\t{usage.MeterSubCategory}\t{usage.MeterName}\t{usage.UsageQuantity}\t{usage.Unit}\t{usage.Currency}\t{usage.Cost:N2}");
            }
        }
    }
}

但是我在这一行中有一个错误:

.GroupBy(r => new { r.ResourceGroupName, r.ResourceName })

它只是说:CS1061:“UsageDetail”不包含“ResourceGroupName”的定义,并且找不到接受“UsageDetail”类型的第一个参数的可访问扩展方法“ResourceGroupName”。
我不是这方面的Maven,这一小段代码已经花费了我很多晚上的工作(我甚至试图更好地理解那些使用CHAT GPT的库,但不幸的是,我发现AI和我一样困惑)。
任何有关如何解决该错误的帮助将受到高度赞赏。谢谢和问候。

lymgl2op

lymgl2op1#

Microsoft.Azure.Management.Resources软件包已被弃用。建议的替代方案是使用Azure.ResourceManager.Consumption。请检查this示例代码是否有帮助?您可以根据您的需求尝试调整该示例。

相关问题