Skip to content

Commit 5ade97d

Browse files
author
Hovsep
committed
Merge pull request Azure#1352 from hovsepm/dev
Bugfix
2 parents 48ae917 + ad04c26 commit 5ade97d

File tree

10 files changed

+238
-31
lines changed

10 files changed

+238
-31
lines changed

src/Common/Commands.Common/PSAzureAccount.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System.Collections.Generic;
16+
1517
namespace Microsoft.Azure.Common.Authentication.Models
1618
{
1719
public class PSAzureAccount
@@ -22,6 +24,6 @@ public class PSAzureAccount
2224

2325
public string Subscriptions { get; set; }
2426

25-
public string Tenants { get; set; }
27+
public List<string> Tenants { get; set; }
2628
}
2729
}

src/Common/Commands.Common/ProfileClientExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Microsoft.Azure.Common.Authentication.Models;
16+
using System;
17+
using System.Collections.Generic;
1618

1719
namespace Microsoft.WindowsAzure.Commands.Common
1820
{
@@ -28,7 +30,7 @@ public static PSAzureAccount ToPSAzureAccount(this AzureAccount account)
2830
Id = account.Id,
2931
Type = account.Type,
3032
Subscriptions = subscriptionsList == null ? "" : subscriptionsList.Replace(",", "\r\n"),
31-
Tenants = tenantsList == null ? "" : tenantsList.Replace(",", "\r\n")
33+
Tenants = tenantsList == null ? null : new List<string>(tenantsList.Split(new [] {","}, StringSplitOptions.RemoveEmptyEntries))
3234
};
3335
}
3436
}

src/ResourceManager/Common/Commands.ResourceManager.Common/AccessTokenExtensions.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,25 @@ public static class AccessTokenExtensions
2222
{
2323
public static string GetDomain(this IAccessToken token)
2424
{
25-
if( token != null && token.UserId !=null && token.UserId.Contains('@'))
25+
if (token != null)
2626
{
27-
return token.UserId.Split(
28-
new[] { '@' },
29-
StringSplitOptions.RemoveEmptyEntries).Last();
27+
return GetDomain(token.UserId);
3028
}
3129

3230
return null;
3331
}
32+
33+
public static string GetDomain(string emailString)
34+
{
35+
string result = null;
36+
if (emailString != null && emailString.Contains('@'))
37+
{
38+
result = emailString.Split(
39+
new[] { '@' },
40+
StringSplitOptions.RemoveEmptyEntries).Last();
41+
}
42+
43+
return result;
44+
}
3445
}
3546
}

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using Hyak.Common;
2727
using System.Management.Automation;
2828
using Microsoft.WindowsAzure.Commands.Common;
29+
using System.Reflection;
2930

3031
namespace Microsoft.Azure.Commands.Profile.Test
3132
{
@@ -214,5 +215,71 @@ public void LoginWithRbacSPNAndCertificateOnly()
214215
AzureRmProfileProvider.Instance.Profile.Context.Account.GetProperty(AzureAccount.Property.CertificateThumbprint));
215216

216217
}
218+
219+
[Fact]
220+
[Trait(Category.RunType, Category.LiveOnly)]
221+
public void GetMultipleTenantsOnLogin()
222+
{
223+
var cmdlt = new AddAzureRMAccountCommand();
224+
// Setup
225+
// NOTE: Use [email protected] credentials for this test case
226+
cmdlt.CommandRuntime = commandRuntimeMock;
227+
228+
// Act
229+
cmdlt.InvokeBeginProcessing();
230+
cmdlt.ExecuteCmdlet();
231+
cmdlt.InvokeEndProcessing();
232+
233+
Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
234+
Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context.Account);
235+
var tenants = AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants);
236+
Assert.NotNull(tenants);
237+
Assert.Equal(3, tenants.Length);
238+
}
239+
240+
[Fact]
241+
[Trait(Category.RunType, Category.LiveOnly)]
242+
public void LoginWithEnvironementName()
243+
{
244+
var cmdlt = new AddAzureRMAccountCommand();
245+
// Setup
246+
cmdlt.CommandRuntime = commandRuntimeMock;
247+
cmdlt.EnvironmentName = "AzureUSGovernment";
248+
249+
// Act
250+
cmdlt.InvokeBeginProcessing();
251+
cmdlt.ExecuteCmdlet();
252+
cmdlt.InvokeEndProcessing();
253+
254+
Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
255+
Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context.Environment);
256+
Assert.Equal("AzureUSGovernment", AzureRmProfileProvider.Instance.Profile.Context.Environment.Name);
257+
}
258+
259+
[Fact]
260+
[Trait(Category.AcceptanceType, Category.CheckIn)]
261+
public void ThrowOnUnknownEnvironment()
262+
{
263+
var cmdlt = new AddAzureRMAccountCommand();
264+
// Setup
265+
cmdlt.CommandRuntime = commandRuntimeMock;
266+
cmdlt.EnvironmentName = "unknown";
267+
var testPassed = false;
268+
269+
// Act
270+
try
271+
{
272+
cmdlt.InvokeBeginProcessing();
273+
}
274+
catch(TargetInvocationException ex)
275+
{
276+
Assert.NotNull(ex);
277+
Assert.NotNull(ex.InnerException);
278+
Assert.Equal("Unable to find environment with name 'unknown'", ex.InnerException.Message);
279+
testPassed = true;
280+
}
281+
282+
Assert.True(testPassed);
283+
}
217284
}
218285
}

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public class AddAzureRMAccountCommand : AzureRMCmdlet , IModuleAssemblyInitializ
4545
[ValidateNotNullOrEmpty]
4646
public AzureEnvironment Environment { get; set; }
4747

48+
[Parameter(Mandatory = false, HelpMessage = "Name of the environment containing the account to log into")]
49+
[ValidateNotNullOrEmpty]
50+
51+
public string EnvironmentName { get; set; }
52+
53+
4854
[Parameter(ParameterSetName = UserParameterSet, Mandatory = false, HelpMessage = "Optional credential")]
4955
[Parameter(ParameterSetName = ServicePrincipalParameterSet, Mandatory = true, HelpMessage = "Credential")]
5056
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false, HelpMessage = "Optional credential")]
@@ -63,8 +69,6 @@ public class AddAzureRMAccountCommand : AzureRMCmdlet , IModuleAssemblyInitializ
6369

6470
[Parameter(ParameterSetName = ServicePrincipalParameterSet, Mandatory = true)]
6571
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet, Mandatory = true)]
66-
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false)]
67-
[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = false)]
6872
public SwitchParameter ServicePrincipal { get; set; }
6973

7074
[Parameter(ParameterSetName = UserParameterSet, Mandatory = false, HelpMessage = "Optional tenant name or ID")]
@@ -90,10 +94,12 @@ public class AddAzureRMAccountCommand : AzureRMCmdlet , IModuleAssemblyInitializ
9094
public string AccountId { get; set; }
9195

9296
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false, HelpMessage = "Subscription", ValueFromPipelineByPropertyName = true)]
97+
[Parameter(ParameterSetName = ServicePrincipalParameterSet, Mandatory = false, HelpMessage = "Subscription", ValueFromPipelineByPropertyName = true)]
9398
[ValidateNotNullOrEmpty]
9499
public string SubscriptionId { get; set; }
95100

96101
[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = false, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName = true)]
102+
[Parameter(ParameterSetName = ServicePrincipalParameterSet, Mandatory = false, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName = true)]
97103
[ValidateNotNullOrEmpty]
98104
public string SubscriptionName { get; set; }
99105

@@ -108,9 +114,21 @@ protected override AzureContext DefaultContext
108114
protected override void BeginProcessing()
109115
{
110116
base.BeginProcessing();
111-
if (Environment == null)
117+
if (Environment == null && EnvironmentName == null)
118+
{
119+
Environment = AzureEnvironment.PublicEnvironments[Microsoft.Azure.Common.Authentication.Models.EnvironmentName.AzureCloud];
120+
}
121+
else if (Environment == null && EnvironmentName != null)
112122
{
113-
Environment = AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud];
123+
if (AzureRmProfileProvider.Instance.Profile.Environments.ContainsKey(EnvironmentName))
124+
{
125+
Environment = AzureRmProfileProvider.Instance.Profile.Environments[EnvironmentName];
126+
}
127+
else
128+
{
129+
throw new PSInvalidOperationException(
130+
string.Format(Resources.UnknownEnvironment, EnvironmentName));
131+
}
114132
}
115133
}
116134

@@ -126,7 +144,8 @@ protected override void ProcessRecord()
126144
if (!string.IsNullOrWhiteSpace(SubscriptionId) &&
127145
!Guid.TryParse(SubscriptionId, out subscrptionIdGuid))
128146
{
129-
throw new PSInvalidOperationException(Resources.InvalidSubscriptionId);
147+
throw new PSInvalidOperationException(
148+
string.Format(Resources.InvalidSubscriptionId, SubscriptionId));
130149
}
131150

132151
AzureAccount azureAccount = new AzureAccount();

src/ResourceManager/Profile/Commands.Profile/Models/ModelExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using Microsoft.Azure.Common.Authentication;
1619
using Microsoft.Azure.Common.Authentication.Models;
1720
using Microsoft.Azure.Subscriptions.Models;
21+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1822

1923
namespace Microsoft.Azure.Commands.ResourceManager.Common
2024
{
@@ -31,5 +35,27 @@ internal static AzureSubscription ToAzureSubscription(this Subscription other, A
3135
context.Tenant.Id.ToString());
3236
return subscription;
3337
}
38+
39+
public static List<AzureTenant> MergeTenants(
40+
this AzureAccount account,
41+
IEnumerable<TenantIdDescription> tenants,
42+
IAccessToken token)
43+
{
44+
List<AzureTenant> result = null;
45+
if (tenants != null)
46+
{
47+
var existingTenants = new List<AzureTenant>();
48+
account.SetProperty(AzureAccount.Property.Tenants, null);
49+
tenants.ForEach((t) =>
50+
{
51+
existingTenants.Add(new AzureTenant { Id = new Guid(t.TenantId), Domain = token.GetDomain() });
52+
account.SetOrAppendProperty(AzureAccount.Property.Tenants, t.TenantId);
53+
});
54+
55+
result = existingTenants;
56+
}
57+
58+
return result;
59+
}
3460
}
3561
}

src/ResourceManager/Profile/Commands.Profile/Models/PSAzureRmAccount.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.Linq;
1617
using System.Configuration;
1718
using Microsoft.Azure.Common.Authentication.Models;
1819
using Microsoft.Azure.Common.Authentication.Utilities;
20+
using System.Collections.Generic;
1921

2022
namespace Microsoft.Azure.Commands.Profile.Models
2123
{
@@ -49,7 +51,7 @@ public static implicit operator PSAzureRmAccount(AzureAccount account)
4951

5052
if (account.IsPropertySet(AzureAccount.Property.Tenants))
5153
{
52-
result.Tenants = account.GetProperty(AzureAccount.Property.Tenants);
54+
result.Tenants = new List<string>(account.GetPropertyAsArray(AzureAccount.Property.Tenants));
5355
}
5456

5557
if (account.IsPropertySet(AzureAccount.Property.CertificateThumbprint))
@@ -87,9 +89,12 @@ public static implicit operator AzureAccount(PSAzureRmAccount account)
8789
result.SetProperty(AzureAccount.Property.AccessToken, account.AccessToken);
8890
}
8991

90-
if (!string.IsNullOrWhiteSpace(account.Tenants))
92+
if (account.Tenants != null &&
93+
account.Tenants.Any(s => !string.IsNullOrWhiteSpace(s)))
9194
{
92-
result.SetProperty(AzureAccount.Property.Tenants, account.Tenants);
95+
result.SetProperty(
96+
AzureAccount.Property.Tenants,
97+
account.Tenants.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray());
9398
}
9499

95100
if (!string.IsNullOrWhiteSpace(account.CertificateThumbprint))
@@ -111,7 +116,7 @@ public static implicit operator AzureAccount(PSAzureRmAccount account)
111116
/// <summary>
112117
/// The tenant ids for the account
113118
/// </summary>
114-
public string Tenants { get; set; }
119+
public List<string> Tenants { get; set; }
115120

116121
/// <summary>
117122
/// The access token for the account (if any)

0 commit comments

Comments
 (0)