From d86fc9108ee1b25f5eab6e3dafc50f53ca20f740 Mon Sep 17 00:00:00 2001 From: Sergey Shandar Date: Fri, 16 Mar 2018 14:13:53 -0700 Subject: [PATCH] IResourceId and ResourceId. --- .../Commands.Common.Strategies.csproj | 2 + .../EntityConfigExtensions.cs | 6 +- .../Commands.Common.Strategies/IResourceId.cs | 24 ++++++ .../Commands.Common.Strategies/ResourceId.cs | 79 +++++++++++++++++++ .../ResourceType.cs | 6 +- .../Commands.Common.Strategies/SdkEngine.cs | 6 +- .../Strategies/ComputeRp/ImageEx.cs | 37 ++------- 7 files changed, 119 insertions(+), 41 deletions(-) create mode 100644 src/ResourceManager/Common/Commands.Common.Strategies/IResourceId.cs create mode 100644 src/ResourceManager/Common/Commands.Common.Strategies/ResourceId.cs diff --git a/src/ResourceManager/Common/Commands.Common.Strategies/Commands.Common.Strategies.csproj b/src/ResourceManager/Common/Commands.Common.Strategies/Commands.Common.Strategies.csproj index 2a1c72c9dbd2..e152a3063157 100644 --- a/src/ResourceManager/Common/Commands.Common.Strategies/Commands.Common.Strategies.csproj +++ b/src/ResourceManager/Common/Commands.Common.Strategies/Commands.Common.Strategies.csproj @@ -60,7 +60,9 @@ + + diff --git a/src/ResourceManager/Common/Commands.Common.Strategies/EntityConfigExtensions.cs b/src/ResourceManager/Common/Commands.Common.Strategies/EntityConfigExtensions.cs index ad95795027d7..2e9817fa6a65 100644 --- a/src/ResourceManager/Common/Commands.Common.Strategies/EntityConfigExtensions.cs +++ b/src/ResourceManager/Common/Commands.Common.Strategies/EntityConfigExtensions.cs @@ -19,8 +19,6 @@ namespace Microsoft.Azure.Commands.Common.Strategies { public static class EntityConfigExtensions { - public const string Providers = "providers"; - public static string IdToString(this IEnumerable id) => "/" + string.Join("/", id); @@ -34,7 +32,7 @@ public static IEnumerable GetIdFromSubscription(this IEntityConfig confi { var resourceGroupId = new[] { - ResourceType.ResourceGroups, config.GetResourceGroupName() + ResourceId.ResourceGroups, config.GetResourceGroupName() }; return config.ResourceGroup == null ? resourceGroupId @@ -42,6 +40,6 @@ public static IEnumerable GetIdFromSubscription(this IEntityConfig confi } internal static IEnumerable GetProvidersId(this IEntityConfig config) - => new[] { Providers }.Concat(config.GetIdFromResourceGroup()); + => new[] { ResourceId.Providers }.Concat(config.GetIdFromResourceGroup()); } } diff --git a/src/ResourceManager/Common/Commands.Common.Strategies/IResourceId.cs b/src/ResourceManager/Common/Commands.Common.Strategies/IResourceId.cs new file mode 100644 index 000000000000..78667e33f453 --- /dev/null +++ b/src/ResourceManager/Common/Commands.Common.Strategies/IResourceId.cs @@ -0,0 +1,24 @@ +// ---------------------------------------------------------------------------------- +// +// 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. +// ---------------------------------------------------------------------------------- + +namespace Microsoft.Azure.Commands.Common.Strategies +{ + public interface IResourceId + { + string SubscriptionId { get; } + string ResourceGroupName { get; } + ResourceType ResourceType { get; } + string Name { get; } + } +} diff --git a/src/ResourceManager/Common/Commands.Common.Strategies/ResourceId.cs b/src/ResourceManager/Common/Commands.Common.Strategies/ResourceId.cs new file mode 100644 index 000000000000..2e5182cc04d9 --- /dev/null +++ b/src/ResourceManager/Common/Commands.Common.Strategies/ResourceId.cs @@ -0,0 +1,79 @@ +// ---------------------------------------------------------------------------------- +// +// 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. +// ---------------------------------------------------------------------------------- + +namespace Microsoft.Azure.Commands.Common.Strategies +{ + public static class ResourceId + { + public const string Subscriptions = "subscriptions"; + public const string ResourceGroups = "resourceGroups"; + public const string Providers = "providers"; + + /// + /// Returns 'null' if the given id is not parsable. + /// + /// + /// + public static IResourceId TryParse(string id) + { + const int EmptyI = 0; + const int SubscriptionsI = 1; + const int SubscriptionIdI = 2; + const int ResourceGroupsI = 3; + const int ResourceGroupNameI = 4; + const int ProvidersI = 5; + const int NamespaceI = 6; + const int ProviderI = 7; + const int NameI = 8; + + var parts = id.Split('/'); + return parts.Length == 9 + && parts[EmptyI] == string.Empty + && parts[SubscriptionsI] == Subscriptions + && parts[ResourceGroupsI] == ResourceGroups + && parts[ProvidersI] == Providers + ? new Implementation( + subscriptionId: parts[SubscriptionIdI], + resourceGroupName: parts[ResourceGroupNameI], + resourceType: new ResourceType( + namespace_: parts[NamespaceI], + provider: parts[ProviderI]), + name: parts[NameI]) + : null; + } + + sealed class Implementation : IResourceId + { + public string Name { get; } + + public string ResourceGroupName { get; } + + public ResourceType ResourceType { get; } + + public string SubscriptionId { get; } + + public Implementation( + string subscriptionId, + string resourceGroupName, + ResourceType resourceType, + string name) + { + SubscriptionId = subscriptionId; + ResourceGroupName = resourceGroupName; + ResourceType = resourceType; + Name = name; + } + } + } +} diff --git a/src/ResourceManager/Common/Commands.Common.Strategies/ResourceType.cs b/src/ResourceManager/Common/Commands.Common.Strategies/ResourceType.cs index 344f2b8744c5..c6ea229c453c 100644 --- a/src/ResourceManager/Common/Commands.Common.Strategies/ResourceType.cs +++ b/src/ResourceManager/Common/Commands.Common.Strategies/ResourceType.cs @@ -15,11 +15,9 @@ namespace Microsoft.Azure.Commands.Common.Strategies { public sealed class ResourceType - { - public const string ResourceGroups = "resourceGroups"; - + { public static ResourceType ResourceGroup { get; } - = new ResourceType(null, ResourceGroups); + = new ResourceType(null, ResourceId.ResourceGroups); /// /// A resource type namespace, for example 'Microsoft.Network'. diff --git a/src/ResourceManager/Common/Commands.Common.Strategies/SdkEngine.cs b/src/ResourceManager/Common/Commands.Common.Strategies/SdkEngine.cs index 012d88068da1..83d25ab0d89f 100644 --- a/src/ResourceManager/Common/Commands.Common.Strategies/SdkEngine.cs +++ b/src/ResourceManager/Common/Commands.Common.Strategies/SdkEngine.cs @@ -20,9 +20,7 @@ namespace Microsoft.Azure.Commands.Common.Strategies /// Engine for REST API calls using Azure SDK. /// public sealed class SdkEngine : IEngine - { - public const string Subscriptions = "subscriptions"; - + { string _SubscriptionId { get; } public SdkEngine(string subscriptionId) @@ -31,7 +29,7 @@ public SdkEngine(string subscriptionId) } public string GetId(IEntityConfig config) - => new[] { Subscriptions, _SubscriptionId } + => new[] { ResourceId.Subscriptions, _SubscriptionId } .Concat(config.GetIdFromSubscription()) .IdToString(); } diff --git a/src/ResourceManager/Compute/Commands.Compute/Strategies/ComputeRp/ImageEx.cs b/src/ResourceManager/Compute/Commands.Compute/Strategies/ComputeRp/ImageEx.cs index e7d2ae2204e8..286629129c15 100644 --- a/src/ResourceManager/Compute/Commands.Compute/Strategies/ComputeRp/ImageEx.cs +++ b/src/ResourceManager/Compute/Commands.Compute/Strategies/ComputeRp/ImageEx.cs @@ -86,43 +86,22 @@ public static async Task UpdateImageAndOsTypeAsync( } else if (imageName.Contains("/")) { - var imageArray = imageName.Split('/'); - if (imageArray.Length != 9) + var resourceId = ResourceId.TryParse(imageName); + if (resourceId == null + || resourceId.ResourceType.Namespace != ComputeStrategy.Namespace + || resourceId.ResourceType.Provider != "images") { throw new ArgumentException(string.Format(Resources.ComputeInvalidImageName, imageName)); } - // has to be "" - var empty = imageArray[0]; - // has to be "subscriptions" - var subscriptions = imageArray[1]; - var subscriptionId = imageArray[2]; - // has to be "resourceGroups" - var resourceGroups = imageArray[3]; - var imageResourceGroupName = imageArray[4]; - // has to be "providers" - var providers = imageArray[5]; - // has to be "Microsoft." - var providerNamespace = imageArray[6]; - // has to be "image" - var provider = imageArray[7]; - var resourceName = imageArray[8]; - if (empty != string.Empty - || subscriptions != SdkEngine.Subscriptions - || resourceGroups != ResourceType.ResourceGroups - || providers != EntityConfigExtensions.Providers - || providerNamespace != ComputeStrategy.Namespace - || provider != "images") - { - throw new ArgumentException(string.Format(Resources.ComputeInvalidImageName, imageName)); - } - - if (compute.SubscriptionId != subscriptionId) + if (compute.SubscriptionId != resourceId.SubscriptionId) { throw new ArgumentException(Resources.ComputeMismatchSubscription); } - var localImage = await compute.Images.GetAsync(imageResourceGroupName, resourceName); + var localImage = await compute.Images.GetAsync( + resourceGroupName: resourceId.ResourceGroupName, + imageName: resourceId.Name); return new ImageAndOsType( localImage.StorageProfile.OsDisk.OsType,