Skip to content

Rackspace Cloud Networks Code Samples

Alan Quillin edited this page May 22, 2013 · 7 revisions

Cloud Networks Provider

Sample code for working with the CloudNetworksProvider

ScreenShot

CloudNetworksProvider requires a CloudIdentity instance for all operations, which can be created using Username/Password or Username/APIKey.

var cloudIdentity = new CloudIdentity() { Username = "username", Password = "password" };

or

var cloudIdentity = new CloudIdentity() { APIKey = "apikey", Username = "username" };

For more information on IdentityProvider

There are 2 ways to pass in the identity credentials to CloudNetworksProvider:

  1. In the constructor

  2. An optional parameter of each method

Our samples below will assume the identity has been passed into the constructor.

Creating a Network

var networksProvider = new CloudNetworksProvider(cloudIdentity);
var cidr = "172.16.0.0/24";
var newNetwork = networksProvider.CreateNetwork(cidr, "your-network-name");

This will return a CloudNetwork object with values for the newly created network that includes the unique Id of the network

Retrieving information about a Network

var networksProvider = new CloudNetworksProvider(cloudIdentity);
var network = networksProvider.ShowNetwork("c1a01b77-2808-4c40-8869-ea7c0836170a");

This returns a CloudNetwork object with the following properties:

Id - the system-generated UUID identifier for the network

Cidr - the CIDR supplied during network creation

Label - the label supplied during network creation

Creating a server on a Network

Creating a server ONLY on a created isolated network

var networksProvider = new CloudServersProvider(cloudIdentity);
var networks = new List<Guid>() { new Guid("9823c221-bc4b-46a1-b79e-ba0736ba3a5d") }; //ID of the specified network
	
var testServer = provider.CreateServer("server-name", "image-identifier", "2", networks: networks);

Create a server on the given network AND with public and private networks

var networksProvider = new CloudServersProvider(cloudIdentity);
var networks = new List<Guid>() { new Guid("9823c221-bc4b-46a1-b79e-ba0736ba3a5d") }; //ID of the specified network
	
var testServer = provider.CreateServer("server-name", "image-identifier", "2", attachToPublicNetwork: true, attachToServiceNetwork:true, networks: networks);

Deleting a Network

var networksProvider = new CloudNetworksProvider(cloudIdentity);
var network = networksProvider.DeleteNetwork("c1a01b77-2808-4c40-8869-ea7c0836170a");

NOTE: If the network has any servers on it, this operation will fail and a UserAuthorizationException will be thrown. If the network has already been deleted, an ItemNotFoundException will be thrown.