Rackspace Cloud Servers API using C#

In this example we will integrate with the Rackspace Cloud Server API using C#. The Rackspace Request class handles the HTTP GET and POST process. You just tell it which REST end point you can to call. It then returns a object of type dynamic.

The class below uses two extension methods, one to convert JSON to ExpandoObject and another to convert ExpandoObject to JSON.

Before you use the Rackspace Request you need to Authenticate with Rackspace API so that you have the Server Management Url and an Auth Token.


using System;
using System.Dynamic;
using System.IO;
using System.Net;
using System.Text;

public class RackspaceRequest
{
    private readonly string _baseUrl;
    private readonly string _authToken;

    public RackspaceRequest(string baseUrl, string authToken)
    {
        if (string.IsNullOrEmpty(baseUrl))
            throw new ArgumentNullException("baseUrl");

        if (string.IsNullOrEmpty(authToken))
            throw new ArgumentNullException("authToken");

        _baseUrl = baseUrl;
        _authToken = authToken;
    }

    public dynamic Request(string method, string serviceUrl, ExpandoObject postData)
    {
        if (string.IsNullOrEmpty(serviceUrl))
            throw new ArgumentNullException("serviceUrl");

        string url = string.Format("{0}{1}", _baseUrl, serviceUrl);

        WebRequest request = WebRequest.Create(url);

        request.Method = method;
        request.ContentType = "application/json";
        request.Headers["X-Auth-Token"] = _authToken;
        
        //Write post data
        if (method == "POST" && postData != null)
        {
            string json = postData.Flatten();
            var bytes = Encoding.ASCII.GetBytes(json);
            request.ContentLength = bytes.Length;
            var requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();    
        }

        string response;

        using (var webResponse = request.GetResponse())
        {
            using (var streamReader = new StreamReader(webResponse.GetResponseStream()))
            {
                response = streamReader.ReadToEnd();
            }
        }

        return response.ToExpando();
    }

    public dynamic Request(string method, string serviceUrl)
    {
        return Request(method, serviceUrl, null);
    }

    public dynamic GetRequest(string serviceUrl)
    {
        return Request("GET", serviceUrl, null);
    }
}


Create the Rackspace Request object by passing in the Server Management Url and the Auth Token


var request = new RackspaceRequest(serverManagementUrl, authToken);

Getting a list of servers:


var serverList = request.GetRequest("/servers");

foreach (var server in serverList.servers)
{
    Console.WriteLine(string.Format("Id: {0}", server.id));
    Console.WriteLine(string.Format("Name: {0}", server.name));
}

Getting a list of images:


var imageList = request.GetRequest("/images");

foreach (var server in imageList.images)
{
    Console.WriteLine(string.Format("Id: {0}", server.id));
    Console.WriteLine(string.Format("Name: {0}", server.name));
}

Create a backup image:


dynamic data = new ExpandoObject();
data.image = new ExpandoObject();
data.image.serverId = XXXXXXX;
data.image.name = string.Format("Backup Image");

dynamic response = request.Request("POST", "/images/", data);

Console.WriteLine(string.Format("id: {0}", response.image.id));
Console.WriteLine(string.Format("serverId: {0}", response.image.serverId));
Console.WriteLine(string.Format("name: {0}", response.image.name));
Console.WriteLine(string.Format("created: {0}", response.image.created));
Console.WriteLine(string.Format("status: {0}", response.image.status));
Console.WriteLine(string.Format("progress: {0}", response.image.progress));

Get an image details:


var response = request.GetRequest(string.Format("/images/{0}", XXXXXXXX));

Console.WriteLine(string.Format("id: {0}", response.image.id));
Console.WriteLine(string.Format("name: {0}", response.image.name));
Console.WriteLine(string.Format("created: {0}", response.image.created));
Console.WriteLine(string.Format("status: {0}", response.image.status));
Console.WriteLine(string.Format("progress: {0}", response.image.progress));



Comments

  1. Eric Wilson August 22nd

    Comment Arrow

    Thank you for this! Helps me get started.

    Do you have any further examples of spinning up a server from and image and deleting it when no longer needed?


Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.