Skip to content

. #213

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

Merged
merged 4 commits into from
Nov 25, 2015
Merged

. #213

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
4 changes: 3 additions & 1 deletion src/Common/Commands.Common/PSAzureAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Collections.Generic;

namespace Microsoft.Azure.Common.Authentication.Models
{
public class PSAzureAccount
Expand All @@ -22,6 +24,6 @@ public class PSAzureAccount

public string Subscriptions { get; set; }

public string Tenants { get; set; }
public List<string> Tenants { get; set; }
}
}
4 changes: 3 additions & 1 deletion src/Common/Commands.Common/ProfileClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Common.Authentication.Models;
using System;
using System.Collections.Generic;

namespace Microsoft.WindowsAzure.Commands.Common
{
Expand All @@ -28,7 +30,7 @@ public static PSAzureAccount ToPSAzureAccount(this AzureAccount account)
Id = account.Id,
Type = account.Type,
Subscriptions = subscriptionsList == null ? "" : subscriptionsList.Replace(",", "\r\n"),
Tenants = tenantsList == null ? "" : tenantsList.Replace(",", "\r\n")
Tenants = tenantsList == null ? null : new List<string>(tenantsList.Split(new [] {","}, StringSplitOptions.RemoveEmptyEntries))
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,25 @@ public static class AccessTokenExtensions
{
public static string GetDomain(this IAccessToken token)
{
if( token != null && token.UserId !=null && token.UserId.Contains('@'))
if (token != null)
{
return token.UserId.Split(
new[] { '@' },
StringSplitOptions.RemoveEmptyEntries).Last();
return GetDomain(token.UserId);
}

return null;
}

public static string GetDomain(string emailString)
{
string result = null;
if (emailString != null && emailString.Contains('@'))
{
result = emailString.Split(
new[] { '@' },
StringSplitOptions.RemoveEmptyEntries).Last();
}

return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using Hyak.Common;
using System.Management.Automation;
using Microsoft.WindowsAzure.Commands.Common;
using System.Reflection;

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

}

[Fact]
[Trait(Category.RunType, Category.LiveOnly)]
public void GetMultipleTenantsOnLogin()
{
var cmdlt = new AddAzureRMAccountCommand();
// Setup
// NOTE: Use [email protected] credentials for this test case
cmdlt.CommandRuntime = commandRuntimeMock;

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

Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context.Account);
var tenants = AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants);
Assert.NotNull(tenants);
Assert.Equal(3, tenants.Length);
}

[Fact]
[Trait(Category.RunType, Category.LiveOnly)]
public void LoginWithEnvironementName()
{
var cmdlt = new AddAzureRMAccountCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.EnvironmentName = "AzureUSGovernment";

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

Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context.Environment);
Assert.Equal("AzureUSGovernment", AzureRmProfileProvider.Instance.Profile.Context.Environment.Name);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void ThrowOnUnknownEnvironment()
{
var cmdlt = new AddAzureRMAccountCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.EnvironmentName = "unknown";
var testPassed = false;

// Act
try
{
cmdlt.InvokeBeginProcessing();
}
catch(TargetInvocationException ex)
{
Assert.NotNull(ex);
Assert.NotNull(ex.InnerException);
Assert.Equal("Unable to find environment with name 'unknown'", ex.InnerException.Message);
testPassed = true;
}

Assert.True(testPassed);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public class AddAzureRMAccountCommand : AzureRMCmdlet , IModuleAssemblyInitializ
[ValidateNotNullOrEmpty]
public AzureEnvironment Environment { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Name of the environment containing the account to log into")]
[ValidateNotNullOrEmpty]

public string EnvironmentName { get; set; }


[Parameter(ParameterSetName = UserParameterSet, Mandatory = false, HelpMessage = "Optional credential")]
[Parameter(ParameterSetName = ServicePrincipalParameterSet, Mandatory = true, HelpMessage = "Credential")]
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false, HelpMessage = "Optional credential")]
Expand All @@ -63,8 +69,6 @@ public class AddAzureRMAccountCommand : AzureRMCmdlet , IModuleAssemblyInitializ

[Parameter(ParameterSetName = ServicePrincipalParameterSet, Mandatory = true)]
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet, Mandatory = true)]
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false)]
[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = false)]
public SwitchParameter ServicePrincipal { get; set; }

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

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

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

Expand All @@ -108,9 +114,21 @@ protected override AzureContext DefaultContext
protected override void BeginProcessing()
{
base.BeginProcessing();
if (Environment == null)
if (Environment == null && EnvironmentName == null)
{
Environment = AzureEnvironment.PublicEnvironments[Microsoft.Azure.Common.Authentication.Models.EnvironmentName.AzureCloud];
}
else if (Environment == null && EnvironmentName != null)
{
Environment = AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud];
if (AzureRmProfileProvider.Instance.Profile.Environments.ContainsKey(EnvironmentName))
{
Environment = AzureRmProfileProvider.Instance.Profile.Environments[EnvironmentName];
}
else
{
throw new PSInvalidOperationException(
string.Format(Resources.UnknownEnvironment, EnvironmentName));
}
}
}

Expand All @@ -126,7 +144,8 @@ protected override void ProcessRecord()
if (!string.IsNullOrWhiteSpace(SubscriptionId) &&
!Guid.TryParse(SubscriptionId, out subscrptionIdGuid))
{
throw new PSInvalidOperationException(Resources.InvalidSubscriptionId);
throw new PSInvalidOperationException(
string.Format(Resources.InvalidSubscriptionId, SubscriptionId));
}

AzureAccount azureAccount = new AzureAccount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.Azure.Subscriptions.Models;
using Microsoft.WindowsAzure.Commands.Utilities.Common;

namespace Microsoft.Azure.Commands.ResourceManager.Common
{
Expand All @@ -31,5 +35,27 @@ internal static AzureSubscription ToAzureSubscription(this Subscription other, A
context.Tenant.Id.ToString());
return subscription;
}

public static List<AzureTenant> MergeTenants(
this AzureAccount account,
IEnumerable<TenantIdDescription> tenants,
IAccessToken token)
{
List<AzureTenant> result = null;
if (tenants != null)
{
var existingTenants = new List<AzureTenant>();
account.SetProperty(AzureAccount.Property.Tenants, null);
tenants.ForEach((t) =>
{
existingTenants.Add(new AzureTenant { Id = new Guid(t.TenantId), Domain = token.GetDomain() });
account.SetOrAppendProperty(AzureAccount.Property.Tenants, t.TenantId);
});

result = existingTenants;
}

return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
// ----------------------------------------------------------------------------------

using System;
using System.Linq;
using System.Configuration;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.Azure.Common.Authentication.Utilities;
using System.Collections.Generic;

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

if (account.IsPropertySet(AzureAccount.Property.Tenants))
{
result.Tenants = account.GetProperty(AzureAccount.Property.Tenants);
result.Tenants = new List<string>(account.GetPropertyAsArray(AzureAccount.Property.Tenants));
}

if (account.IsPropertySet(AzureAccount.Property.CertificateThumbprint))
Expand Down Expand Up @@ -87,9 +89,12 @@ public static implicit operator AzureAccount(PSAzureRmAccount account)
result.SetProperty(AzureAccount.Property.AccessToken, account.AccessToken);
}

if (!string.IsNullOrWhiteSpace(account.Tenants))
if (account.Tenants != null &&
account.Tenants.Any(s => !string.IsNullOrWhiteSpace(s)))
{
result.SetProperty(AzureAccount.Property.Tenants, account.Tenants);
result.SetProperty(
AzureAccount.Property.Tenants,
account.Tenants.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray());
}

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

/// <summary>
/// The access token for the account (if any)
Expand Down
Loading