Skip to content

Commit

Permalink
IResourceId and ResourceId.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Mar 16, 2018
1 parent 117f1cb commit d86fc91
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
<Compile Include="INestedResourceConfig.cs" />
<Compile Include="INestedResourceConfigVisitor.cs" />
<Compile Include="INestedResourceStrategy.cs" />
<Compile Include="IResourceId.cs" />
<Compile Include="Property.cs" />
<Compile Include="ResourceId.cs" />
<Compile Include="ResourceType.cs" />
<Compile Include="SdkEngine.cs" />
<Compile Include="IEngine.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> id)
=> "/" + string.Join("/", id);

Expand All @@ -34,14 +32,14 @@ public static IEnumerable<string> GetIdFromSubscription(this IEntityConfig confi
{
var resourceGroupId = new[]
{
ResourceType.ResourceGroups, config.GetResourceGroupName()
ResourceId.ResourceGroups, config.GetResourceGroupName()
};
return config.ResourceGroup == null
? resourceGroupId
: resourceGroupId.Concat(config.GetProvidersId());
}

internal static IEnumerable<string> GetProvidersId(this IEntityConfig config)
=> new[] { Providers }.Concat(config.GetIdFromResourceGroup());
=> new[] { ResourceId.Providers }.Concat(config.GetIdFromResourceGroup());
}
}
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -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";

/// <summary>
/// Returns 'null' if the given id is not parsable.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>
/// A resource type namespace, for example 'Microsoft.Network'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ namespace Microsoft.Azure.Commands.Common.Strategies
/// Engine for REST API calls using Azure SDK.
/// </summary>
public sealed class SdkEngine : IEngine
{
public const string Subscriptions = "subscriptions";

{
string _SubscriptionId { get; }

public SdkEngine(string subscriptionId)
Expand All @@ -31,7 +29,7 @@ public SdkEngine(string subscriptionId)
}

public string GetId(IEntityConfig config)
=> new[] { Subscriptions, _SubscriptionId }
=> new[] { ResourceId.Subscriptions, _SubscriptionId }
.Concat(config.GetIdFromSubscription())
.IdToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,43 +86,22 @@ public static async Task<ImageAndOsType> 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,
Expand Down

0 comments on commit d86fc91

Please sign in to comment.