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 7 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
10 changes: 9 additions & 1 deletion 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 All @@ -26,17 +27,24 @@ internal class ClientContext
/// </summary>
public Uri BaseUri { get; set; }

/// <summary>
/// pipeline transport
/// </summary>
public HttpPipelineTransport Transport { get; set; }
m-nash marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Initializes a new instance of the <see cref="ClientContext"/> class.
/// </summary>
/// <param name="clientOptions"></param>
/// <param name="credential"></param>
/// <param name="uri"></param>
internal ClientContext(ArmClientOptions clientOptions, TokenCredential credential, Uri uri)
/// <param name="transport"></param>
internal ClientContext(ArmClientOptions clientOptions, TokenCredential credential, Uri uri, HttpPipelineTransport transport = default)
{
ClientOptions = clientOptions;
Credential = credential;
BaseUri = uri;
Transport = transport;
}
}
}
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; }
internal ArmClientOptions(LocationData location, IList<HttpPipelinePolicy> perCallPolicies, IList<HttpPipelinePolicy> perRetryPolicies)
m-nash marked this conversation as resolved.
Show resolved Hide resolved
{
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 @@ -153,8 +153,9 @@ public override ArmResponse<GenericResource> Get(CancellationToken cancellationT
/// <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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ internal OperationsBase(ClientContext clientContext, ResourceIdentifier id)
Credential = clientContext.Credential;
BaseUri = clientContext.BaseUri;
Diagnostics = new ClientDiagnostics(ClientOptions);
Transport = (HttpClientTransport)clientContext.Transport;

Validate(id);
}

internal ClientDiagnostics Diagnostics { get; }

internal HttpClientTransport Transport { get; }

/// <summary>
/// Gets the resource identifier.
/// </summary>
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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading