List valid Data Center Locations for a Subscription using Windows Azure Management API

Windows Azure Management API enables you to manage Azure subscription through codes. In this post, I will show, how could you list valid datacenter locations available for given subscription using Windows Azure Management API?

Windows Azure Management API is REST based API and allows you to perform almost all management level tasks with Azure subscription.

Any client making call to Azure portal using Management API has to authenticate itself before making call. Authenticating being done between Azure portal and client calling REST based Azure Management API through the certificates.

Read here to create certificate for Azure subscription

Read here to upload certificate

Video on Creating and Uploading Certificate for Windows Azure

Very first let us create a class representing Locations. There is property representing location name.

image

Essentially you need to perform below steps,

You need to create a web request to below URL. Provide desired subscription Id to fetch the locations. After subscription you need to add locations to get all the locations for given subscription

image

While making request you need to make sure you are calling the correct version and adding the cross ponding pfx certificate of your subscription level certificate. To list locations for given subscription header should me either 2010-10-28 or later .

image

abc is password to use PFX certificate

Get the stream and convert response stream in string

image

You will get the XML response in below format,

image

Once XML is there in form of string you need to extract Location name from XML element using LINQ to XML. You can parse location from xml as below

image

On running you should get all the hosted service. For me there is only one hosted service debugmode9 under my Azure subscription.

image

For your reference full source code is as below,


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleClient.ServiceReference1;
using System.Xml.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.IO;

namespace ConsoleClient
{
class Program
{
static void Main(string[] args)
{
var result = GetLocations("-4761-bced-b75fcde0d7e1").ToList();

foreach (var r in result)
{
Console.WriteLine(r.LocationName);
}
Console.ReadKey(true);
}

 

private static  List<Location> GetLocations(string SubscriptionId)
{
var request = (HttpWebRequest)WebRequest
.Create("<a href="https://management.core.windows.net/&quot;+SubscriptionId+&quot;/locations">https://management.core.windows.net/"+SubscriptionId+"/locations</a>");

request.Headers.Add("x-ms-version:2010-10-28");
X509Certificate2 certificate =
new X509Certificate2("D:\\debugmodepfx.pfx", "abc");
request.ClientCertificates.Add(certificate);

var response = request.GetResponse().GetResponseStream();
var xmlofResponse = new StreamReader(response).ReadToEnd();
XDocument doc = XDocument.Parse(xmlofResponse);
XElement document = XElement.Parse(xmlofResponse);

XNamespace ns = "<a href="http://schemas.microsoft.com/windowsazure">http://schemas.microsoft.com/windowsazure</a>";
var locations = (from a in document.Descendants(ns + "Location")
select new Location
{
LocationName = a.Element(ns + "Name").Value,
}).ToList();
return locations;

}

public class Location
{
public string LocationName { get; set; }
}

}

&nbsp;

I hope this post was useful. Thanks for reading Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

One response to “List valid Data Center Locations for a Subscription using Windows Azure Management API”

  1. […] List valid Data Center Locations for a Subscription using Windows Azure Management API […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com