Example #1
0
 public void CreateCloudService(string serviceName, string label=null, string location=null, string affinityGroup=null)
 {
     if(!string.IsNullOrEmpty(location) && !string.IsNullOrEmpty(affinityGroup))
     {
         throw new ArgumentException("Location and AffinityGroup cannot both be specified");
     }
     if(string.IsNullOrEmpty(location) && string.IsNullOrEmpty(location))
     {
         var locationItem = GetDefaultLocation();
         if(locationItem == null)
         {
             throw new Exception("Unable to find default location");
         }
         location = locationItem.Name;
     }
     string base64Label;
     if(!string.IsNullOrEmpty(label))
     {
         base64Label = Convert.ToBase64String(Encoding.UTF8.GetBytes(label));
     }
     else 
     {
         base64Label = Convert.ToBase64String(Encoding.UTF8.GetBytes(serviceName));
     }
     var requestData = new CreateHostedServiceRequest
     {
         ServiceName = serviceName,
         Label = base64Label,
         Location = location,
         AffinityGroup = affinityGroup
     };
     string requestXml;
     using (var writer = new Utf8StringWriter())
     {
         var serializer = new XmlSerializer(typeof(CreateHostedServiceRequest), "http://schemas.microsoft.com/windowsazure");
         serializer.Serialize(writer, requestData);
         requestXml = writer.ToString();
     }
     string url = string.Format("https://management.core.windows.net/{0}/services/hostedservices", _subscriptionIdentifier);
     var request = CreateRequest(url, _managementCertificate);
     request.Method = "POST";
     using(var stream = request.GetRequestStream())
     using(var writer = new StreamWriter(stream))
     {
         writer.Write(requestXml);
     }
     
     try 
     {
         using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
         using (var responseStream = response.GetResponseStream())
         using (var reader = new StreamReader(responseStream))
         {
             string responseData = reader.ReadToEnd();
             if(response.StatusCode != HttpStatusCode.Created)
             {
                 string message = string.Format("Failed to create cloud service, return status {0}", response.StatusCode);
                 if(!string.IsNullOrEmpty(responseData))
                 {
                     message += ", Details: " + responseData;
                 }
                 throw new Exception(message);
             }
         }
     }
     catch(WebException webEx)
     {
         var resp = webEx.Response;
         var responseDetails = (new System.IO.StreamReader(resp.GetResponseStream(), true)).ReadToEnd();
         if(!string.IsNullOrEmpty(responseDetails))
         {
             throw new Exception("Failed to create cloud service, Details: " + responseDetails, webEx);
         }
         else 
         {
             throw;
         }
     }
 }
Example #2
0
        public void UpgradeCloudServiceDeployment(string serviceName, string blobUrl, string configurationData, string deploymentSlot)
        {
            string base64Label = Convert.ToBase64String(Encoding.UTF8.GetBytes(serviceName));
            string base64Configuation = Convert.ToBase64String(Encoding.UTF8.GetBytes(configurationData));
            var requestData = new UpgradeDeploymentRequest
            {
                Mode = UpgradeDeploymentRequest.EnumUpgradeDeploymentMode.Auto,
                PackageUrl = blobUrl,
                Label = base64Label,
                Configuration = base64Configuation,
                Force = true
            };
            string requestXml;
            using (var writer = new Utf8StringWriter())
            {
                var serializer = new XmlSerializer(typeof(UpgradeDeploymentRequest), "http://schemas.microsoft.com/windowsazure");
                serializer.Serialize(writer, requestData);
                requestXml = writer.ToString();
            }
            string url = string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}/?comp=upgrade", _subscriptionIdentifier, Uri.EscapeUriString(serviceName), Uri.EscapeUriString(deploymentSlot));
            var request = CreateRequest(url, _managementCertificate);
            request.Method = "POST";
            using (var stream = request.GetRequestStream())
            using (var writer = new StreamWriter(stream))
            {
                writer.Write(requestXml);
            }

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (var responseStream = response.GetResponseStream())
                using (var reader = new StreamReader(responseStream))
                {
                    string responseData = reader.ReadToEnd();
                    if (response.StatusCode != HttpStatusCode.Accepted)
                    {
                        string message = string.Format("Failed to create cloud service deployment, return status {0}", response.StatusCode);
                        if (!string.IsNullOrEmpty(responseData))
                        {
                            message += ", Details: " + responseData;
                        }
                        throw new Exception(message);
                    }
                }
            }
            catch (WebException webEx)
            {
                var resp = webEx.Response;
                var responseDetails = (new System.IO.StreamReader(resp.GetResponseStream(), true)).ReadToEnd();
                if (!string.IsNullOrEmpty(responseDetails))
                {
                    throw new Exception("Failed to create cloud service deployment, Details: " + responseDetails, webEx);
                }
                else
                {
                    throw;
                }
            }
        }