4

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.

Muhammad Murad Haider
  • 1,045
  • 1
  • 9
  • 29

3 Answers3

5

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

Michael B
  • 10,997
  • 4
  • 29
  • 65
  • great... what comes for api-version btw? – Muhammad Murad Haider Jan 21 '16 at 19:29
  • @MuhammadMuradHaider There is a link at the top of the page that gives [required headers and parameters](https://msdn.microsoft.com/en-us/library/azure/mt163630.aspx) including the api-version - just to spoil the surprise it is `2015-05-01-preview` ;) – Michael B Jan 21 '16 at 19:35
  • First link is broken for me but looks like it's now documented here: https://docs.microsoft.com/en-us/rest/api/compute/virtualmachinesizes/list – Pat Myron Apr 29 '19 at 17:49
0

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

Mehul Bhalala
  • 702
  • 7
  • 16
0

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();
Mehul Bhalala
  • 702
  • 7
  • 16