Muhammad Murad Haider
Muhammad Murad Haider

Reputation: 1477

Programmatically getting the list of azure virtual machine sizes

I am new to Azure management libraries for .net. How can we enumerate VM instance sizes available with respect to subscription or in general using Azure Management libraries for .Net or Rest APIs? Please suggest.

Upvotes: 4

Views: 3405

Answers (3)

Mehul Bhalala
Mehul Bhalala

Reputation: 800

You can get list of VM Size by using Certificate Base Authentication

Get Certificate method

private static X509Certificate2 GetStoreCertificate(string subscriptionId, string thumbprint)
    {
        List<StoreLocation> locations = new List<StoreLocation>
        { 
            StoreLocation.CurrentUser, 
            StoreLocation.LocalMachine
        };

        foreach (var location in locations)
        {
            X509Store store = new X509Store(StoreName.My, location);
            try
            {
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                X509Certificate2Collection certificates = store.Certificates.Find(
                X509FindType.FindByThumbprint, thumbprint, false);
                if (certificates.Count == 1)
                {
                    return certificates[0];
                }
            }
            finally
            {
                store.Close();
            }
        }

        throw new ArgumentException(string.Format("A Certificate with Thumbprint '{0}' could not be located.",thumbprint));
    }

here i describe same way to get VM size

private static X509Certificate2 Certificate = GetStoreCertificate(Your-subscriptionid,Your-thumbprint);
Microsoft.Azure.CertificateCloudCredentials credentials = new Microsoft.Azure.CertificateCloudCredentials(Your-subscriptionid, Certificate);

 var computeClient = new ComputeManagementClient(credentials) { SubscriptionId = Your-subscriptionid};
 var virtualMachineSize = computeClient.VirtualMachineSizes.List(Your-region_name).ToList();

Upvotes: 0

Mehul Bhalala
Mehul Bhalala

Reputation: 800

You can get list of VM sizes by region fillter

AuthenticationContext authenticationContext = new AuthenticationContext("https://login.windows.net/tenantdomainname.onmicrosoft.com"]);
UserCredential uc = new UserCredential(authusername,authpassword);
token = authenticationContext.AcquireToken("https://management.core.windows.net/", nativetenantid, uc);

var credentials = new TokenCredentials(token);
var computeClient = new ComputeManagementClient(credentials) { SubscriptionId = subscriptionid};
var virtualMachineSize = computeClient.VirtualMachineSizes.List(region_name).ToList();

you must need create one native client api on Azure Active Directory for token base authentication otherwise you can also use certification base authentication for client authorization.

i am using Microsoft.Azure.Management.Compute.dll, v10.0.0.0 for compute resources.

you can download here: https://www.nuget.org/packages/Microsoft.Azure.Management.Compute/13.0.4-prerelease

Upvotes: 0

Michael B
Michael B

Reputation: 12228

You can get a list of VM sizes for a region by calling

https://management.azure.com/subscriptions/{subscription-id}/providers/Microsoft.Compute/locations/{location}/vmSizes?api-version={api-version}

As documented here - List all available virtual machine sizes in a region

There is also a .Net Class for the same, but I've not found any examples of it being used - documented here - VirtualMachineSizeOperationsExtensions.List

Upvotes: 5

Related Questions