PageRenderTime 116ms CodeModel.GetById 39ms RepoModel.GetById 8ms app.codeStats 1ms

/src/ServiceManagement/Sql/Commands.SqlDatabase/Firewall/Cmdlet/NewAzureSqlDatabaseServerFirewallRule.cs

https://github.com/IrisClasson/azure-sdk-tools
C# | 239 lines | 149 code | 24 blank | 66 comment | 3 complexity | 0f43b55b3d3ea6a398be3850af19d3bb MD5 | raw file
Possible License(s): Apache-2.0
  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright Microsoft Corporation
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ----------------------------------------------------------------------------------
  14. namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Firewall.Cmdlet
  15. {
  16. using Microsoft.WindowsAzure.Commands.SqlDatabase.Model;
  17. using Microsoft.WindowsAzure.Commands.SqlDatabase.Properties;
  18. using Microsoft.WindowsAzure.Management.Sql;
  19. using Microsoft.WindowsAzure.Management.Sql.Models;
  20. using System;
  21. using System.Globalization;
  22. using System.Management.Automation;
  23. /// <summary>
  24. /// Creates a new firewall rule for a Windows Azure SQL Database server in the selected subscription.
  25. /// </summary>
  26. [Cmdlet(VerbsCommon.New, "AzureSqlDatabaseServerFirewallRule",
  27. DefaultParameterSetName = IpRangeParameterSet,
  28. SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Low)]
  29. public class NewAzureSqlDatabaseServerFirewallRule : SqlDatabaseCmdletBase
  30. {
  31. /// <summary>
  32. /// The default rule name for allowing all Azure services. This is used when a
  33. /// rule name is not specified for the AllowAllAzureServicesParameterSet parameter
  34. /// set
  35. /// </summary>
  36. private const string AllowAllAzureServicesRuleName = "AllowAllAzureServices";
  37. /// <summary>
  38. /// The special IP for the beginning and ending of the firewall rule that will
  39. /// allow all azure services to connect to the server.
  40. /// </summary>
  41. private const string AllowAzureServicesRuleAddress = "0.0.0.0";
  42. #region Parameter Sets
  43. /// <summary>
  44. /// Parameter set that uses an IP Range
  45. /// </summary>
  46. internal const string IpRangeParameterSet = "IpRange";
  47. /// <summary>
  48. /// Parameter set for allowing all azure services
  49. /// </summary>
  50. internal const string AllowAllAzureServicesParameterSet = "AllowAllAzureServices";
  51. #endregion
  52. /// <summary>
  53. /// Gets or sets the name of the server to connect add the firewall rule to
  54. /// </summary>
  55. [Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true,
  56. HelpMessage = "SQL Database server name.")]
  57. [ValidateNotNullOrEmpty]
  58. public string ServerName
  59. {
  60. get;
  61. set;
  62. }
  63. /// <summary>
  64. /// Gets or sets the name of the fire wall rule
  65. /// </summary>
  66. [Parameter(Mandatory = true, ParameterSetName = IpRangeParameterSet,
  67. HelpMessage = "SQL Database server firewall rule name.")]
  68. [Parameter(Mandatory = false, ParameterSetName = AllowAllAzureServicesParameterSet,
  69. HelpMessage = "SQL Database server firewall rule name.")]
  70. [ValidateNotNullOrEmpty]
  71. public string RuleName
  72. {
  73. get;
  74. set;
  75. }
  76. /// <summary>
  77. /// Gets or sets the starting IP address for the rule
  78. /// </summary>
  79. [Parameter(Mandatory = true, HelpMessage = "Start of the IP Range.",
  80. ParameterSetName = IpRangeParameterSet)]
  81. [ValidateNotNullOrEmpty]
  82. public string StartIpAddress
  83. {
  84. get;
  85. set;
  86. }
  87. /// <summary>
  88. /// Gets or sets the ending IP address for the firewall rule
  89. /// </summary>
  90. [Parameter(Mandatory = true, HelpMessage = "End of the IP Range.",
  91. ParameterSetName = IpRangeParameterSet)]
  92. [ValidateNotNullOrEmpty]
  93. public string EndIpAddress
  94. {
  95. get;
  96. set;
  97. }
  98. /// <summary>
  99. /// Gets or sets whether or not to allow all windows azure services to connect
  100. /// </summary>
  101. [Parameter(Mandatory = true, HelpMessage = "Allow all Azure services access to the server.",
  102. ParameterSetName = AllowAllAzureServicesParameterSet)]
  103. [ValidateNotNullOrEmpty]
  104. public SwitchParameter AllowAllAzureServices
  105. {
  106. get;
  107. set;
  108. }
  109. /// <summary>
  110. /// Gets or sets whether or not to force the operation to proceed.
  111. /// </summary>
  112. [Parameter(HelpMessage = "Do not confirm on the creation of the firewall rule")]
  113. public SwitchParameter Force
  114. {
  115. get;
  116. set;
  117. }
  118. /// <summary>
  119. /// Creates a new firewall rule on the specified server.
  120. /// </summary>
  121. /// <param name="parameterSetName">The parameter set for the command.</param>
  122. /// <param name="serverName">The name of the server in which to create the firewall rule.</param>
  123. /// <param name="ruleName">The name of the new firewall rule.</param>
  124. /// <param name="startIpAddress">The starting IP address for the firewall rule.</param>
  125. /// <param name="endIpAddress">The ending IP address for the firewall rule.</param>
  126. /// <returns>The context to the newly created firewall rule.</returns>
  127. internal SqlDatabaseServerFirewallRuleContext NewAzureSqlDatabaseServerFirewallRuleProcess(
  128. string parameterSetName,
  129. string serverName,
  130. string ruleName,
  131. string startIpAddress,
  132. string endIpAddress)
  133. {
  134. // Get the SQL management client for the current subscription
  135. SqlManagementClient sqlManagementClient = GetCurrentSqlClient();
  136. // Create the firewall rule
  137. FirewallRuleCreateResponse response = sqlManagementClient.FirewallRules.Create(
  138. serverName,
  139. new FirewallRuleCreateParameters()
  140. {
  141. Name = ruleName,
  142. StartIPAddress = startIpAddress,
  143. EndIPAddress = endIpAddress,
  144. });
  145. SqlDatabaseServerFirewallRuleContext operationContext = new SqlDatabaseServerFirewallRuleContext()
  146. {
  147. OperationDescription = CommandRuntime.ToString(),
  148. OperationStatus = Services.Constants.OperationSuccess,
  149. OperationId = response.RequestId,
  150. ServerName = serverName,
  151. RuleName = ruleName,
  152. StartIpAddress = response.FirewallRule.StartIPAddress,
  153. EndIpAddress = response.FirewallRule.EndIPAddress,
  154. };
  155. return operationContext;
  156. }
  157. /// <summary>
  158. /// Process the command.
  159. /// </summary>
  160. public override void ExecuteCmdlet()
  161. {
  162. // Do nothing if force is not specified and user cancelled the operation
  163. string verboseDescription = string.Format(
  164. CultureInfo.InvariantCulture,
  165. Resources.NewAzureSqlDatabaseServerFirewallRuleDescription,
  166. this.RuleName,
  167. this.ServerName);
  168. string verboseWarning = string.Format(
  169. CultureInfo.InvariantCulture,
  170. Resources.NewAzureSqlDatabaseServerFirewallRuleWarning,
  171. this.RuleName,
  172. this.ServerName);
  173. if (!this.Force.IsPresent &&
  174. !this.ShouldProcess(verboseDescription, verboseWarning, Resources.ShouldProcessCaption))
  175. {
  176. return;
  177. }
  178. try
  179. {
  180. SqlDatabaseServerFirewallRuleContext context = null;
  181. switch (this.ParameterSetName)
  182. {
  183. case IpRangeParameterSet:
  184. context = this.NewAzureSqlDatabaseServerFirewallRuleProcess(
  185. this.ParameterSetName,
  186. this.ServerName,
  187. this.RuleName,
  188. this.StartIpAddress,
  189. this.EndIpAddress);
  190. break;
  191. case AllowAllAzureServicesParameterSet:
  192. // Determine which rule name to use.
  193. string ruleName = AllowAllAzureServicesRuleName;
  194. if (this.MyInvocation.BoundParameters.ContainsKey("RuleName"))
  195. {
  196. ruleName = this.RuleName;
  197. }
  198. // Create the rule
  199. context = this.NewAzureSqlDatabaseServerFirewallRuleProcess(
  200. this.ParameterSetName,
  201. this.ServerName,
  202. ruleName,
  203. AllowAzureServicesRuleAddress,
  204. AllowAzureServicesRuleAddress);
  205. break;
  206. }
  207. this.WriteObject(context, true);
  208. }
  209. catch (Exception ex)
  210. {
  211. this.WriteErrorDetails(ex);
  212. }
  213. }
  214. }
  215. }