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 @@ -18,13 +18,16 @@
using Xunit;
using System;
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using System.Collections.Generic;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Microsoft.WindowsAzure.Commands.Common;
using Moq;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Profile;
using Microsoft.Azure.Commands.Profile.Models;

namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
{
Expand Down Expand Up @@ -271,5 +274,40 @@ public void AzurePSComletMessageQueue()

Assert.Equal(500, queue.Count);
}


[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetAzureRmSubscriptionPaginatedResult()
{
var tenants = new List<string> { Guid.NewGuid().ToString(), DefaultTenant.ToString() };
var secondsubscriptionInTheFirstTenant = Guid.NewGuid().ToString();
var firstList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
var secondList = new List<string> { Guid.NewGuid().ToString() };
var thirdList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
var fourthList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);

var dataStore = new MemoryDataStore();
AzureSession.DataStore = dataStore;
var commandRuntimeMock = new MockCommandRuntime();
AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory();
var profile = new AzureRMProfile();
profile.Environments.Add("foo", AzureEnvironment.PublicEnvironments.Values.FirstOrDefault());
profile.Context = Context;
var cmdlt = new GetAzureRMSubscriptionCommand();
// Setup
cmdlt.DefaultProfile = profile;
cmdlt.CommandRuntime = commandRuntimeMock;

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

Assert.True(commandRuntimeMock.OutputPipeline.Count == 7);
Assert.Equal("Disabled", ((PSAzureSubscription)commandRuntimeMock.OutputPipeline[2]).State);
Assert.Equal("LinkToNextPage", ((PSAzureSubscription)commandRuntimeMock.OutputPipeline[2]).SubscriptionName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public SubscriptionClient GetSubscriptionClient()
{
StatusCode = HttpStatusCode.OK,
RequestId = Guid.NewGuid().ToString(),
NextLink = "LinkToNextPage",
Subscriptions =
new List<Subscription>(
subscriptionList.Select(
Expand All @@ -113,6 +114,33 @@ public SubscriptionClient GetSubscriptionClient()
};
}

return Task.FromResult(result);
});
subscriptionMock.Setup(
(s) => s.ListNextAsync("LinkToNextPage", It.IsAny<CancellationToken>())).Returns(
(string nextLink, CancellationToken token) =>
{
SubscriptionListResult result = null;
if (_subscriptions.Count > 0)
{
var subscriptionList = _subscriptions.Dequeue();
result = new SubscriptionListResult
{
StatusCode = HttpStatusCode.OK,
RequestId = Guid.NewGuid().ToString(),
Subscriptions =
new List<Subscription>(
subscriptionList.Select(
sub =>
new Subscription
{
DisplayName = nextLink,
Id = sub,
State = "Disabled",
SubscriptionId = sub
}))
};
}
return Task.FromResult(result);
});
var client = new Mock<SubscriptionClient>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System.Security;
using Microsoft.Azure.Commands.Profile.Models;


namespace Microsoft.Azure.Commands.ResourceManager.Common
{
public class RMProfileClient
Expand Down Expand Up @@ -286,13 +287,14 @@ private AzureSubscription GetFirstSubscription(string tenantId)
public IEnumerable<AzureSubscription> GetSubscriptions(string tenantId)
{
IEnumerable<AzureSubscription> subscriptionList= new List<AzureSubscription>();
string listNextLink = null;
if (string.IsNullOrWhiteSpace(tenantId))
{
subscriptionList = ListSubscriptions();
}
else
{
subscriptionList = ListSubscriptions(tenantId);
subscriptionList = ListSubscriptions(tenantId, ref listNextLink);
}

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

public IEnumerable<AzureSubscription> ListSubscriptions(string tenant)
public IEnumerable<AzureSubscription> ListSubscriptions(string tenant, ref string listNextLink)
{
return ListSubscriptionsForTenant(_profile.Context.Account, _profile.Context.Environment, null,
ShowDialog.Never, tenant);
return ListSubscriptionsForTenant(
_profile.Context.Account,
_profile.Context.Environment,
null,
ShowDialog.Never,
tenant,
ref listNextLink);
}

public IEnumerable<AzureSubscription> ListSubscriptions()
{
string listNextLink = null;

List<AzureSubscription> subscriptions = new List<AzureSubscription>();
foreach (var tenant in ListTenants())
{
try
{
subscriptions.AddRange(
ListSubscriptions(
(tenant.Id == Guid.Empty) ? tenant.Domain:tenant.Id.ToString()));
(tenant.Id == Guid.Empty) ? tenant.Domain:tenant.Id.ToString(),
ref listNextLink));
}
catch (AadAuthenticationException)
{
Expand Down Expand Up @@ -616,8 +626,13 @@ private List<AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironm
return result;
}

private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount account, AzureEnvironment environment,
SecureString password, ShowDialog promptBehavior, string tenantId)
private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(
AzureAccount account,
AzureEnvironment environment,
SecureString password,
ShowDialog promptBehavior,
string tenantId,
ref string listNextLink)
{
IAccessToken accessToken = null;

Expand All @@ -636,9 +651,18 @@ private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount a
new TokenCloudCredentials(accessToken.AccessToken),
environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
{
var subscriptions = subscriptionClient.Subscriptions.List();
Microsoft.Azure.Subscriptions.Models.SubscriptionListResult subscriptions = null;
if(listNextLink == null)
{
subscriptions = subscriptionClient.Subscriptions.List();
}
else
{
subscriptions = subscriptionClient.Subscriptions.ListNext(listNextLink);
}
if (subscriptions != null && subscriptions.Subscriptions != null)
{
listNextLink = subscriptions.NextLink;
return
subscriptions.Subscriptions.Select(
(s) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
using Microsoft.Azure.Commands.Profile.Models;
using Microsoft.Azure.Commands.Profile.Properties;
using Microsoft.Azure.Commands.ResourceManager.Common;
using System.Collections.Generic;
using System;
using Microsoft.WindowsAzure.Commands.Common;

namespace Microsoft.Azure.Commands.Profile
{
Expand Down Expand Up @@ -92,7 +95,41 @@ public override void ExecuteCmdlet()
{
try
{
WriteObject(_client.GetSubscriptions(tenant).Select((s) => (PSAzureSubscription)s), enumerateCollection: true);
var tenantsList = new List<string>();

if (string.IsNullOrWhiteSpace(tenant))
{
tenantsList.AddRange(_client.ListTenants()
.Select(t => (t.Id == Guid.Empty) ? t.Domain : t.Id.ToString()));
}
else
{
tenantsList.Add(tenant);
}

foreach (var tenantId in tenantsList)
{
try
{
string listNextLink = null;
do
{
var subscriptions = _client.ListSubscriptions(tenantId, ref listNextLink);
WriteObject(subscriptions.Select((s) => (PSAzureSubscription)s), enumerateCollection: true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hovsepm I'm not sure what actually happens in this case, where we are writing mulsiple collections to the output stream. To be safe, I would perform a WriteObject for each subscription.

} while (listNextLink != null);
}
catch (AadAuthenticationException)
{
if (!string.IsNullOrWhiteSpace(tenant))
{
throw;
}
WriteWarning(string.Format(
Microsoft.Azure.Commands.Profile.Properties.Resources.UnableToLogin,
AzureRmProfileProvider.Instance.Profile.Context.Account,
tenant));
}
}
}
catch (AadAuthenticationException exception)
{
Expand Down