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

Cache the creation of HTTP pipeline in ArmClient #20636

Merged
Merged
Show file tree
Hide file tree
Changes from 12 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
9 changes: 8 additions & 1 deletion common/ManagementCoreShared/ClientContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,24 @@ internal class ClientContext
/// </summary>
public Uri BaseUri { get; set; }

/// <summary>
/// pipeline
/// </summary>
public HttpPipeline Pipeline { get; set; }

/// <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="pipeline"></param>
internal ClientContext(ArmClientOptions clientOptions, TokenCredential credential, Uri uri, HttpPipeline pipeline)
{
ClientOptions = clientOptions;
Credential = credential;
BaseUri = uri;
Pipeline = pipeline;
}
}
}
18 changes: 12 additions & 6 deletions sdk/resourcemanager/Azure.ResourceManager.Core/src/ArmClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Azure.Core;
using Azure.Core.Pipeline;

namespace Azure.ResourceManager.Core
{
Expand All @@ -23,7 +23,7 @@ public class ArmClient
/// <summary>
/// Get the tenant operations <see cref="TenantOperations"/> class.
/// </summary>
public TenantOperations Tenant => _tenant ??= new TenantOperations(ClientOptions, Credential, BaseUri);
public TenantOperations Tenant => _tenant ??= new TenantOperations(ClientOptions, Credential, BaseUri, Pipeline);

/// <summary>
/// Initializes a new instance of the <see cref="ArmClient"/> class for mocking.
Expand Down Expand Up @@ -86,6 +86,7 @@ private ArmClient(
TokenCredential credential,
ArmClientOptions options)
{
Pipeline = ManagementPipelineBuilder.Build(credential, baseUri, options);
Credential = credential;
BaseUri = baseUri;
if (credential is null)
Expand Down Expand Up @@ -118,13 +119,18 @@ private ArmClient(
/// </summary>
protected virtual Uri BaseUri { get; private set; }

/// <summary>
/// Gets the HTTP pipeline.
/// </summary>
protected virtual HttpPipeline Pipeline { get; private set; }

/// <summary>
/// Gets the Azure subscriptions.
/// </summary>
/// <returns> Subscription container. </returns>
public virtual SubscriptionContainer GetSubscriptions()
{
return new SubscriptionContainer(new ClientContext(ClientOptions, Credential, BaseUri));
return new SubscriptionContainer(new ClientContext(ClientOptions, Credential, BaseUri, Pipeline));
}

/// <summary>
Expand All @@ -134,7 +140,7 @@ public virtual SubscriptionContainer GetSubscriptions()
/// <returns> Resource operations of the resource. </returns>
public ResourceGroupOperations GetResourceGroupOperations(ResourceGroupResourceIdentifier id)
{
return new ResourceGroupOperations(new SubscriptionOperations(new ClientContext(ClientOptions, Credential, BaseUri), id.SubscriptionId), id.ResourceGroupName);
return new ResourceGroupOperations(new SubscriptionOperations(new ClientContext(ClientOptions, Credential, BaseUri, Pipeline), id.SubscriptionId), id.ResourceGroupName);
}

/// <summary>
Expand All @@ -148,7 +154,7 @@ public ResourceGroupOperations GetResourceGroupOperations(ResourceGroupResourceI
public virtual T GetResourceOperations<T>(string subscription, string resourceGroup, string name)
where T : OperationsBase
{
var subOp = new SubscriptionOperations(new ClientContext(ClientOptions, Credential, BaseUri), subscription);
var subOp = new SubscriptionOperations(new ClientContext(ClientOptions, Credential, BaseUri, Pipeline), subscription);
var rgOp = subOp.GetResourceGroups().Get(resourceGroup);
return Activator.CreateInstance(
typeof(T),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core;
using System;
using System.Globalization;
using System.Threading;
Expand Down Expand Up @@ -33,13 +32,13 @@ internal ContainerBase(ClientContext clientContext, TIdentifier id)
: base(clientContext, id)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ContainerBase{TOperations, TIdentifier}"/> class.
/// </summary>
/// <param name="parent"> The resource representing the parent resource. </param>
protected ContainerBase(ResourceOperationsBase parent)
: base(new ClientContext(parent.ClientOptions, parent.Credential, parent.BaseUri), parent.Id)
: base(new ClientContext(parent.ClientOptions, parent.Credential, parent.BaseUri, parent.Pipeline), parent.Id)
{
Parent = parent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected ExtensionResourceContainer(OperationsBase operations)
/// <param name="operations"> The operations to copy the client options from. </param>
/// <param name="parentId"> The resource Id of the parent resource. </param>
protected ExtensionResourceContainer(OperationsBase operations, ResourceIdentifier parentId)
: base(new ClientContext(operations.ClientOptions, operations.Credential, operations.BaseUri), parentId)
: base(new ClientContext(operations.ClientOptions, operations.Credential, operations.BaseUri, operations.Pipeline), parentId)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class ExtensionResourceOperationsBase : OperationsBase
/// </summary>
/// <param name="genericOperations"> The operations to copy the client options from. </param>
internal ExtensionResourceOperationsBase(OperationsBase genericOperations)
: this(new ClientContext(genericOperations.ClientOptions, genericOperations.Credential, genericOperations.BaseUri), genericOperations.Id)
: this(new ClientContext(genericOperations.ClientOptions, genericOperations.Credential, genericOperations.BaseUri, genericOperations.Pipeline), genericOperations.Id)
{
}

Expand Down Expand Up @@ -54,7 +54,7 @@ protected ExtensionResourceOperationsBase(OperationsBase genericOperations)
/// <param name="genericOperations"> The operations to copy the client options from. </param>
/// <param name="id"> The identifier of the extension resource. </param>
protected ExtensionResourceOperationsBase(OperationsBase genericOperations, ResourceIdentifier id)
: base(new ClientContext(genericOperations.ClientOptions, genericOperations.Credential, genericOperations.BaseUri), id)
: base(new ClientContext(genericOperations.ClientOptions, genericOperations.Credential, genericOperations.BaseUri, genericOperations.Pipeline), id)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ internal OperationsBase(ClientContext clientContext, ResourceIdentifier id)
Id = id;
Credential = clientContext.Credential;
BaseUri = clientContext.BaseUri;
Pipeline = clientContext.Pipeline;
Diagnostics = new ClientDiagnostics(ClientOptions);

Validate(id);
}

Expand All @@ -58,6 +59,11 @@ internal OperationsBase(ClientContext clientContext, ResourceIdentifier id)
/// </summary>
protected internal virtual Uri BaseUri { get; private set; }

/// <summary>
/// Gets the HTTP pipeline.
/// </summary>
protected internal virtual HttpPipeline Pipeline { get; }

/// <summary>
/// Gets the valid Azure resource type for the current operations.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.ResourceManager.Core;
using Azure.ResourceManager.Core.Extensions;

namespace Azure.ResourceManager.Core
Expand All @@ -31,7 +29,7 @@ protected ResourceOperationsBase()
/// <param name="id"></param>
internal ResourceOperationsBase(ClientContext clientContext, ResourceIdentifier id)
: base(clientContext, id)
{
{
}
}

Expand All @@ -58,7 +56,7 @@ protected ResourceOperationsBase()
/// <param name="parentOperations"> The resource representing the parent resource. </param>
/// <param name="id"> The identifier of the resource that is the target of operations. </param>
protected ResourceOperationsBase(OperationsBase parentOperations, ResourceIdentifier id)
: base(new ClientContext(parentOperations.ClientOptions, parentOperations.Credential, parentOperations.BaseUri), id)
: base(new ClientContext(parentOperations.ClientOptions, parentOperations.Credential, parentOperations.BaseUri, parentOperations.Pipeline), id)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.ResourceManager.Core.Adapters;
using Azure.ResourceManager.Resources;
using System.Threading;
using System.Threading.Tasks;
using Azure.ResourceManager.Resources;

namespace Azure.ResourceManager.Core
{
Expand All @@ -18,7 +16,7 @@ public class LocationContainer : OperationsBase
/// </summary>
/// <param name="subscriptionOperations"> The subscription that this location container belongs to. </param>
internal LocationContainer(SubscriptionOperations subscriptionOperations)
: base(new ClientContext(subscriptionOperations.ClientOptions, subscriptionOperations.Credential, subscriptionOperations.BaseUri), subscriptionOperations.Id)
: base(new ClientContext(subscriptionOperations.ClientOptions, subscriptionOperations.Credential, subscriptionOperations.BaseUri, subscriptionOperations.Pipeline), subscriptionOperations.Id)
{
Id = subscriptionOperations.Id;
}
Expand All @@ -42,7 +40,7 @@ internal LocationContainer(SubscriptionOperations subscriptionOperations)
/// <returns> Subscription container. </returns>
public SubscriptionContainer GetSubscriptions()
{
return new SubscriptionContainer(new ClientContext(ClientOptions, Credential, BaseUri));
return new SubscriptionContainer(new ClientContext(ClientOptions, Credential, BaseUri, Pipeline));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core;
using Azure.ResourceManager.Core.Adapters;
using Azure.ResourceManager.Resources;
using System;
using System.Threading;
using Azure.Core;
using Azure.ResourceManager.Resources;

namespace Azure.ResourceManager.Core
{
Expand Down Expand Up @@ -111,13 +110,13 @@ protected override void Validate(ResourceIdentifier identifier)
/// <returns> An instance of <see cref="ResourceOperationsBase{TenantResourceIdentifier, Subscription}"/>. </returns>
protected override ResourceOperationsBase<SubscriptionResourceIdentifier, Subscription> GetOperation(string subscriptionGuid)
{
return new SubscriptionOperations(new ClientContext(ClientOptions, Credential, BaseUri), subscriptionGuid);
return new SubscriptionOperations(new ClientContext(ClientOptions, Credential, BaseUri, Pipeline), subscriptionGuid);
}

//TODO: can make static?
private Func<ResourceManager.Resources.Models.Subscription, Subscription> Converter()
{
return s => new Subscription(new SubscriptionOperations(new ClientContext(ClientOptions, Credential, BaseUri), s.SubscriptionId), new SubscriptionData(s));
return s => new Subscription(new SubscriptionOperations(new ClientContext(ClientOptions, Credential, BaseUri, Pipeline), s.SubscriptionId), new SubscriptionData(s));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
// Licensed under the MIT License.

using System;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.ResourceManager.Resources;
using Azure.Core.Pipeline;

namespace Azure.ResourceManager.Core
{
Expand All @@ -26,8 +23,9 @@ public class TenantOperations : OperationsBase
/// <param name="options"> The client parameters to use in these operations. </param>
/// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
/// <param name="baseUri"> The base URI of the service. </param>
internal TenantOperations(ArmClientOptions options, TokenCredential credential, Uri baseUri)
: base(new ClientContext(options, credential, baseUri), ResourceIdentifier.RootResourceIdentifier)
/// <param name="pipeline"> The HTTP pipeline for sending and receiving REST requests and responses. </param>
internal TenantOperations(ArmClientOptions options, TokenCredential credential, Uri baseUri, HttpPipeline pipeline)
: base(new ClientContext(options, credential, baseUri, pipeline), ResourceIdentifier.RootResourceIdentifier)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.Identity;
using NUnit.Framework;

namespace Azure.ResourceManager.Core.Tests.Scenario
{
[Parallelizable]
public class HttpPipelineTests : ResourceManagerTestBase
YalinLi0312 marked this conversation as resolved.
Show resolved Hide resolved
{
private ArmClient _client;

public HttpPipelineTests(bool isAsync)
: base(isAsync) //, RecordedTestMode.Record)
{
}

[SetUp]
public void SetUp()
{
_client = GetArmClient();
}

[Test]
YalinLi0312 marked this conversation as resolved.
Show resolved Hide resolved
public async Task ValidateHttpPipelines()
{
await _client.DefaultSubscription
.GetResourceGroups().Construct("westus")
.CreateOrUpdateAsync("test-CacheHttpPipeline");
YalinLi0312 marked this conversation as resolved.
Show resolved Hide resolved
await foreach (var rg in _client.DefaultSubscription.GetResourceGroups().ListAsync())
{
Assert.AreEqual(rg.Pipeline.GetHashCode(), _client.DefaultSubscription.Pipeline.GetHashCode());
}
}
}
}
Loading