-
Notifications
You must be signed in to change notification settings - Fork 433
Read default subscription from Azure CLI profile for all subscription-scoped commands #1974
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
+508
−14
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
11804a9
Initial plan
Copilot 1c6ebbd
Add isDefault field to subscription_list tool response and update des…
Copilot 5c04535
Address code review: tighten description and improve sorting readability
Copilot 7a6be82
Replace AZURE_SUBSCRIPTION_ID with Azure CLI profile for default subs…
Copilot 7c6c198
Address review: use specific exception types in catch blocks
Copilot bf5fbae
Extract Azure CLI profile logic to shared AzureCliProfileHelper; wire…
Copilot 2c640d6
Rename test file to match class name AzureCliProfileHelperTests
Copilot 0a655a6
Address PR review comments: short-circuit IO, guard empty user profil…
Copilot 48d3b98
Address review: cache default subscription, document sync I/O, fix te…
Copilot 412a927
Merge main into copilot/fix-issue-1079, resolve conflict in Subscript…
Copilot 2a30356
Fix caching: only cache CLI profile read, not env var lookup
Copilot 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
Some comments aren't visible on the classic Files Changed page.
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
19 changes: 19 additions & 0 deletions
19
core/Azure.Mcp.Core/src/Areas/Subscription/Models/SubscriptionInfo.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,19 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Azure.Mcp.Core.Areas.Subscription.Models; | ||
|
|
||
| /// <summary> | ||
| /// Represents an Azure subscription with default subscription indicator. | ||
| /// </summary> | ||
| /// <param name="SubscriptionId">The subscription ID.</param> | ||
| /// <param name="DisplayName">The display name of the subscription.</param> | ||
| /// <param name="State">The subscription state (e.g., Enabled, Disabled).</param> | ||
| /// <param name="TenantId">The tenant ID associated with the subscription.</param> | ||
| /// <param name="IsDefault">Whether this subscription is the default as configured via 'az account set' in the Azure CLI profile, or via the AZURE_SUBSCRIPTION_ID environment variable.</param> | ||
| internal record SubscriptionInfo( | ||
| string SubscriptionId, | ||
| string DisplayName, | ||
| string? State, | ||
| string? TenantId, | ||
| bool IsDefault); |
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
148 changes: 148 additions & 0 deletions
148
....Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Areas/Subscription/AzureCliProfileHelperTests.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,148 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Azure.Mcp.Core.Helpers; | ||
| using Xunit; | ||
|
|
||
| namespace Azure.Mcp.Core.UnitTests.Areas.Subscription; | ||
|
|
||
| public class AzureCliProfileHelperTests | ||
| { | ||
| [Fact] | ||
| public void ParseDefaultSubscriptionId_ValidProfile_ReturnsDefaultId() | ||
| { | ||
| var profileJson = """ | ||
| { | ||
| "subscriptions": [ | ||
| { | ||
| "id": "sub-1111-1111", | ||
| "name": "Subscription One", | ||
| "state": "Enabled", | ||
| "tenantId": "tenant-1111", | ||
| "isDefault": false | ||
| }, | ||
| { | ||
| "id": "sub-2222-2222", | ||
| "name": "Subscription Two", | ||
| "state": "Enabled", | ||
| "tenantId": "tenant-2222", | ||
| "isDefault": true | ||
| }, | ||
| { | ||
| "id": "sub-3333-3333", | ||
| "name": "Subscription Three", | ||
| "state": "Enabled", | ||
| "tenantId": "tenant-3333", | ||
| "isDefault": false | ||
| } | ||
| ] | ||
| } | ||
| """; | ||
|
|
||
| var result = AzureCliProfileHelper.ParseDefaultSubscriptionId(profileJson); | ||
|
|
||
| Assert.Equal("sub-2222-2222", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseDefaultSubscriptionId_NoDefaultInProfile_ReturnsNull() | ||
| { | ||
| var profileJson = """ | ||
| { | ||
| "subscriptions": [ | ||
| { | ||
| "id": "sub-1111-1111", | ||
| "name": "Subscription One", | ||
| "state": "Enabled", | ||
| "tenantId": "tenant-1111", | ||
| "isDefault": false | ||
| } | ||
| ] | ||
| } | ||
| """; | ||
|
|
||
| var result = AzureCliProfileHelper.ParseDefaultSubscriptionId(profileJson); | ||
|
|
||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseDefaultSubscriptionId_EmptySubscriptions_ReturnsNull() | ||
| { | ||
| var profileJson = """ | ||
| { | ||
| "subscriptions": [] | ||
| } | ||
| """; | ||
|
|
||
| var result = AzureCliProfileHelper.ParseDefaultSubscriptionId(profileJson); | ||
|
|
||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseDefaultSubscriptionId_NoSubscriptionsProperty_ReturnsNull() | ||
| { | ||
| var profileJson = """ | ||
| { | ||
| "installationId": "some-id" | ||
| } | ||
| """; | ||
|
|
||
| var result = AzureCliProfileHelper.ParseDefaultSubscriptionId(profileJson); | ||
|
|
||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseDefaultSubscriptionId_MissingIdOnDefault_ReturnsNull() | ||
| { | ||
| var profileJson = """ | ||
| { | ||
| "subscriptions": [ | ||
| { | ||
| "name": "Subscription One", | ||
| "isDefault": true | ||
| } | ||
| ] | ||
| } | ||
| """; | ||
|
|
||
| var result = AzureCliProfileHelper.ParseDefaultSubscriptionId(profileJson); | ||
|
|
||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetAzureProfilePath_WhenUserProfileAvailable_ReturnsExpectedPath() | ||
| { | ||
| var result = AzureCliProfileHelper.GetAzureProfilePath(); | ||
|
|
||
| // In containerized/CI environments, user profile may not be available | ||
| if (string.IsNullOrEmpty(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))) | ||
| { | ||
| Assert.Null(result); | ||
| } | ||
| else | ||
| { | ||
| Assert.NotNull(result); | ||
| Assert.Contains(".azure", result); | ||
| Assert.EndsWith("azureProfile.json", result); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetAzureProfilePath_ReturnsNullOrValidPath() | ||
| { | ||
| var result = AzureCliProfileHelper.GetAzureProfilePath(); | ||
|
|
||
| // The method must either return null (empty user profile) or a valid absolute path | ||
| if (result != null) | ||
| { | ||
| Assert.False(string.IsNullOrWhiteSpace(result)); | ||
| Assert.Contains(".azure", result); | ||
| Assert.EndsWith("azureProfile.json", result); | ||
| Assert.True(Path.IsPathRooted(result), "Path should be absolute, not relative"); | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.