Freigeben über


How to list all available VM sizes in a region using .NET (ARM endpoint)

Today, I had a query from a developer asking how to silent authenticate and fetch the list of available VM’s sizes from a particular region using .NET code. They wanted to fetch this detail from their worker role more precisely. They wanted to call the URI as in this article silent authenticated https://msdn.microsoft.com/en-us/library/azure/mt269440.aspx

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

On first sight, I thought this as an RDFE endpoint(older portal/SMAPI), but on closer look this turned to be an ARM end point.

How to identify the url is an RDFE/ARM endpoint?

Please note, for RDFE end point we may have to either use certificate based or native client way of authentication.

Since this is an ARM endpoint, we need to follow the service principal way to get the bearer token which is needed for the URI GET call’s.

Step1:-

Perform the following action one by one carefully as in this URL -  https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-service-principal-portal/

  1. Create an Active Directory application
  2. Get client id and authentication key
  3. Get tenant id
  4. Set delegated permissions
  5. Assign application to role

Step2:-

using System;
using System.IO;
using System.Net;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

            var context = new AuthenticationContext("https://login.microsoftonline.com/+ "your_tenantid");
ClientCredential credential = new ClientCredential("your_client_ID", "your_client_secret");
AuthenticationResult result = context.AcquireToken("https://management.azure.com/", credential);
var token = result.CreateAuthorizationHeader().Substring("Bearer ".Length);

            string uri = @"https://management.azure.com/subscriptions/<your_subscription_Id>/providers/Microsoft.Compute/locations/Southeast Asia/vmSizes?api-version=2015-05-01-preview";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Headers.Add("Authorization:Bearer " + token);
var response = request.GetResponse().GetResponseStream();
var output = new StreamReader(response).ReadToEnd();

            Console.WriteLine(output);
}
}
}

P.s:- I have used Adal 2.28.2.795 to avoid async complexities.

on executing, image

Comments

  • Anonymous
    November 05, 2016
    Great guide!