Using AWS REST api how to access Openstack Nova api

asked 2014-11-11 08:32:18 -0500

gayask gravatar image

updated 2014-11-11 11:19:30 -0500

larsks gravatar image

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;
        }
edit retag flag offensive close merge delete

Comments

Dear gayask,

At first thanks for your intrest on openstack.

For sharing purpose, its better to upload your code on Github . The ask.openstack.org is question/answer system.

Mzoorikh gravatar imageMzoorikh ( 2014-11-11 14:35:56 -0500 )edit