Windows Azure Management API enables you to manage the Azure subscription through
code. In this post, I will show how you can list valid
datacenter locations available for a given subscription using Windows Azure
Management API.
Windows Azure Management API is a REST based API and allows you to perform almost
all management level tasks with an Azure subscription.
Any client making a call to Azure portal using the Management API has to authenticate
itself before making a 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
First, let us create a class representing Locations. There is property
representing location name.
Essentially you need to perform the 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
- 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 .
abc is password to use PFX certificate.
- Get the stream and convert response stream in string
You will get the XML response in below format,
- 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
On running you should get all the hosted
service. For me there is only one hosted service debugmode9 under my Azure
subscription.
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("697714da-b267-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("https://management.core.windows.net/"+SubscriptionId+"/locations");
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 =
"http://schemas.microsoft.com/windowsazure";
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;
}
}
}
I hope this post was useful. Thanks for reading.