public string CreateSnapshot(string instanceId, Identity identity)
        {
            var snapshotName                      = "Snapshot-" + Guid.NewGuid().ToString().Substring(0, 4);
            IList <OpenstackImage> list           = new List <OpenstackImage>();
            RequestManager         requestManager = new RequestManager(identity);
            var uri = string.Format("/servers/{0}/action", instanceId);

            var bodyObject = new RequestBodyWrapper()
            {
                createImage = new RequestBody()
                {
                    name     = snapshotName,
                    metadata = new Metadata()
                    {
                        CreatedBy = "Created by Atlas"
                    }
                }
            };

            System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string  body     = oSerializer.Serialize(bodyObject);
            JObject response = requestManager.Action(uri, "nova", body, HttpStatusCode.Accepted);

            return(snapshotName);
        }
Example #2
0
        public AuthData GetAuthData(string openstackComponentName)
        {
            var            authData = new AuthData();
            var            uri      = new Uri(string.Format("{0}/tokens", _identity.AuthEndpoint));
            HttpWebRequest request  = (HttpWebRequest)HttpWebRequest.Create(uri);

            request.Method      = "POST";
            request.ContentType = "application/json";
            var bodyObject = new RequestBodyWrapper()
            {
                auth = new RequestBody()
                {
                    passwordCredentials = new PasswordCredentials()
                    {
                        password = _identity.Password,
                        username = _identity.Username,
                    },
                    tenantName = _identity.TenantName
                }
            };

            System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string body = oSerializer.Serialize(bodyObject);

            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(body);
            request.ContentLength = bytes.Length;
            System.IO.Stream os = request.GetRequestStream();
            os.Write(bytes, 0, bytes.Length); //Push it out there
            os.Close();

            HttpStatusCode  statusCode;
            HttpWebResponse response;

            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;
            }
            statusCode = response.StatusCode;
            if (statusCode.Equals(HttpStatusCode.OK))
            {
                try
                {
                    byte[]         responseBody = { };
                    JsonTextReader reader       = new JsonTextReader(new StreamReader(response.GetResponseStream()));
                    reader.Read();
                    JsonSerializer se              = new JsonSerializer();
                    JObject        parsedData      = (JObject)se.Deserialize(reader);
                    var            responseContent = parsedData["access"];
                    authData.AuthToken = responseContent["token"]["id"].ToString();
                    _identity.TenantId = responseContent["token"]["tenant"]["id"].ToString();
                    foreach (var endpoint in responseContent["serviceCatalog"])
                    {
                        var subEndpoint  = endpoint["endpoints"];
                        var endpointName = endpoint["name"].ToString();
                        if (endpointName.Equals(openstackComponentName))
                        {
                            var endpointUrl = subEndpoint.FirstOrDefault()["publicURL"].ToString();
                            authData.Endpoint = endpointUrl;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception occured ", ex.Message);
                }
            }
            response.Close();
            return(authData);
        }
        public AuthData GetAuthData(string openstackComponentName)
        {
            var authData = new AuthData();
            var uri = new Uri(string.Format("{0}/tokens", _identity.AuthEndpoint));
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.Method = "POST";
            request.ContentType = "application/json";
            var bodyObject = new RequestBodyWrapper()
            {
                auth = new RequestBody()
                {
                    passwordCredentials = new PasswordCredentials()
                    {
                        password = _identity.Password,
                        username = _identity.Username,
                    },
                    tenantName = _identity.TenantName
                }
            };
            System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string body = oSerializer.Serialize(bodyObject);

            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(body);
            request.ContentLength = bytes.Length;
            System.IO.Stream os = request.GetRequestStream();
            os.Write(bytes, 0, bytes.Length); //Push it out there
            os.Close();

            HttpStatusCode statusCode;
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;
            }
            statusCode = response.StatusCode;
            if (statusCode.Equals(HttpStatusCode.OK))
            {
                try
                {
                    byte[] responseBody = { };
                    JsonTextReader reader = new JsonTextReader(new StreamReader(response.GetResponseStream()));
                    reader.Read();
                    JsonSerializer se = new JsonSerializer();
                    JObject parsedData = (JObject)se.Deserialize(reader);
                    var responseContent = parsedData["access"];
                     authData.AuthToken = responseContent["token"]["id"].ToString();
                    _identity.TenantId = responseContent["token"]["tenant"]["id"].ToString();
                    foreach (var endpoint in responseContent["serviceCatalog"])
                    {
                        var subEndpoint = endpoint["endpoints"];
                        var endpointName = endpoint["name"].ToString();
                        if (endpointName.Equals(openstackComponentName))
                        {
                            var endpointUrl = subEndpoint.FirstOrDefault()["publicURL"].ToString();
                            authData.Endpoint = endpointUrl;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception occured ", ex.Message);
                }
            }
            response.Close();
            return authData;
        }
        public string CreateSnapshot(string instanceId, Identity identity)
        {
            var snapshotName = "Snapshot-" + Guid.NewGuid().ToString().Substring(0, 4);
            IList<OpenstackImage> list = new List<OpenstackImage>();
            RequestManager requestManager = new RequestManager(identity);
            var uri = string.Format("/servers/{0}/action", instanceId);

            var bodyObject = new RequestBodyWrapper()
            {
                createImage = new RequestBody()
                {
                    name = snapshotName,
                    metadata = new Metadata()
                    {
                        CreatedBy = "Created by Atlas"
                    }
                }
            };
            System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string body = oSerializer.Serialize(bodyObject);
            JObject response = requestManager.Action(uri, "nova", body, HttpStatusCode.Accepted);
            return snapshotName;
        }