Skip to content
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

[Az.Accounts] Fixed returning duplicate subscription ids. #19450

Merged
merged 5 commits into from
Sep 13, 2022
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
25 changes: 24 additions & 1 deletion src/Accounts/Accounts.Test/AzureRMProfileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public void SingleTenantSubscriptionListSucceed()

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetSubscriptionListByNameCorrect()
public void GetSubscriptionListByNameSameIdCorrect()
{
var tenants = new List<string> { DefaultTenant.ToString() };
var firstList = new List<string> { DefaultSubscription.ToString() };
Expand All @@ -514,6 +514,29 @@ public void GetSubscriptionListByNameCorrect()
client.TryGetSubscriptionListByName(DefaultTenant.ToString(),
MockSubscriptionClientFactory.GetSubscriptionNameFromId(DefaultSubscription.ToString()),
out subValueList);
Assert.Single(subValueList);
}


[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetSubscriptionListByNameCorrect()
{
var subId1 = "a11a11aa-aaaa-aaaa-aaaa-aaaa1111aaaa";
var subId2 = "aaaa11aa-aaaa-aaaa-aaaa-aaaa1111aaaa";

var tenants = new List<string> { DefaultTenant.ToString() };
var firstList = new List<string> { subId1 };
var secondList = new List<string> { subId2 };
var thirdList = firstList;
var fourthList = firstList;
wyunchi-ms marked this conversation as resolved.
Show resolved Hide resolved
var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);
var tenantResults = client.ListTenants();
Assert.Single(tenantResults);
IEnumerable<IAzureSubscription> subValueList;
client.TryGetSubscriptionListByName(DefaultTenant.ToString(),
"SameNameForGetSubscriptionByName",
out subValueList);
Assert.Equal(2, subValueList.Count());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public MockSubscriptionClientFactory()

public static string GetSubscriptionNameFromId(string id)
{
if(id == "a11a11aa-aaaa-aaaa-aaaa-aaaa1111aaaa" || id == "aaaa11aa-aaaa-aaaa-aaaa-aaaa1111aaaa")
{
return "SameNameForGetSubscriptionByName";
}
return "Sub-" + id;
}

Expand Down
1 change: 1 addition & 0 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
-->

## Upcoming Release
* Deduplicated subscriptions belonging to multiple tenants while using `Get-AzSubscription` with parameter `SubscriptionName`. [#19427]

## Version 2.10.0
* Supported returning all subscriptions with specified name while using `Get-AzSubscription` with parameter `SubscriptionName`. [#19295]
Expand Down
29 changes: 25 additions & 4 deletions src/Accounts/Accounts/Models/RMProfileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,32 @@ public bool TryGetSubscriptionByName(string tenantId, string subscriptionName, o
return subscription != null;
}

public bool TryGetSubscriptionListByName(string tenantId, string subscriptionName, out IEnumerable<IAzureSubscription> subscriptionList)
public bool TryGetSubscriptionListByName(string tenantId, string subscriptionName, out IEnumerable<IAzureSubscription> subscriptions)
{
subscriptionList = ListSubscriptions(tenantId);
subscriptionList = subscriptionList.Where(s => s.Name.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase));
return subscriptionList.Any();
subscriptions = ListSubscriptions(tenantId).Where(s => s.Name.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase));
List<IAzureSubscription> subscriptionList = new List<IAzureSubscription>();
HashSet<Guid> existedSubscriptionIds = new HashSet<Guid>();

// Consider subscription in Home tenant first, exclude duplicate subscriptions by id.
foreach(IAzureSubscription subscription in subscriptions)
{
if (subscription is PSAzureSubscription && subscription.GetTenant() != null
&& subscription.GetHomeTenant().Equals(subscription.GetTenant()) && existedSubscriptionIds.Add(subscription.GetId()))
{
subscriptionList.Add(subscription);
}

}
// Consider other subscriptions.
foreach (IAzureSubscription subscription in subscriptions)
{
if (existedSubscriptionIds.Add(subscription.GetId()))
{
subscriptionList.Add(subscription);
}
}
subscriptions = subscriptionList;
return subscriptions.Any();
}

private IAzureSubscription GetFirstSubscription(string tenantId)
Expand Down