/// <summary>
        /// Updates an existing server-level Firewall Rule for an Azure SQL
        /// Database Server.
        /// </summary>
        /// <param name='serverName'>
        /// Required. The name of the Azure SQL Database Server that has the
        /// Firewall Rule to be updated.
        /// </param>
        /// <param name='ruleName'>
        /// Required. The name of the Firewall Rule to be updated.
        /// </param>
        /// <param name='parameters'>
        /// Required. The parameters for the Update Firewall Rule operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Represents the firewall rule update response.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Sql.Models.FirewallRuleUpdateResponse> UpdateAsync(string serverName, string ruleName, FirewallRuleUpdateParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (serverName == null)
            {
                throw new ArgumentNullException("serverName");
            }
            if (ruleName == null)
            {
                throw new ArgumentNullException("ruleName");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (parameters.EndIPAddress == null)
            {
                throw new ArgumentNullException("parameters.EndIPAddress");
            }
            if (parameters.Name == null)
            {
                throw new ArgumentNullException("parameters.Name");
            }
            if (parameters.StartIPAddress == null)
            {
                throw new ArgumentNullException("parameters.StartIPAddress");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("serverName", serverName);
                tracingParameters.Add("ruleName", ruleName);
                tracingParameters.Add("parameters", parameters);
                Tracing.Enter(invocationId, this, "UpdateAsync", tracingParameters);
            }

            // Construct URL
            string url     = "/" + (this.Client.Credentials.SubscriptionId != null ? this.Client.Credentials.SubscriptionId.Trim() : "") + "/services/sqlservers/servers/" + serverName.Trim() + "/firewallrules/" + ruleName.Trim();
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Put;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2012-03-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Serialize Request
                string    requestContent = null;
                XDocument requestDoc     = new XDocument();

                XElement serviceResourceElement = new XElement(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure"));
                requestDoc.Add(serviceResourceElement);

                XElement nameElement = new XElement(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                nameElement.Value = parameters.Name;
                serviceResourceElement.Add(nameElement);

                XElement startIPAddressElement = new XElement(XName.Get("StartIPAddress", "http://schemas.microsoft.com/windowsazure"));
                startIPAddressElement.Value = parameters.StartIPAddress;
                serviceResourceElement.Add(startIPAddressElement);

                XElement endIPAddressElement = new XElement(XName.Get("EndIPAddress", "http://schemas.microsoft.com/windowsazure"));
                endIPAddressElement.Value = parameters.EndIPAddress;
                serviceResourceElement.Add(endIPAddressElement);

                requestContent      = requestDoc.ToString();
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType         = new MediaTypeHeaderValue("application/xml");
                httpRequest.Content.Headers.ContentType.CharSet = "utf-8";

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        Tracing.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        Tracing.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    FirewallRuleUpdateResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new FirewallRuleUpdateResponse();
                    XDocument responseDoc = XDocument.Parse(responseContent);

                    XElement serviceResourceElement2 = responseDoc.Element(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure"));
                    if (serviceResourceElement2 != null)
                    {
                        FirewallRule serviceResourceInstance = new FirewallRule();
                        result.FirewallRule = serviceResourceInstance;

                        XElement startIPAddressElement2 = serviceResourceElement2.Element(XName.Get("StartIPAddress", "http://schemas.microsoft.com/windowsazure"));
                        if (startIPAddressElement2 != null)
                        {
                            string startIPAddressInstance = startIPAddressElement2.Value;
                            serviceResourceInstance.StartIPAddress = startIPAddressInstance;
                        }

                        XElement endIPAddressElement2 = serviceResourceElement2.Element(XName.Get("EndIPAddress", "http://schemas.microsoft.com/windowsazure"));
                        if (endIPAddressElement2 != null)
                        {
                            string endIPAddressInstance = endIPAddressElement2.Value;
                            serviceResourceInstance.EndIPAddress = endIPAddressInstance;
                        }

                        XElement nameElement2 = serviceResourceElement2.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                        if (nameElement2 != null)
                        {
                            string nameInstance = nameElement2.Value;
                            serviceResourceInstance.Name = nameInstance;
                        }

                        XElement typeElement = serviceResourceElement2.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure"));
                        if (typeElement != null)
                        {
                            string typeInstance = typeElement.Value;
                            serviceResourceInstance.Type = typeInstance;
                        }

                        XElement stateElement = serviceResourceElement2.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure"));
                        if (stateElement != null)
                        {
                            string stateInstance = stateElement.Value;
                            serviceResourceInstance.State = stateInstance;
                        }
                    }

                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
 /// <summary>
 /// Updates an existing server-level firewall rule for a SQL Database
 /// server that belongs to a subscription.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/gg715280.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Sql.IFirewallRuleOperations.
 /// </param>
 /// <param name='serverName'>
 /// The name of the SQL database server to which this rule will be
 /// applied.
 /// </param>
 /// <param name='ruleName'>
 /// The name of the firewall rule to be updated.
 /// </param>
 /// <param name='parameters'>
 /// Parameters for the Update Firewall Rule operation.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static FirewallRuleUpdateResponse Update(this IFirewallRuleOperations operations, string serverName, string ruleName, FirewallRuleUpdateParameters parameters)
 {
     try
     {
         return(operations.UpdateAsync(serverName, ruleName, parameters).Result);
     }
     catch (AggregateException ex)
     {
         if (ex.InnerExceptions.Count > 1)
         {
             throw;
         }
         else
         {
             throw ex.InnerException;
         }
     }
 }
 /// <summary>
 /// Updates an existing server-level firewall rule for a SQL Database
 /// server that belongs to a subscription.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/gg715280.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Sql.IFirewallRuleOperations.
 /// </param>
 /// <param name='serverName'>
 /// The name of the SQL database server to which this rule will be
 /// applied.
 /// </param>
 /// <param name='ruleName'>
 /// The name of the firewall rule to be updated.
 /// </param>
 /// <param name='parameters'>
 /// Parameters for the Update Firewall Rule operation.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task <FirewallRuleUpdateResponse> UpdateAsync(this IFirewallRuleOperations operations, string serverName, string ruleName, FirewallRuleUpdateParameters parameters)
 {
     return(operations.UpdateAsync(serverName, ruleName, parameters, CancellationToken.None));
 }
Beispiel #4
0
 /// <summary>
 /// Updates an existing server-level Firewall Rule for an Azure SQL
 /// Database Server.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Sql.IFirewallRuleOperations.
 /// </param>
 /// <param name='serverName'>
 /// Required. The name of the Azure SQL Database Server that has the
 /// Firewall Rule to be updated.
 /// </param>
 /// <param name='ruleName'>
 /// Required. The name of the Firewall Rule to be updated.
 /// </param>
 /// <param name='parameters'>
 /// Required. The parameters for the Update Firewall Rule operation.
 /// </param>
 /// <returns>
 /// Represents the firewall rule update response.
 /// </returns>
 public static FirewallRuleUpdateResponse Update(this IFirewallRuleOperations operations, string serverName, string ruleName, FirewallRuleUpdateParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IFirewallRuleOperations)s).UpdateAsync(serverName, ruleName, parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }