Skip to content

Commit 4b8dd40

Browse files
committed
Merge pull request #1058 from vivsriaus/Headers
Add cmdlet info headers (CommandName and ParameterSetName)
2 parents 8d5cedc + 36a1a5a commit 4b8dd40

File tree

8 files changed

+107
-22
lines changed

8 files changed

+107
-22
lines changed

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Commands.Resources.Rest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
<Compile Include="Handlers\AuthenticationHandler.cs" />
137137
<Compile Include="Handlers\RetryHandler.cs" />
138138
<Compile Include="Handlers\TracingHandler.cs" />
139+
<Compile Include="Handlers\CmdletInfoHandler.cs" />
139140
<Compile Include="Handlers\UserAgentHandler.cs" />
140141
<Compile Include="Implementation\GetAzureResourceGroupDeploymentOperationCmdlet.cs" />
141142
<Compile Include="Implementation\Lock\GetAzureResourceLockCmdlet.cs" />

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Components/ApiVersionHelper.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components
2424
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Providers;
2525
using Microsoft.Azure.Common.Authentication;
2626
using Microsoft.Azure.Common.Authentication.Models;
27+
using System.Collections.Generic;
2728

2829
/// <summary>
2930
/// Helper class for determining the API version
@@ -37,15 +38,15 @@ internal static class ApiVersionHelper
3738
/// <param name="resourceId">The resource Id.</param>
3839
/// <param name="cancellationToken">The cancellation token</param>
3940
/// <param name="pre">When specified, indicates if pre-release API versions should be considered.</param>
40-
internal static Task<string> DetermineApiVersion(AzureContext context, string resourceId, CancellationToken cancellationToken, bool? pre = null)
41+
internal static Task<string> DetermineApiVersion(AzureContext context, string resourceId, CancellationToken cancellationToken, bool? pre = null, Dictionary<string, string> cmdletHeaderValues = null)
4142
{
4243
var providerNamespace = ResourceIdUtility.GetExtensionProviderNamespace(resourceId)
4344
?? ResourceIdUtility.GetProviderNamespace(resourceId);
4445

4546
var resourceType = ResourceIdUtility.GetExtensionResourceType(resourceId: resourceId, includeProviderNamespace: false)
4647
?? ResourceIdUtility.GetResourceType(resourceId: resourceId, includeProviderNamespace: false);
4748

48-
return ApiVersionHelper.DetermineApiVersion(context: context, providerNamespace: providerNamespace, resourceType: resourceType, cancellationToken: cancellationToken, pre: pre);
49+
return ApiVersionHelper.DetermineApiVersion(context: context, providerNamespace: providerNamespace, resourceType: resourceType, cancellationToken: cancellationToken, pre: pre, cmdletHeaderValues: cmdletHeaderValues);
4950
}
5051

5152
/// <summary>
@@ -56,15 +57,16 @@ internal static Task<string> DetermineApiVersion(AzureContext context, string re
5657
/// <param name="resourceType">The resource type.</param>
5758
/// <param name="cancellationToken">The cancellation token</param>
5859
/// <param name="pre">When specified, indicates if pre-release API versions should be considered.</param>
59-
internal static Task<string> DetermineApiVersion(AzureContext context, string providerNamespace, string resourceType, CancellationToken cancellationToken, bool? pre = null)
60+
internal static Task<string> DetermineApiVersion(AzureContext context, string providerNamespace, string resourceType, CancellationToken cancellationToken, bool? pre = null, Dictionary<string, string> cmdletHeaderValues = null)
6061
{
6162
var cacheKey = ApiVersionCache.GetCacheKey(providerNamespace: providerNamespace, resourceType: resourceType);
6263
var apiVersions = ApiVersionCache.Instance
6364
.AddOrGetExisting(cacheKey: cacheKey, getFreshData: () => ApiVersionHelper.GetApiVersionsForResourceType(
6465
context,
6566
providerNamespace: providerNamespace,
6667
resourceType: resourceType,
67-
cancellationToken: cancellationToken));
68+
cancellationToken: cancellationToken,
69+
cmdletHeaderValues: cmdletHeaderValues));
6870

6971
apiVersions = apiVersions.CoalesceEnumerable().ToArray();
7072
var apiVersionsToSelectFrom = apiVersions;
@@ -96,9 +98,9 @@ internal static Task<string> DetermineApiVersion(AzureContext context, string pr
9698
/// <param name="providerNamespace">The provider namespace.</param>
9799
/// <param name="resourceType">The resource type.</param>
98100
/// <param name="cancellationToken">The cancellation token.</param>
99-
private static string[] GetApiVersionsForResourceType(AzureContext context, string providerNamespace, string resourceType, CancellationToken cancellationToken)
101+
private static string[] GetApiVersionsForResourceType(AzureContext context, string providerNamespace, string resourceType, CancellationToken cancellationToken, Dictionary<string, string> cmdletHeaderValues = null)
100102
{
101-
var resourceManagerClient = ResourceManagerClientHelper.GetResourceManagerClient(context);
103+
var resourceManagerClient = ResourceManagerClientHelper.GetResourceManagerClient(context, cmdletHeaderValues);
102104

103105
var defaultSubscription = context.Subscription;
104106

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Components/HttpClientHelper.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,21 @@ public abstract class HttpClientHelper
3636
/// </summary>
3737
private readonly IEnumerable<ProductInfoHeaderValue> headerValues;
3838

39+
/// <summary>
40+
/// The cmdlet info header values.
41+
/// </summary>
42+
private readonly Dictionary<string,string> cmdletHeaderValues;
43+
3944
/// <summary>
4045
/// Initializes a new instance of the <see cref="HttpClientHelper"/> class.
4146
/// </summary>
4247
/// <param name="credentials">The subscription cloud credentials.</param>
4348
/// <param name="headerValues">The header values.</param>
44-
protected HttpClientHelper(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues)
49+
protected HttpClientHelper(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues, Dictionary<string,string> cmdletHeaderValues)
4550
{
4651
this.credentials = credentials;
4752
this.headerValues = headerValues;
53+
this.cmdletHeaderValues = cmdletHeaderValues;
4854
}
4955

5056
/// <summary>
@@ -57,6 +63,7 @@ public virtual HttpClient CreateHttpClient(params DelegatingHandler[] primaryHan
5763
{
5864
new AuthenticationHandler(cloudCredentials: credentials),
5965
new UserAgentHandler(headerValues: headerValues),
66+
new CmdletInfoHandler(cmdletHeaderValues: cmdletHeaderValues),
6067
new TracingHandler(),
6168
new RetryHandler(),
6269
};

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Components/HttpClientHelperFactory.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ protected HttpClientHelperFactory()
4747
/// </summary>
4848
/// <param name="credentials">The credentials.</param>
4949
/// <param name="headerValues">The headers.</param>
50-
public virtual HttpClientHelper CreateHttpClientHelper(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues)
50+
public virtual HttpClientHelper CreateHttpClientHelper(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues, Dictionary<string,string> cmdletHeaderValues)
5151
{
52-
return new HttpClientHelperImpl(credentials: credentials, headerValues: headerValues);
52+
return new HttpClientHelperImpl(credentials: credentials, headerValues: headerValues, cmdletHeaderValues: cmdletHeaderValues);
5353
}
5454

5555
/// <summary>
@@ -62,8 +62,8 @@ private class HttpClientHelperImpl : HttpClientHelper
6262
/// </summary>
6363
/// <param name="credentials">The credentials.</param>
6464
/// <param name="headerValues">The headers.</param>
65-
public HttpClientHelperImpl(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues)
66-
: base(credentials: credentials, headerValues: headerValues)
65+
public HttpClientHelperImpl(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues, Dictionary<string, string> cmdletHeaderValues)
66+
: base(credentials: credentials, headerValues: headerValues, cmdletHeaderValues: cmdletHeaderValues)
6767
{
6868
}
6969
}

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Components/ResourceManagerClientHelper.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.RestClients;
2020
using Microsoft.Azure.Common.Authentication;
2121
using Microsoft.Azure.Common.Authentication.Models;
22+
using System.Collections.Generic;
2223

2324
/// <summary>
2425
/// Helper class for constructing <see cref="ResourceManagerRestRestClient"/>.
@@ -29,7 +30,7 @@ internal static class ResourceManagerClientHelper
2930
/// Gets a new instance of the <see cref="ResourceManagerRestRestClient"/>.
3031
/// </summary>
3132
/// <param name="context">The azure profile.</param>
32-
internal static ResourceManagerRestRestClient GetResourceManagerClient(AzureContext context)
33+
internal static ResourceManagerRestRestClient GetResourceManagerClient(AzureContext context, Dictionary<string, string> cmdletHeaderValues = null)
3334
{
3435
var endpoint = context.Environment.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager);
3536

@@ -46,7 +47,8 @@ internal static ResourceManagerRestRestClient GetResourceManagerClient(AzureCont
4647
httpClientHelper: HttpClientHelperFactory.Instance
4748
.CreateHttpClientHelper(
4849
credentials: AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(context),
49-
headerValues: AzureSession.ClientFactory.UserAgents));
50+
headerValues: AzureSession.ClientFactory.UserAgents,
51+
cmdletHeaderValues: cmdletHeaderValues));
5052
}
5153
}
5254
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Handlers
16+
{
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Net.Http;
20+
using System.Net.Http.Headers;
21+
using System.Threading;
22+
using System.Threading.Tasks;
23+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
24+
25+
/// <summary>
26+
/// The cmdlet info handler.
27+
/// </summary>
28+
public class CmdletInfoHandler : DelegatingHandler
29+
{
30+
/// <summary>
31+
/// The product info to add as headers.
32+
/// </summary>
33+
private readonly Dictionary<string, string> cmdletHeaderValues;
34+
35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="CmdletInfoHandler" /> class.
37+
/// </summary>
38+
/// <param name="headerValues">The product info to add as headers.</param>
39+
public CmdletInfoHandler(Dictionary<string, string> cmdletHeaderValues)
40+
{
41+
this.cmdletHeaderValues = cmdletHeaderValues;
42+
}
43+
44+
/// <summary>
45+
/// Add the custom headers to the outgoing request.
46+
/// </summary>
47+
/// <param name="request">The HTTP request message.</param>
48+
/// <param name="cancellationToken">The cancellation token.</param>
49+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
50+
{
51+
foreach (KeyValuePair<string, string> kvp in cmdletHeaderValues)
52+
{
53+
request.Headers.Add(kvp.Key, kvp.Value);
54+
}
55+
56+
return await base
57+
.SendAsync(request, cancellationToken)
58+
.ConfigureAwait(continueOnCapturedContext: false);
59+
}
60+
}
61+
}

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Implementation/ResourceManagerCmdletBase.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2828
using Common;
2929
using Microsoft.Azure.Common.Authentication;
3030
using Microsoft.Azure.Common.Authentication.Models;
31-
using Microsoft.WindowsAzure.Commands.Utilities.Common;
3231
using Newtonsoft.Json.Linq;
32+
using System.Collections.Generic;
3333

3434
/// <summary>
3535
/// The base class for resource manager cmdlets.
@@ -212,10 +212,11 @@ protected Task<string> DetermineApiVersion(string resourceId, bool? pre = null)
212212
{
213213
return string.IsNullOrWhiteSpace(this.ApiVersion)
214214
? ApiVersionHelper.DetermineApiVersion(
215-
DefaultContext,
215+
context: DefaultContext,
216216
resourceId: resourceId,
217217
cancellationToken: this.CancellationToken.Value,
218-
pre: pre ?? this.Pre)
218+
pre: pre ?? this.Pre,
219+
cmdletHeaderValues: this.GetCmdletHeaders())
219220
: Task.FromResult(this.ApiVersion);
220221
}
221222

@@ -233,7 +234,8 @@ protected Task<string> DetermineApiVersion(string providerNamespace, string reso
233234
providerNamespace: providerNamespace,
234235
resourceType: resourceType,
235236
cancellationToken: this.CancellationToken.Value,
236-
pre: pre ?? this.Pre)
237+
pre: pre ?? this.Pre,
238+
cmdletHeaderValues: this.GetCmdletHeaders())
237239
: Task.FromResult(this.ApiVersion);
238240
}
239241

@@ -257,7 +259,17 @@ public ResourceManagerRestRestClient GetResourcesClient()
257259
httpClientHelper: HttpClientHelperFactory.Instance
258260
.CreateHttpClientHelper(
259261
credentials: AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(DefaultContext),
260-
headerValues: AzureSession.ClientFactory.UserAgents));
262+
headerValues: AzureSession.ClientFactory.UserAgents,
263+
cmdletHeaderValues: this.GetCmdletHeaders()));
264+
}
265+
266+
private Dictionary<string, string> GetCmdletHeaders()
267+
{
268+
return new Dictionary<string, string>
269+
{
270+
{"ParameterSetName", this.ParameterSetName },
271+
{"CommandName", this.CommandRuntime.ToString() }
272+
};
261273
}
262274

263275
/// <summary>

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/ResourcesController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ public TestHttpClientHelperFactory(SubscriptionCloudCredentials credentials)
244244
/// </summary>
245245
/// <param name="credentials">The credentials.</param>
246246
/// <param name="headerValues">The headers.</param>
247-
public override HttpClientHelper CreateHttpClientHelper(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues)
247+
public override HttpClientHelper CreateHttpClientHelper(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues, Dictionary<string, string> cmdletHeaderValues)
248248
{
249-
return new HttpClientHelperImpl(credentials: this.credential, headerValues: headerValues);
249+
return new HttpClientHelperImpl(credentials: this.credential, headerValues: headerValues, cmdletHeaderValues: cmdletHeaderValues);
250250
}
251251

252252
/// <summary>
@@ -259,8 +259,8 @@ private class HttpClientHelperImpl : HttpClientHelper
259259
/// </summary>
260260
/// <param name="credentials">The credentials.</param>
261261
/// <param name="headerValues">The headers.</param>
262-
public HttpClientHelperImpl(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues)
263-
: base(credentials: credentials, headerValues: headerValues)
262+
public HttpClientHelperImpl(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues, Dictionary<string, string> cmdletHeaderValues)
263+
: base(credentials: credentials, headerValues: headerValues, cmdletHeaderValues: cmdletHeaderValues)
264264
{
265265
}
266266

0 commit comments

Comments
 (0)