Skip to content

Commit ae50903

Browse files
authored
Merge pull request Azure#9385 from antmarti-microsoft/antmarti/update_export_cmd
Add new options to Export-AzResourceGroup
2 parents 2bf2d1d + ae0f68d commit ae50903

File tree

11 files changed

+2222
-16
lines changed

11 files changed

+2222
-16
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
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.ResourceManager.Cmdlets.Entities.ResourceIds
16+
{
17+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
18+
using System;
19+
using System.Collections.Generic;
20+
using System.Linq;
21+
22+
/// <summary>
23+
/// Represents a resource group-level resource Id
24+
/// </summary>
25+
public class ResourceGroupLevelResourceId : ResourceId
26+
{
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="ResourceGroupLevelResourceId"/> class
29+
/// </summary>
30+
/// <param name="subscriptionId">The subscription Id</param>
31+
/// <param name="resourceGroup">The resource group</param>
32+
/// <param name="routingScope">The routing scope</param>
33+
/// <param name="parentScopes">The parent scopes</param>
34+
private ResourceGroupLevelResourceId(string subscriptionId, string resourceGroup, ResourceIdScope routingScope, IEnumerable<ResourceIdScope> parentScopes)
35+
: base(routingScope, parentScopes, ResourceGroupLevelResourceId.FormatResourceId(subscriptionId, resourceGroup, routingScope, parentScopes))
36+
{
37+
this.SubscriptionId = subscriptionId;
38+
this.ResourceGroup = resourceGroup;
39+
}
40+
41+
/// <summary>
42+
/// Gets the subscription Id
43+
/// </summary>
44+
public string SubscriptionId { get; }
45+
46+
/// <summary>
47+
/// Gets the resource group
48+
/// </summary>
49+
public string ResourceGroup { get; }
50+
51+
/// <summary>
52+
/// Enumerates the parent resource Ids
53+
/// </summary>
54+
/// <param name="orderByDepthAscending">Order by depth ascending?</param>
55+
public IEnumerable<ResourceGroupLevelResourceId> GetParents(bool orderByDepthAscending)
56+
=> this.RoutingScope
57+
.GetParents(orderByDepthAscending)
58+
.Select(routingScope => new ResourceGroupLevelResourceId(this.SubscriptionId, this.ResourceGroup, routingScope, this.ParentScopes));
59+
60+
/// <summary>
61+
/// Generates a child resource Id
62+
/// </summary>
63+
/// <param name="nestedType">The last segment of the child resource type</param>
64+
/// <param name="nestedName">The last segment of the child resource name</param>
65+
public ResourceGroupLevelResourceId GetChild(string nestedType, string nestedName)
66+
=> new ResourceGroupLevelResourceId(this.SubscriptionId, this.ResourceGroup, this.RoutingScope.GetChild(nestedType, nestedName), this.ParentScopes);
67+
68+
/// <summary>
69+
/// Gets the root resource Id
70+
/// </summary>
71+
public ResourceGroupLevelResourceId GetRootResourceId()
72+
=> this.IsRootResource ? this : this.GetParents(orderByDepthAscending: true).First();
73+
74+
#region Parsing
75+
/// <summary>
76+
/// Formats a resource group-level resource Id
77+
/// </summary>
78+
/// <param name="subscriptionId">The subscription Id</param>
79+
/// <param name="resourceGroup">The resource group</param>
80+
/// <param name="routingScope">The routing scope</param>
81+
/// <param name="parentScopes">The parent scopes</param>
82+
private static string FormatResourceId(string subscriptionId, string resourceGroup, ResourceIdScope routingScope, IEnumerable<ResourceIdScope> parentScopes)
83+
=> $"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/{ResourceId.FormatUnqualifiedId(routingScope, parentScopes)}";
84+
85+
/// <summary>
86+
/// Tries to create a new <see cref="ResourceGroupLevelResourceId"/>
87+
/// </summary>
88+
/// <param name="resourceId">The resource Id string</param>
89+
/// <param name="output">The resource Id output</param>
90+
public static bool TryParse(string resourceId, out ResourceGroupLevelResourceId output)
91+
{
92+
output = null;
93+
94+
if (resourceId == null)
95+
{
96+
return false;
97+
}
98+
99+
var segments = resourceId.Trim('/').SplitRemoveEmpty('/');
100+
if (segments.Length < 5)
101+
{
102+
return false;
103+
}
104+
105+
if (!"subscriptions".EqualsInsensitively(segments[0]))
106+
{
107+
return false;
108+
}
109+
110+
if (!"resourceGroups".EqualsInsensitively(segments[2]))
111+
{
112+
return false;
113+
}
114+
115+
if (!"providers".EqualsInsensitively(segments[4]))
116+
{
117+
return false;
118+
}
119+
120+
ResourceIdScope routingScope;
121+
IEnumerable<ResourceIdScope> parentScopes;
122+
if (!ResourceId.TryParseResourceScopes(
123+
pathSegments: segments,
124+
startIndex: 4,
125+
routingScope: out routingScope,
126+
parentScopes: out parentScopes))
127+
{
128+
return false;
129+
}
130+
131+
output = new ResourceGroupLevelResourceId(
132+
subscriptionId: segments[1],
133+
resourceGroup: segments[3],
134+
routingScope: routingScope,
135+
parentScopes: parentScopes);
136+
return true;
137+
}
138+
139+
/// <summary>
140+
/// Creates a new <see cref="ResourceGroupLevelResourceId"/>
141+
/// </summary>
142+
/// <param name="resourceId">The resource Id string</param>
143+
public static ResourceGroupLevelResourceId Parse(string resourceId)
144+
{
145+
ResourceGroupLevelResourceId output;
146+
if (!ResourceGroupLevelResourceId.TryParse(resourceId, out output))
147+
{
148+
throw new ArgumentException($"Unable to parse fully qualified resource id '{resourceId}'.", nameof(resourceId));
149+
}
150+
151+
return output;
152+
}
153+
154+
/// <summary>
155+
/// Creates a new <see cref="TenantLevelResourceId"/>
156+
/// </summary>
157+
/// <param name="subscriptionId">The subscription Id</param>
158+
/// <param name="resourceGroup">The resource group</param>
159+
/// <param name="routingResourceId">The routing resource Id</param>
160+
/// <param name="output">The resource Id output</param>
161+
public static bool TryCreateFromRoutingResourceId(string subscriptionId, string resourceGroup, string routingResourceId, out ResourceGroupLevelResourceId output)
162+
=> TryParse($"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/{routingResourceId}", out output);
163+
164+
/// <summary>
165+
/// Creates a new <see cref="TenantLevelResourceId"/>
166+
/// </summary>
167+
/// <param name="subscriptionId">The subscription Id</param>
168+
/// <param name="resourceGroup">The resource group</param>
169+
/// <param name="providerNamespace">The provider namespace</param>
170+
/// <param name="typeHierarchy">The type hierarchy</param>
171+
/// <param name="nameHierarchy">The name hierarchy</param>
172+
public static ResourceGroupLevelResourceId Create(string subscriptionId, string resourceGroup, string providerNamespace, IEnumerable<string> typeHierarchy, IEnumerable<string> nameHierarchy)
173+
=> new ResourceGroupLevelResourceId(subscriptionId, resourceGroup, ResourceIdScope.Create(providerNamespace, typeHierarchy, nameHierarchy), Enumerable.Empty<ResourceIdScope>());
174+
#endregion
175+
176+
#region Equality
177+
/// <summary>
178+
/// Overload for the == operator
179+
/// </summary>
180+
/// <param name="obj1">The first resource Id</param>
181+
/// <param name="obj2">The second resource Id</param>
182+
public static bool operator ==(ResourceGroupLevelResourceId obj1, ResourceGroupLevelResourceId obj2)
183+
{
184+
if (object.ReferenceEquals(obj1, obj2))
185+
{
186+
return true;
187+
}
188+
189+
if (object.ReferenceEquals(obj1, null) || object.ReferenceEquals(obj2, null))
190+
{
191+
return false;
192+
}
193+
194+
return StringComparer.OrdinalIgnoreCase.Equals(obj1.FullyQualifiedId, obj2.FullyQualifiedId);
195+
}
196+
197+
/// <summary>
198+
/// Overload for the != operator
199+
/// </summary>
200+
/// <param name="obj1">The first resource Id</param>
201+
/// <param name="obj2">The second resource Id</param>
202+
public static bool operator !=(ResourceGroupLevelResourceId obj1, ResourceGroupLevelResourceId obj2)
203+
=> !(obj1 == obj2);
204+
205+
/// <summary>
206+
/// Equality check
207+
/// </summary>
208+
/// <param name="obj">The object to compare to</param>
209+
public override bool Equals(object obj)
210+
=> this == (obj as ResourceGroupLevelResourceId);
211+
212+
/// <summary>
213+
/// Gets the HashCode for this resource Id
214+
/// </summary>
215+
public override int GetHashCode()
216+
=> StringComparer.OrdinalIgnoreCase.GetHashCode(this.FullyQualifiedId);
217+
#endregion
218+
}
219+
}

0 commit comments

Comments
 (0)