Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nibhati - 5572 - make Credentials and Options internal #20243

Merged
merged 8 commits into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/ManagementCoreShared/ClientContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using Azure.Core;
using Azure.Core.Pipeline;

namespace Azure.ResourceManager.Core
{
Expand Down
19 changes: 19 additions & 0 deletions sdk/resourcemanager/Azure.ResourceManager.Core/src/ApiVersions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,24 @@ public void SetApiVersion(ResourceType resourceType, string apiVersion)
_nonLoadedResourceToApiVersion[resourceType.ToString()] = apiVersion;
}
}

internal ApiVersions Clone()
{
ApiVersions copy = new ApiVersions(_clientOptions);
copy.ProviderOperations = ProviderOperations;

copy._loadedResourceToApiVersions = new Dictionary<string, PropertyWrapper>();
foreach (var property in _loadedResourceToApiVersions)
{
copy._loadedResourceToApiVersions.Add(property.Key, property.Value);
}

copy._nonLoadedResourceToApiVersion = new Dictionary<string, string>();
foreach (var property in _nonLoadedResourceToApiVersion)
{
copy._nonLoadedResourceToApiVersion.Add(property.Key, property.Value);
}
return copy;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private ArmClient(
if (credential is null)
throw new ArgumentNullException(nameof(credential));

ClientOptions = options ?? new ArmClientOptions();
ClientOptions = options?.Clone() ?? new ArmClientOptions();
DefaultSubscription = string.IsNullOrWhiteSpace(defaultSubscriptionId)
? GetDefaultSubscription()
: GetSubscriptions().TryGet(defaultSubscriptionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public sealed class ArmClientOptions : ClientOptions
/// <summary>
/// Gets the ApiVersions object
/// </summary>
public ApiVersions ApiVersions { get; }
public ApiVersions ApiVersions { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="ArmClientOptions"/> class.
Expand Down Expand Up @@ -53,18 +53,31 @@ internal ArmClientOptions(LocationData defaultLocation, ArmClientOptions other =
if (defaultLocation is null)
throw new ArgumentNullException(nameof(defaultLocation));

// Will go away when moved into core since we will have directy acces the policies and transport, so just need to set those
// Will go away when moved into core since we will have directly access the policies and transport, so just need to set those
if (!ReferenceEquals(other, null))
Copy(other);
DefaultLocation = defaultLocation;
ApiVersionOverrides = new Dictionary<string, string>();
ApiVersions = new ApiVersions(this);
}

/// <summary>
/// Gets the Api version overrides.
/// </summary>
public Dictionary<string, string> ApiVersionOverrides { get; private set; }
private ArmClientOptions(LocationData location, IList<HttpPipelinePolicy> perCallPolicies, IList<HttpPipelinePolicy> perRetryPolicies)
{
if (location is null)
throw new ArgumentNullException(nameof(location));

DefaultLocation = location;
PerCallPolicies = new List<HttpPipelinePolicy>();
foreach (var call in perCallPolicies)
{
PerCallPolicies.Add(call);
}
PerRetryPolicies = new List<HttpPipelinePolicy>();
foreach (var retry in perRetryPolicies)
{
PerCallPolicies.Add(retry);
}
ApiVersions = new ApiVersions(this);
}

/// <summary>
/// Gets the default location to use if can't be inherited from parent.
Expand Down Expand Up @@ -163,5 +176,13 @@ private void Copy(ArmClientOptions other)
AddPolicy(pol, HttpPipelinePosition.PerRetry);
}
}

internal ArmClientOptions Clone()
{
ArmClientOptions copy = new ArmClientOptions(DefaultLocation, PerCallPolicies, PerRetryPolicies);
copy.ApiVersions = ApiVersions.Clone();
copy.Transport = Transport;
return copy;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ public ArmResponse<GenericResource> AddTag(string key, string value, Cancellatio
GenericResource resource = GetResource();
// Potential optimization on tags set, remove NOOP to bypass the call.
resource.Data.Tags[key] = value;
var apiVersion = GetApiVersion(cancellationToken);
return new PhArmResponse<GenericResource, ResourceManager.Resources.Models.GenericResource>(
Operations.StartUpdateById(Id, GetApiVersion(cancellationToken), resource.Data, cancellationToken).WaitForCompletionAsync(cancellationToken).EnsureCompleted(),
Operations.StartUpdateById(Id, apiVersion, resource.Data, cancellationToken).WaitForCompletionAsync(cancellationToken).EnsureCompleted(),
v => new GenericResource(this, new GenericResourceData(v)));
}

Expand All @@ -115,7 +116,8 @@ public async Task<ArmResponse<GenericResource>> AddTagAsync(string key, string v
{
GenericResource resource = await GetResourceAsync(cancellationToken).ConfigureAwait(false);
resource.Data.Tags[key] = value;
var op = await Operations.StartUpdateByIdAsync(Id, await GetApiVersionAsync(cancellationToken).ConfigureAwait(false), resource.Data, cancellationToken).ConfigureAwait(false);
var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);
var op = await Operations.StartUpdateByIdAsync(Id, apiVersion, resource.Data, cancellationToken).ConfigureAwait(false);
return new PhArmResponse<GenericResource, ResourceManager.Resources.Models.GenericResource>(
await op.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false),
v => new GenericResource(this, new GenericResourceData(v)));
Expand All @@ -126,8 +128,9 @@ public ArmOperation<GenericResource> StartAddTag(string key, string value, Cance
{
GenericResource resource = GetResource();
resource.Data.Tags[key] = value;
var apiVersion = GetApiVersion(cancellationToken);
return new PhArmOperation<GenericResource, ResourceManager.Resources.Models.GenericResource>(
Operations.StartUpdateById(Id, GetApiVersion(cancellationToken), resource.Data, cancellationToken).WaitForCompletionAsync(cancellationToken).EnsureCompleted(),
Operations.StartUpdateById(Id, apiVersion, resource.Data, cancellationToken).WaitForCompletionAsync(cancellationToken).EnsureCompleted(),
v => new GenericResource(this, new GenericResourceData(v)));
}

Expand All @@ -136,7 +139,8 @@ public async Task<ArmOperation<GenericResource>> StartAddTagAsync(string key, st
{
GenericResource resource = await GetResourceAsync(cancellationToken).ConfigureAwait(false);
resource.Data.Tags[key] = value;
var op = await Operations.StartUpdateByIdAsync(Id, await GetApiVersionAsync(cancellationToken).ConfigureAwait(false), resource.Data, cancellationToken).ConfigureAwait(false);
var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);
var op = await Operations.StartUpdateByIdAsync(Id, apiVersion, resource.Data, cancellationToken).ConfigureAwait(false);
return new PhArmOperation<GenericResource, ResourceManager.Resources.Models.GenericResource>(
await op.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false),
v => new GenericResource(this, new GenericResourceData(v)));
Expand All @@ -145,16 +149,18 @@ await op.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false),
/// <inheritdoc/>
public override ArmResponse<GenericResource> Get(CancellationToken cancellationToken = default)
{
var apiVersion = GetApiVersion(cancellationToken);
return new PhArmResponse<GenericResource, ResourceManager.Resources.Models.GenericResource>(
Operations.GetById(Id, GetApiVersion(cancellationToken), cancellationToken),
Operations.GetById(Id, apiVersion, cancellationToken),
v => new GenericResource(this, new GenericResourceData(v)));
}

/// <inheritdoc/>
public override async Task<ArmResponse<GenericResource>> GetAsync(CancellationToken cancellationToken = default)
{
var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);
m-nash marked this conversation as resolved.
Show resolved Hide resolved
return new PhArmResponse<GenericResource, ResourceManager.Resources.Models.GenericResource>(
await Operations.GetByIdAsync(Id, await GetApiVersionAsync(cancellationToken).ConfigureAwait(false), cancellationToken).ConfigureAwait(false),
await Operations.GetByIdAsync(Id, apiVersion, cancellationToken).ConfigureAwait(false),
v => new GenericResource(this, new GenericResourceData(v)));
}

Expand All @@ -173,8 +179,9 @@ public ArmResponse<GenericResource> SetTags(IDictionary<string, string> tags, Ca
{
GenericResource resource = GetResource();
resource.Data.Tags.ReplaceWith(tags);
var apiVersion = GetApiVersion(cancellationToken);
return new PhArmResponse<GenericResource, ResourceManager.Resources.Models.GenericResource>(
Operations.StartUpdateById(Id, GetApiVersion(cancellationToken), resource.Data, cancellationToken).WaitForCompletionAsync(cancellationToken).EnsureCompleted(),
Operations.StartUpdateById(Id, apiVersion, resource.Data, cancellationToken).WaitForCompletionAsync(cancellationToken).EnsureCompleted(),
v => new GenericResource(this, new GenericResourceData(v)));
}

Expand All @@ -183,7 +190,8 @@ public async Task<ArmResponse<GenericResource>> SetTagsAsync(IDictionary<string,
{
GenericResource resource = await GetResourceAsync(cancellationToken).ConfigureAwait(false);
resource.Data.Tags.ReplaceWith(tags);
var op = await Operations.StartUpdateByIdAsync(Id, await GetApiVersionAsync(cancellationToken).ConfigureAwait(false), resource.Data, cancellationToken).ConfigureAwait(false);
var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);
var op = await Operations.StartUpdateByIdAsync(Id, apiVersion, resource.Data, cancellationToken).ConfigureAwait(false);
return new PhArmResponse<GenericResource, ResourceManager.Resources.Models.GenericResource>(
await op.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false),
v => new GenericResource(this, new GenericResourceData(v)));
Expand All @@ -194,8 +202,9 @@ public ArmOperation<GenericResource> StartSetTags(IDictionary<string, string> ta
{
GenericResource resource = GetResource();
resource.Data.Tags.ReplaceWith(tags);
var apiVersion = GetApiVersion(cancellationToken);
return new PhArmOperation<GenericResource, ResourceManager.Resources.Models.GenericResource>(
Operations.StartUpdateById(Id, GetApiVersion(cancellationToken), resource.Data, cancellationToken).WaitForCompletionAsync(cancellationToken).EnsureCompleted(),
Operations.StartUpdateById(Id, apiVersion, resource.Data, cancellationToken).WaitForCompletionAsync(cancellationToken).EnsureCompleted(),
v => new GenericResource(this, new GenericResourceData(v)));
}

Expand All @@ -204,7 +213,8 @@ public async Task<ArmOperation<GenericResource>> StartSetTagsAsync(IDictionary<s
{
GenericResource resource = await GetResourceAsync(cancellationToken).ConfigureAwait(false);
resource.Data.Tags.ReplaceWith(tags);
var op = await Operations.StartUpdateByIdAsync(Id, await GetApiVersionAsync(cancellationToken).ConfigureAwait(false), resource.Data, cancellationToken).ConfigureAwait(false);
var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);
var op = await Operations.StartUpdateByIdAsync(Id, apiVersion, resource.Data, cancellationToken).ConfigureAwait(false);
return new PhArmOperation<GenericResource, ResourceManager.Resources.Models.GenericResource>(
await op.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false),
v => new GenericResource(this, new GenericResourceData(v)));
Expand All @@ -215,8 +225,9 @@ public ArmResponse<GenericResource> RemoveTag(string key, CancellationToken canc
{
GenericResource resource = GetResource();
resource.Data.Tags.Remove(key);
var apiVersion = GetApiVersion(cancellationToken);
return new PhArmResponse<GenericResource, ResourceManager.Resources.Models.GenericResource>(
Operations.StartUpdateById(Id, GetApiVersion(cancellationToken), resource.Data, cancellationToken).WaitForCompletionAsync(cancellationToken).EnsureCompleted(),
Operations.StartUpdateById(Id, apiVersion, resource.Data, cancellationToken).WaitForCompletionAsync(cancellationToken).EnsureCompleted(),
v => new GenericResource(this, new GenericResourceData(v)));
}

Expand All @@ -225,7 +236,8 @@ public async Task<ArmResponse<GenericResource>> RemoveTagAsync(string key, Cance
{
GenericResource resource = await GetResourceAsync(cancellationToken).ConfigureAwait(false);
resource.Data.Tags.Remove(key);
var op = await Operations.StartUpdateByIdAsync(Id, await GetApiVersionAsync(cancellationToken).ConfigureAwait(false), resource.Data, cancellationToken).ConfigureAwait(false);
var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);
var op = await Operations.StartUpdateByIdAsync(Id, apiVersion, resource.Data, cancellationToken).ConfigureAwait(false);
return new PhArmResponse<GenericResource, ResourceManager.Resources.Models.GenericResource>(
await op.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false),
v => new GenericResource(this, new GenericResourceData(v)));
Expand All @@ -236,8 +248,9 @@ public ArmOperation<GenericResource> StartRemoveTag(string key, CancellationToke
{
GenericResource resource = GetResource();
resource.Data.Tags.Remove(key);
var apiVersion = GetApiVersion(cancellationToken);
return new PhArmOperation<GenericResource, ResourceManager.Resources.Models.GenericResource>(
Operations.StartUpdateById(Id, GetApiVersion(cancellationToken), resource.Data, cancellationToken).WaitForCompletionAsync(cancellationToken).EnsureCompleted(),
Operations.StartUpdateById(Id, apiVersion, resource.Data, cancellationToken).WaitForCompletionAsync(cancellationToken).EnsureCompleted(),
v => new GenericResource(this, new GenericResourceData(v)));
}

Expand All @@ -246,7 +259,8 @@ public async Task<ArmOperation<GenericResource>> StartRemoveTagAsync(string key,
{
GenericResource resource = await GetResourceAsync(cancellationToken).ConfigureAwait(false);
resource.Data.Tags.Remove(key);
var op = await Operations.StartUpdateByIdAsync(Id, await GetApiVersionAsync(cancellationToken).ConfigureAwait(false), resource.Data, cancellationToken).ConfigureAwait(false);
var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);
var op = await Operations.StartUpdateByIdAsync(Id, apiVersion, resource.Data, cancellationToken).ConfigureAwait(false);
return new PhArmOperation<GenericResource, ResourceManager.Resources.Models.GenericResource>(
await op.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false),
v => new GenericResource(this, new GenericResourceData(v)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal OperationsBase(ClientContext clientContext, ResourceIdentifier id)
Credential = clientContext.Credential;
BaseUri = clientContext.BaseUri;
Diagnostics = new ClientDiagnostics(ClientOptions);

Validate(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,15 @@ public async Task TestClientContextPolicy()
var client1 = GetArmClient(options1);

Console.WriteLine("-----Client 1-----");
await foreach (var sub in client1.GetSubscriptions().ListAsync())
{
Console.WriteLine($"Check 1: Found {sub.Data.DisplayName}");
}
_ = await client1.DefaultSubscription.GetResourceGroups().Construct(LocationData.WestUS2).CreateOrUpdateAsync(Recording.GenerateAssetName("testrg"));
Assert.AreEqual(2, dummyPolicy1.numMsgGot);

options1.AddPolicy(dummyPolicy2, HttpPipelinePosition.PerCall);
await foreach (var sub in client1.GetSubscriptions().ListAsync())
{
Console.WriteLine($"Check 2: Found {sub.Data.DisplayName}");
}

_ = await client1.DefaultSubscription.GetResourceGroups().Construct(LocationData.WestUS2).CreateOrUpdateAsync(Recording.GenerateAssetName("test2Rg-"));

Assert.AreEqual(3, dummyPolicy1.numMsgGot);
//Assert.AreEqual(0, dummyPolicy2.numMsgGot); uncomment for ADO #5572
Assert.AreEqual(0, dummyPolicy2.numMsgGot);
}

private class dummyPolicy : HttpPipelineSynchronousPolicy
Expand All @@ -62,5 +57,28 @@ public override void OnReceivedResponse(HttpMessage message)
Interlocked.Add(ref numMsgGot, 2);
}
}

[TestCase]
[RecordedTest]
public void ValidateOptionsTestLocation()
{
var x = new ArmClientOptions();
var y = x.Clone();
Assert.IsTrue(ReferenceEquals(x.DefaultLocation, y.DefaultLocation));
}

[TestCase]
[RecordedTest]
public void ValidateOptionsTestApiVersions()
{
var x = new ArmClientOptions();
var y = x.Clone();
Assert.IsFalse(ReferenceEquals(x.ApiVersions, y.ApiVersions));
Assert.AreEqual(x.ApiVersions.TryGetApiVersion("{Microsoft.Resources/subscriptions/resourceGroups}"), y.ApiVersions.TryGetApiVersion("{Microsoft.Resources/subscriptions/resourceGroups}"));

x.ApiVersions.SetApiVersion("{Microsoft.Resources/subscriptions/resourceGroups}", "1500-10-10");
Assert.IsFalse(ReferenceEquals(x.ApiVersions, y.ApiVersions));
Assert.AreNotEqual(x.ApiVersions, y.ApiVersions);
}
}
}
Loading