Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Using aws REST api how to access openstack nova api

Hi,

I am trying to get the list of openstack server using aws sdk . For reference I shared my code as follows

public void ListInstance(string inboundQueueUrl) { string[] retMessages = null;

        /*
         http://queue.amazonaws.com/A29E9VSPHGOG23/queue1
        ?Action=ReceiveMessage
        &AWSAccessKeyId=0GS7553JW74RRM612K02EXAMPLE
        &Version=2007-05-01
        &Expires=2007-05-12T12:00:00Z
        &Signature=CN2SbNq%2B2Vw1W3lbc7wpM5gzDHE=
        &NumberOfMessages=2
         */

        // Create a request for the URL.         
        string date = System.DateTime.UtcNow.ToString("s");
        string stringToSign = string.Format("DescribeInstances" + date);
        string signature = CalculateAWSSignature(stringToSign, true);

        StringBuilder sb = new StringBuilder();
        sb.Append(inboundQueueUrl);
        sb.Append("?Action=DescribeInstances");
        sb.Append("&Version=2013-10-15");
        sb.AppendFormat("&AWSAccessKeyId={0}", m_AWSAccessKeyID);
        sb.AppendFormat("&Expires={0}", date);
        sb.AppendFormat("&Signature={0}", signature);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());
        HttpWebResponse response = null;
        Stream dataStream = null;
        StreamReader reader = null;

        try
        {
            request.Credentials = CredentialCache.DefaultCredentials;

            response = (HttpWebResponse)request.GetResponse();

            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();

            // Open the stream using a StreamReader for easy access.
            reader = new StreamReader(dataStream);

            // Read the content.
            string resString = reader.ReadToEnd();

            if (!string.IsNullOrEmpty(resString) && resString.ToUpper().IndexOf("SUCCESS") > 0)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(resString);
                XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
                namespaceManager.AddNamespace("msg", "http://queue.amazonaws.com/doc/2007-05-01/");

                XmlNodeList xmlMsgs = xmlDoc.DocumentElement.SelectNodes("/msg:ReceiveMessageResponse/msg:Message/msg:MessageBody", namespaceManager);

                if (xmlMsgs != null && xmlMsgs.Count > 0)
                {
                    retMessages = new string[xmlMsgs.Count];
                    int i = 0;
                    foreach (XmlNode var in xmlMsgs)
                    {
                        retMessages[i++] = var.InnerText;
                    }
                }
            }//end if
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            // Cleanup the streams and the response.
            if (reader != null)
                reader.Close();

            if (dataStream != null)
                dataStream.Close();

            if (response != null)
                response.Close();
        }

    }

private string CalculateAWSSignature(string data, bool urlEncode) { ASCIIEncoding ae = new ASCIIEncoding(); HMACSHA1 signature = new HMACSHA1(ae.GetBytes(m_AWSSecretKey)); string retSignature = Convert.ToBase64String(signature.ComputeHash(ae.GetBytes(data.ToCharArray()))); return urlEncode ? HttpUtility.UrlEncode(retSignature) : retSignature; }