-
Notifications
You must be signed in to change notification settings - Fork 717
Added support for prompting for parameter values in run mode #10235
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
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
88fc925
Added support for prompting for parameter values
davidfowl 985ca1c
Refactor HandleUnresolvedParametersAsync to use List for inputs inste…
davidfowl 42001b5
Add unit tests for ParameterProcessor functionality
davidfowl c7f95fc
Refactor HandleUnresolvedParametersAsync for improved testing and int…
davidfowl 03e5755
Refactor HandleUnresolvedParametersAsync test to use array for parame…
davidfowl 4adeb7a
Refactor HandleUnresolvedParametersAsync tests to improve async handl…
davidfowl cfdc386
Refactor HandleUnresolvedParametersAsync test to enhance interaction …
davidfowl a44dfae
Refactor exception handling in environment variable tests to use Miss…
davidfowl 8954f93
Add MissingParameterValueException for handling missing parameter values
davidfowl a6d7c4c
PR feedback
davidfowl 19e90b6
Fix capitalization in interaction titles for unresolved parameters
davidfowl 9ad2e0d
Update HandleUnresolvedParametersAsync to set Required property to fa…
davidfowl 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Aspire.Hosting; | ||
|
|
||
| /// <summary> | ||
| /// The exception that is thrown when a parameter resource cannot be initialized because its value is missing or cannot be resolved. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This exception is typically thrown when: | ||
| /// <list type="bullet"> | ||
| /// <item><description>A parameter value is not provided in configuration and has no default value</description></item> | ||
| /// <item><description>A parameter's value callback throws an exception during execution</description></item> | ||
| /// <item><description>A parameter's value cannot be retrieved from the configured source (e.g., user secrets, environment variables)</description></item> | ||
| /// </list> | ||
| /// </remarks> | ||
| public class MissingParameterValueException : DistributedApplicationException | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MissingParameterValueException"/> class with a specified error message. | ||
| /// </summary> | ||
| /// <param name="message">The message that describes the error.</param> | ||
| public MissingParameterValueException(string message) : base(message) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MissingParameterValueException"/> class with a specified error message | ||
| /// and a reference to the inner exception that is the cause of this exception. | ||
| /// </summary> | ||
| /// <param name="message">The error message that explains the reason for the exception.</param> | ||
| /// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param> | ||
| public MissingParameterValueException(string message, Exception innerException) : base(message, innerException) | ||
| { | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| #pragma warning disable ASPIREINTERACTION001 | ||
|
|
||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Dashboard.Model; | ||
| using Aspire.Hosting.ApplicationModel; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Aspire.Hosting.Orchestrator; | ||
|
|
||
| /// <summary> | ||
| /// Handles processing of parameter resources during application orchestration. | ||
| /// </summary> | ||
| internal sealed class ParameterProcessor( | ||
| ResourceNotificationService notificationService, | ||
| ResourceLoggerService loggerService, | ||
| IInteractionService interactionService, | ||
davidfowl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ILogger<ParameterProcessor> logger) | ||
| { | ||
| private readonly List<ParameterResource> _unresolvedParameters = []; | ||
|
|
||
| public async Task InitializeParametersAsync(IEnumerable<ParameterResource> parameterResources) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this take a cancellation token? |
||
| { | ||
| // Initialize all parameter resources by setting their WaitForValueTcs. | ||
| // This allows them to be processed asynchronously later. | ||
| foreach (var parameterResource in parameterResources) | ||
| { | ||
| parameterResource.WaitForValueTcs = new(TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
|
||
| await ProcessParameterAsync(parameterResource).ConfigureAwait(false); | ||
| } | ||
|
|
||
| // If interaction service is available, we can handle unresolved parameters. | ||
| // This will allow the user to provide values for parameters that could not be initialized. | ||
| if (interactionService.IsAvailable) | ||
| { | ||
| // All parameters have been processed, we can now handle unresolved parameters if any. | ||
| if (_unresolvedParameters.Count > 0) | ||
| { | ||
| // Start the loop that will allow the user to specify values for unresolved parameters. | ||
| _ = Task.Run(async () => | ||
| { | ||
| try | ||
| { | ||
| await HandleUnresolvedParametersAsync().ConfigureAwait(false); | ||
|
|
||
| logger.LogDebug("All unresolved parameters have been handled successfully."); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "Failed to handle unresolved parameters."); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private async Task ProcessParameterAsync(ParameterResource parameterResource) | ||
| { | ||
| try | ||
| { | ||
| var value = parameterResource.Value ?? ""; | ||
|
|
||
| await notificationService.PublishUpdateAsync(parameterResource, s => | ||
| { | ||
| return s with | ||
| { | ||
| Properties = s.Properties.SetResourceProperty(KnownProperties.Parameter.Value, value, parameterResource.Secret), | ||
| State = new(KnownResourceStates.Active, KnownResourceStateStyles.Success) | ||
| }; | ||
| }) | ||
| .ConfigureAwait(false); | ||
|
|
||
| parameterResource.WaitForValueTcs?.TrySetResult(value); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| // Missing parameter values throw a MissingParameterValueException. | ||
| if (interactionService.IsAvailable && ex is MissingParameterValueException) | ||
| { | ||
| // If interaction service is available, we can prompt the user to provide a value. | ||
| // Add the parameter to unresolved parameters list. | ||
| _unresolvedParameters.Add(parameterResource); | ||
|
|
||
| loggerService.GetLogger(parameterResource) | ||
| .LogWarning(ex, "Parameter resource {ResourceName} could not be initialized. Waiting for user input.", parameterResource.Name); | ||
| } | ||
| else | ||
| { | ||
| // If interaction service is not available, we log the error and set the state to error. | ||
| parameterResource.WaitForValueTcs?.TrySetException(ex); | ||
|
|
||
| loggerService.GetLogger(parameterResource) | ||
| .LogError(ex, "Failed to initialize parameter resource {ResourceName}.", parameterResource.Name); | ||
| } | ||
|
|
||
| var stateText = ex is MissingParameterValueException ? | ||
| "Value missing" : | ||
| "Error initializing parameter"; | ||
|
|
||
| await notificationService.PublishUpdateAsync(parameterResource, s => | ||
| { | ||
| return s with | ||
| { | ||
| State = new(stateText, KnownResourceStateStyles.Error), | ||
| Properties = s.Properties.SetResourceProperty(KnownProperties.Parameter.Value, ex.Message), | ||
| IsHidden = false | ||
| }; | ||
| }) | ||
| .ConfigureAwait(false); | ||
| } | ||
| } | ||
|
|
||
| // Internal for testing purposes. | ||
| private async Task HandleUnresolvedParametersAsync() | ||
| { | ||
| await HandleUnresolvedParametersAsync(_unresolvedParameters).ConfigureAwait(false); | ||
| } | ||
|
|
||
| // Internal for testing purposes - allows passing specific parameters to test. | ||
| internal async Task HandleUnresolvedParametersAsync(IList<ParameterResource> unresolvedParameters) | ||
| { | ||
| // This method will continue in a loop until all unresolved parameters are resolved. | ||
| while (unresolvedParameters.Count > 0) | ||
| { | ||
| // First we show a notification that there are unresolved parameters. | ||
| var result = await interactionService.PromptMessageBarAsync( | ||
| "Unresolved parameters", | ||
| "There are unresolved parameters that need to be set. Please provide values for them.", | ||
| new MessageBarInteractionOptions | ||
| { | ||
| Intent = MessageIntent.Warning, | ||
| PrimaryButtonText = "Enter values" | ||
| }) | ||
| .ConfigureAwait(false); | ||
|
|
||
| if (result.Data) | ||
| { | ||
| // Now we build up a new form base on the unresolved parameters. | ||
| var inputs = new List<InteractionInput>(); | ||
|
|
||
| foreach (var parameter in unresolvedParameters) | ||
| { | ||
| // Create an input for each unresolved parameter. | ||
| inputs.Add(new InteractionInput | ||
| { | ||
| InputType = parameter.Secret ? InputType.SecretText : InputType.Text, | ||
| Label = parameter.Name, | ||
| Placeholder = "Enter value for " + parameter.Name, | ||
| }); | ||
| } | ||
|
|
||
| var valuesPrompt = await interactionService.PromptInputsAsync( | ||
| "Set unresolved parameters", | ||
| "Please provide values for the unresolved parameters.", | ||
| inputs, | ||
| new InputsDialogInteractionOptions | ||
| { | ||
| PrimaryButtonText = "Save", | ||
| ShowDismiss = true | ||
| }) | ||
| .ConfigureAwait(false); | ||
|
|
||
| if (!valuesPrompt.Canceled) | ||
| { | ||
| // Iterate through the unresolved parameters and set their values based on user input. | ||
| for (var i = unresolvedParameters.Count - 1; i >= 0; i--) | ||
| { | ||
| var parameter = unresolvedParameters[i]; | ||
| var inputValue = valuesPrompt.Data[i].Value; | ||
|
|
||
| if (string.IsNullOrEmpty(inputValue)) | ||
| { | ||
| // If the input value is null, we skip this parameter. | ||
| continue; | ||
| } | ||
|
|
||
| parameter.WaitForValueTcs?.TrySetResult(inputValue); | ||
|
|
||
| // Update the parameter resource state to active with the provided value. | ||
| await notificationService.PublishUpdateAsync(parameter, s => | ||
| { | ||
| return s with | ||
| { | ||
| Properties = s.Properties.SetResourceProperty(KnownProperties.Parameter.Value, inputValue, parameter.Secret), | ||
| State = new(KnownResourceStates.Active, KnownResourceStateStyles.Success) | ||
| }; | ||
| }) | ||
| .ConfigureAwait(false); | ||
|
|
||
| // Remove the parameter from unresolved parameters list. | ||
| unresolvedParameters.RemoveAt(i); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
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.