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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<package id="Microsoft.WindowsAzure.Management" version="4.1.1" targetFramework="net45" />
<package id="Moq" version="4.2.1402.2112" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
<package id="xunit.runner.visualstudio" version="2.1.0-beta4-build1109" targetFramework="net45" />
<package id="xunit" version="1.9.2" targetFramework="net45" />
<package id="xunit.extensions" version="1.9.2" targetFramework="net45" />
<package id="xunit.runner.visualstudio" version="2.1.0-beta4-build1109" targetFramework="net45" />
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ Select Y to enable data collection [Y/N]:</value>
<data name="NoSubscriptionFound" xml:space="preserve">
<value>The provided account {0} does not have access to any subscriptions. Please try logging in with different credentials.</value>
</data>
<data name="SubscriptionIdNotFound" xml:space="preserve">
<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>
</data>
<data name="SubscriptionNameNotFound" xml:space="preserve">
<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>
</data>
<data name="TenantNotFound" xml:space="preserve">
<value>Tenant '{0}' was not found. Please verify that your account has access to this tenant.</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public RMProfileClient(AzureRMProfile profile)
}
}

public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment, string tenantId, string subscriptionId, SecureString password)
public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment, string tenantId, string subscriptionId, string subscriptionName, SecureString password)
{
AzureSubscription newSubscription = null;
AzureTenant newTenant = null;
Expand All @@ -55,7 +55,7 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
if (!string.IsNullOrEmpty(tenantId))
{
var token = AcquireAccessToken(account, environment, tenantId, password, promptBehavior);
TryGetTenantSubscription(token, account, environment, tenantId, subscriptionId, out newSubscription, out newTenant);
TryGetTenantSubscription(token, account, environment, tenantId, subscriptionId, subscriptionName, out newSubscription, out newTenant);
}
// (tenant is not provided and subscription is present) OR
// (tenant is not provided and subscription is not provided)
Expand All @@ -67,7 +67,7 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
AzureSubscription tempSubscription;
var token = AcquireAccessToken(account, environment, tenant.Id.ToString(), password,
ShowDialog.Auto);
if (newTenant == null && TryGetTenantSubscription(token, account, environment, tenant.Id.ToString(), subscriptionId, out tempSubscription, out tempTenant) &&
if (newTenant == null && TryGetTenantSubscription(token, account, environment, tenant.Id.ToString(), subscriptionId, subscriptionName, out tempSubscription, out tempTenant) &&
newTenant == null)
{
newTenant = tempTenant;
Expand All @@ -78,7 +78,18 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,

if (newSubscription == null)
{
throw new PSInvalidOperationException(String.Format(Properties.Resources.NoSubscriptionFound, account.Id));
if (subscriptionId != null)
{
throw new PSInvalidOperationException(String.Format(Properties.Resources.SubscriptionIdNotFound, account.Id, subscriptionId));
}
else if (subscriptionName != null)
{
throw new PSInvalidOperationException(String.Format(Properties.Resources.SubscriptionNameNotFound, account.Id, subscriptionId));
}
else
{
throw new PSInvalidOperationException(String.Format(Properties.Resources.NoSubscriptionFound, account.Id));
}
}

_profile.Context = new AzureContext(newSubscription, account, environment, newTenant);
Expand Down Expand Up @@ -155,7 +166,7 @@ public bool TryGetSubscription(string tenantId, string subscriptionId, out Azure
var token = AcquireAccessToken(_profile.Context.Account, _profile.Context.Environment,
tenantId, null, ShowDialog.Never);
return TryGetTenantSubscription(token, _profile.Context.Account, _profile.Context.Environment,
tenantId, subscriptionId, out subscription, out tenant);
tenantId, subscriptionId, null, out subscription, out tenant);
}

public bool TryGetSubscriptionByName(string tenantId, string subscriptionName, out AzureSubscription subscription)
Expand Down Expand Up @@ -283,10 +294,10 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
AzureEnvironment environment,
string tenantId,
string subscriptionId,
string subscriptionName,
out AzureSubscription subscription,
out AzureTenant tenant)
{

using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>(
new TokenCloudCredentials(accessToken.AccessToken),
environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
Expand All @@ -304,14 +315,21 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
var subscriptions = subscriptionClient.Subscriptions.List().Subscriptions;
if (subscriptions != null && subscriptions.Any())
{
if (subscriptions.Count > 1)
if (subscriptionName != null)
{
subscriptionFromServer = subscriptions.FirstOrDefault(s => s.DisplayName.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase));
}
else
{
WriteWarningMessage(string.Format(
"Tenant '{0}' contains more than one subscription. First one will be selected for further use. " +
"To select another subscription, use Set-AzureRmContext.",
tenantId));
if (subscriptions.Count > 1)
{
WriteWarningMessage(string.Format(
"Tenant '{0}' contains more than one subscription. First one will be selected for further use. " +
"To select another subscription, use Set-AzureRmContext.",
tenantId));
}
subscriptionFromServer = subscriptions.First();
}
subscriptionFromServer = subscriptions.First();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,41 @@ public void LoginWithNoSubscriptionAndTenant()
Assert.NotNull(AzureRMCmdlet.DefaultProfile.Context);
Assert.Equal("microsoft.com", AzureRMCmdlet.DefaultProfile.Context.Tenant.Domain);
}

[Fact]
[Trait(Category.AcceptanceType, Category.LiveOnly)]
public void LoginWithSubscriptionname()
{
var cmdlt = new AddAzureRMAccountCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
cmdlt.SubscriptionName = "Node CLI Test";

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

Assert.NotNull(AzureRMCmdlet.DefaultProfile.Context);
Assert.Equal("microsoft.com", AzureRMCmdlet.DefaultProfile.Context.Tenant.Domain);
}

[Fact]
[Trait(Category.AcceptanceType, Category.LiveOnly)]
public void LoginWithBothSubscriptionIdAndNameThrowsCloudException()
{
var cmdlt = new AddAzureRMAccountCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
cmdlt.SubscriptionName = "Node CLI Test";
cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";

// Act
cmdlt.InvokeBeginProcessing();
Assert.Throws<PSInvalidOperationException>(() => cmdlt.ExecuteCmdlet());
cmdlt.InvokeEndProcessing();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using Microsoft.Azure.Commands.Profile.Models;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.Azure.Common.Authentication;
using System;
using Microsoft.Azure.Commands.Profile.Properties;

namespace Microsoft.Azure.Commands.Profile
{
Expand Down Expand Up @@ -56,6 +58,10 @@ public class AddAzureRMAccountCommand : AzureRMCmdlet , IModuleAssemblyInitializ
[ValidateNotNullOrEmpty]
public string SubscriptionId { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Subscription")]
[ValidateNotNullOrEmpty]
public string SubscriptionName { get; set; }

protected override AzureContext DefaultContext
{
get
Expand All @@ -75,6 +81,17 @@ protected override void BeginProcessing()

protected override void ProcessRecord()
{
if (SubscriptionId != null && SubscriptionName != null)
{
throw new PSInvalidOperationException(Resources.BothSubscriptionIdAndNameProvided);
}

Guid subscrptionIdGuid;
if (SubscriptionId != null && !Guid.TryParse(SubscriptionId, out subscrptionIdGuid))
{
throw new PSInvalidOperationException(Resources.InvalidSubscriptionId);
}

AzureAccount azureAccount = new AzureAccount();

if (!string.IsNullOrEmpty(AccessToken))
Expand Down Expand Up @@ -109,7 +126,7 @@ protected override void ProcessRecord()

var profileClient = new RMProfileClient(AzureRMCmdlet.DefaultProfile);

WriteObject((PSAzureProfile)profileClient.Login(azureAccount, Environment, Tenant, SubscriptionId, password));
WriteObject((PSAzureProfile)profileClient.Login(azureAccount, Environment, Tenant, SubscriptionId, SubscriptionName, password));
}

/// <summary>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,15 @@
<data name="AzureProfileMustNotBeNull" xml:space="preserve">
<value>Selected profile must not be null.</value>
</data>
<data name="BothSubscriptionIdAndNameProvided" xml:space="preserve">
<value>Please provide either a subscription ID or a subscription name.</value>
</data>
<data name="CommonTenantAuthFailed" xml:space="preserve">
<value>Could not authenticate your user account {0} with the common tenant. Please login again using Login-AzureRmAccount.</value>
</data>
<data name="InvalidSubscriptionId" xml:space="preserve">
<value>The provided subscription ID "{0}" is not a valid Guid.</value>
</data>
<data name="NoAccountProvided" xml:space="preserve">
<value>(no account provided)</value>
</data>
Expand Down