Azure Management Libraries

This is the second blog post in the small series on experiences and learnings gained while setting up a Minecraft server for the kids. The first spoke primarily about Azure Automation, this one will touch upon the new .NET libraries for Azure Management.

The challenge was the following: enable the kids to start the virtual machine running the Minecraft server without giving them access to the overall subscription

We create a small app running in the taskbar. When the app starts it will show a yellow triangle indicating that the status of the virtual machine is being established.

image

Depending on the whether the instance status is StoppedDeallocated or ReadyRole either a red cross

image

or a green check mark will be shown

image

Right clicking will display the menu items (they should be self-explanatory)

image

For this to work a couple of setting values are required. They are the following:

  • Service Name: This is the name of the cloud service where you virtual machine is deployed.
  • Virtual Machine: This is the name of the virtual machine.
  • Management Certificate: The thumbprint of the management certificate for your subscription.
  • Subscription ID: The ID for your Azure subscription.

Easiest way to get the thumbprint and subscription ID is using the PowerShell command Get-AzurePublishSettingsFile. This will download a file containing both as well as some other information.

<?xml version="1.0" encoding="utf-8"?>
<PublishData>
  <PublishProfile
    SchemaVersion="2.0"
    PublishMethod="AzureServiceManagementAPI">
    <Subscription
      ServiceManagementUrl="https://management.core.windows.net"
      Id="5fbxxxxxxxxxxxxxxxxxxxxxxxxxxxfe06e"
      Name="[Name of your Azure subscripton]"
      ManagementCertificate="MIIKPAIBAzCeI2S2N5Sbz4kAyL60DtKY=" />
  </PublishProfile>
</PublishData>

The settings dialog can be seen below.

image

Note: Yes, I know. If you changes the values of the service name and the virtual machine you could start and stop other VMs, so this is not something you would give to your evil nephew. However, for the case of my kids then, with the fear of loosing their pocket money for the next 200 years, I think we are OK.

So much for the app, but how does it work? How to communicate with Azure?

Create a new project in Visual Studio (I’m using 2013, so I don’t know if it will work in 2012).

Load the Microsoft Azure Management Libraries using Nuget. This package contains everything.

image

You could do with only the Microsoft Azure Compute Management Library if you want to minimize the footprint, but why settle for anything but the whole package.

Before we can do anything we need to authenticate towards Azure.

The way this is currently done is by using a X509 certificate. So in my helper class I’ve created a small method returning a SubscriptionCloudCredentials. It can be seen below.

public SubscriptionCloudCredentials GetCredentials()
{
    return new CertificateCloudCredentials(this.subscriptionId, 
        new X509Certificate2(Convert.FromBase64String(this.base64EncodedCert)));
}

The subscriptionId and base64EncodedCert are two member variables containing the ID and certificate thumbprint.

Using the CloudContext it is possible to create a ComputeManagementClient. I’ve defined a private member

private ComputeManagementClient computeManagement;

and create it like

computeManagement =
    CloudContext.Clients.CreateComputeManagementClient(GetCredentials());

To get the DeploymentStatus you can call the following:

var status = this.computeManagement
	    .Deployments
	    .GetByName(this.serviceName, this.virtualMachineName)
	    .Status;

Where this.serviceName and this.virtualMachineName are two private string members containing the two values respectively.

To start the virtual machine I’ve defined an async method

public async Task StartVMAsync(DeploymentStatus status)

The reason for passing in the status is to check that if

status.Equals(DeploymentStatus.Running)

we return.

The actual call to start the virtual machine is

var task = await this.computeManagement
	.VirtualMachines
	.StartAsync(this.serviceName, this.virtualMachineName, this.virtualMachineName,
    new CancellationToken());

Likewise a StopVMAsync method is defined containing the call to stop the virtual machine:

var task = await this.computeManagement
	.VirtualMachines
	.ShutdownAsync(this.serviceName, 
				   this.virtualMachineName, 
				   this.virtualMachineName,
		new VirtualMachineShutdownParameters()
		{
			PostShutdownAction = PostShutdownAction.StoppedDeallocated
		},
		new CancellationToken());

And that is basically it. Of course the above should be packaged nicely and then called from the taskbar app.

Time permitting I will push the code to GitHub, Codeplex or similar for people to download.

The official Service Management Client Library Reference can be found on MSDN.

About strobaek

.NET developer/architect. Runner, espresso drinker and lover of gourmet food.
This entry was posted in Azure. Bookmark the permalink.

7 Responses to Azure Management Libraries

  1. berst maximilien says:

    Hello, could you send me the link to the github you talk about i’m really interested in your project.

  2. strobaek says:

    Hi,

    I haven’t got around to actually pushing it to GitHub jet, but give me a few days and I’ll send you a link.

    Karsten

  3. Shweta says:

    How can I get the list of all VMs present under given subscription?

  4. strobaek says:

    Hi,

    Sorry for the late reply.

    Do you require this information from code or from e.g. PowerShell?

    Regardless check out the documentation at http://azure.microsoft.com

  5. Evil Nephew says:

    How about that GitHub link ???

  6. strobaek says:

    Hi, Yeah never got around to do that. Will try in the next couple of days.

Leave a Reply

Your email address will not be published.