-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
ArmClient.cs
315 lines (279 loc) · 17.1 KB
/
ArmClient.cs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.ResourceManager.ManagementGroups;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Resources.Models;
namespace Azure.ResourceManager
{
/// <summary>
/// The entry point for all ARM clients.
/// </summary>
public partial class ArmClient
{
private TenantResource _tenant;
private SubscriptionResource _defaultSubscription;
private readonly ClientDiagnostics _subscriptionClientDiagnostics;
private bool? _canUseTagResource;
internal virtual Dictionary<ResourceType, string> ApiVersionOverrides { get; } = new Dictionary<ResourceType, string>();
internal ConcurrentDictionary<string, Dictionary<string, string>> ResourceApiVersionCache { get; } = new ConcurrentDictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
internal ConcurrentDictionary<string, string> NamespaceVersionCache { get; } = new ConcurrentDictionary<string, string>();
/// <summary>
/// Initializes a new instance of the <see cref="ArmClient"/> class for mocking.
/// </summary>
protected ArmClient()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArmClient"/> class.
/// </summary>
/// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
/// <exception cref="ArgumentNullException"> If <see cref="TokenCredential"/> is null. </exception>
#pragma warning disable AZC0007 // DO provide a minimal constructor that takes only the parameters required to connect to the service.
public ArmClient(TokenCredential credential) : this(credential, default, default)
#pragma warning restore AZC0007 // DO provide a minimal constructor that takes only the parameters required to connect to the service.
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArmClient"/> class.
/// </summary>
/// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
/// <param name="defaultSubscriptionId"> The id of the default Azure subscription. </param>
/// <exception cref="ArgumentNullException"> If <see cref="TokenCredential"/> is null. </exception>
public ArmClient(TokenCredential credential, string defaultSubscriptionId) : this(credential, defaultSubscriptionId, default)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArmClient"/> class.
/// </summary>
/// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
/// <param name="defaultSubscriptionId"> The id of the default Azure subscription. </param>
/// <param name="options"> The client parameters to use in these operations. </param>
/// <exception cref="ArgumentNullException"> If <see cref="TokenCredential"/> is null. </exception>
public ArmClient(TokenCredential credential, string defaultSubscriptionId, ArmClientOptions options)
{
Argument.AssertNotNull(credential, nameof(credential));
options ??= new ArmClientOptions();
ArmEnvironment environment = options.Environment.HasValue ? options.Environment.Value : ArmEnvironment.AzurePublicCloud;
Argument.AssertNotNull(environment.Endpoint, nameof(environment.Endpoint));
Endpoint = environment.Endpoint;
Pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, environment.DefaultScope));
Diagnostics = options.Diagnostics;
_subscriptionClientDiagnostics = new ClientDiagnostics("Azure.ResourceManager", SubscriptionResource.ResourceType.Namespace, Diagnostics);
CopyApiVersionOverrides(options);
_tenant = new TenantResource(this);
_defaultSubscription = string.IsNullOrWhiteSpace(defaultSubscriptionId) ? null :
new SubscriptionResource(this, SubscriptionResource.CreateResourceIdentifier(defaultSubscriptionId));
}
internal virtual bool CanUseTagResource(CancellationToken cancellationToken = default)
{
if (_canUseTagResource == null)
{
var tagRp = GetDefaultSubscription(cancellationToken).GetResourceProvider(TagResource.ResourceType.Namespace, cancellationToken: cancellationToken);
_canUseTagResource = tagRp.Value.Data.ResourceTypes.Any(rp => rp.ResourceType == TagResource.ResourceType.Type);
}
return _canUseTagResource.Value;
}
internal virtual async Task<bool> CanUseTagResourceAsync(CancellationToken cancellationToken = default)
{
if (_canUseTagResource == null)
{
var tagRp = await GetDefaultSubscription(cancellationToken).GetResourceProviderAsync(TagResource.ResourceType.Namespace, cancellationToken: cancellationToken).ConfigureAwait(false);
_canUseTagResource = tagRp.Value.Data.ResourceTypes.Any(rp => rp.ResourceType == TagResource.ResourceType.Type);
}
return _canUseTagResource.Value;
}
private void CopyApiVersionOverrides(ArmClientOptions options)
{
foreach (var keyValuePair in options.ResourceApiVersionOverrides)
{
ApiVersionOverrides.Add(keyValuePair.Key, keyValuePair.Value);
}
}
/// <summary>
/// Gets the api version override if it has been set for the current client options.
/// </summary>
/// <param name="resourceType"> The resource type to get the version for. </param>
/// <param name="apiVersion"> The api version to variable to set. </param>
internal virtual bool TryGetApiVersion(ResourceType resourceType, out string apiVersion)
{
return ApiVersionOverrides.TryGetValue(resourceType, out apiVersion);
}
/// <summary>
/// Gets the diagnostic options used for this client.
/// </summary>
internal virtual DiagnosticsOptions Diagnostics { get; }
/// <summary>
/// Gets the base URI of the service.
/// </summary>
internal virtual Uri Endpoint { get; private set; }
/// <summary>
/// Gets the HTTP pipeline.
/// </summary>
internal virtual HttpPipeline Pipeline { get; private set; }
/// <summary>
/// Gets the Azure subscriptions.
/// </summary>
/// <returns> Subscription collection. </returns>
public virtual SubscriptionCollection GetSubscriptions() => _tenant.GetSubscriptions();
/// <summary>
/// Gets the tenants.
/// </summary>
/// <returns> Tenant collection. </returns>
public virtual TenantCollection GetTenants()
{
return new TenantCollection(this);
}
/// <summary>
/// Gets the default subscription.
/// </summary>
/// <returns> Resource operations of the Subscription. </returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual SubscriptionResource GetDefaultSubscription(CancellationToken cancellationToken = default)
#pragma warning restore AZC0015 // Unexpected client method return type.
{
using var scope = _subscriptionClientDiagnostics.CreateScope("ArmClient.GetDefaultSubscription");
scope.Start();
try
{
if (_defaultSubscription == null)
{
_defaultSubscription = GetSubscriptions().GetAll(cancellationToken).FirstOrDefault();
}
else if (_defaultSubscription.HasData)
{
return _defaultSubscription;
}
else
{
_defaultSubscription = _defaultSubscription.Get(cancellationToken);
}
if (_defaultSubscription is null)
{
throw new InvalidOperationException("No subscriptions found for the given credentials");
}
return _defaultSubscription;
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Gets the default subscription.
/// </summary>
/// <returns> Resource operations of the Subscription. </returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual async Task<SubscriptionResource> GetDefaultSubscriptionAsync(CancellationToken cancellationToken = default)
#pragma warning restore AZC0015 // Unexpected client method return type.
{
using var scope = _subscriptionClientDiagnostics.CreateScope("ArmClient.GetDefaultSubscription");
scope.Start();
try
{
if (_defaultSubscription == null)
{
_defaultSubscription = await GetSubscriptions().GetAllAsync(cancellationToken).FirstOrDefaultAsync(_ => true, cancellationToken).ConfigureAwait(false);
}
else if (_defaultSubscription.HasData)
{
return _defaultSubscription;
}
else
{
_defaultSubscription = await _defaultSubscription.GetAsync(cancellationToken).ConfigureAwait(false);
}
if (_defaultSubscription is null)
{
throw new InvalidOperationException("No subscriptions found for the given credentials");
}
return _defaultSubscription;
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary> Gets a collection of GenericResources. </summary>
/// <returns> An object representing collection of GenericResources and their operations. </returns>
public virtual GenericResourceCollection GetGenericResources() => _tenant.GetGenericResources();
/// <summary> Gets all resource providers for a subscription. </summary>
/// <param name="top"> [This parameter is no longer supported.] The number of results to return. </param>
/// <param name="expand"> The properties to include in the results. For example, use &$expand=metadata in the query string to retrieve resource provider metadata. To include property aliases in response, use $expand=resourceTypes/aliases. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
[ForwardsClientCalls]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.ObsoleteAttribute("This method is obsolete as the `top` parameter is not supported by service and will be removed in a future release.", false)]
public virtual Pageable<TenantResourceProvider> GetTenantResourceProviders(int? top, string expand, CancellationToken cancellationToken = default) => _tenant.GetTenantResourceProviders(expand, cancellationToken);
/// <summary> Gets all resource providers for a subscription. </summary>
/// <param name="top"> [This parameter is no longer supported.] The number of results to return. </param>
/// <param name="expand"> The properties to include in the results. For example, use &$expand=metadata in the query string to retrieve resource provider metadata. To include property aliases in response, use $expand=resourceTypes/aliases. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
[ForwardsClientCalls]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.ObsoleteAttribute("This method is obsolete as the `top` parameter is not supported by service and will be removed in a future release.", false)]
public virtual AsyncPageable<TenantResourceProvider> GetTenantResourceProvidersAsync(int? top, string expand, CancellationToken cancellationToken = default) => _tenant.GetTenantResourceProvidersAsync(expand, cancellationToken);
/// <summary> Gets all resource providers for a subscription. </summary>
/// <param name="expand"> The properties to include in the results. For example, use &$expand=metadata in the query string to retrieve resource provider metadata. To include property aliases in response, use $expand=resourceTypes/aliases. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
[ForwardsClientCalls]
public virtual Pageable<TenantResourceProvider> GetTenantResourceProviders(string expand = null, CancellationToken cancellationToken = default) => _tenant.GetTenantResourceProviders(expand, cancellationToken);
/// <summary> Gets all resource providers for a subscription. </summary>
/// <param name="expand"> The properties to include in the results. For example, use &$expand=metadata in the query string to retrieve resource provider metadata. To include property aliases in response, use $expand=resourceTypes/aliases. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
[ForwardsClientCalls]
public virtual AsyncPageable<TenantResourceProvider> GetTenantResourceProvidersAsync(string expand = null, CancellationToken cancellationToken = default) => _tenant.GetTenantResourceProvidersAsync(expand, cancellationToken);
/// <summary> Gets the specified resource provider at the tenant level. </summary>
/// <param name="resourceProviderNamespace"> The namespace of the resource provider. </param>
/// <param name="expand"> The $expand query parameter. For example, to include property aliases in response, use $expand=resourceTypes/aliases. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
/// <exception cref="ArgumentNullException"> <paramref name="resourceProviderNamespace"/> is null. </exception>
[ForwardsClientCalls]
public virtual Response<TenantResourceProvider> GetTenantResourceProvider(string resourceProviderNamespace, string expand = null, CancellationToken cancellationToken = default) => _tenant.GetTenantResourceProvider(resourceProviderNamespace, expand, cancellationToken);
/// <summary> Gets the specified resource provider at the tenant level. </summary>
/// <param name="resourceProviderNamespace"> The namespace of the resource provider. </param>
/// <param name="expand"> The $expand query parameter. For example, to include property aliases in response, use $expand=resourceTypes/aliases. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
/// <exception cref="ArgumentNullException"> <paramref name="resourceProviderNamespace"/> is null. </exception>
[ForwardsClientCalls]
public virtual async Task<Response<TenantResourceProvider>> GetTenantResourceProviderAsync(string resourceProviderNamespace, string expand = null, CancellationToken cancellationToken = default) => await _tenant.GetTenantResourceProviderAsync(resourceProviderNamespace, expand, cancellationToken).ConfigureAwait(false);
/// <summary>
/// Gets the management group collection for this tenant.
/// </summary>
/// <returns> A collection of the management groups. </returns>
public virtual ManagementGroupCollection GetManagementGroups() => _tenant.GetManagementGroups();
/// <summary>
/// Gets a client using this instance of ArmClient to copy the client settings from.
/// </summary>
/// <typeparam name="T"> The type of <see cref="ArmResource"/> that will be constructed. </typeparam>
/// <param name="resourceFactory"> Delegate method that will construct the client. </param>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual T GetResourceClient<T>(Func<T> resourceFactory)
where T : ArmResource
{
return resourceFactory();
}
private readonly ConcurrentDictionary<Type, object> _clientCache = new ConcurrentDictionary<Type, object>();
/// <summary>
/// Gets a cached client to use for extension methods.
/// </summary>
/// <typeparam name="T"> The type of client to get. </typeparam>
/// <param name="clientFactory"> The constructor factory for the client. </param>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual T GetCachedClient<T>(Func<ArmClient, T> clientFactory)
where T : class
{
return _clientCache.GetOrAdd(typeof(T), (type) => { return clientFactory(this); }) as T;
}
}
}