Skip to content

Hiding Profile property and CurrentProfile static property #210

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 2 commits into from
Mar 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
39 changes: 29 additions & 10 deletions src/Common/Commands.Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,39 @@ namespace Microsoft.WindowsAzure.Commands.Utilities.Common
public abstract class AzurePSCmdlet : PSCmdlet
{
private readonly RecordingTracingInterceptor _httpTracingInterceptor = new RecordingTracingInterceptor();
protected static AzureProfile _currentProfile = null;

[Parameter(Mandatory = false, HelpMessage = "In-memory profile.")]
public AzureProfile Profile { get; set; }

public static AzureProfile CurrentProfile { get; set; }
/// <summary>
/// Sets the current profile - the profile used when no Profile is explicitly passed in. Should be used only by
/// Profile cmdlets and tests that need to set up a particular profile
/// </summary>
public static AzureProfile CurrentProfile
{
private get
{
if (_currentProfile == null)
{
_currentProfile = InitializeDefaultProfile();
SetTokenCacheForProfile(_currentProfile);
}

return _currentProfile;
}

set
{
SetTokenCacheForProfile(value);
_currentProfile = value;
}
}

protected static TokenCache DefaultDiskTokenCache { get; set; }

protected static TokenCache DefaultMemoryTokenCache { get; set; }

protected static AzureProfile DefaultProfile { get; set; }

static AzurePSCmdlet()
{
if (!TestMockSupport.RunningMocked)
Expand All @@ -50,9 +71,7 @@ static AzurePSCmdlet()
if (!TestMockSupport.RunningMocked)
{
InitializeTokenCaches();
DefaultProfile = InitializeDefaultProfile();
CurrentProfile = DefaultProfile;
UpdateSessionStateForProfile(CurrentProfile);
SetTokenCacheForProfile(CurrentProfile);
AzureSession.DataStore = new DiskDataStore();
}
}
Expand Down Expand Up @@ -96,7 +115,7 @@ protected static void InitializeTokenCaches()
/// Update the token cache when setting the profile
/// </summary>
/// <param name="profile"></param>
protected static void UpdateSessionStateForProfile(AzureProfile profile)
protected static void SetTokenCacheForProfile(AzureProfile profile)
{
var defaultProfilePath = Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile);
if (string.Equals(profile.ProfilePath, defaultProfilePath, StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -124,7 +143,7 @@ protected override void BeginProcessing()
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithParameterSetLog, this.GetType().Name, ParameterSetName));
}

if (Profile.Context != null && Profile.Context.Account != null && Profile.Context.Account.Id != null)
if (Profile != null && Profile.Context != null && Profile.Context.Account != null && Profile.Context.Account.Id != null)
{
WriteDebugWithTimestamp(string.Format("using account id '{0}'...", Profile.Context.Account.Id));
}
Expand All @@ -137,14 +156,14 @@ protected override void BeginProcessing()
/// <summary>
/// Ensure that there is a profile for the command
/// </summary>
private void InitializeProfile()
protected virtual void InitializeProfile()
{
if (Profile == null)
{
Profile = AzurePSCmdlet.CurrentProfile;
}

UpdateSessionStateForProfile(Profile);
SetTokenCacheForProfile(Profile);
}

/// <summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Common/Commands.Common/Properties/Resources.Designer.cs

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

3 changes: 3 additions & 0 deletions src/Common/Commands.Common/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1370,4 +1370,7 @@ use and privacy statement at &lt;url&gt; and (c) agree to sharing my contact inf
<data name="MissingSubscriptionInProfileProperties" xml:space="preserve">
<value>Property bag Hashtable must contain a 'SubscriptionId'.</value>
</data>
<data name="AzureProfileMustNotBeNull" xml:space="preserve">
<value>Selected profile must not be null.</value>
</data>
</root>
9 changes: 8 additions & 1 deletion src/Common/Commands.Profile/Profile/NewAzureProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public class NewAzureProfileCommand : AzurePSCmdlet
internal const string EnvironmentKey = "Environment";
internal const string StorageAccountKey = "StorageAccount";


[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = CertificateParameterSet)]
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ServicePrincipalParameterSet)]
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = AccessTokenParameterSet)]
Expand Down Expand Up @@ -97,6 +96,9 @@ public class NewAzureProfileCommand : AzurePSCmdlet
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = PropertyBagParameterSet)]
public Hashtable Properties { get; set; }

// do not use the Profile parameter for this cmdlet
private new AzureProfile Profile { get; set; }

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public override void ExecuteCmdlet()
{
Expand Down Expand Up @@ -127,6 +129,11 @@ public override void ExecuteCmdlet()
WriteObject(azureProfile);
}

protected override void InitializeProfile()
{
// do not initialize the current profile for this cmdlet
}

private void InitializeAzureProfile(AzureProfile profile, string parameterSet, AzureProfileSettings settings)
{
var profileClient = new ProfileClient(profile);
Expand Down
10 changes: 5 additions & 5 deletions src/Common/Commands.Profile/Profile/SelectAzureProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Microsoft.WindowsAzure.Commands.Profile
/// Creates new Microsoft Azure profile.
/// </summary>
[Cmdlet(VerbsCommon.Select, "AzureProfile"), OutputType(typeof(AzureProfile))]
public class SelectAzureProfileCommand : SubscriptionCmdletBase
public class SelectAzureProfileCommand : AzurePSCmdlet
{
internal const string NewProfileParameterSet = "NewProfile";
internal const string DefaultProfilePrameterSet = "DefaultProfile";
Expand All @@ -44,25 +44,25 @@ public class SelectAzureProfileCommand : SubscriptionCmdletBase
[Parameter(ParameterSetName=DefaultProfilePrameterSet, Mandatory=true)]
public SwitchParameter Default { get; set; }

public SelectAzureProfileCommand() : base(true)
protected override void InitializeProfile()
{
// do not initialize the profile for this cmdlet
}

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public override void ExecuteCmdlet()
{
if (ParameterSetName == DefaultProfilePrameterSet)
{
Profile = AzurePSCmdlet.DefaultProfile;
Profile = AzurePSCmdlet.InitializeDefaultProfile();
}

if (Profile == null)
{
throw new ArgumentException("Selected profile must not be null.");
throw new ArgumentException(Resources.AzureProfileMustNotBeNull);
}

AzurePSCmdlet.CurrentProfile = Profile;
AzurePSCmdlet.UpdateSessionStateForProfile(AzurePSCmdlet.CurrentProfile);
WriteObject(Profile);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public class EnvironmentSetupHelper
public EnvironmentSetupHelper()
{
AzureSession.DataStore = new MockDataStore();
AzurePSCmdlet.CurrentProfile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
ProfileClient = new ProfileClient(AzurePSCmdlet.CurrentProfile);
var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
AzurePSCmdlet.CurrentProfile = profile;
ProfileClient = new ProfileClient(profile);

// Ignore SSL errors
System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => true;
Expand Down Expand Up @@ -104,12 +105,12 @@ private void SetupAzureEnvironmentFromEnvironmentVariables(AzureModule mode)

if (csmEnvironment != null)
{
environment.Endpoints[AzureEnvironment.Endpoint.ResourceManager] = csmEnvironment.BaseUri.AbsoluteUri;
environment.Endpoints[AzureEnvironment.Endpoint.ResourceManager] = csmEnvironment.BaseUri.AbsoluteUri;
}

if (rdfeEnvironment != null)
{
environment.Endpoints[AzureEnvironment.Endpoint.ServiceManagement] = rdfeEnvironment.BaseUri.AbsoluteUri;
environment.Endpoints[AzureEnvironment.Endpoint.ServiceManagement] = rdfeEnvironment.BaseUri.AbsoluteUri;
}

if (!ProfileClient.Profile.Environments.ContainsKey(testEnvironmentName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,9 @@ public void SetAzureSubscriptionAddsSubscriptionWithCertificate()
public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnAdd()
{
// Setup
var profile = Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile);
AzurePSCmdlet.CurrentProfile =
new AzureProfile(profile);
ProfileClient client = new ProfileClient(AzurePSCmdlet.CurrentProfile);
var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
AzurePSCmdlet.CurrentProfile = profile;
ProfileClient client = new ProfileClient(profile);
client.AddOrSetEnvironment(azureEnvironment);
client.Profile.Save();
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
Expand Down Expand Up @@ -260,10 +259,9 @@ public void SetAzureSubscriptionThrowsExceptionWithoutCertificateOnAdd()
public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnSet()
{
// Setup
var profile = Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile);
AzurePSCmdlet.CurrentProfile =
new AzureProfile(profile);
ProfileClient client = new ProfileClient(AzurePSCmdlet.CurrentProfile);
var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
AzurePSCmdlet.CurrentProfile = profile;
ProfileClient client = new ProfileClient(profile);
client.AddOrSetAccount(azureAccount);
client.AddOrSetEnvironment(azureEnvironment);
client.AddOrSetSubscription(azureSubscription1);
Expand Down Expand Up @@ -291,9 +289,9 @@ public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnSet(
public void SetAzureSubscriptionDerivesEnvironmentFromServiceEndpointParameterOnSet()
{
// Setup
AzurePSCmdlet.CurrentProfile =
new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
ProfileClient client = new ProfileClient(AzurePSCmdlet.CurrentProfile);
var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
AzurePSCmdlet.CurrentProfile = profile;
ProfileClient client = new ProfileClient(profile);
client.AddOrSetAccount(azureAccount);
client.AddOrSetEnvironment(azureEnvironment);
client.AddOrSetSubscription(azureSubscription1);
Expand Down Expand Up @@ -384,9 +382,9 @@ public void SetAzureSubscriptionDerivesEnvironmentFromBothEndpointParameters()
public void SetAzureSubscriptionUpdatesSubscriptionWithCertificate()
{
// Setup
AzurePSCmdlet.CurrentProfile =
new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
ProfileClient client = new ProfileClient(AzurePSCmdlet.CurrentProfile);
var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
AzurePSCmdlet.CurrentProfile = profile;
ProfileClient client = new ProfileClient(profile);
client.AddOrSetAccount(azureAccount);
client.AddOrSetEnvironment(azureEnvironment);
client.AddOrSetSubscription(azureSubscription1);
Expand Down Expand Up @@ -517,6 +515,8 @@ public void ImportPublishSettingsWorksForCustomProfile()
// Setup
AzureSession.DataStore.WriteFile("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings",
Commands.Common.Test.Properties.Resources.ValidProfileChina);
var oldProfile = new AzureProfile();
AzurePSCmdlet.CurrentProfile = oldProfile;
var profile = new AzureProfile();
ProfileClient client = new ProfileClient(profile);
var oldDataStore = FileUtilities.DataStore;
Expand All @@ -543,8 +543,8 @@ public void ImportPublishSettingsWorksForCustomProfile()
Assert.Equal(client.GetSubscription(subscription.Id).Environment, expectedEnv);
}
Assert.Equal(1, commandRuntimeMock.OutputPipeline.Count);
Assert.Equal(AzurePSCmdlet.CurrentProfile.Subscriptions.Count, 0);
Assert.Equal(AzurePSCmdlet.CurrentProfile.Accounts.Count, 0);
Assert.Equal(oldProfile.Subscriptions.Count, 0);
Assert.Equal(oldProfile.Accounts.Count, 0);
}
finally
{
Expand Down Expand Up @@ -603,7 +603,7 @@ public void SelectAzureSubscriptionByNameUpdatesProfile()
[Fact]
public void SelectAzureSubscriptionByNameUpdatesCustomProfile()
{
SetupDefaultProfile();
var client = SetupDefaultProfile();
var profile = SetupCustomProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();

Expand All @@ -622,7 +622,7 @@ public void SelectAzureSubscriptionByNameUpdatesCustomProfile()
Assert.NotNull(cmdlt.Profile.Context.Subscription);
Assert.Equal(azureSubscription2.Id, cmdlt.Profile.Context.Subscription.Id);
// current profile unchanged
Assert.Equal(azureSubscription1.Id, AzurePSCmdlet.CurrentProfile.Context.Subscription.Id);
Assert.Equal(azureSubscription1.Id, client.Profile.Context.Subscription.Id);
}

[Fact]
Expand Down Expand Up @@ -943,12 +943,12 @@ public void CanCreateProfileFromHashWithTokenAuth()
[Fact]
public void CanCreateAzureProfileWithFile()
{
var savedProfile = AzurePSCmdlet.CurrentProfile;
var dataStore = AzureSession.DataStore;
try
{
AzureSession.DataStore = new MockDataStore();
AzurePSCmdlet.CurrentProfile = new AzureProfile();
var oldProfile = new AzureProfile();
AzurePSCmdlet.CurrentProfile = oldProfile;
string myFile = Path.GetTempFileName();
var profile = SetupCustomProfile(myFile);
var cmdlet = new NewAzureProfileCommand();
Expand Down Expand Up @@ -983,13 +983,12 @@ public void CanCreateAzureProfileWithFile()
}

// current profile does not change
Assert.Equal(AzurePSCmdlet.CurrentProfile.Accounts.Count, 0);
Assert.Equal(AzurePSCmdlet.CurrentProfile.Subscriptions.Count, 0);
Assert.Equal(oldProfile.Accounts.Count, 0);
Assert.Equal(oldProfile.Subscriptions.Count, 0);
}
finally
{
AzureSession.DataStore = dataStore;
AzurePSCmdlet.CurrentProfile = savedProfile;
}
}

Expand Down Expand Up @@ -1100,9 +1099,9 @@ private void ValidateCredential(PSCredential credential, AzureProfile profile,

private ProfileClient SetupDefaultProfile()
{
AzurePSCmdlet.CurrentProfile =
new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
ProfileClient client = new ProfileClient(AzurePSCmdlet.CurrentProfile);
var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
AzurePSCmdlet.CurrentProfile = profile;
ProfileClient client = new ProfileClient(profile);
client.AddOrSetEnvironment(azureEnvironment);
client.AddOrSetAccount(azureAccount);
client.AddOrSetSubscription(azureSubscription1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
using Microsoft.WindowsAzure.Commands.Profile;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Microsoft.Azure.Test;
using System;
Expand Down Expand Up @@ -99,7 +100,10 @@ public void RunPSTestWithToken(Func<AzureContext, string, string> testBuilder ,
() =>
{
savedAuthFactory = AzureSession.AuthenticationFactory;
var profile = AzurePSCmdlet.CurrentProfile;
var command = new GetAzureSubscriptionCommand();
command.CommandRuntime = new MockCommandRuntime();
command.InvokeBeginProcessing();
var profile = command.Profile;
var context = profile.Context;
var account = context.Account;
var tenant = account.IsPropertySet(AzureAccount.Property.Tenants)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void TestMakeArmCallWithCreatedProfile()
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestMakeRdfeCallWithCreatedProfile()
{
ProfileTestController.NewRdfeInstance.RunPSTestWithToken((context, token) => string.Format("Test-NewAzureProfileInARMMode {0} {1} {2}", token, context.Account.Id, context.Subscription.Id));
ProfileTestController.NewRdfeInstance.RunPSTestWithToken((context, token) => string.Format("Test-NewAzureProfileInRDFEMode {0} {1} {2}", token, context.Account.Id, context.Subscription.Id));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ function Test-SelectDefaultProfile
$profile = $(New-AzureProfile -SubscriptionId $sub -AccessToken $token -AccountId $user)
Assert-AreEqual "AzureCloud" $profile.Context.Environment.Name
Select-AzureProfile $profile
Select-AzureProfile -Default
$current = [Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet]::CurrentProfile
Assert-NotNull $($current.ProfilePath)
$profile2 = Select-AzureProfile -Default
Assert-NotNull $($profile2.ProfilePath)
}

<#
Expand Down