-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Add Get-AzureRMSubscription and common test project [#102950504] #924
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0268fb0
Add Get-AzureRMSubscription cmdlet
markcowl 00081c0
Merge branch 'dev' of github.com:Azure/azure-powershell into getsubsc…
markcowl 5dfed00
[#102950504]: Add common test project and tests for RMProfileClient
markcowl 45ed6b5
[#102950504]: Merging with upstream
markcowl df712db
[#102950504]: merging with upstream
markcowl 05a34da
merging with upstream
markcowl bfb0a64
[Finish #102950504]: Added unit and scenario tests; updated test infr…
markcowl 464b5aa
Fixing test project and adding new projects to test targets
markcowl ae65e64
removing unnecessary app.config files
markcowl 11f2729
More fixes to test prohect file
markcowl 255991c
Minor fixes to test project
markcowl fcca131
removing sparse test project
markcowl 668d267
Correcting name of test project for resource manager profile tests
markcowl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Common/Commands.ResourceManager.Common/ModelExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Azure.Common.Authentication.Models; | ||
| using Microsoft.Azure.Subscriptions.Models; | ||
|
|
||
| namespace Microsoft.Azure.Commands.ResourceManager.Common | ||
| { | ||
| public static class ModelExtensions | ||
| { | ||
|
|
||
| public static AzureSubscription ToAzureSubscription(this Subscription other, AzureContext context) | ||
| { | ||
| var subscription = new AzureSubscription(); | ||
| subscription.Account = context.Account != null ? context.Account.Id : null; | ||
| subscription.Environment = context.Environment != null ? context.Environment.Name : EnvironmentName.AzureCloud; | ||
| subscription.Id = new Guid(other.SubscriptionId); | ||
| subscription.Name = other.DisplayName; | ||
| subscription.SetProperty(AzureSubscription.Property.Tenants, | ||
| context.Tenant.Id.ToString()); | ||
| return subscription; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,6 +140,18 @@ public List<AzureTenant> ListTenants(string tenant) | |
| .ToList(); | ||
| } | ||
|
|
||
| public bool TryGetSubscription(string tenantId, string subscriptionId, out AzureSubscription subscription) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(tenantId)) | ||
| { | ||
| throw new ArgumentNullException("Please provide a valid tenant Id"); | ||
| } | ||
|
|
||
| AzureTenant tenant; | ||
| return TryGetTenantSubscription(_profile.Context.Account, _profile.Context.Environment, | ||
| tenantId, subscriptionId, null, ShowDialog.Never, out subscription, out tenant); | ||
| } | ||
|
|
||
| private bool TryGetTenantSubscription( | ||
| AzureAccount account, | ||
| AzureEnvironment environment, | ||
|
|
@@ -177,7 +189,8 @@ private bool TryGetTenantSubscription( | |
| if (subscriptions.Count > 1) | ||
| { | ||
| WriteWarningMessage(string.Format( | ||
| "Tenant '{0}' contains more than one subscription. First one will be selected for further use.", | ||
| "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(); | ||
|
|
@@ -233,12 +246,91 @@ private List<AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironm | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// List all tenants for the account in the profile context | ||
| /// </summary> | ||
| /// <returns>The list of tenants for the default account.</returns> | ||
| public IEnumerable<AzureTenant> ListTenants() | ||
| { | ||
| return ListAccountTenants(_profile.Context.Account, _profile.Context.Environment, null, ShowDialog.Never); | ||
| } | ||
|
|
||
| private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount account, AzureEnvironment environment, | ||
|
Member
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. Future Refactoring: Since TryGetTenantSubscription does List() internally as well, we should consider calling ListSubscriptionsForTenant() inside TryGetTenantSubscription. |
||
| SecureString password, ShowDialog promptBehavior, string tenantId) | ||
| { | ||
| var accessToken = AzureSession.AuthenticationFactory.Authenticate( | ||
| account, | ||
| environment, | ||
| tenantId, | ||
| password, | ||
| promptBehavior); | ||
| using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>( | ||
| new TokenCloudCredentials(accessToken.AccessToken), | ||
| environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager))) | ||
| { | ||
| var subscriptions = subscriptionClient.Subscriptions.List(); | ||
| if (subscriptions != null && subscriptions.Subscriptions != null) | ||
| { | ||
| return | ||
| subscriptions.Subscriptions.Select( | ||
| (s) => | ||
| s.ToAzureSubscription(new AzureContext(_profile.Context.Subscription, account, | ||
| environment, CreateTenantFromString(tenantId)))); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public IEnumerable<AzureSubscription> ListSubscriptions(string tenant) | ||
| { | ||
| return ListSubscriptionsForTenant(_profile.Context.Account, _profile.Context.Environment, null, | ||
| ShowDialog.Never, tenant); | ||
| } | ||
|
|
||
| public IEnumerable<AzureSubscription> ListSubscriptions() | ||
| { | ||
| List<AzureSubscription> subscriptions = new List<AzureSubscription>(); | ||
| foreach (var tenant in ListTenants()) | ||
| { | ||
| try | ||
| { | ||
| subscriptions.AddRange(ListSubscriptions(tenant.Id.ToString())); | ||
| } | ||
| catch (AadAuthenticationException) | ||
| { | ||
| WriteWarningMessage(string.Format("Could not authenticate user account {0} with tenant {1}. " + | ||
| "Subscriptions in this tenant will not be listed. Please login again using Login-AzureRMAccount " + | ||
| "to view the subscriptions in this tenant.", _profile.Context.Account, tenant)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| return subscriptions; | ||
| } | ||
|
|
||
| private void WriteWarningMessage(string message) | ||
| { | ||
| if (WarningLog != null) | ||
| { | ||
| WarningLog(message); | ||
| } | ||
| } | ||
|
|
||
| private static AzureTenant CreateTenantFromString(string tenantOrDomain) | ||
| { | ||
| AzureTenant result = new AzureTenant(); | ||
| Guid id; | ||
| if (Guid.TryParse(tenantOrDomain, out id)) | ||
| { | ||
| result.Id = id; | ||
| } | ||
| else | ||
| { | ||
| result.Domain = tenantOrDomain; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/Common/Commands.ResourceManager.Profile.Test/AzureRMProfileTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // ---------------------------------------------------------------------------------- | ||
| // | ||
| // 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.Common.Authentication; | ||
| using Microsoft.Azure.Common.Authentication.Models; | ||
| using System.Linq; | ||
| using Xunit; | ||
| using System; | ||
| using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; | ||
| using System.Collections.Generic; | ||
| using Microsoft.WindowsAzure.Commands.ScenarioTest; | ||
|
|
||
| namespace Microsoft.Azure.Commands.ResourceManager.Common.Test | ||
| { | ||
|
|
||
| public class AzureRMProfileTests | ||
| { | ||
| private const string DefaultAccount = "[email protected]"; | ||
| private static Guid DefaultSubscription = Guid.NewGuid(); | ||
| private static string DefaultSubscriptionName = "Contoso Subscription"; | ||
| private static string DefaultDomain = "contoso.com"; | ||
| private static Guid DefaultTenant = Guid.NewGuid(); | ||
|
|
||
| private static RMProfileClient SetupTestEnvironment(List<string> tenants, params List<string>[] subscriptionLists) | ||
| { | ||
| AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory(DefaultAccount, | ||
| Guid.NewGuid().ToString(), DefaultTenant.ToString()); | ||
| var subscriptionList = new Queue<List<string>>(subscriptionLists); | ||
| var clientFactory = new MockSubscriptionClientFactory(tenants, subscriptionList); | ||
| var mock = new MockClientFactory(new List<object> | ||
| { | ||
| clientFactory.GetSubscriptionClient() | ||
| }, true); | ||
| mock.MoqClients = true; | ||
| AzureSession.ClientFactory = mock; | ||
| var context = new AzureContext(new AzureSubscription() | ||
| { | ||
| Account = DefaultAccount, | ||
| Environment = EnvironmentName.AzureCloud, | ||
| Id = DefaultSubscription, | ||
| Name = DefaultSubscriptionName | ||
| }, | ||
| new AzureAccount() { Id = DefaultAccount, Type = AzureAccount.AccountType.User }, | ||
| AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud], | ||
| new AzureTenant() { Domain = DefaultDomain, Id = DefaultTenant }); | ||
| var profile = new AzureRMProfile(); | ||
| profile.Context = context; | ||
| return new RMProfileClient(profile); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait(Category.AcceptanceType, Category.CheckIn)] | ||
| public void MultipleTenantsAndSubscriptionsSucceed() | ||
| { | ||
| var tenants = new List<string> {Guid.NewGuid().ToString(), DefaultTenant.ToString()}; | ||
| var firstList = new List<string> { DefaultSubscription.ToString(), Guid.NewGuid().ToString() }; | ||
| var secondList = new List<string> { Guid.NewGuid().ToString()}; | ||
| var client = SetupTestEnvironment(tenants, firstList, secondList); | ||
| var subResults = new List<AzureSubscription>(client.ListSubscriptions()); | ||
| Assert.Equal(3, subResults.Count); | ||
| var tenantResults = client.ListTenants(); | ||
| Assert.Equal(2, tenantResults.Count()); | ||
| tenantResults = client.ListTenants(DefaultTenant.ToString()); | ||
| Assert.Equal(1, tenantResults.Count()); | ||
| AzureSubscription subValue; | ||
| Assert.True(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue)); | ||
| Assert.Equal(DefaultSubscription.ToString(), subValue.Id.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait(Category.AcceptanceType, Category.CheckIn)] | ||
| public void SingleTenantAndSubscriptionSucceeds() | ||
| { | ||
| var tenants = new List<string> {DefaultTenant.ToString()}; | ||
| var subscriptions = new List<string> {DefaultSubscription.ToString()}; | ||
| var client = SetupTestEnvironment(tenants, subscriptions); | ||
| var subResults = new List<AzureSubscription>(client.ListSubscriptions()); | ||
| Assert.Equal(1, subResults.Count); | ||
| var tenantResults = client.ListTenants(); | ||
| Assert.Equal(1, tenantResults.Count()); | ||
| tenantResults = client.ListTenants(DefaultTenant.ToString()); | ||
| Assert.Equal(1, tenantResults.Count()); | ||
| AzureSubscription subValue; | ||
| Assert.True(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue)); | ||
| Assert.Equal(DefaultSubscription.ToString(), subValue.Id.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait(Category.AcceptanceType, Category.CheckIn)] | ||
| public void SubscriptionNotFoundDoesNotThrow() | ||
| { | ||
| var tenants = new List<string> { DefaultTenant.ToString() }; | ||
| var subscriptions = new List<string> { Guid.NewGuid().ToString() }; | ||
| var client = SetupTestEnvironment(tenants, subscriptions); | ||
| var subResults = new List<AzureSubscription>(client.ListSubscriptions()); | ||
| Assert.Equal(1, subResults.Count); | ||
| AzureSubscription subValue; | ||
| Assert.False(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue)); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait(Category.AcceptanceType, Category.CheckIn)] | ||
| public void NoTenantsDoesNotThrow() | ||
| { | ||
| var tenants = new List<string> { }; | ||
| var subscriptions = new List<string> { Guid.NewGuid().ToString() }; | ||
| var client = SetupTestEnvironment(tenants, subscriptions); | ||
| Assert.Equal(0, client.ListSubscriptions().Count()); | ||
| Assert.Equal(0, client.ListTenants().Count()); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait(Category.AcceptanceType, Category.CheckIn)] | ||
| public void NoSubscriptionsInListThrows() | ||
| { | ||
| var tenants = new List<string> { DefaultTenant.ToString() }; | ||
| var subscriptions = new List<string> () ; | ||
| var client = SetupTestEnvironment(tenants, subscriptions); | ||
| Assert.Equal(0, client.ListSubscriptions().Count()); | ||
| AzureSubscription subValue; | ||
| Assert.False(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue)); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: extra line