Skip to content

Commit d86fc91

Browse files
IResourceId and ResourceId.
1 parent 117f1cb commit d86fc91

File tree

7 files changed

+119
-41
lines changed

7 files changed

+119
-41
lines changed

src/ResourceManager/Common/Commands.Common.Strategies/Commands.Common.Strategies.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
<Compile Include="INestedResourceConfig.cs" />
6161
<Compile Include="INestedResourceConfigVisitor.cs" />
6262
<Compile Include="INestedResourceStrategy.cs" />
63+
<Compile Include="IResourceId.cs" />
6364
<Compile Include="Property.cs" />
65+
<Compile Include="ResourceId.cs" />
6466
<Compile Include="ResourceType.cs" />
6567
<Compile Include="SdkEngine.cs" />
6668
<Compile Include="IEngine.cs" />

src/ResourceManager/Common/Commands.Common.Strategies/EntityConfigExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ namespace Microsoft.Azure.Commands.Common.Strategies
1919
{
2020
public static class EntityConfigExtensions
2121
{
22-
public const string Providers = "providers";
23-
2422
public static string IdToString(this IEnumerable<string> id)
2523
=> "/" + string.Join("/", id);
2624

@@ -34,14 +32,14 @@ public static IEnumerable<string> GetIdFromSubscription(this IEntityConfig confi
3432
{
3533
var resourceGroupId = new[]
3634
{
37-
ResourceType.ResourceGroups, config.GetResourceGroupName()
35+
ResourceId.ResourceGroups, config.GetResourceGroupName()
3836
};
3937
return config.ResourceGroup == null
4038
? resourceGroupId
4139
: resourceGroupId.Concat(config.GetProvidersId());
4240
}
4341

4442
internal static IEnumerable<string> GetProvidersId(this IEntityConfig config)
45-
=> new[] { Providers }.Concat(config.GetIdFromResourceGroup());
43+
=> new[] { ResourceId.Providers }.Concat(config.GetIdFromResourceGroup());
4644
}
4745
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
15+
namespace Microsoft.Azure.Commands.Common.Strategies
16+
{
17+
public interface IResourceId
18+
{
19+
string SubscriptionId { get; }
20+
string ResourceGroupName { get; }
21+
ResourceType ResourceType { get; }
22+
string Name { get; }
23+
}
24+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
15+
namespace Microsoft.Azure.Commands.Common.Strategies
16+
{
17+
public static class ResourceId
18+
{
19+
public const string Subscriptions = "subscriptions";
20+
public const string ResourceGroups = "resourceGroups";
21+
public const string Providers = "providers";
22+
23+
/// <summary>
24+
/// Returns 'null' if the given id is not parsable.
25+
/// </summary>
26+
/// <param name="id"></param>
27+
/// <returns></returns>
28+
public static IResourceId TryParse(string id)
29+
{
30+
const int EmptyI = 0;
31+
const int SubscriptionsI = 1;
32+
const int SubscriptionIdI = 2;
33+
const int ResourceGroupsI = 3;
34+
const int ResourceGroupNameI = 4;
35+
const int ProvidersI = 5;
36+
const int NamespaceI = 6;
37+
const int ProviderI = 7;
38+
const int NameI = 8;
39+
40+
var parts = id.Split('/');
41+
return parts.Length == 9
42+
&& parts[EmptyI] == string.Empty
43+
&& parts[SubscriptionsI] == Subscriptions
44+
&& parts[ResourceGroupsI] == ResourceGroups
45+
&& parts[ProvidersI] == Providers
46+
? new Implementation(
47+
subscriptionId: parts[SubscriptionIdI],
48+
resourceGroupName: parts[ResourceGroupNameI],
49+
resourceType: new ResourceType(
50+
namespace_: parts[NamespaceI],
51+
provider: parts[ProviderI]),
52+
name: parts[NameI])
53+
: null;
54+
}
55+
56+
sealed class Implementation : IResourceId
57+
{
58+
public string Name { get; }
59+
60+
public string ResourceGroupName { get; }
61+
62+
public ResourceType ResourceType { get; }
63+
64+
public string SubscriptionId { get; }
65+
66+
public Implementation(
67+
string subscriptionId,
68+
string resourceGroupName,
69+
ResourceType resourceType,
70+
string name)
71+
{
72+
SubscriptionId = subscriptionId;
73+
ResourceGroupName = resourceGroupName;
74+
ResourceType = resourceType;
75+
Name = name;
76+
}
77+
}
78+
}
79+
}

src/ResourceManager/Common/Commands.Common.Strategies/ResourceType.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
namespace Microsoft.Azure.Commands.Common.Strategies
1616
{
1717
public sealed class ResourceType
18-
{
19-
public const string ResourceGroups = "resourceGroups";
20-
18+
{
2119
public static ResourceType ResourceGroup { get; }
22-
= new ResourceType(null, ResourceGroups);
20+
= new ResourceType(null, ResourceId.ResourceGroups);
2321

2422
/// <summary>
2523
/// A resource type namespace, for example 'Microsoft.Network'.

src/ResourceManager/Common/Commands.Common.Strategies/SdkEngine.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ namespace Microsoft.Azure.Commands.Common.Strategies
2020
/// Engine for REST API calls using Azure SDK.
2121
/// </summary>
2222
public sealed class SdkEngine : IEngine
23-
{
24-
public const string Subscriptions = "subscriptions";
25-
23+
{
2624
string _SubscriptionId { get; }
2725

2826
public SdkEngine(string subscriptionId)
@@ -31,7 +29,7 @@ public SdkEngine(string subscriptionId)
3129
}
3230

3331
public string GetId(IEntityConfig config)
34-
=> new[] { Subscriptions, _SubscriptionId }
32+
=> new[] { ResourceId.Subscriptions, _SubscriptionId }
3533
.Concat(config.GetIdFromSubscription())
3634
.IdToString();
3735
}

src/ResourceManager/Compute/Commands.Compute/Strategies/ComputeRp/ImageEx.cs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -86,43 +86,22 @@ public static async Task<ImageAndOsType> UpdateImageAndOsTypeAsync(
8686
}
8787
else if (imageName.Contains("/"))
8888
{
89-
var imageArray = imageName.Split('/');
90-
if (imageArray.Length != 9)
89+
var resourceId = ResourceId.TryParse(imageName);
90+
if (resourceId == null
91+
|| resourceId.ResourceType.Namespace != ComputeStrategy.Namespace
92+
|| resourceId.ResourceType.Provider != "images")
9193
{
9294
throw new ArgumentException(string.Format(Resources.ComputeInvalidImageName, imageName));
9395
}
94-
// has to be ""
95-
var empty = imageArray[0];
96-
// has to be "subscriptions"
97-
var subscriptions = imageArray[1];
98-
var subscriptionId = imageArray[2];
99-
// has to be "resourceGroups"
100-
var resourceGroups = imageArray[3];
101-
var imageResourceGroupName = imageArray[4];
102-
// has to be "providers"
103-
var providers = imageArray[5];
104-
// has to be "Microsoft."
105-
var providerNamespace = imageArray[6];
106-
// has to be "image"
107-
var provider = imageArray[7];
108-
var resourceName = imageArray[8];
10996

110-
if (empty != string.Empty
111-
|| subscriptions != SdkEngine.Subscriptions
112-
|| resourceGroups != ResourceType.ResourceGroups
113-
|| providers != EntityConfigExtensions.Providers
114-
|| providerNamespace != ComputeStrategy.Namespace
115-
|| provider != "images")
116-
{
117-
throw new ArgumentException(string.Format(Resources.ComputeInvalidImageName, imageName));
118-
}
119-
120-
if (compute.SubscriptionId != subscriptionId)
97+
if (compute.SubscriptionId != resourceId.SubscriptionId)
12198
{
12299
throw new ArgumentException(Resources.ComputeMismatchSubscription);
123100
}
124101

125-
var localImage = await compute.Images.GetAsync(imageResourceGroupName, resourceName);
102+
var localImage = await compute.Images.GetAsync(
103+
resourceGroupName: resourceId.ResourceGroupName,
104+
imageName: resourceId.Name);
126105

127106
return new ImageAndOsType(
128107
localImage.StorageProfile.OsDisk.OsType,

0 commit comments

Comments
 (0)