kyuz0
kyuz0

Reputation: 111

Query Azure Compute Quotas from .NET SDK

I am looking for examples of how to use the Azure .NET SDK to query for the current usage and remaining quotas for Compute resources (say Dsv3 vCPUs).

Thanks!

Upvotes: 0

Views: 121

Answers (1)

Jim Xu
Jim Xu

Reputation: 23121

According to my test, we can use Azure .Net SDK Microsoft.Azure.Management.Compute.Fluent to list the usage of Microsoft.Comput resource in one Azure Subscription. For more details, please refer to the 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. Code
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("-----------------------");
}

enter image description here

Upvotes: 2

Related Questions