Skip to content
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
89 changes: 78 additions & 11 deletions src/Common/Commands.Common.Test/Common/ProfileClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void ProfileMigratesAccountsAndDefaultSubscriptions()
Assert.Equal("https://api-dogfood.resources.windows-int.net", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.ResourceManager]);
Assert.Equal("https://management-preview.core.windows-int.net/", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.ServiceManagement]);
Assert.Equal(".database.windows.net", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix]);

// Verify subscriptions
Assert.Equal(3, client.Profile.Subscriptions.Count);
Assert.False(client.Profile.Subscriptions.ContainsKey(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E")));
Expand Down Expand Up @@ -157,7 +157,7 @@ public void ProfileMigratesAccountsSkipsBadOnesAndBacksUpFile()

// Verify Environment migration
Assert.Equal(2, client.Profile.Environments.Count);

// Verify subscriptions
Assert.Equal(3, client.Profile.Subscriptions.Count);
Assert.True(client.Profile.Subscriptions.ContainsKey(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")));
Expand All @@ -168,7 +168,7 @@ public void ProfileMigratesAccountsSkipsBadOnesAndBacksUpFile()

Assert.True(client.Profile.Subscriptions.ContainsKey(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")));
Assert.Equal("Test Bad Cert", client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].Name);

// Verify accounts
Assert.Equal(2, client.Profile.Accounts.Count);
Assert.Equal("[email protected]", client.Profile.Accounts["[email protected]"].Id);
Expand Down Expand Up @@ -204,10 +204,10 @@ public void ProfileMigratesCorruptedFileAndCreatedBackup()

// Verify Environment migration
Assert.Equal(2, client.Profile.Environments.Count);

// Verify subscriptions
Assert.Equal(0, client.Profile.Subscriptions.Count);

// Verify accounts
Assert.Equal(0, client.Profile.Accounts.Count);

Expand Down Expand Up @@ -258,7 +258,7 @@ public void AddAzureAccountReturnsAccountWithAllSubscriptionsInRdfeMode()
ProfileClient.DataStore = dataStore;
ProfileClient client = new ProfileClient();

var account = client.AddAccountAndLoadSubscriptions(new AzureAccount { Id = "test", Type = AzureAccount.AccountType.User }, AzureEnvironment.PublicEnvironments[ EnvironmentName.AzureCloud], null);
var account = client.AddAccountAndLoadSubscriptions(new AzureAccount { Id = "test", Type = AzureAccount.AccountType.User }, AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud], null);

Assert.Equal("test", account.Id);
Assert.Equal(3, account.GetSubscriptions(client.Profile).Count);
Expand Down Expand Up @@ -603,7 +603,7 @@ public void SetAzureEnvironmentUpdatesEnvironment()
Assert.Equal(2, client.Profile.Environments.Count);

Assert.Throws<ArgumentNullException>(() => client.AddOrSetEnvironment(null));

var env2 = client.AddOrSetEnvironment(azureEnvironment);
Assert.Equal(env2.Name, azureEnvironment.Name);
Assert.NotNull(env2.Endpoints[AzureEnvironment.Endpoint.ServiceManagement]);
Expand Down Expand Up @@ -672,7 +672,7 @@ public void AddOrSetAzureSubscriptionChecksAndUpdates()
Assert.Equal(subscription, azureSubscription1);
Assert.Throws<ArgumentNullException>(() => client.AddOrSetSubscription(null));
Assert.Throws<ArgumentNullException>(() => client.AddOrSetSubscription(
new AzureSubscription { Id = new Guid(), Environment = null, Name = "foo"}));
new AzureSubscription { Id = new Guid(), Environment = null, Name = "foo" }));
}

[Fact]
Expand Down Expand Up @@ -792,7 +792,7 @@ public void RefreshSubscriptionsWorksWithMooncake()
MockDataStore dataStore = new MockDataStore();
ProfileClient.DataStore = dataStore;
ProfileClient client = new ProfileClient();

client.Profile.Accounts[azureAccount.Id] = azureAccount;

var subscriptions = client.RefreshSubscriptions(client.Profile.Environments[EnvironmentName.AzureChinaCloud]);
Expand Down Expand Up @@ -878,7 +878,7 @@ public void SetAzureSubscriptionAsDefaultSetsDefaultAndCurrent()
Assert.Equal(azureSubscription2.Id, client.Profile.DefaultSubscription.Id);
Assert.Equal(azureSubscription2.Id, AzureSession.CurrentContext.Subscription.Id);
Assert.Throws<ArgumentException>(() => client.SetSubscriptionAsDefault("bad", null));
Assert.Throws<ArgumentNullException>(() => client.SetSubscriptionAsDefault(null, null));
Assert.Throws<ArgumentException>(() => client.SetSubscriptionAsDefault(null, null));
}

[Fact]
Expand Down Expand Up @@ -917,7 +917,7 @@ public void SetAzureSubscriptionAsCurrentSetsCurrent()

Assert.Equal(azureSubscription2.Id, AzureSession.CurrentContext.Subscription.Id);
Assert.Throws<ArgumentException>(() => client.SetSubscriptionAsCurrent("bad", null));
Assert.Throws<ArgumentNullException>(() => client.SetSubscriptionAsCurrent(null, null));
Assert.Throws<ArgumentException>(() => client.SetSubscriptionAsCurrent(null, null));
}

[Fact]
Expand Down Expand Up @@ -1076,6 +1076,73 @@ public void ImportPublishSettingsUsesPassedInEnvironment()
Assert.Equal(7, client.Profile.Subscriptions.Count);
}

[Fact]
public void SelectAzureSubscriptionByIdWorks()
{
MockDataStore dataStore = new MockDataStore();
ProfileClient.DataStore = dataStore;
ProfileClient client = new ProfileClient();

var tempSubscriptions = new List<AzureSubscription>
{
new AzureSubscription
{
Id = new Guid("11111111-1383-4740-8A69-748C5B63ADBA"),
Name = "Same Name Subscription",
Environment = azureEnvironment.Name,
Account = azureAccount.Id,
Properties = new Dictionary<AzureSubscription.Property, string>
{
{ AzureSubscription.Property.Default, "True" }
}
},

new AzureSubscription
{
Id = new Guid("22222222-1383-4740-8A69-748C5B63ADBA"),
Name = "Same Name Subscription",
Environment = azureEnvironment.Name,
Account = azureAccount.Id,
Properties = new Dictionary<AzureSubscription.Property, string>()
},

new AzureSubscription
{
Id = new Guid("33333333-1383-4740-8A69-748C5B63ADBA"),
Name = "Same Name Subscription",
Environment = azureEnvironment.Name,
Account = azureAccount.Id,
Properties = new Dictionary<AzureSubscription.Property, string>()
}
};

client.Profile.Accounts[azureAccount.Id] = azureAccount;
client.AddOrSetEnvironment(azureEnvironment);

foreach (var s in tempSubscriptions)
{
client.AddOrSetSubscription(s);
}

client.SetSubscriptionAsCurrent(tempSubscriptions[0].Name, tempSubscriptions[0].Account);
client.Profile.Save();

Assert.Equal(tempSubscriptions[0].Id, AzureSession.CurrentContext.Subscription.Id);

var cmdlt = new SelectAzureSubscriptionCommand();

cmdlt.CommandRuntime = new MockCommandRuntime();
cmdlt.SubscriptionId = tempSubscriptions[2].Id.ToString();
cmdlt.SetParameterSet("SelectSubscriptionByIdParameterSet");

// Act
cmdlt.InvokeBeginProcessing();
cmdlt.ExecuteCmdlet();
cmdlt.InvokeEndProcessing();

Assert.Equal(tempSubscriptions[2].Id, AzureSession.CurrentContext.Subscription.Id);
}

[Fact]
public void ImportPublishSettingsAddsSecondCertificate()
{
Expand Down
32 changes: 16 additions & 16 deletions src/Common/Commands.Common/Common/ProfileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ private AzureAccount GetSubscriptionAccount(Guid subscriptionId)

#endregion

#region Subscripton management
#region Subscription management

public AzureSubscription AddOrSetSubscription(AzureSubscription subscription)
{
Expand Down Expand Up @@ -521,31 +521,31 @@ public AzureSubscription GetSubscription(string name)
}
}

public AzureSubscription SetSubscriptionAsCurrent(Guid id, string accountName)
public AzureSubscription SetSubscriptionAsCurrent(string name, string accountName)
{
var subscription = Profile.Subscriptions.Values.FirstOrDefault(s => s.Id == id);
var subscription = Profile.Subscriptions.Values.FirstOrDefault(s => s.Name == name);

if (subscription == null)
{
throw new ArgumentException(string.Format(Resources.InvalidSubscriptionId, id), "id");
throw new ArgumentException(string.Format(Resources.InvalidSubscriptionName, name), "name");
}

return SetSubscriptionAsCurrent(subscription.Name, accountName);
return SetSubscriptionAsCurrent(subscription.Id, accountName);
}

public AzureSubscription SetSubscriptionAsCurrent(string name, string accountName)
public AzureSubscription SetSubscriptionAsCurrent(Guid id, string accountName)
{
if (string.IsNullOrEmpty(name))
if (Guid.Empty == id)
{
throw new ArgumentNullException("name", string.Format(Resources.InvalidSubscription, name));
throw new ArgumentNullException("id", string.Format(Resources.InvalidSubscriptionId, id));
}

AzureSubscription currentSubscription = null;
var subscription = Profile.Subscriptions.Values.FirstOrDefault(s => s.Name == name);
var subscription = Profile.Subscriptions.Values.FirstOrDefault(s => s.Id == id);

if (subscription == null)
{
throw new ArgumentException(string.Format(Resources.InvalidSubscription, name), "name");
throw new ArgumentException(string.Format(Resources.InvalidSubscriptionId, id), "id");
}
else
{
Expand All @@ -561,21 +561,21 @@ public AzureSubscription SetSubscriptionAsCurrent(string name, string accountNam
return currentSubscription;
}

public AzureSubscription SetSubscriptionAsDefault(Guid id, string accountName)
public AzureSubscription SetSubscriptionAsDefault(string name, string accountName)
{
var subscription = Profile.Subscriptions.Values.FirstOrDefault(s => s.Id == id);
var subscription = Profile.Subscriptions.Values.FirstOrDefault(s => s.Name == name);

if (subscription == null)
{
throw new ArgumentException(string.Format(Resources.InvalidSubscriptionId, id), "id");
throw new ArgumentException(string.Format(Resources.InvalidSubscriptionName, name), "name");
}

return SetSubscriptionAsDefault(subscription.Name, accountName);
return SetSubscriptionAsDefault(subscription.Id, accountName);
}

public AzureSubscription SetSubscriptionAsDefault(string name, string accountName)
public AzureSubscription SetSubscriptionAsDefault(Guid id, string accountName)
{
AzureSubscription subscription = SetSubscriptionAsCurrent(name, accountName);
AzureSubscription subscription = SetSubscriptionAsCurrent(id, accountName);

if (subscription != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static class PowerShellExtensions
public static string CredentialImportFormat = "Import-AzurePublishSettingsFile '{0}'";

/// <summary>
/// Gets a powershell varibale from the current session and convernts it back to it's original type.
/// Gets a powershell variable from the current session and converts it back to it's original type.
/// </summary>
/// <typeparam name="T">The powershell object original type</typeparam>
/// <param name="powershell">The PowerShell instance</param>
Expand Down