-
Notifications
You must be signed in to change notification settings - Fork 492
Introduce the new options pattern #2637
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
Open
hallipr
wants to merge
7
commits into
main
Choose a base branch
from
users/pahallis/options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
75de5fc
Introduce new options pattern
hallipr 29e4e4b
fix: register ISubscriptionResolver mock in CommandFactoryHelpers
hallipr adb378b
Refactor ISubscriptionResolver to accept string instead of ParseResult
hallipr 11d12bf
Fix whitespace
hallipr f2c5c23
Fix whitespace
hallipr 08715da
Potential fix for pull request finding
hallipr 9da663d
Potential fix for pull request finding
hallipr 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
39 changes: 39 additions & 0 deletions
39
core/Azure.Mcp.Core/src/Commands/Subscription/SubscriptionCommand`2.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,39 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using Azure.Mcp.Core.Options; | ||
| using Azure.Mcp.Core.Services.Azure.Subscription; | ||
| using Microsoft.Mcp.Core.Commands; | ||
|
|
||
| namespace Azure.Mcp.Core.Commands.Subscription; | ||
|
|
||
| public abstract class SubscriptionCommand< | ||
| [DynamicallyAccessedMembers(TrimAnnotations.CommandAnnotations)] TOptions, TResult> | ||
| : AuthenticatedCommand<TOptions, TResult> where TOptions : class, ISubscriptionOption | ||
| { | ||
| private readonly ISubscriptionResolver _subscriptionResolver; | ||
|
|
||
| protected SubscriptionCommand(ISubscriptionResolver subscriptionResolver) | ||
| { | ||
| _subscriptionResolver = subscriptionResolver; | ||
| } | ||
|
|
||
| public override void ValidateOptions(TOptions options, ValidationResult validationResult) | ||
| { | ||
| base.ValidateOptions(options, validationResult); | ||
|
|
||
| if (string.IsNullOrEmpty(options.Subscription)) | ||
| { | ||
| validationResult.Errors.Add("Missing Required options: --subscription"); | ||
| } | ||
| } | ||
|
|
||
| public override TOptions BindOptions(ParseResult parseResult) | ||
| { | ||
| var options = base.BindOptions(parseResult); | ||
| // Always post-process subscription via resolver (env var / CLI profile fallback) | ||
| options.Subscription = _subscriptionResolver.ResolveSubscription(options.Subscription); | ||
| return options; | ||
| } | ||
| } |
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,9 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Azure.Mcp.Core.Options; | ||
|
|
||
| public interface ISubscriptionOption | ||
| { | ||
| string? Subscription { get; set; } | ||
| } |
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,32 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Azure.Mcp.Core.Options; | ||
|
|
||
| /// <summary> | ||
| /// Description constants for global options shared across all Azure MCP commands. | ||
| /// Used with <see cref="Microsoft.Mcp.Core.Options.OptionAttribute"/> on options classes. | ||
| /// </summary> | ||
| public static class OptionDescriptions | ||
| { | ||
| public const string Subscription = | ||
| "Specifies the Azure subscription to use. Accepts either a subscription ID (GUID) or display name. " + | ||
| "If not specified, the AZURE_SUBSCRIPTION_ID environment variable will be used instead."; | ||
|
|
||
| public const string Tenant = | ||
|
hallipr marked this conversation as resolved.
|
||
| "The Microsoft Entra ID tenant ID or name. " + | ||
| "This can be either the GUID identifier or the display name of your Entra ID tenant."; | ||
|
|
||
| public const string AuthMethod = | ||
| "Authentication method to use. " + | ||
| "Options: 'credential' (Azure CLI/managed identity), 'key' (access key), or 'connectionString'."; | ||
|
|
||
| public const string ResourceGroup = | ||
| "The name of the Azure resource group. This is a logical container for Azure resources."; | ||
|
|
||
| public const string Scope = | ||
| "Scope at which the role assignment or definition applies to, " + | ||
| "e.g., /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333, " + | ||
| "/subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup, " + | ||
| "or /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup/providers/Microsoft.Compute/virtualMachines/myVM."; | ||
| } | ||
|
hallipr marked this conversation as resolved.
|
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,46 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
hallipr marked this conversation as resolved.
|
||
| using Azure.Core; | ||
| using Microsoft.Mcp.Core.Options; | ||
|
|
||
| namespace Azure.Mcp.Core.Options; | ||
|
|
||
| public static class RetryOptionsExtensions | ||
| { | ||
| public static void ConfigureRetryOptions(this ClientOptions clientOptions, RetryPolicyOptions? retryPolicy) | ||
|
hallipr marked this conversation as resolved.
|
||
| { | ||
| if (retryPolicy == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (retryPolicy.MaxRetries.HasValue) | ||
| { | ||
| clientOptions.Retry.MaxRetries = retryPolicy.MaxRetries.Value; | ||
| } | ||
|
|
||
| if (retryPolicy.Mode.HasValue) | ||
| { | ||
| clientOptions.Retry.Mode = retryPolicy.Mode.Value; | ||
| } | ||
|
|
||
| if (retryPolicy.DelaySeconds.HasValue) | ||
| { | ||
| clientOptions.Retry.Delay = TimeSpan.FromSeconds(retryPolicy.DelaySeconds.Value); | ||
| } | ||
|
|
||
| if (retryPolicy.MaxDelaySeconds.HasValue) | ||
| { | ||
| clientOptions.Retry.MaxDelay = TimeSpan.FromSeconds(retryPolicy.MaxDelaySeconds.Value); | ||
| } | ||
|
|
||
| if (retryPolicy.NetworkTimeoutSeconds.HasValue) | ||
| { | ||
| clientOptions.Retry.NetworkTimeout = TimeSpan.FromSeconds(retryPolicy.NetworkTimeoutSeconds.Value); | ||
| } | ||
| } | ||
| } | ||
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
25 changes: 25 additions & 0 deletions
25
core/Azure.Mcp.Core/src/Services/Azure/Subscription/ISubscriptionResolver.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,25 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.CommandLine.Parsing; | ||
|
|
||
| namespace Azure.Mcp.Core.Services.Azure.Subscription; | ||
|
|
||
| /// <summary> | ||
| /// Resolves subscription values from command-line arguments, environment variables, | ||
| /// and Azure CLI profile, providing a testable seam for subscription resolution. | ||
| /// </summary> | ||
| public interface ISubscriptionResolver | ||
| { | ||
| /// <summary> | ||
| /// Resolves the subscription from the provided value, falling back to Azure CLI profile | ||
| /// or AZURE_SUBSCRIPTION_ID environment variable. | ||
| /// </summary> | ||
| string? ResolveSubscription(string? subscription); | ||
|
|
||
| /// <summary> | ||
| /// Checks if a subscription is available from the command option, Azure CLI profile, | ||
| /// or AZURE_SUBSCRIPTION_ID environment variable. | ||
| /// </summary> | ||
| bool HasSubscriptionAvailable(CommandResult commandResult); | ||
| } |
24 changes: 24 additions & 0 deletions
24
core/Azure.Mcp.Core/src/Services/Azure/Subscription/SubscriptionResolver.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,24 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.CommandLine.Parsing; | ||
| using Microsoft.Mcp.Core.Helpers; | ||
|
|
||
| namespace Azure.Mcp.Core.Services.Azure.Subscription; | ||
|
|
||
| /// <summary> | ||
| /// Default implementation that resolves subscriptions from command-line arguments, | ||
| /// Azure CLI profile, and the AZURE_SUBSCRIPTION_ID environment variable. | ||
| /// </summary> | ||
| public sealed class SubscriptionResolver : ISubscriptionResolver | ||
| { | ||
| public string? ResolveSubscription(string? subscription) | ||
| { | ||
| subscription = subscription?.Trim('"', '\''); | ||
| subscription = CommandHelper.GetSubscription(subscription); | ||
| return subscription; | ||
| } | ||
|
|
||
| public bool HasSubscriptionAvailable(CommandResult commandResult) => | ||
| CommandHelper.HasSubscriptionAvailable(commandResult); | ||
| } |
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
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.