从 .NET SDK 查询 Azure 计算配额

Query Azure Compute Quotas from .NET SDK

我正在寻找有关如何使用 Azure .NET SDK 查询计算资源(例如 Dsv3 vCPU)的当前使用情况和剩余配额的示例。

谢谢!

根据我的测试,我们可以使用 Azure .Net SDK Microsoft.Azure.Management.Compute.Fluent 列出一个 Azure 订阅中 Microsoft.Comput 资源的使用情况。详情请参考document

  1. Use Azure CLI to create a service pricipal
 az login
 az ad sp create-for-rbac --name <ServicePrincipalName>
 az role assignment create --assignee <ServicePrincipalName> --role Contributor
  1. 代码
var tenantId = "<your tenant id>";
var clientId = "<your sp app id> ";
var clientSecret = "<your sp passowrd>";
var subscriptionId = "<your subscription id>";
AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
                       clientId,
                       clientSecret,
                       tenantId,
                        AzureEnvironment.AzureGlobalCloud);
RestClient restClient = RestClient.Configure()
                                  .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
                                  .WithCredentials(credentials)
                                  .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                                  .Build();
ComputeManagementClientclient = new ComputeManagementClient(restClient);
client.SubscriptionId = subscriptionId;

foreach (var s1 in await UsageOperationsExtensions.ListAsync(client.Usage, Region.AsiaSouthEast.Name)) {

                Console.WriteLine("Name: " + s1.Name.LocalizedValue +"\nUnit: "+ UsageInner.Unit + "\nCurrentValue: " + s1.CurrentValue + "\nLimit: " + s1.Limit);
                Console.WriteLine("-----------------------");
}