-
Notifications
You must be signed in to change notification settings - Fork 4k
Added Get-AzureRMTenant [#102955260] #918
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.Azure.Common.Authentication; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.Commands.ResourceManager.Common | ||
{ | ||
public static class AccessTokenExtensions | ||
{ | ||
public static string GetDomain(this IAccessToken token) | ||
{ | ||
if( token != null && token.UserId !=null && token.UserId.Contains('@')) | ||
{ | ||
return token.UserId.Split( | ||
new[] { '@' }, | ||
StringSplitOptions.RemoveEmptyEntries).Last(); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using Microsoft.WindowsAzure.Commands.Utilities.Common; | ||
using Microsoft.Azure.Commands.Profile; | ||
using Microsoft.Azure.Commands.ResourceManager.Common; | ||
using Microsoft.Azure.Common.Authentication; | ||
using Microsoft.Azure.Common.Authentication.Models; | ||
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; | ||
using Microsoft.WindowsAzure.Commands.ScenarioTest; | ||
using System.Linq; | ||
using Xunit; | ||
using System; | ||
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; | ||
using Hyak.Common; | ||
using System.Management.Automation; | ||
|
||
namespace Microsoft.Azure.Commands.Profile.Test | ||
{ | ||
public class TenantCmdletTests | ||
{ | ||
private MemoryDataStore dataStore; | ||
private MockCommandRuntime commandRuntimeMock; | ||
|
||
public TenantCmdletTests() | ||
{ | ||
dataStore = new MemoryDataStore(); | ||
AzureSession.DataStore = dataStore; | ||
commandRuntimeMock = new MockCommandRuntime(); | ||
AzureRMCmdlet.DefaultProfile = new AzureRMProfile(); | ||
} | ||
|
||
[Fact] | ||
[Trait(Category.AcceptanceType, Category.LiveOnly)] | ||
public void GetTenantWithTenantParameter() | ||
{ | ||
var cmdlt = new GetAzureRMTenantCommand(); | ||
// Setup | ||
cmdlt.CommandRuntime = commandRuntimeMock; | ||
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47"; | ||
|
||
// Act | ||
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null); | ||
cmdlt.InvokeBeginProcessing(); | ||
cmdlt.ExecuteCmdlet(); | ||
cmdlt.InvokeEndProcessing(); | ||
|
||
Assert.True(commandRuntimeMock.OutputPipeline.Count == 2); | ||
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", ((AzureTenant)commandRuntimeMock.OutputPipeline[1]).Id.ToString()); | ||
Assert.Equal("microsoft.com", ((AzureTenant)commandRuntimeMock.OutputPipeline[1]).Domain); | ||
} | ||
|
||
[Fact] | ||
[Trait(Category.AcceptanceType, Category.LiveOnly)] | ||
public void GetTenantWithDomainParameter() | ||
{ | ||
var cmdlt = new GetAzureRMTenantCommand(); | ||
// Setup | ||
cmdlt.CommandRuntime = commandRuntimeMock; | ||
cmdlt.Tenant = "microsoft.com"; | ||
|
||
// Act | ||
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null); | ||
cmdlt.InvokeBeginProcessing(); | ||
cmdlt.ExecuteCmdlet(); | ||
cmdlt.InvokeEndProcessing(); | ||
|
||
Assert.True(commandRuntimeMock.OutputPipeline.Count == 2); | ||
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", ((AzureTenant)commandRuntimeMock.OutputPipeline[1]).Id.ToString()); | ||
Assert.Equal("microsoft.com", ((AzureTenant)commandRuntimeMock.OutputPipeline[1]).Domain); | ||
} | ||
|
||
[Fact] | ||
[Trait(Category.AcceptanceType, Category.LiveOnly)] | ||
public void GetTenantWithoutParameters() | ||
{ | ||
var cmdlt = new GetAzureRMTenantCommand(); | ||
// Setup | ||
cmdlt.CommandRuntime = commandRuntimeMock; | ||
|
||
// Act | ||
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null); | ||
cmdlt.InvokeBeginProcessing(); | ||
cmdlt.ExecuteCmdlet(); | ||
cmdlt.InvokeEndProcessing(); | ||
|
||
Assert.True(commandRuntimeMock.OutputPipeline.Count == 2); | ||
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", ((AzureTenant)commandRuntimeMock.OutputPipeline[1]).Id.ToString()); | ||
Assert.Equal("microsoft.com", ((AzureTenant)commandRuntimeMock.OutputPipeline[1]).Domain); | ||
} | ||
|
||
private void Login(string subscriptionId, string tenantId) | ||
{ | ||
var cmdlt = new LoginAzureRMAccountCommand(); | ||
// Setup | ||
cmdlt.CommandRuntime = commandRuntimeMock; | ||
cmdlt.SubscriptionId = subscriptionId; | ||
cmdlt.Tenant = tenantId; | ||
|
||
// Act | ||
cmdlt.InvokeBeginProcessing(); | ||
cmdlt.ExecuteCmdlet(); | ||
cmdlt.InvokeEndProcessing(); | ||
|
||
Assert.NotNull(AzureRMCmdlet.DefaultProfile.Context); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using Microsoft.Azure.Commands.ResourceManager.Common; | ||
using Microsoft.Azure.Common.Authentication.Models; | ||
using System.Collections.Generic; | ||
using System.Management.Automation; | ||
|
||
namespace Microsoft.Azure.Commands.Profile | ||
{ | ||
/// <summary> | ||
/// Cmdlet to get user tenant information. | ||
/// </summary> | ||
[Cmdlet(VerbsCommon.Get, "AzureRMTenant")] | ||
[Alias("Get-AzureRMDomain")] | ||
[OutputType(typeof(List<AzureTenant>))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this should just be AzureTenant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the task details - "If no tenant is specified, list all tenants.". So it can be a list. |
||
public class GetAzureRMTenantCommand : AzureRMCmdlet | ||
{ | ||
[Parameter(Mandatory = false, Position = 0, ValueFromPipelineByPropertyName = true)] | ||
[Alias("Domain")] | ||
[ValidateNotNullOrEmpty] | ||
public string Tenant { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs a parameter attribute - it should be Mandatory=False, ValueFromPipelineByPropertyName = true There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed. |
||
|
||
protected override void ProcessRecord() | ||
{ | ||
var profileClient = new RMProfileClient(AzureRMCmdlet.DefaultProfile); | ||
|
||
WriteObject(profileClient.ListTenants(Tenant), enumerateCollection: true); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to add some mocked tests for this - we can do it using the existing infrastructure, or using Moq infrastructure (I'm checking some in with the subscription cmdlet). Please file a PBI for this and we can take it up afterward.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done #103553984