Skip to content

Commit b2a02e7

Browse files
committed
Merge pull request #170 from Azure/dev
.
2 parents e04d6df + 7e29780 commit b2a02e7

File tree

15 files changed

+363
-106
lines changed

15 files changed

+363
-106
lines changed

src/ResourceManager/Profile/Commands.Profile.Test/ContextCmdletTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System;
2525
using Microsoft.Azure.Commands.Profile.Models;
2626
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
27+
using System.Management.Automation;
2728

2829
namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
2930
{
@@ -58,7 +59,7 @@ public void GetAzureContext()
5859
var context = (PSAzureContext) commandRuntimeMock.OutputPipeline[0];
5960
Assert.Equal("test", context.Subscription.SubscriptionName);
6061
}
61-
62+
6263
[Fact]
6364
[Trait(Category.AcceptanceType, Category.CheckIn)]
6465
public void SelectAzureContextWithNoSubscriptionAndTenant()
@@ -76,7 +77,8 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
7677
// Verify
7778
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
7879
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
79-
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.TenantId);
80+
// TenantId is not sufficient to change the context.
81+
Assert.NotEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.TenantId);
8082
}
8183

8284
[Fact]

src/ResourceManager/Profile/Commands.Profile.Test/LoginCmdletTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void LoginWithSubscriptionAndTenant()
5050
// Setup
5151
cmdlt.CommandRuntime = commandRuntimeMock;
5252
cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
53-
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
53+
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
5454

5555
// Act
5656
cmdlt.InvokeBeginProcessing();
@@ -69,7 +69,7 @@ public void LoginWithInvalidSubscriptionAndTenantThrowsCloudException()
6969
// Setup
7070
cmdlt.CommandRuntime = commandRuntimeMock;
7171
cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a5";
72-
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
72+
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
7373

7474
// Act
7575
cmdlt.InvokeBeginProcessing();
@@ -119,7 +119,7 @@ public void LoginWithNoSubscriptionAndTenant()
119119
var cmdlt = new AddAzureRMAccountCommand();
120120
// Setup
121121
cmdlt.CommandRuntime = commandRuntimeMock;
122-
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
122+
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
123123

124124
// Act
125125
cmdlt.InvokeBeginProcessing();
@@ -137,7 +137,7 @@ public void LoginWithSubscriptionname()
137137
var cmdlt = new AddAzureRMAccountCommand();
138138
// Setup
139139
cmdlt.CommandRuntime = commandRuntimeMock;
140-
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
140+
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
141141
cmdlt.SubscriptionName = "Node CLI Test";
142142

143143
// Act
@@ -156,7 +156,7 @@ public void LoginWithBothSubscriptionIdAndNameThrowsCloudException()
156156
var cmdlt = new AddAzureRMAccountCommand();
157157
// Setup
158158
cmdlt.CommandRuntime = commandRuntimeMock;
159-
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
159+
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
160160
cmdlt.SubscriptionName = "Node CLI Test";
161161
cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
162162

src/ResourceManager/Profile/Commands.Profile.Test/SessionRecords/Microsoft.Azure.Commands.Profile.Test.SubscriptionCmdletTests/PipingWithRmContextWorks.json

Lines changed: 224 additions & 32 deletions
Large diffs are not rendered by default.

src/ResourceManager/Profile/Commands.Profile.Test/SubscriptionCmdletTests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function Test-PipingWithContext
5252
$name = $firstSubscription.SubscriptionName
5353
$nameContext = Get-AzureRmSubscription -SubscriptionName $name | Set-AzureRmContext
5454
$idContext = Get-AzureRmSubscription -SubscriptionId $id | Set-AzureRmContext
55+
$contextByName = Set-AzureRmContext -SubscriptionName $name
5556
Assert-True { $nameContext -ne $null }
5657
Assert-True { $nameContext.Subscription -ne $null }
5758
Assert-True { $nameContext.Subscription.SubscriptionId -ne $null }
@@ -62,6 +63,11 @@ function Test-PipingWithContext
6263
Assert-True { $idContext.Subscription.SubscriptionName -ne $null }
6364
Assert-AreEqual $idContext.Subscription.SubscriptionId $nameContext.Subscription.SubscriptionId
6465
Assert-AreEqual $idContext.Subscription.SubscriptionName $nameContext.Subscription.SubscriptionName
66+
Assert-True { $contextByName -ne $null }
67+
Assert-True { $contextByName.Subscription -ne $null }
68+
Assert-True { $contextByName.Subscription.SubscriptionId -ne $null }
69+
Assert-True { $contextByName.Subscription.SubscriptionName -ne $null }
70+
Assert-AreEqual $contextByName.Subscription.SubscriptionName $nameContext.Subscription.SubscriptionName
6571
}
6672

6773
function Test-SetAzureRmContextEndToEnd

src/ResourceManager/Profile/Commands.Profile.Test/TenantCmdletTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void GetTenantWithTenantParameter()
4949
var cmdlt = new GetAzureRMTenantCommand();
5050
// Setup
5151
cmdlt.CommandRuntime = commandRuntimeMock;
52-
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
52+
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
5353

5454
// Act
5555
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
@@ -69,7 +69,7 @@ public void GetTenantWithDomainParameter()
6969
var cmdlt = new GetAzureRMTenantCommand();
7070
// Setup
7171
cmdlt.CommandRuntime = commandRuntimeMock;
72-
cmdlt.Tenant = "microsoft.com";
72+
cmdlt.TenantId = "microsoft.com";
7373

7474
// Act
7575
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
@@ -107,7 +107,7 @@ private void Login(string subscriptionId, string tenantId)
107107
// Setup
108108
cmdlt.CommandRuntime = commandRuntimeMock;
109109
cmdlt.SubscriptionId = subscriptionId;
110-
cmdlt.Tenant = tenantId;
110+
cmdlt.TenantId = tenantId;
111111

112112
// Act
113113
cmdlt.InvokeBeginProcessing();

src/ResourceManager/Profile/Commands.Profile/Account/AddAzureRmAccount.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ public class AddAzureRMAccountCommand : AzureRMCmdlet , IModuleAssemblyInitializ
4848
[Parameter(ParameterSetName = "User", Mandatory = false, HelpMessage = "Optional tenant name or ID")]
4949
[Parameter(ParameterSetName = "ServicePrincipal", Mandatory = true, HelpMessage = "TenantId name or ID")]
5050
[Parameter(ParameterSetName = "AccessToken", Mandatory = false, HelpMessage = "TenantId name or ID")]
51+
[Alias("Tenant")]
5152
[ValidateNotNullOrEmpty]
52-
public string Tenant { get; set; }
53+
public string TenantId { get; set; }
5354

5455
[Parameter(ParameterSetName = "AccessToken", Mandatory = true, HelpMessage = "AccessToken")]
5556
[ValidateNotNullOrEmpty]
@@ -126,9 +127,9 @@ protected override void ProcessRecord()
126127
password = Credential.Password;
127128
}
128129

129-
if (!string.IsNullOrEmpty(Tenant))
130+
if (!string.IsNullOrEmpty(TenantId))
130131
{
131-
azureAccount.SetProperty(AzureAccount.Property.Tenants, new[] { Tenant });
132+
azureAccount.SetProperty(AzureAccount.Property.Tenants, new[] { TenantId });
132133
}
133134

134135
if( AzureRmProfileProvider.Instance.Profile == null)
@@ -138,7 +139,7 @@ protected override void ProcessRecord()
138139

139140
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);
140141

141-
WriteObject((PSAzureProfile)profileClient.Login(azureAccount, Environment, Tenant, SubscriptionId,
142+
WriteObject((PSAzureProfile)profileClient.Login(azureAccount, Environment, TenantId, SubscriptionId,
142143
SubscriptionName, password));
143144
}
144145

src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,36 @@
1717
using Microsoft.Azure.Commands.Profile.Models;
1818
using Microsoft.Azure.Commands.ResourceManager.Common;
1919
using Microsoft.WindowsAzure.Commands.Common;
20+
using Microsoft.Azure.Commands.Profile.Properties;
2021

2122
namespace Microsoft.Azure.Commands.Profile
2223
{
2324
/// <summary>
2425
/// Cmdlet to change current Azure context.
2526
/// </summary>
26-
[Cmdlet(VerbsCommon.Set, "AzureRmContext", DefaultParameterSetName =TenantIdAndSubscriptionIdParameterSet)]
27+
[Cmdlet(VerbsCommon.Set, "AzureRmContext", DefaultParameterSetName = SubscriptionNameParameterSet)]
2728
[Alias("Select-AzureRmSubscription")]
2829
[OutputType(typeof(PSAzureContext))]
2930
public class SetAzureRMContextCommand : AzureRMCmdlet
3031
{
31-
private const string TenantIdParameterSet = "TenantId";
32-
private const string SubscriptionIdParameterSet = "Subscription";
33-
private const string TenantIdAndSubscriptionIdParameterSet = "TenantIdAndSubscriptionId";
32+
private const string SubscriptionNameParameterSet = "SubscriptionName";
33+
private const string SubscriptionIdParameterSet = "SubscriptionId";
3434
private const string ContextParameterSet = "Context";
3535

36-
[Parameter(ParameterSetName = TenantIdParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName=true)]
37-
[Parameter(ParameterSetName = TenantIdAndSubscriptionIdParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName=true)]
36+
[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName = true)]
37+
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName = true)]
3838
[ValidateNotNullOrEmpty]
3939
public string TenantId { get; set; }
40-
41-
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = true, HelpMessage = "Subscription", ValueFromPipelineByPropertyName=true)]
42-
[Parameter(ParameterSetName = TenantIdAndSubscriptionIdParameterSet, Mandatory = true, HelpMessage = "Subscription", ValueFromPipelineByPropertyName=true)]
40+
41+
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = true, HelpMessage = "Subscription", ValueFromPipelineByPropertyName = true)]
4342
[ValidateNotNullOrEmpty]
4443
public string SubscriptionId { get; set; }
4544

46-
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName=true)]
47-
[Parameter(ParameterSetName = TenantIdAndSubscriptionIdParameterSet, Mandatory = false, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName=true)]
45+
[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = true, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName = true)]
4846
[ValidateNotNullOrEmpty]
49-
public string SubscriptionName{ get; set; }
50-
51-
[Parameter(ParameterSetName = ContextParameterSet, Mandatory = true, HelpMessage = "Context", ValueFromPipeline=true)]
47+
public string SubscriptionName { get; set; }
48+
49+
[Parameter(ParameterSetName = ContextParameterSet, Mandatory = true, HelpMessage = "Context", ValueFromPipeline = true)]
5250
public PSAzureContext Context { get; set; }
5351

5452
protected override void ProcessRecord()
@@ -58,14 +56,40 @@ protected override void ProcessRecord()
5856
AzureRmProfileProvider.Instance.Profile.SetContextWithCache(new AzureContext(Context.Subscription, Context.Account,
5957
Context.Environment, Context.Tenant));
6058
}
61-
else
59+
else if (ParameterSetName == SubscriptionNameParameterSet)
6260
{
6361
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);
64-
profileClient.SetCurrentContext(SubscriptionId, TenantId);
65-
if (!string.IsNullOrWhiteSpace(SubscriptionName))
62+
AzureSubscription subscription = null;
63+
string tenantId = AzureRmProfileProvider.Instance.Profile.Context.Tenant.Id.ToString();
64+
65+
if (string.IsNullOrWhiteSpace(TenantId))
66+
{
67+
WriteVerbose(
68+
string.Format(
69+
Resources.CurrentTenantInUse,
70+
tenantId));
71+
}
72+
else
6673
{
67-
AzureRmProfileProvider.Instance.Profile.Context.Subscription.Name = SubscriptionName;
74+
tenantId = TenantId;
6875
}
76+
77+
if (!profileClient.TryGetSubscriptionByName(
78+
tenantId,
79+
SubscriptionName,
80+
out subscription))
81+
{
82+
throw new ItemNotFoundException(
83+
string.Format(Resources.SubscriptionNameNotFoundError, SubscriptionName));
84+
}
85+
86+
profileClient.SetCurrentContext(subscription.Id.ToString(), tenantId, verifySubscription: false);
87+
AzureRmProfileProvider.Instance.Profile.Context.Subscription.Name = SubscriptionName;
88+
}
89+
else if (ParameterSetName == SubscriptionIdParameterSet)
90+
{
91+
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);
92+
profileClient.SetCurrentContext(SubscriptionId, TenantId);
6993
}
7094
WriteObject((PSAzureContext)AzureRmProfileProvider.Instance.Profile.Context);
7195
}

0 commit comments

Comments
 (0)