/src/Network/Network/Common/NetworkBaseCmdlet.cs

https://github.com/Azure/azure-powershell · C# · 130 lines · 104 code · 13 blank · 13 comment · 12 complexity · a5b752fba6a2e5212f4911033302919e MD5 · raw file

  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. using System;
  15. using Microsoft.Azure.Commands.ResourceManager.Common;
  16. using Microsoft.Azure.Commands.Network.Common;
  17. using System.Collections.Generic;
  18. using System.Text.RegularExpressions;
  19. namespace Microsoft.Azure.Commands.Network
  20. {
  21. public abstract class NetworkBaseCmdlet : AzureRMCmdlet
  22. {
  23. private NetworkClient _networkClient;
  24. public const string IPv4 = "IPv4";
  25. public const string IPv6 = "IPv6";
  26. public const string All = "All";
  27. public const string DisabledRuleGroupsAlias = "DisabledRuleGroups";
  28. public NetworkClient NetworkClient
  29. {
  30. get
  31. {
  32. if (_networkClient == null)
  33. {
  34. _networkClient = new NetworkClient(DefaultProfile.DefaultContext);
  35. }
  36. this._networkClient.VerboseLogger = WriteVerboseWithTimestamp;
  37. this._networkClient.ErrorLogger = WriteErrorWithTimestamp;
  38. this._networkClient.WarningLogger = WriteWarningWithTimestamp;
  39. return _networkClient;
  40. }
  41. set { _networkClient = value; }
  42. }
  43. public override void ExecuteCmdlet()
  44. {
  45. base.ExecuteCmdlet();
  46. try
  47. {
  48. Execute();
  49. }
  50. catch (Rest.Azure.CloudException ex)
  51. {
  52. throw new NetworkCloudException(ex);
  53. }
  54. catch (Microsoft.Azure.Management.Network.Models.ErrorException ex)
  55. {
  56. Rest.Azure.CloudException rex = NetworkResourceManagerProfile.Mapper.Map<Rest.Azure.CloudException>(ex);
  57. throw new NetworkCloudException(rex);
  58. }
  59. catch (Microsoft.Azure.Management.Network.Models.ErrorResponseException ex)
  60. {
  61. Rest.Azure.CloudException rex = NetworkResourceManagerProfile.Mapper.Map<Rest.Azure.CloudException>(ex);
  62. throw new NetworkCloudException(rex);
  63. }
  64. }
  65. public virtual void Execute()
  66. {
  67. }
  68. public static string GetResourceGroup(string resourceId)
  69. {
  70. const string resourceGroup = "resourceGroups";
  71. var startIndex = resourceId.IndexOf(resourceGroup, StringComparison.OrdinalIgnoreCase) + resourceGroup.Length + 1;
  72. var endIndex = resourceId.IndexOf("/", startIndex, StringComparison.OrdinalIgnoreCase);
  73. return resourceId.Substring(startIndex, endIndex - startIndex);
  74. }
  75. public static string GetResourceName(string resourceId, string resourceName, string instanceName = null, string version = null)
  76. {
  77. if (string.IsNullOrEmpty(resourceId)) { return null; }
  78. Regex r = (instanceName == null && version == null)
  79. ? new Regex(@"(.*?)/" + resourceName + @"/(?<rgname>\S+)", RegexOptions.IgnoreCase)
  80. : new Regex(@"(.*?)/" + resourceName + @"/(?<rgname>\S+)/" + instanceName + @"/(?<instanceId>\S+)", RegexOptions.IgnoreCase);
  81. Match m = r.Match(resourceId);
  82. return m.Success ? m.Groups["rgname"].Value : null;
  83. }
  84. public static bool IsResourcePresent(Action fn)
  85. {
  86. try
  87. {
  88. fn();
  89. }
  90. catch (Rest.Azure.CloudException exception)
  91. {
  92. if (exception.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
  93. {
  94. return false;
  95. }
  96. throw;
  97. }
  98. catch (Microsoft.Azure.Management.Network.Models.ErrorException exception)
  99. {
  100. if (exception.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
  101. {
  102. return false;
  103. }
  104. throw;
  105. }
  106. catch (Microsoft.Azure.Management.Network.Models.ErrorResponseException exception)
  107. {
  108. if (exception.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
  109. {
  110. return false;
  111. }
  112. throw;
  113. }
  114. return true;
  115. }
  116. }
  117. }