/// <summary> /// Creates a new firewall rule on the specified server. /// </summary> /// <param name="parameterSetName">The parameter set for the command.</param> /// <param name="serverName">The name of the server in which to create the firewall rule.</param> /// <param name="ruleName">The name of the new firewall rule.</param> /// <param name="startIpAddress">The starting IP address for the firewall rule.</param> /// <param name="endIpAddress">The ending IP address for the firewall rule.</param> /// <returns>The context to the newly created firewall rule.</returns> internal SqlDatabaseServerFirewallRuleContext NewAzureSqlDatabaseServerFirewallRuleProcess( string parameterSetName, string serverName, string ruleName, string startIpAddress, string endIpAddress) { // Get the SQL management client for the current subscription SqlManagementClient sqlManagementClient = SqlDatabaseCmdletBase.GetCurrentSqlClient(); // Create the firewall rule FirewallRuleCreateResponse response = sqlManagementClient.FirewallRules.Create( serverName, new FirewallRuleCreateParameters() { Name = ruleName, StartIPAddress = startIpAddress, EndIPAddress = endIpAddress, }); SqlDatabaseServerFirewallRuleContext operationContext = new SqlDatabaseServerFirewallRuleContext() { OperationDescription = CommandRuntime.ToString(), OperationStatus = Services.Constants.OperationSuccess, OperationId = response.RequestId, ServerName = serverName, RuleName = ruleName, StartIpAddress = response.StartIPAddress, EndIpAddress = response.EndIPAddress, }; return(operationContext); }
/// <summary> /// Adds a new server-level Firewall Rule for an Azure SQL Database /// Server. /// </summary> /// <param name='serverName'> /// Required. The name of the Azure SQL Database Server to which this /// rule will be applied. /// </param> /// <param name='parameters'> /// Required. The parameters for the Create Firewall Rule operation. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Contains the response to a Create Firewall Rule operation. /// </returns> public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Sql.Models.FirewallRuleCreateResponse> CreateAsync(string serverName, FirewallRuleCreateParameters parameters, CancellationToken cancellationToken) { // Validate if (serverName == null) { throw new ArgumentNullException("serverName"); } 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("parameters", parameters); Tracing.Enter(invocationId, this, "CreateAsync", tracingParameters); } // Construct URL string url = "/" + (this.Client.Credentials.SubscriptionId != null ? this.Client.Credentials.SubscriptionId.Trim() : "") + "/services/sqlservers/servers/" + serverName.Trim() + "/firewallrules"; 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.Post; 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.Created) { 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 FirewallRuleCreateResponse result = null; // Deserialize Response cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new FirewallRuleCreateResponse(); 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(); } } }