Skip to content

Add cmdlet info headers (CommandName and ParameterSetName) #1058

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

Merged
merged 1 commit into from
Oct 3, 2015
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
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<Compile Include="Handlers\AuthenticationHandler.cs" />
<Compile Include="Handlers\RetryHandler.cs" />
<Compile Include="Handlers\TracingHandler.cs" />
<Compile Include="Handlers\CmdletInfoHandler.cs" />
<Compile Include="Handlers\UserAgentHandler.cs" />
<Compile Include="Implementation\GetAzureResourceGroupDeploymentOperationCmdlet.cs" />
<Compile Include="Implementation\Lock\GetAzureResourceLockCmdlet.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Providers;
using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Models;
using System.Collections.Generic;

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

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

return ApiVersionHelper.DetermineApiVersion(context: context, providerNamespace: providerNamespace, resourceType: resourceType, cancellationToken: cancellationToken, pre: pre);
return ApiVersionHelper.DetermineApiVersion(context: context, providerNamespace: providerNamespace, resourceType: resourceType, cancellationToken: cancellationToken, pre: pre, cmdletHeaderValues: cmdletHeaderValues);
}

/// <summary>
Expand All @@ -56,15 +57,16 @@ internal static Task<string> DetermineApiVersion(AzureContext context, string re
/// <param name="resourceType">The resource type.</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <param name="pre">When specified, indicates if pre-release API versions should be considered.</param>
internal static Task<string> DetermineApiVersion(AzureContext context, string providerNamespace, string resourceType, CancellationToken cancellationToken, bool? pre = null)
internal static Task<string> DetermineApiVersion(AzureContext context, string providerNamespace, string resourceType, CancellationToken cancellationToken, bool? pre = null, Dictionary<string, string> cmdletHeaderValues = null)
{
var cacheKey = ApiVersionCache.GetCacheKey(providerNamespace: providerNamespace, resourceType: resourceType);
var apiVersions = ApiVersionCache.Instance
.AddOrGetExisting(cacheKey: cacheKey, getFreshData: () => ApiVersionHelper.GetApiVersionsForResourceType(
context,
providerNamespace: providerNamespace,
resourceType: resourceType,
cancellationToken: cancellationToken));
cancellationToken: cancellationToken,
cmdletHeaderValues: cmdletHeaderValues));

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

var defaultSubscription = context.Subscription;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@ public abstract class HttpClientHelper
/// </summary>
private readonly IEnumerable<ProductInfoHeaderValue> headerValues;

/// <summary>
/// The cmdlet info header values.
/// </summary>
private readonly Dictionary<string,string> cmdletHeaderValues;

/// <summary>
/// Initializes a new instance of the <see cref="HttpClientHelper"/> class.
/// </summary>
/// <param name="credentials">The subscription cloud credentials.</param>
/// <param name="headerValues">The header values.</param>
protected HttpClientHelper(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues)
protected HttpClientHelper(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues, Dictionary<string,string> cmdletHeaderValues)
{
this.credentials = credentials;
this.headerValues = headerValues;
this.cmdletHeaderValues = cmdletHeaderValues;
}

/// <summary>
Expand All @@ -57,6 +63,7 @@ public virtual HttpClient CreateHttpClient(params DelegatingHandler[] primaryHan
{
new AuthenticationHandler(cloudCredentials: credentials),
new UserAgentHandler(headerValues: headerValues),
new CmdletInfoHandler(cmdletHeaderValues: cmdletHeaderValues),
new TracingHandler(),
new RetryHandler(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ protected HttpClientHelperFactory()
/// </summary>
/// <param name="credentials">The credentials.</param>
/// <param name="headerValues">The headers.</param>
public virtual HttpClientHelper CreateHttpClientHelper(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues)
public virtual HttpClientHelper CreateHttpClientHelper(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues, Dictionary<string,string> cmdletHeaderValues)
{
return new HttpClientHelperImpl(credentials: credentials, headerValues: headerValues);
return new HttpClientHelperImpl(credentials: credentials, headerValues: headerValues, cmdletHeaderValues: cmdletHeaderValues);
}

/// <summary>
Expand All @@ -62,8 +62,8 @@ private class HttpClientHelperImpl : HttpClientHelper
/// </summary>
/// <param name="credentials">The credentials.</param>
/// <param name="headerValues">The headers.</param>
public HttpClientHelperImpl(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues)
: base(credentials: credentials, headerValues: headerValues)
public HttpClientHelperImpl(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues, Dictionary<string, string> cmdletHeaderValues)
: base(credentials: credentials, headerValues: headerValues, cmdletHeaderValues: cmdletHeaderValues)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.RestClients;
using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Models;
using System.Collections.Generic;

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

Expand All @@ -46,7 +47,8 @@ internal static ResourceManagerRestRestClient GetResourceManagerClient(AzureCont
httpClientHelper: HttpClientHelperFactory.Instance
.CreateHttpClientHelper(
credentials: AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(context),
headerValues: AzureSession.ClientFactory.UserAgents));
headerValues: AzureSession.ClientFactory.UserAgents,
cmdletHeaderValues: cmdletHeaderValues));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Handlers
{
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;

/// <summary>
/// The cmdlet info handler.
/// </summary>
public class CmdletInfoHandler : DelegatingHandler
{
/// <summary>
/// The product info to add as headers.
/// </summary>
private readonly Dictionary<string, string> cmdletHeaderValues;

/// <summary>
/// Initializes a new instance of the <see cref="CmdletInfoHandler" /> class.
/// </summary>
/// <param name="headerValues">The product info to add as headers.</param>
public CmdletInfoHandler(Dictionary<string, string> cmdletHeaderValues)
{
this.cmdletHeaderValues = cmdletHeaderValues;
}

/// <summary>
/// Add the custom headers to the outgoing request.
/// </summary>
/// <param name="request">The HTTP request message.</param>
/// <param name="cancellationToken">The cancellation token.</param>
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
foreach (KeyValuePair<string, string> kvp in cmdletHeaderValues)
{
request.Headers.Add(kvp.Key, kvp.Value);
}

return await base
.SendAsync(request, cancellationToken)
.ConfigureAwait(continueOnCapturedContext: false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
using Common;
using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

/// <summary>
/// The base class for resource manager cmdlets.
Expand Down Expand Up @@ -212,10 +212,11 @@ protected Task<string> DetermineApiVersion(string resourceId, bool? pre = null)
{
return string.IsNullOrWhiteSpace(this.ApiVersion)
? ApiVersionHelper.DetermineApiVersion(
DefaultContext,
context: DefaultContext,
resourceId: resourceId,
cancellationToken: this.CancellationToken.Value,
pre: pre ?? this.Pre)
pre: pre ?? this.Pre,
cmdletHeaderValues: this.GetCmdletHeaders())
: Task.FromResult(this.ApiVersion);
}

Expand All @@ -233,7 +234,8 @@ protected Task<string> DetermineApiVersion(string providerNamespace, string reso
providerNamespace: providerNamespace,
resourceType: resourceType,
cancellationToken: this.CancellationToken.Value,
pre: pre ?? this.Pre)
pre: pre ?? this.Pre,
cmdletHeaderValues: this.GetCmdletHeaders())
: Task.FromResult(this.ApiVersion);
}

Expand All @@ -257,7 +259,17 @@ public ResourceManagerRestRestClient GetResourcesClient()
httpClientHelper: HttpClientHelperFactory.Instance
.CreateHttpClientHelper(
credentials: AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(DefaultContext),
headerValues: AzureSession.ClientFactory.UserAgents));
headerValues: AzureSession.ClientFactory.UserAgents,
cmdletHeaderValues: this.GetCmdletHeaders()));
}

private Dictionary<string, string> GetCmdletHeaders()
{
return new Dictionary<string, string>
{
{"ParameterSetName", this.ParameterSetName },
{"CommandName", this.CommandRuntime.ToString() }
};
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ public TestHttpClientHelperFactory(SubscriptionCloudCredentials credentials)
/// </summary>
/// <param name="credentials">The credentials.</param>
/// <param name="headerValues">The headers.</param>
public override HttpClientHelper CreateHttpClientHelper(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues)
public override HttpClientHelper CreateHttpClientHelper(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues, Dictionary<string, string> cmdletHeaderValues)
{
return new HttpClientHelperImpl(credentials: this.credential, headerValues: headerValues);
return new HttpClientHelperImpl(credentials: this.credential, headerValues: headerValues, cmdletHeaderValues: cmdletHeaderValues);
}

/// <summary>
Expand All @@ -259,8 +259,8 @@ private class HttpClientHelperImpl : HttpClientHelper
/// </summary>
/// <param name="credentials">The credentials.</param>
/// <param name="headerValues">The headers.</param>
public HttpClientHelperImpl(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues)
: base(credentials: credentials, headerValues: headerValues)
public HttpClientHelperImpl(SubscriptionCloudCredentials credentials, IEnumerable<ProductInfoHeaderValue> headerValues, Dictionary<string, string> cmdletHeaderValues)
: base(credentials: credentials, headerValues: headerValues, cmdletHeaderValues: cmdletHeaderValues)
{
}

Expand Down