Skip to content

Commit 19db6a3

Browse files
committed
Merge pull request Azure#996 from jianghaolu/dev
[fixes #103913308] Add SubscriptionName to Login-AzureRMAccount
2 parents 898f889 + a227052 commit 19db6a3

File tree

8 files changed

+133
-15
lines changed

8 files changed

+133
-15
lines changed

src/ResourceManager/Automation/Commands.Automation.Test/packages.config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<package id="Microsoft.WindowsAzure.Management" version="4.1.1" targetFramework="net45" />
1717
<package id="Moq" version="4.2.1402.2112" targetFramework="net45" />
1818
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
19-
<package id="xunit.runner.visualstudio" version="2.1.0-beta4-build1109" targetFramework="net45" />
2019
<package id="xunit" version="1.9.2" targetFramework="net45" />
2120
<package id="xunit.extensions" version="1.9.2" targetFramework="net45" />
2221
<package id="xunit.runner.visualstudio" version="2.1.0-beta4-build1109" targetFramework="net45" />

src/ResourceManager/Common/Commands.ResourceManager.Common/Properties/Resources.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Common/Commands.ResourceManager.Common/Properties/Resources.resx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,13 @@ Select Y to enable data collection [Y/N]:</value>
151151
<data name="NoSubscriptionFound" xml:space="preserve">
152152
<value>The provided account {0} does not have access to any subscriptions. Please try logging in with different credentials.</value>
153153
</data>
154+
<data name="SubscriptionIdNotFound" xml:space="preserve">
155+
<value>The provided account {0} does not have access to subscription ID "{1}". Please try logging in with different credentials not a different subscription ID.</value>
156+
</data>
157+
<data name="SubscriptionNameNotFound" xml:space="preserve">
158+
<value>The provided account {0} does not have access to subscription name "{1}". Please try logging in with different credentials not a different subscription name.</value>
159+
</data>
154160
<data name="TenantNotFound" xml:space="preserve">
155161
<value>Tenant '{0}' was not found. Please verify that your account has access to this tenant.</value>
156162
</data>
157-
</root>
163+
</root>

src/ResourceManager/Common/Commands.ResourceManager.Common/RMProfileClient.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public RMProfileClient(AzureRMProfile profile)
4444
}
4545
}
4646

47-
public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment, string tenantId, string subscriptionId, SecureString password)
47+
public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment, string tenantId, string subscriptionId, string subscriptionName, SecureString password)
4848
{
4949
AzureSubscription newSubscription = null;
5050
AzureTenant newTenant = null;
@@ -55,7 +55,7 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
5555
if (!string.IsNullOrEmpty(tenantId))
5656
{
5757
var token = AcquireAccessToken(account, environment, tenantId, password, promptBehavior);
58-
TryGetTenantSubscription(token, account, environment, tenantId, subscriptionId, out newSubscription, out newTenant);
58+
TryGetTenantSubscription(token, account, environment, tenantId, subscriptionId, subscriptionName, out newSubscription, out newTenant);
5959
}
6060
// (tenant is not provided and subscription is present) OR
6161
// (tenant is not provided and subscription is not provided)
@@ -67,7 +67,7 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
6767
AzureSubscription tempSubscription;
6868
var token = AcquireAccessToken(account, environment, tenant.Id.ToString(), password,
6969
ShowDialog.Auto);
70-
if (newTenant == null && TryGetTenantSubscription(token, account, environment, tenant.Id.ToString(), subscriptionId, out tempSubscription, out tempTenant) &&
70+
if (newTenant == null && TryGetTenantSubscription(token, account, environment, tenant.Id.ToString(), subscriptionId, subscriptionName, out tempSubscription, out tempTenant) &&
7171
newTenant == null)
7272
{
7373
newTenant = tempTenant;
@@ -78,7 +78,18 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
7878

7979
if (newSubscription == null)
8080
{
81-
throw new PSInvalidOperationException(String.Format(Properties.Resources.NoSubscriptionFound, account.Id));
81+
if (subscriptionId != null)
82+
{
83+
throw new PSInvalidOperationException(String.Format(Properties.Resources.SubscriptionIdNotFound, account.Id, subscriptionId));
84+
}
85+
else if (subscriptionName != null)
86+
{
87+
throw new PSInvalidOperationException(String.Format(Properties.Resources.SubscriptionNameNotFound, account.Id, subscriptionId));
88+
}
89+
else
90+
{
91+
throw new PSInvalidOperationException(String.Format(Properties.Resources.NoSubscriptionFound, account.Id));
92+
}
8293
}
8394

8495
_profile.Context = new AzureContext(newSubscription, account, environment, newTenant);
@@ -155,7 +166,7 @@ public bool TryGetSubscription(string tenantId, string subscriptionId, out Azure
155166
var token = AcquireAccessToken(_profile.Context.Account, _profile.Context.Environment,
156167
tenantId, null, ShowDialog.Never);
157168
return TryGetTenantSubscription(token, _profile.Context.Account, _profile.Context.Environment,
158-
tenantId, subscriptionId, out subscription, out tenant);
169+
tenantId, subscriptionId, null, out subscription, out tenant);
159170
}
160171

161172
public bool TryGetSubscriptionByName(string tenantId, string subscriptionName, out AzureSubscription subscription)
@@ -283,10 +294,10 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
283294
AzureEnvironment environment,
284295
string tenantId,
285296
string subscriptionId,
297+
string subscriptionName,
286298
out AzureSubscription subscription,
287299
out AzureTenant tenant)
288300
{
289-
290301
using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>(
291302
new TokenCloudCredentials(accessToken.AccessToken),
292303
environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
@@ -304,14 +315,21 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
304315
var subscriptions = subscriptionClient.Subscriptions.List().Subscriptions;
305316
if (subscriptions != null && subscriptions.Any())
306317
{
307-
if (subscriptions.Count > 1)
318+
if (subscriptionName != null)
319+
{
320+
subscriptionFromServer = subscriptions.FirstOrDefault(s => s.DisplayName.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase));
321+
}
322+
else
308323
{
309-
WriteWarningMessage(string.Format(
310-
"Tenant '{0}' contains more than one subscription. First one will be selected for further use. " +
311-
"To select another subscription, use Set-AzureRmContext.",
312-
tenantId));
324+
if (subscriptions.Count > 1)
325+
{
326+
WriteWarningMessage(string.Format(
327+
"Tenant '{0}' contains more than one subscription. First one will be selected for further use. " +
328+
"To select another subscription, use Set-AzureRmContext.",
329+
tenantId));
330+
}
331+
subscriptionFromServer = subscriptions.First();
313332
}
314-
subscriptionFromServer = subscriptions.First();
315333
}
316334
}
317335
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,41 @@ public void LoginWithNoSubscriptionAndTenant()
128128
Assert.NotNull(AzureRMCmdlet.DefaultProfile.Context);
129129
Assert.Equal("microsoft.com", AzureRMCmdlet.DefaultProfile.Context.Tenant.Domain);
130130
}
131+
132+
[Fact]
133+
[Trait(Category.AcceptanceType, Category.LiveOnly)]
134+
public void LoginWithSubscriptionname()
135+
{
136+
var cmdlt = new AddAzureRMAccountCommand();
137+
// Setup
138+
cmdlt.CommandRuntime = commandRuntimeMock;
139+
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
140+
cmdlt.SubscriptionName = "Node CLI Test";
141+
142+
// Act
143+
cmdlt.InvokeBeginProcessing();
144+
cmdlt.ExecuteCmdlet();
145+
cmdlt.InvokeEndProcessing();
146+
147+
Assert.NotNull(AzureRMCmdlet.DefaultProfile.Context);
148+
Assert.Equal("microsoft.com", AzureRMCmdlet.DefaultProfile.Context.Tenant.Domain);
149+
}
150+
151+
[Fact]
152+
[Trait(Category.AcceptanceType, Category.LiveOnly)]
153+
public void LoginWithBothSubscriptionIdAndNameThrowsCloudException()
154+
{
155+
var cmdlt = new AddAzureRMAccountCommand();
156+
// Setup
157+
cmdlt.CommandRuntime = commandRuntimeMock;
158+
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
159+
cmdlt.SubscriptionName = "Node CLI Test";
160+
cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
161+
162+
// Act
163+
cmdlt.InvokeBeginProcessing();
164+
Assert.Throws<PSInvalidOperationException>(() => cmdlt.ExecuteCmdlet());
165+
cmdlt.InvokeEndProcessing();
166+
}
131167
}
132168
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using Microsoft.Azure.Commands.Profile.Models;
2121
using Microsoft.Azure.Commands.ResourceManager.Common;
2222
using Microsoft.Azure.Common.Authentication;
23+
using System;
24+
using Microsoft.Azure.Commands.Profile.Properties;
2325

2426
namespace Microsoft.Azure.Commands.Profile
2527
{
@@ -56,6 +58,10 @@ public class AddAzureRMAccountCommand : AzureRMCmdlet , IModuleAssemblyInitializ
5658
[ValidateNotNullOrEmpty]
5759
public string SubscriptionId { get; set; }
5860

61+
[Parameter(Mandatory = false, HelpMessage = "Subscription")]
62+
[ValidateNotNullOrEmpty]
63+
public string SubscriptionName { get; set; }
64+
5965
protected override AzureContext DefaultContext
6066
{
6167
get
@@ -75,6 +81,17 @@ protected override void BeginProcessing()
7581

7682
protected override void ProcessRecord()
7783
{
84+
if (SubscriptionId != null && SubscriptionName != null)
85+
{
86+
throw new PSInvalidOperationException(Resources.BothSubscriptionIdAndNameProvided);
87+
}
88+
89+
Guid subscrptionIdGuid;
90+
if (SubscriptionId != null && !Guid.TryParse(SubscriptionId, out subscrptionIdGuid))
91+
{
92+
throw new PSInvalidOperationException(Resources.InvalidSubscriptionId);
93+
}
94+
7895
AzureAccount azureAccount = new AzureAccount();
7996

8097
if (!string.IsNullOrEmpty(AccessToken))
@@ -109,7 +126,7 @@ protected override void ProcessRecord()
109126

110127
var profileClient = new RMProfileClient(AzureRMCmdlet.DefaultProfile);
111128

112-
WriteObject((PSAzureProfile)profileClient.Login(azureAccount, Environment, Tenant, SubscriptionId, password));
129+
WriteObject((PSAzureProfile)profileClient.Login(azureAccount, Environment, Tenant, SubscriptionId, SubscriptionName, password));
113130
}
114131

115132
/// <summary>

src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,15 @@
120120
<data name="AzureProfileMustNotBeNull" xml:space="preserve">
121121
<value>Selected profile must not be null.</value>
122122
</data>
123+
<data name="BothSubscriptionIdAndNameProvided" xml:space="preserve">
124+
<value>Please provide either a subscription ID or a subscription name.</value>
125+
</data>
123126
<data name="CommonTenantAuthFailed" xml:space="preserve">
124127
<value>Could not authenticate your user account {0} with the common tenant. Please login again using Login-AzureRmAccount.</value>
125128
</data>
129+
<data name="InvalidSubscriptionId" xml:space="preserve">
130+
<value>The provided subscription ID "{0}" is not a valid Guid.</value>
131+
</data>
126132
<data name="NoAccountProvided" xml:space="preserve">
127133
<value>(no account provided)</value>
128134
</data>

0 commit comments

Comments
 (0)