-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathArmClientExtensions.cs
More file actions
60 lines (56 loc) · 2.56 KB
/
Copy pathArmClientExtensions.cs
File metadata and controls
60 lines (56 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using Azure.Core;
using Azure.ResourceManager;
using ModularPipelines.Azure.Exceptions;
using ModularPipelines.Azure.Scopes;
namespace ModularPipelines.Azure.Extensions;
/// <summary>
/// Extension methods for <see cref="ArmClient"/>.
/// </summary>
public static class ArmClientExtensions
{
/// <summary>
/// Gets the <see cref="ResourceIdentifier"/> for the specified Azure scope.
/// </summary>
/// <param name="armClient">The ARM client.</param>
/// <param name="azureScope">The Azure scope to resolve.</param>
/// <returns>The resolved <see cref="ResourceIdentifier"/>.</returns>
/// <exception cref="AzureResourceNotFoundException">
/// Thrown when the specified <see cref="AzureResourceIdentifier"/> cannot be found in the resource group.
/// </exception>
/// <exception cref="UnsupportedAzureScopeException">
/// Thrown when the <paramref name="azureScope"/> is of an unsupported type.
/// </exception>
public static async Task<ResourceIdentifier> GetResourceIdentifierAsync(this ArmClient armClient, AzureScope azureScope)
{
switch (azureScope)
{
case AzureResourceIdentifier azureResourceIdentifier:
{
await foreach (var page in armClient
.GetResourceGroupResource(azureResourceIdentifier.ToResourceGroupIdentifier())
.GetGenericResourcesAsync()
.AsPages())
{
foreach (var genericResource in page.Values)
{
if (genericResource.Id.Name == azureResourceIdentifier.ResourceName)
{
return genericResource.Id;
}
}
}
throw new AzureResourceNotFoundException(azureResourceIdentifier);
}
case AzureResourceGroupIdentifier azureResourceGroupIdentifier:
return azureResourceGroupIdentifier.ToResourceGroupIdentifier();
case AzureSubscriptionIdentifier azureSubscriptionIdentifier:
return azureSubscriptionIdentifier.ToSubscriptionIdentifier();
case AzureManagementGroupIdentifier azureManagementGroupIdentifier:
return azureManagementGroupIdentifier.ToManagementGroupIdentifier();
default:
{
throw new UnsupportedAzureScopeException(azureScope);
}
}
}
}