Manage Azure Networks Using The .NET Azure Management Libraries

In this post I’m going to show you how to manage Azure Networks using the .NET Azure Management libraries.

In general I find the Azure Management Libraries a pleasure to work with except for the network management library. Retrieving your network configuration is easy enough but using it to configure your network is more difficult than it should be. I did this investigation out of curiosity more than anything else and I recommend using PowerShell to manage Azure instead of the management library. But enough ranting and on to the demo.

I used Visual Studio 2015 for this demo and .Net 4.5, I already had the Azure SDK installed in my environment so I’m not sure if it is a requirement for the management libraries but I doubt it.

For this demo I used an empty console project but there are project templates for Azure management projects in Visual Studio if you want to do something more complicated.

Manage Azure Networks Using The .NET Azure Management Libraries

After creating the your console application install the management library nuget packages.

install-package microsoft.windowsazure.management

It will install a slew of other packages so it might take a few minutes on a slow internet connection.

To create the credentials you need to manage Azure you have to download your .publishsettings file, if you are not sure how to do it look here it is in the first part of the post. Open the .publishsettings file in a text editor and copy the Id and ManagementCertificate strings and paste them into the code below.

var cred = new Microsoft.Azure.CertificateCloudCredentials("yoursubid", new X509Certificate2(Convert.FromBase64String("yourcert")));

The retrieve your network settings use the NetworkManagementClient class with your credentials created above and iterate over the properties in the Networks object, for example to list subnets:

using (var netclient = new NetworkManagementClient(cred))
{
var networks = netclient.Networks.List();
Console.WriteLine("Networks:");
networks.VirtualNetworkSites.ToList().ForEach(x =>
{
Console.WriteLine(x.Name);
x.Subnets.ToList().ForEach(y => Console.WriteLine(y.Name + " " +              y.AddressPrefix));
}
);
}

This was the easy part, to configure your network you have to retrieve the current configuration as xml, manipulate it and then send it back to Azure.

var config = netclient.Networks.GetConfiguration();


XmlDocument doc = new XmlDocument();
doc.LoadXml(config.Configuration);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

string rootns = @"http://schemas.microsoft.com/ServiceHosting/2011/07/NetworkConfiguration";
nsmgr.AddNamespace("ns1",rootns );

var subnets = doc.SelectSingleNode("//ns1:Subnets",nsmgr);

//Create SubNet node
XmlNode newsub = doc.CreateNode(XmlNodeType.Element, "Subnet", rootns);
XmlAttribute subname = doc.CreateAttribute("name");
subname.Value = "NewSubName";
newsub.Attributes.Append(subname);

//Create AddressPrefix node
XmlNode newaddr = doc.CreateNode(XmlNodeType.Element, "AddressPrefix", rootns);
newaddr.InnerText = "10.32.1.0/24";
newsub.AppendChild(newaddr);

//Add to Subnets
subnets.AppendChild(newsub);

var sw = new System.IO.StringWriter();
doc.WriteContentTo(new XmlTextWriter(sw));
var newconfig = new NetworkSetConfigurationParameters(sw.ToString());

var result = netclient.Networks.SetConfiguration(newconfig);

Console.WriteLine(result.Status);

At least it is worth all the trouble and you end up with a new subnet.

Manage Azure Networks Using The .NET Azure Management Libraries

Francois Delport

Published by

Francois Delport

I am a cloud and devops consultant, technology fan and previously a professional C# developer with a keen interest in system design and architecture. Currently I am involved in projects using Azure, the Microsoft stack and DevOps. I am based in Melbourne, Australia. Email: [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *