Skip to content

Select-AzureRmSubscription: support tab-complete on subscription ID and tenant ID #2210

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 1 commit into from
May 5, 2016
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 @@ -12,17 +12,15 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Xunit;
using System;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.Azure.Commands.Profile.Models;
using Microsoft.Azure.ServiceManagemenet.Common;
using Microsoft.Azure.ServiceManagemenet.Common.Models;
using Microsoft.WindowsAzure.Commands.Common;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Xunit.Extensions;
using System;
using Xunit;
using Xunit.Abstractions;
using Microsoft.Azure.ServiceManagemenet.Common.Models;

namespace Microsoft.Azure.Commands.Profile.Test
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.Azure.Commands.Profile;
using Microsoft.Azure.Commands.Profile.Models;
using Microsoft.Azure.ServiceManagemenet.Common.Models;
using Microsoft.WindowsAzure.Commands.Common;
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Xunit;
using Microsoft.Azure.Commands.Profile.Models;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using System.Management.Automation;
using Xunit;
using Xunit.Abstractions;
using Microsoft.Azure.ServiceManagemenet.Common.Models;

namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
{
public class ContextCmdletTests : RMTestBase
{
private MemoryDataStore dataStore;
private MockCommandRuntime commandRuntimeMock;

public ContextCmdletTests(ITestOutputHelper output)
{
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
Expand All @@ -45,6 +47,7 @@ public ContextCmdletTests(ITestOutputHelper output)
public void GetAzureContext()
{
var cmdlt = new GetAzureRMContextCommand();

// Setup
cmdlt.CommandRuntime = commandRuntimeMock;

Expand All @@ -55,18 +58,28 @@ public void GetAzureContext()

// Verify
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
var context = (PSAzureContext) commandRuntimeMock.OutputPipeline[0];
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
Assert.Equal("test", context.Subscription.SubscriptionName);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SelectAzureContextWithNoSubscriptionAndTenant()
{
var cmdlt = new SetAzureRMContextCommand();
var tenantToSet = "72f988bf-86f1-41af-91ab-2d7cd011db47";

// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";

// Make sure that the tenant ID we are attempting to set is
// valid for the account
var account = AzureRmProfileProvider.Instance.Profile.Context.Account;
var existingTenants = account.GetProperty(AzureAccount.Property.Tenants);
var allowedTenants = existingTenants == null ? tenantToSet : existingTenants + "," + tenantToSet;
account.SetProperty(AzureAccount.Property.Tenants, allowedTenants);

((RuntimeDefinedParameterDictionary)cmdlt.GetDynamicParameters())["TenantId"].Value = tenantToSet;

// Act
cmdlt.InvokeBeginProcessing();
Expand All @@ -76,15 +89,17 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
// Verify
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];

// TenantId is not sufficient to change the context.
Assert.NotEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.TenantId);
Assert.NotEqual(tenantToSet, context.Tenant.TenantId);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SelectAzureContextWithNoSubscriptionAndNoTenant()
{
var cmdlt = new SetAzureRMContextCommand();

// Setup
cmdlt.CommandRuntime = commandRuntimeMock;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function Test-SetAzureRmContextEndToEnd
$context = Get-AzureRmContext
Assert-AreEqual $context.Subscription.SubscriptionId $secondSubscription.SubscriptionId
$junkSubscriptionId = "49BC3D95-9A30-40F8-81E0-3CDEF0C3F8A5"
Assert-ThrowsContains {Set-AzureRmContext -SubscriptionId $junkSubscriptionId} "does not exist"
Assert-ThrowsContains {Set-AzureRmContext -SubscriptionId $junkSubscriptionId} "does not belong"
}

function Test-SetAzureRmContextWithoutSubscription
Expand All @@ -95,7 +95,7 @@ function Test-SetAzureRmContextWithoutSubscription

Set-AzureRmContext -TenantId $tenantId
$context = Get-AzureRmContext

Assert-True { $context.Subscription -ne $null }
Assert-True { $context.Tenant -ne $null }
Assert-AreEqual $context.Tenant.TenantId $firstSubscription.TenantId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,30 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Management.Automation;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.Azure.Commands.Profile.Models;
using Microsoft.Azure.Commands.Profile.Properties;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.WindowsAzure.Commands.Common;
using Microsoft.Azure.Commands.Profile.Properties;
using System;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using System.Collections.ObjectModel;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.Profile
{
/// <summary>
/// Cmdlet to change current Azure context.
/// Cmdlet to change current Azure context.
/// </summary>
[Cmdlet(VerbsCommon.Set, "AzureRmContext", DefaultParameterSetName = SubscriptionNameParameterSet)]
[Alias("Select-AzureRmSubscription")]
[OutputType(typeof(PSAzureContext))]
public class SetAzureRMContextCommand : AzureRMCmdlet
public class SetAzureRMContextCommand : AzureRMCmdlet, IDynamicParameters
{
private const string SubscriptionNameParameterSet = "SubscriptionName";
private const string SubscriptionIdParameterSet = "SubscriptionId";
private const string ContextParameterSet = "Context";

[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName = true)]
[Alias("Domain")]
[ValidateNotNullOrEmpty]
public string TenantId { get; set; }

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

[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = false, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
Expand All @@ -51,6 +44,22 @@ public class SetAzureRMContextCommand : AzureRMCmdlet
[Parameter(ParameterSetName = ContextParameterSet, Mandatory = true, HelpMessage = "Context", ValueFromPipeline = true)]
public PSAzureContext Context { get; set; }

private string TenantId
{
get
{
return _tenantId == null ? null : (string)_tenantId.Value;
}
}

private string SubscriptionId
{
get
{
return _subscriptionId == null ? null : (string)_subscriptionId.Value;
}
}

public override void ExecuteCmdlet()
{
if (ParameterSetName == ContextParameterSet)
Expand All @@ -60,7 +69,7 @@ public override void ExecuteCmdlet()
}
else if (ParameterSetName == SubscriptionNameParameterSet || ParameterSetName == SubscriptionIdParameterSet)
{
if (string.IsNullOrWhiteSpace(SubscriptionId)
if (string.IsNullOrWhiteSpace(SubscriptionId)
&& string.IsNullOrWhiteSpace(SubscriptionName)
&& string.IsNullOrWhiteSpace(TenantId))
{
Expand All @@ -82,7 +91,7 @@ public override void ExecuteCmdlet()
AzureRmProfileProvider.Instance.Profile.Context.Subscription != null &&
AzureRmProfileProvider.Instance.Profile.Context.Subscription.State != null &&
!AzureRmProfileProvider.Instance.Profile.Context.Subscription.State.Equals(
"Enabled",
"Enabled",
StringComparison.OrdinalIgnoreCase))
{
WriteWarning(string.Format(
Expand All @@ -91,5 +100,55 @@ public override void ExecuteCmdlet()
}
WriteObject((PSAzureContext)AzureRmProfileProvider.Instance.Profile.Context);
}

public object GetDynamicParameters()
{
return CreateDynamicParameterDictionary();
}

private RuntimeDefinedParameterDictionary CreateDynamicParameterDictionary()
{
var runtimeDefinedParameterDictionary = new RuntimeDefinedParameterDictionary();

var subscriptionIdAttributes = new Collection<Attribute>
{
new ParameterAttribute
{
ParameterSetName = SubscriptionIdParameterSet,
Mandatory = false,
HelpMessage = "Subscription",
ValueFromPipelineByPropertyName = true
},
new ValidateSetAttribute(AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Subscriptions)),
};

var tenantIdAttributes = new Collection<Attribute>
{
new ParameterAttribute
{
ParameterSetName = SubscriptionNameParameterSet,
Mandatory = false,
HelpMessage = "TenantId name or ID",
ValueFromPipelineByPropertyName = true
},
new ParameterAttribute
{
ParameterSetName = SubscriptionIdParameterSet,
Mandatory = false,
HelpMessage = "TenantId name or ID",
ValueFromPipelineByPropertyName = true
},
new AliasAttribute("Domain"),
new ValidateSetAttribute(AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants)),
};

_tenantId = new RuntimeDefinedParameter("TenantId", typeof(string), tenantIdAttributes);
_subscriptionId = new RuntimeDefinedParameter("SubscriptionId", typeof(string), subscriptionIdAttributes);

runtimeDefinedParameterDictionary.Add("SubscriptionId", _subscriptionId);
runtimeDefinedParameterDictionary.Add("TenantId", _tenantId);

return runtimeDefinedParameterDictionary;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
.Where(s => "enabled".Equals(s.State, StringComparison.OrdinalIgnoreCase) ||
"warned".Equals(s.State, StringComparison.OrdinalIgnoreCase));

account.SetProperty(AzureAccount.Property.Subscriptions, subscriptions.Select(i => i.SubscriptionId).ToArray());

if (subscriptions.Any())
{
if (subscriptionName != null)
Expand Down