Skip to content

Add azure profile parameter #134

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

Closed
wants to merge 4 commits into from
Closed
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 @@ -58,7 +58,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.5-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common.Storage/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<package id="Hyak.Common" version="1.0.1" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.4-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.5-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.12.0-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.5-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
<HintPath>..\..\packages\Microsoft.Azure.Common.2.0.1\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
Expand Down
5 changes: 1 addition & 4 deletions src/Common/Commands.Common.Test/Common/ProfileCmdltsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public ProfileCmdltsTests() : base()
AzureSession.DataStore = dataStore;
commandRuntimeMock = new MockCommandRuntime();
SetMockData();
AzureSession.Profile = new AzureProfile();
}

[Fact]
Expand Down Expand Up @@ -89,7 +88,7 @@ public void ClearAzureProfileClearsCustomProfile()

cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.Force = new SwitchParameter(true);
cmdlt.SubscriptionDataFile = subscriptionDataFile;
cmdlt.Profile = new AzureProfile(subscriptionDataFile);

// Act
cmdlt.InvokeBeginProcessing();
Expand Down Expand Up @@ -507,7 +506,6 @@ public void SelectAzureSubscriptionByNameUpdatesProfile()
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
cmdlt.SubscriptionName = azureSubscription2.Name;
Assert.NotEqual(azureSubscription2.Id, AzureSession.Profile.CurrentContext.Subscription.Id);

// Act
cmdlt.InvokeBeginProcessing();
Expand Down Expand Up @@ -637,7 +635,6 @@ public void SelectAzureSubscriptionByInvalidGuidThrowsException()
cmdlt.SetParameterSet("SelectSubscriptionByIdParameterSet");
string invalidGuid = "foo";
cmdlt.SubscriptionId = invalidGuid;
Assert.Null(AzureSession.Profile.CurrentContext.Subscription);

// Act
cmdlt.InvokeBeginProcessing();
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common.Test/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<package id="Hyak.Common" version="1.0.1" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.4-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.5-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.12.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.0.5486.28526-prerelease" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
Expand Down
107 changes: 60 additions & 47 deletions src/Common/Commands.Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ namespace Microsoft.WindowsAzure.Commands.Utilities.Common
{
public abstract class AzurePSCmdlet : PSCmdlet
{
private readonly RecordingTracingInterceptor httpTracingInterceptor = new RecordingTracingInterceptor();
private readonly RecordingTracingInterceptor _httpTracingInterceptor = new RecordingTracingInterceptor();

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

static AzurePSCmdlet()
{
Expand All @@ -37,25 +40,73 @@ static AzurePSCmdlet()
AzureSession.ClientFactory.UserAgents.Add(AzurePowerShell.UserAgentValue);
}

public AzurePSCmdlet()
/// <summary>
/// Cmdlet begin process. Write to logs, setup Http Tracing and initialize profile
/// </summary>
protected override void BeginProcessing()
{
InitializeProfile();

if (string.IsNullOrEmpty(ParameterSetName))
{
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithoutParameterSetLog, this.GetType().Name));
}
else
{
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithParameterSetLog, this.GetType().Name, ParameterSetName));
}

if (CurrentContext != null && CurrentContext.Account != null && CurrentContext.Account.Id != null)
{
WriteDebugWithTimestamp(string.Format("using account id '{0}'...", CurrentContext.Account.Id));
}

RecordingTracingInterceptor.AddToContext(_httpTracingInterceptor);

base.BeginProcessing();
}

private void InitializeProfile()
{
AzureSession.Profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
// Load profile from disk
var profileFromDisk = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
if (AzureSession.Profile == null ||
AzureSession.Profile.ProfilePath == profileFromDisk.ProfilePath)
{
AzureSession.Profile = profileFromDisk;
}

DefaultProfileClient = new ProfileClient(AzureSession.Profile);
// If profile parameter is not specified, use one from session
if (Profile == null)
{
Profile = AzureSession.Profile;
}
}

/// <summary>
/// End processing. Flush messages in tracing interceptor and save profile.
/// </summary>
protected override void EndProcessing()
{
string message = string.Format(Resources.EndProcessingLog, this.GetType().Name);
WriteDebugWithTimestamp(message);

RecordingTracingInterceptor.RemoveFromContext(_httpTracingInterceptor);
FlushMessagesFromTracingInterceptor();

base.EndProcessing();
}

public AzureContext CurrentContext
{
get { return AzureSession.Profile.CurrentContext; }
get { return Profile.CurrentContext; }
}

public bool HasCurrentSubscription
{
get { return AzureSession.Profile.CurrentContext.Subscription != null; }
get { return Profile.CurrentContext.Subscription != null; }
}

public ProfileClient DefaultProfileClient { get; private set; }

protected string CurrentPath()
{
// SessionState is only available within Powershell so default to
Expand Down Expand Up @@ -192,53 +243,15 @@ protected override void ProcessRecord()
}
}

/// <summary>
/// Cmdlet begin process
/// </summary>
protected override void BeginProcessing()
{
if (string.IsNullOrEmpty(ParameterSetName))
{
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithoutParameterSetLog, this.GetType().Name));
}
else
{
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithParameterSetLog, this.GetType().Name, ParameterSetName));
}

if (CurrentContext != null && CurrentContext.Account != null && CurrentContext.Account.Id != null)
{
WriteDebugWithTimestamp(string.Format("using account id '{0}'...", CurrentContext.Account.Id));
}

RecordingTracingInterceptor.AddToContext(httpTracingInterceptor);

base.BeginProcessing();
}

private void FlushMessagesFromTracingInterceptor()
{
string message;
while (httpTracingInterceptor.MessageQueue.TryDequeue(out message))
while (_httpTracingInterceptor.MessageQueue.TryDequeue(out message))
{
base.WriteDebug(message);
}
}

/// <summary>
/// End processing
/// </summary>
protected override void EndProcessing()
{
string message = string.Format(Resources.EndProcessingLog, this.GetType().Name);
WriteDebugWithTimestamp(message);

RecordingTracingInterceptor.RemoveFromContext(httpTracingInterceptor);
FlushMessagesFromTracingInterceptor();

base.EndProcessing();
}

/// <summary>
/// Asks for confirmation before executing the action.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/CloudBaseCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected virtual T CreateChannel()
}

string certificateThumbprint = CurrentContext.Account.Id;
Debug.Assert(DefaultProfileClient.Profile.Accounts[certificateThumbprint].Type == AzureAccount.AccountType.Certificate);
Debug.Assert(Profile.Accounts[certificateThumbprint].Type == AzureAccount.AccountType.Certificate);

return ChannelHelper.CreateServiceManagementChannel<T>(
ServiceBinding,
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 @@ -60,7 +60,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.5-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
30 changes: 10 additions & 20 deletions src/Common/Commands.Common/SubscriptionCmdletBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.WindowsAzure.Commands.Common.Properties;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using System.Management.Automation;

namespace Microsoft.WindowsAzure.Commands.Utilities.Profile
{
Expand All @@ -27,39 +24,32 @@ namespace Microsoft.WindowsAzure.Commands.Utilities.Profile
/// </summary>
public abstract class SubscriptionCmdletBase : AzurePSCmdlet
{
[Parameter(Mandatory = false, HelpMessage = "[Deprecated]: File storing subscription data, if not set uses default.")]
public string SubscriptionDataFile { get; set; }

private readonly bool _saveProfile;

protected SubscriptionCmdletBase(bool saveProfile)
protected SubscriptionCmdletBase(bool saveProfile)
{
this._saveProfile = saveProfile;
_saveProfile = saveProfile;
}

protected override void BeginProcessing()
{
if (!string.IsNullOrEmpty(SubscriptionDataFile))
{
ProfileClient = new ProfileClient(new AzureProfile(SubscriptionDataFile));
WriteWarning(Resources.SubscriptionDataFileDeprecated);
}
else
{
ProfileClient = new ProfileClient(AzureSession.Profile);
}
base.BeginProcessing();

ProfileClient = new ProfileClient(Profile);
ProfileClient.WarningLog = WriteWarning;
ProfileClient.DebugLog = WriteDebug;
}

protected override void EndProcessing()
{

if (_saveProfile)
{
ProfileClient.Profile.Save();
Profile.Save();
}
}

base.EndProcessing();
}
public ProfileClient ProfileClient { get; set; }

}
}
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.4-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.5-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.12.0-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
Expand Down
1 change: 0 additions & 1 deletion src/Common/Commands.Profile/Account/AddAzureAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Microsoft.WindowsAzure.Commands.Common;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.WindowsAzure.Commands.Common.Properties;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Microsoft.WindowsAzure.Commands.Utilities.Profile;

namespace Microsoft.WindowsAzure.Commands.Profile
Expand Down
3 changes: 2 additions & 1 deletion src/Common/Commands.Profile/Account/GetAzureAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Linq;
using System.Management.Automation;
using Microsoft.WindowsAzure.Commands.Common;
using Microsoft.Azure.Common.Authentication.Models;
Expand All @@ -37,7 +38,7 @@ public GetAzureAccount() : base(false)

public override void ExecuteCmdlet()
{
IEnumerable<AzureAccount> accounts = DefaultProfileClient.ListAccounts(Name);
IEnumerable<AzureAccount> accounts = Profile.Accounts.Values.Where(a => Name == null || a.Id == Name);
List<PSAzureAccount> output = new List<PSAzureAccount>();
foreach (AzureAccount account in accounts) {
output.Add(account.ToPSAzureAccount());
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Profile/Commands.Profile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.5-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
<HintPath>..\..\packages\Microsoft.Azure.Common.2.0.1\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
Expand Down
10 changes: 5 additions & 5 deletions src/Common/Commands.Profile/Subscription/GetAzureSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public override void ExecuteCmdlet()
switch (ParameterSetName)
{
case "ByName":
WriteSubscriptions(ProfileClient.RefreshSubscriptions(AzureSession.Profile.CurrentContext.Environment)
WriteSubscriptions(ProfileClient.RefreshSubscriptions(Profile.CurrentContext.Environment)
.Where(s => SubscriptionName == null || s.Name.Equals(SubscriptionName, StringComparison.InvariantCultureIgnoreCase)));
break;
case "ById":
Expand Down Expand Up @@ -103,7 +103,7 @@ public void GetCurrent()
// since current is strictly in-memory and we want the real
// current subscription.
//
if (AzureSession.Profile.CurrentContext.Subscription == null)
if (Profile.CurrentContext.Subscription == null)
{
WriteError(new ErrorRecord(
new InvalidOperationException(Resources.InvalidSelectedSubscription),
Expand All @@ -112,7 +112,7 @@ public void GetCurrent()
}
else
{
WriteSubscriptions(AzureSession.Profile.CurrentContext.Subscription);
WriteSubscriptions(Profile.CurrentContext.Subscription);
}
}

Expand Down Expand Up @@ -148,7 +148,7 @@ private PSAzureSubscription ConstructPsAzureSubscription(AzureSubscription subsc
psObject.DefaultAccount = subscription.Account;
psObject.Accounts = ProfileClient.Profile.Accounts.Values.Where(a => a.HasSubscription(subscription.Id)).ToArray();
psObject.IsDefault = subscription.IsPropertySet(AzureSubscription.Property.Default);
psObject.IsCurrent = AzureSession.Profile.CurrentContext.Subscription != null && AzureSession.Profile.CurrentContext.Subscription.Id == subscription.Id;
psObject.IsCurrent = Profile.CurrentContext.Subscription != null && Profile.CurrentContext.Subscription.Id == subscription.Id;
psObject.CurrentStorageAccountName = subscription.GetProperty(AzureSubscription.Property.StorageAccount);
psObject.TenantId = subscription.GetPropertyAsArray(AzureSubscription.Property.Tenants).FirstOrDefault();
return psObject;
Expand All @@ -160,7 +160,7 @@ private PSAzureSubscriptionExtended ConstructPsAzureSubscriptionExtended(AzureSu
{
var response = client.Subscriptions.Get();
var environment = ProfileClient.GetEnvironmentOrDefault(subscription.Environment);
var account = DefaultProfileClient.Profile.Accounts[subscription.Account];
var account = ProfileClient.Profile.Accounts[subscription.Account];
bool isCert = account.Type == AzureAccount.AccountType.Certificate;

PSAzureSubscriptionExtended result = new PSAzureSubscriptionExtended(ConstructPsAzureSubscription(subscription))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override void ExecuteCmdlet()
}

AzureSubscription defaultSubscription = ProfileClient.Profile.DefaultSubscription;
Debug.Assert(AzureSession.Profile.CurrentContext != null);
Debug.Assert(Profile.CurrentContext != null);
}

private bool IsDirectory()
Expand Down
Loading