/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
- // ----------------------------------------------------------------------------------
- //
- // Copyright Microsoft Corporation
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- // http://www.apache.org/licenses/LICENSE-2.0
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- // ----------------------------------------------------------------------------------
-
- using System;
- using Microsoft.Azure.Commands.ResourceManager.Common;
- using Microsoft.Azure.Commands.Network.Common;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
-
- namespace Microsoft.Azure.Commands.Network
- {
- public abstract class NetworkBaseCmdlet : AzureRMCmdlet
- {
-
- private NetworkClient _networkClient;
-
- public const string IPv4 = "IPv4";
- public const string IPv6 = "IPv6";
- public const string All = "All";
- public const string DisabledRuleGroupsAlias = "DisabledRuleGroups";
-
- public NetworkClient NetworkClient
- {
- get
- {
- if (_networkClient == null)
- {
- _networkClient = new NetworkClient(DefaultProfile.DefaultContext);
- }
-
- this._networkClient.VerboseLogger = WriteVerboseWithTimestamp;
- this._networkClient.ErrorLogger = WriteErrorWithTimestamp;
- this._networkClient.WarningLogger = WriteWarningWithTimestamp;
- return _networkClient;
- }
-
- set { _networkClient = value; }
- }
- public override void ExecuteCmdlet()
- {
- base.ExecuteCmdlet();
- try
- {
- Execute();
- }
- catch (Rest.Azure.CloudException ex)
- {
- throw new NetworkCloudException(ex);
- }
- catch (Microsoft.Azure.Management.Network.Models.ErrorException ex)
- {
- Rest.Azure.CloudException rex = NetworkResourceManagerProfile.Mapper.Map<Rest.Azure.CloudException>(ex);
- throw new NetworkCloudException(rex);
- }
- catch (Microsoft.Azure.Management.Network.Models.ErrorResponseException ex)
- {
- Rest.Azure.CloudException rex = NetworkResourceManagerProfile.Mapper.Map<Rest.Azure.CloudException>(ex);
- throw new NetworkCloudException(rex);
- }
- }
- public virtual void Execute()
- {
- }
-
- public static string GetResourceGroup(string resourceId)
- {
- const string resourceGroup = "resourceGroups";
-
- var startIndex = resourceId.IndexOf(resourceGroup, StringComparison.OrdinalIgnoreCase) + resourceGroup.Length + 1;
- var endIndex = resourceId.IndexOf("/", startIndex, StringComparison.OrdinalIgnoreCase);
-
- return resourceId.Substring(startIndex, endIndex - startIndex);
- }
-
- public static string GetResourceName(string resourceId, string resourceName, string instanceName = null, string version = null)
- {
- if (string.IsNullOrEmpty(resourceId)) { return null; }
- Regex r = (instanceName == null && version == null)
- ? new Regex(@"(.*?)/" + resourceName + @"/(?<rgname>\S+)", RegexOptions.IgnoreCase)
- : new Regex(@"(.*?)/" + resourceName + @"/(?<rgname>\S+)/" + instanceName + @"/(?<instanceId>\S+)", RegexOptions.IgnoreCase);
- Match m = r.Match(resourceId);
- return m.Success ? m.Groups["rgname"].Value : null;
- }
-
- public static bool IsResourcePresent(Action fn)
- {
- try
- {
- fn();
- }
- catch (Rest.Azure.CloudException exception)
- {
- if (exception.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
- {
- return false;
- }
- throw;
- }
- catch (Microsoft.Azure.Management.Network.Models.ErrorException exception)
- {
- if (exception.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
- {
- return false;
- }
- throw;
- }
- catch (Microsoft.Azure.Management.Network.Models.ErrorResponseException exception)
- {
- if (exception.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
- {
- return false;
- }
- throw;
- }
-
- return true;
- }
- }
- }