Skip to content

Adding PsVersion to userAgent #3154

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 4 commits into from
Oct 29, 2016
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
38 changes: 35 additions & 3 deletions src/Common/Commands.Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ namespace Microsoft.WindowsAzure.Commands.Utilities.Common
/// </summary>
public abstract class AzurePSCmdlet : PSCmdlet, IDisposable
{
private const string PSVERSION = "PSVersion";
private const string DEFAULT_PSVERSION = "3.0.0.0";

public ConcurrentQueue<string> DebugMessages { get; private set; }

private RecordingTracingInterceptor _httpTracingInterceptor;
Expand All @@ -57,6 +60,35 @@ protected virtual bool IsErrorMetricEnabled
get { return true; }
}

/// <summary>
/// Indicates installed PowerShell version
/// </summary>
private string _psVersion;

/// <summary>
/// Get PsVersion returned from PowerShell.Runspace instance
/// </summary>
protected string PSVersion
{
get
{
if (string.IsNullOrEmpty(_psVersion))
{
if(this.Host != null)
{
_psVersion = this.Host.Version.ToString();
}
else
{
//We are doing this for perf. reasons. This code will execute during tests and so reducing the perf. overhead while running tests.
_psVersion = DEFAULT_PSVERSION;
}
}

return _psVersion;
}
}

/// <summary>
/// Gets the PowerShell module name used for user agent header.
/// By default uses "Azure PowerShell"
Expand Down Expand Up @@ -243,9 +275,9 @@ protected virtual void TearDownDebuggingTraces()

protected virtual void SetupHttpClientPipeline()
{
ProductInfoHeaderValue userAgentValue = new ProductInfoHeaderValue(
ModuleName, string.Format("v{0}", ModuleVersion));
AzureSession.ClientFactory.UserAgents.Add(userAgentValue);
AzureSession.ClientFactory.UserAgents.Add(new ProductInfoHeaderValue(ModuleName, string.Format("v{0}", ModuleVersion)));
AzureSession.ClientFactory.UserAgents.Add(new ProductInfoHeaderValue(PSVERSION, string.Format("v{0}", PSVersion)));

AzureSession.ClientFactory.AddHandler(
new CmdletInfoHandler(this.CommandRuntime.ToString(),
this.ParameterSetName, this._clientRequestId));
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/Commands.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<Compile Include="AzurePSDataCollectionProfile.cs" />
<Compile Include="AzurePowerShell.cs" />
<Compile Include="AzureRmProfileProvider.cs" />
<Compile Include="AzureSMProfileProvder.cs" />
<Compile Include="AzureSMProfileProvider.cs" />
<Compile Include="ConcurrentQueueExtensions.cs" />
<Compile Include="Constants.cs" />
<Compile Include="ContextExtensions.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
using System.Reflection;
using Xunit;
using Xunit.Abstractions;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Diagnostics;

namespace Microsoft.Azure.Commands.Profile.Test
{
Expand All @@ -40,6 +43,42 @@ public LoginCmdletTests(ITestOutputHelper output)
AzureRmProfileProvider.Instance.Profile = new AzureRMProfile();
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetPsVersionFromUserAgent()
{
var cmdlt = new AddAzureRMAccountCommand();

int preProcessingUserAgentCount = AzureSession.ClientFactory.UserAgents.Count;
Debug.WriteLine("UserAgents count prior to cmdLet processing = {0}", preProcessingUserAgentCount.ToString());
foreach (ProductInfoHeaderValue hv in AzureSession.ClientFactory.UserAgents)
{
Debug.WriteLine("Product:{0} - Version:{1}", hv.Product.Name, hv.Product.Version);
}

cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";

cmdlt.InvokeBeginProcessing();
int postProcessingUserAgentCount = AzureSession.ClientFactory.UserAgents.Count;
Debug.WriteLine("UserAgents count prior to cmdLet post processing = {0}", postProcessingUserAgentCount.ToString());
Assert.True(AzureSession.ClientFactory.UserAgents.Count >= preProcessingUserAgentCount);
HashSet<ProductInfoHeaderValue> piHv = AzureSession.ClientFactory.UserAgents;
string psUserAgentString = string.Empty;

foreach(ProductInfoHeaderValue hv in piHv)
{
if(hv.Product.Name.Equals("PSVersion") && (!string.IsNullOrEmpty(hv.Product.Version)))
{
psUserAgentString = string.Format("{0}-{1}", hv.Product.Name, hv.Product.Version);
}
}

Assert.NotEmpty(psUserAgentString);
Assert.Contains("PSVersion", psUserAgentString);
}

[Fact]
[Trait(Category.RunType, Category.LiveOnly)]
public void LoginWithSubscriptionAndTenant()
Expand Down