Skip to content

Commit ccfae47

Browse files
author
Hovsep Mkrtchyan
committed
Added pagination in Get-AzureRmSubscription cmdlet
1 parent 1d15258 commit ccfae47

File tree

4 files changed

+136
-9
lines changed

4 files changed

+136
-9
lines changed

src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
using Xunit;
1919
using System;
2020
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
21+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2122
using System.Collections.Generic;
2223
using Microsoft.IdentityModel.Clients.ActiveDirectory;
2324
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2425
using Microsoft.WindowsAzure.Commands.Common;
2526
using Moq;
2627
using System.Collections.Concurrent;
2728
using System.Threading.Tasks;
29+
using Microsoft.Azure.Commands.Profile;
30+
using Microsoft.Azure.Commands.Profile.Models;
2831

2932
namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
3033
{
@@ -271,5 +274,40 @@ public void AzurePSComletMessageQueue()
271274

272275
Assert.Equal(500, queue.Count);
273276
}
277+
278+
279+
[Fact]
280+
[Trait(Category.AcceptanceType, Category.CheckIn)]
281+
public void GetAzureRmSubscriptionPaginatedResult()
282+
{
283+
var tenants = new List<string> { Guid.NewGuid().ToString(), DefaultTenant.ToString() };
284+
var secondsubscriptionInTheFirstTenant = Guid.NewGuid().ToString();
285+
var firstList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
286+
var secondList = new List<string> { Guid.NewGuid().ToString() };
287+
var thirdList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
288+
var fourthList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
289+
var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);
290+
291+
var dataStore = new MemoryDataStore();
292+
AzureSession.DataStore = dataStore;
293+
var commandRuntimeMock = new MockCommandRuntime();
294+
AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory();
295+
var profile = new AzureRMProfile();
296+
profile.Environments.Add("foo", AzureEnvironment.PublicEnvironments.Values.FirstOrDefault());
297+
profile.Context = Context;
298+
var cmdlt = new GetAzureRMSubscriptionCommand();
299+
// Setup
300+
cmdlt.DefaultProfile = profile;
301+
cmdlt.CommandRuntime = commandRuntimeMock;
302+
303+
// Act
304+
cmdlt.InvokeBeginProcessing();
305+
cmdlt.ExecuteCmdlet();
306+
cmdlt.InvokeEndProcessing();
307+
308+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 7);
309+
Assert.Equal("Disabled", ((PSAzureSubscription)commandRuntimeMock.OutputPipeline[2]).State);
310+
Assert.Equal("LinkToNextPage", ((PSAzureSubscription)commandRuntimeMock.OutputPipeline[2]).SubscriptionName);
311+
}
274312
}
275313
}

src/ResourceManager/Profile/Commands.Profile.Test/MockSubscriptionClientFactory.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public SubscriptionClient GetSubscriptionClient()
9999
{
100100
StatusCode = HttpStatusCode.OK,
101101
RequestId = Guid.NewGuid().ToString(),
102+
NextLink = "LinkToNextPage",
102103
Subscriptions =
103104
new List<Subscription>(
104105
subscriptionList.Select(
@@ -113,6 +114,33 @@ public SubscriptionClient GetSubscriptionClient()
113114
};
114115
}
115116

117+
return Task.FromResult(result);
118+
});
119+
subscriptionMock.Setup(
120+
(s) => s.ListNextAsync("LinkToNextPage", It.IsAny<CancellationToken>())).Returns(
121+
(string nextLink, CancellationToken token) =>
122+
{
123+
SubscriptionListResult result = null;
124+
if (_subscriptions.Count > 0)
125+
{
126+
var subscriptionList = _subscriptions.Dequeue();
127+
result = new SubscriptionListResult
128+
{
129+
StatusCode = HttpStatusCode.OK,
130+
RequestId = Guid.NewGuid().ToString(),
131+
Subscriptions =
132+
new List<Subscription>(
133+
subscriptionList.Select(
134+
sub =>
135+
new Subscription
136+
{
137+
DisplayName = nextLink,
138+
Id = sub,
139+
State = "Disabled",
140+
SubscriptionId = sub
141+
}))
142+
};
143+
}
116144
return Task.FromResult(result);
117145
});
118146
var client = new Mock<SubscriptionClient>();

src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using System.Security;
2828
using Microsoft.Azure.Commands.Profile.Models;
2929

30+
3031
namespace Microsoft.Azure.Commands.ResourceManager.Common
3132
{
3233
public class RMProfileClient
@@ -286,13 +287,14 @@ private AzureSubscription GetFirstSubscription(string tenantId)
286287
public IEnumerable<AzureSubscription> GetSubscriptions(string tenantId)
287288
{
288289
IEnumerable<AzureSubscription> subscriptionList= new List<AzureSubscription>();
290+
string listNextLink = null;
289291
if (string.IsNullOrWhiteSpace(tenantId))
290292
{
291293
subscriptionList = ListSubscriptions();
292294
}
293295
else
294296
{
295-
subscriptionList = ListSubscriptions(tenantId);
297+
subscriptionList = ListSubscriptions(tenantId, ref listNextLink);
296298
}
297299

298300
return subscriptionList;
@@ -377,22 +379,30 @@ public IEnumerable<AzureTenant> ListTenants()
377379
return ListAccountTenants(_profile.Context.Account, _profile.Context.Environment, null, ShowDialog.Never);
378380
}
379381

380-
public IEnumerable<AzureSubscription> ListSubscriptions(string tenant)
382+
public IEnumerable<AzureSubscription> ListSubscriptions(string tenant, ref string listNextLink)
381383
{
382-
return ListSubscriptionsForTenant(_profile.Context.Account, _profile.Context.Environment, null,
383-
ShowDialog.Never, tenant);
384+
return ListSubscriptionsForTenant(
385+
_profile.Context.Account,
386+
_profile.Context.Environment,
387+
null,
388+
ShowDialog.Never,
389+
tenant,
390+
ref listNextLink);
384391
}
385392

386393
public IEnumerable<AzureSubscription> ListSubscriptions()
387394
{
395+
string listNextLink = null;
396+
388397
List<AzureSubscription> subscriptions = new List<AzureSubscription>();
389398
foreach (var tenant in ListTenants())
390399
{
391400
try
392401
{
393402
subscriptions.AddRange(
394403
ListSubscriptions(
395-
(tenant.Id == Guid.Empty) ? tenant.Domain:tenant.Id.ToString()));
404+
(tenant.Id == Guid.Empty) ? tenant.Domain:tenant.Id.ToString(),
405+
ref listNextLink));
396406
}
397407
catch (AadAuthenticationException)
398408
{
@@ -616,8 +626,13 @@ private List<AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironm
616626
return result;
617627
}
618628

619-
private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount account, AzureEnvironment environment,
620-
SecureString password, ShowDialog promptBehavior, string tenantId)
629+
private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(
630+
AzureAccount account,
631+
AzureEnvironment environment,
632+
SecureString password,
633+
ShowDialog promptBehavior,
634+
string tenantId,
635+
ref string listNextLink)
621636
{
622637
IAccessToken accessToken = null;
623638

@@ -636,9 +651,18 @@ private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount a
636651
new TokenCloudCredentials(accessToken.AccessToken),
637652
environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
638653
{
639-
var subscriptions = subscriptionClient.Subscriptions.List();
654+
Microsoft.Azure.Subscriptions.Models.SubscriptionListResult subscriptions = null;
655+
if(listNextLink == null)
656+
{
657+
subscriptions = subscriptionClient.Subscriptions.List();
658+
}
659+
else
660+
{
661+
subscriptions = subscriptionClient.Subscriptions.ListNext(listNextLink);
662+
}
640663
if (subscriptions != null && subscriptions.Subscriptions != null)
641664
{
665+
listNextLink = subscriptions.NextLink;
642666
return
643667
subscriptions.Subscriptions.Select(
644668
(s) =>

src/ResourceManager/Profile/Commands.Profile/Subscription/GetAzureRMSubscription.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
using Microsoft.Azure.Commands.Profile.Models;
1919
using Microsoft.Azure.Commands.Profile.Properties;
2020
using Microsoft.Azure.Commands.ResourceManager.Common;
21+
using System.Collections.Generic;
22+
using System;
23+
using Microsoft.WindowsAzure.Commands.Common;
2124

2225
namespace Microsoft.Azure.Commands.Profile
2326
{
@@ -92,7 +95,41 @@ public override void ExecuteCmdlet()
9295
{
9396
try
9497
{
95-
WriteObject(_client.GetSubscriptions(tenant).Select((s) => (PSAzureSubscription)s), enumerateCollection: true);
98+
var tenantsList = new List<string>();
99+
100+
if (string.IsNullOrWhiteSpace(tenant))
101+
{
102+
tenantsList.AddRange(_client.ListTenants()
103+
.Select(t => (t.Id == Guid.Empty) ? t.Domain : t.Id.ToString()));
104+
}
105+
else
106+
{
107+
tenantsList.Add(tenant);
108+
}
109+
110+
foreach (var tenantId in tenantsList)
111+
{
112+
try
113+
{
114+
string listNextLink = null;
115+
do
116+
{
117+
var subscriptions = _client.ListSubscriptions(tenantId, ref listNextLink);
118+
WriteObject(subscriptions.Select((s) => (PSAzureSubscription)s), enumerateCollection: true);
119+
} while (listNextLink != null);
120+
}
121+
catch (AadAuthenticationException)
122+
{
123+
if (!string.IsNullOrWhiteSpace(tenant))
124+
{
125+
throw;
126+
}
127+
WriteWarning(string.Format(
128+
Microsoft.Azure.Commands.Profile.Properties.Resources.UnableToLogin,
129+
AzureRmProfileProvider.Instance.Profile.Context.Account,
130+
tenant));
131+
}
132+
}
96133
}
97134
catch (AadAuthenticationException exception)
98135
{

0 commit comments

Comments
 (0)