Skip to content

Commit a834c4b

Browse files
Merge.
2 parents 70d7ba6 + d86fc91 commit a834c4b

File tree

7 files changed

+117
-41
lines changed

7 files changed

+117
-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: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -89,43 +89,20 @@ public static async Task<ImageAndOsType> UpdateImageAndOsTypeAsync(
8989
}
9090
else if (imageName.Contains("/"))
9191
{
92-
var imageArray = imageName.Split('/');
93-
if (imageArray.Length != 9)
92+
var resourceId = ResourceId.TryParse(imageName);
93+
if (resourceId == null
94+
|| resourceId.ResourceType.Namespace != ComputeStrategy.Namespace
95+
|| resourceId.ResourceType.Provider != "images")
9496
{
9597
throw new ArgumentException(string.Format(Resources.ComputeInvalidImageName, imageName));
9698
}
97-
// has to be ""
98-
var empty = imageArray[0];
99-
// has to be "subscriptions"
100-
var subscriptions = imageArray[1];
101-
var subscriptionId = imageArray[2];
102-
// has to be "resourceGroups"
103-
var resourceGroups = imageArray[3];
104-
var imageResourceGroupName = imageArray[4];
105-
// has to be "providers"
106-
var providers = imageArray[5];
107-
// has to be "Microsoft."
108-
var providerNamespace = imageArray[6];
109-
// has to be "image"
110-
var provider = imageArray[7];
111-
var resourceName = imageArray[8];
11299

113-
if (empty != string.Empty
114-
|| subscriptions != SdkEngine.Subscriptions
115-
|| resourceGroups != ResourceType.ResourceGroups
116-
|| providers != EntityConfigExtensions.Providers
117-
|| providerNamespace != ComputeStrategy.Namespace
118-
|| provider != "images")
119-
{
120-
throw new ArgumentException(string.Format(Resources.ComputeInvalidImageName, imageName));
121-
}
122-
123-
if (compute.SubscriptionId != subscriptionId)
100+
if (compute.SubscriptionId != resourceId.SubscriptionId)
124101
{
125102
throw new ArgumentException(Resources.ComputeMismatchSubscription);
126103
}
127104

128-
return await compute.GetImageAndOsTypeAsync(imageResourceGroupName, resourceName);
105+
return await compute.GetImageAndOsTypeAsync(resourceId.ResourceGroupName, resourceId.Name);
129106
}
130107
else
131108
{

0 commit comments

Comments
 (0)