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

Rename CommandType to Name on resource command types #6350

Merged
merged 2 commits into from
Oct 18, 2024
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
4 changes: 2 additions & 2 deletions playground/Stress/Stress.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

var serviceBuilder = builder.AddProject<Projects.Stress_ApiService>("stress-apiservice", launchProfileName: null);
serviceBuilder.WithCommand(
type: "icon-test",
name: "icon-test",
displayName: "Icon test",
executeCommand: (c) =>
{
return Task.FromResult(CommandResults.Success());
},
iconName: "CloudDatabase");
serviceBuilder.WithCommand(
type: "icon-test-highlighted",
name: "icon-test-highlighted",
displayName: "Icon test highlighted",
executeCommand: (c) =>
{
Expand Down
10 changes: 5 additions & 5 deletions src/Aspire.Dashboard/Model/ResourceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ public int Compare(ResourceViewModel? x, ResourceViewModel? y)
}
}

[DebuggerDisplay("CommandType = {CommandType}, DisplayName = {DisplayName}")]
[DebuggerDisplay("Name = {Name}, DisplayName = {DisplayName}")]
public sealed class CommandViewModel
{
private sealed record IconKey(string IconName, IconVariant IconVariant);
private static readonly ConcurrentDictionary<IconKey, CustomIcon?> s_iconCache = new();

public string CommandType { get; }
public string Name { get; }
public CommandViewModelState State { get; }
public string DisplayName { get; }
public string DisplayDescription { get; }
Expand All @@ -110,12 +110,12 @@ private sealed record IconKey(string IconName, IconVariant IconVariant);
public string IconName { get; }
public IconVariant IconVariant { get; }

public CommandViewModel(string commandType, CommandViewModelState state, string displayName, string displayDescription, string confirmationMessage, Value? parameter, bool isHighlighted, string iconName, IconVariant iconVariant)
public CommandViewModel(string name, CommandViewModelState state, string displayName, string displayDescription, string confirmationMessage, Value? parameter, bool isHighlighted, string iconName, IconVariant iconVariant)
{
ArgumentException.ThrowIfNullOrWhiteSpace(commandType);
ArgumentException.ThrowIfNullOrWhiteSpace(name);
ArgumentException.ThrowIfNullOrWhiteSpace(displayName);

CommandType = commandType;
Name = name;
State = state;
DisplayName = displayName;
DisplayDescription = displayDescription;
Expand Down
4 changes: 2 additions & 2 deletions src/Aspire.Dashboard/ResourceService/DashboardClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ public async Task<ResourceCommandResponseViewModel> ExecuteResourceCommandAsync(

var request = new ResourceCommandRequest()
{
CommandType = command.CommandType,
CommandName = command.Name,
Parameter = command.Parameter,
ResourceName = resourceName,
ResourceType = resourceType
Expand All @@ -544,7 +544,7 @@ public async Task<ResourceCommandResponseViewModel> ExecuteResourceCommandAsync(
}
catch (RpcException ex)
{
_logger.LogError(ex, "Error executing command \"{CommandType}\" on resource \"{ResourceName}\": {StatusCode}", command.CommandType, resourceName, ex.StatusCode);
_logger.LogError(ex, "Error executing command \"{CommandName}\" on resource \"{ResourceName}\": {StatusCode}", command.Name, resourceName, ex.StatusCode);

var errorMessage = ex.StatusCode == StatusCode.Unimplemented ? "Command not implemented" : "Unknown error. See logs for details";

Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Dashboard/ResourceService/Partials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ ImmutableArray<VolumeViewModel> GetVolumes()
ImmutableArray<CommandViewModel> GetCommands()
{
return Commands
.Select(c => new CommandViewModel(c.CommandType, MapState(c.State), c.DisplayName, c.DisplayDescription, c.ConfirmationMessage, c.Parameter, c.IsHighlighted, c.IconName, MapIconVariant(c.IconVariant)))
.Select(c => new CommandViewModel(c.Name, MapState(c.State), c.DisplayName, c.DisplayDescription, c.ConfirmationMessage, c.Parameter, c.IsHighlighted, c.IconName, MapIconVariant(c.IconVariant)))
.ToImmutableArray();
static CommandViewModelState MapState(ResourceCommandState state)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Aspire.Hosting.ApplicationModel;

internal static class CommandsConfigurationExtensions
{
internal const string StartType = "resource-start";
internal const string StopType = "resource-stop";
internal const string RestartType = "resource-restart";
internal const string StartCommandName = "resource-start";
internal const string StopCommandName = "resource-stop";
internal const string RestartCommandName = "resource-restart";

internal static void AddLifeCycleCommands(this IResource resource)
{
Expand All @@ -20,7 +20,7 @@ internal static void AddLifeCycleCommands(this IResource resource)
}

resource.Annotations.Add(new ResourceCommandAnnotation(
type: StartType,
name: StartCommandName,
displayName: "Start",
executeCommand: async context =>
{
Expand Down Expand Up @@ -52,7 +52,7 @@ internal static void AddLifeCycleCommands(this IResource resource)
isHighlighted: true));

resource.Annotations.Add(new ResourceCommandAnnotation(
type: StopType,
name: StopCommandName,
displayName: "Stop",
executeCommand: async context =>
{
Expand Down Expand Up @@ -84,7 +84,7 @@ internal static void AddLifeCycleCommands(this IResource resource)
isHighlighted: true));

resource.Annotations.Add(new ResourceCommandAnnotation(
type: RestartType,
name: RestartCommandName,
displayName: "Restart",
executeCommand: async context =>
{
Expand Down
4 changes: 2 additions & 2 deletions src/Aspire.Hosting/ApplicationModel/CustomResourceSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ internal void Deconstruct(out string name, out object? value, out bool isSensiti
/// <summary>
/// A snapshot of a resource command.
/// </summary>
/// <param name="Type">The type of command. The type uniquely identifies the command.</param>
/// <param name="Name">The name of command. The name uniquely identifies the command.</param>
/// <param name="State">The state of the command.</param>
/// <param name="DisplayName">The display name visible in UI for the command.</param>
/// <param name="DisplayDescription">
Expand All @@ -172,7 +172,7 @@ internal void Deconstruct(out string name, out object? value, out bool isSensiti
/// <param name="IconName">The icon name for the command. The name should be a valid FluentUI icon name. https://aka.ms/fluentui-system-icons</param>
/// <param name="IconVariant">The icon variant.</param>
/// <param name="IsHighlighted">A flag indicating whether the command is highlighted in the UI.</param>
public sealed record ResourceCommandSnapshot(string Type, ResourceCommandState State, string DisplayName, string? DisplayDescription, object? Parameter, string? ConfirmationMessage, string? IconName, IconVariant? IconVariant, bool IsHighlighted);
public sealed record ResourceCommandSnapshot(string Name, ResourceCommandState State, string DisplayName, string? DisplayDescription, object? Parameter, string? ConfirmationMessage, string? IconName, IconVariant? IconVariant, bool IsHighlighted);

/// <summary>
/// A report produced by a health check about a resource.
Expand Down
10 changes: 5 additions & 5 deletions src/Aspire.Hosting/ApplicationModel/ResourceCommandAnnotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class ResourceCommandAnnotation : IResourceAnnotation
/// Initializes a new instance of the <see cref="ResourceCommandAnnotation"/> class.
/// </summary>
public ResourceCommandAnnotation(
string type,
string name,
string displayName,
Func<UpdateCommandStateContext, ResourceCommandState> updateState,
Func<ExecuteCommandContext, Task<ExecuteCommandResult>> executeCommand,
Expand All @@ -26,12 +26,12 @@ public ResourceCommandAnnotation(
IconVariant? iconVariant,
bool isHighlighted)
{
ArgumentNullException.ThrowIfNull(type);
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(displayName);
ArgumentNullException.ThrowIfNull(updateState);
ArgumentNullException.ThrowIfNull(executeCommand);

Type = type;
Name = name;
DisplayName = displayName;
UpdateState = updateState;
ExecuteCommand = executeCommand;
Expand All @@ -44,9 +44,9 @@ public ResourceCommandAnnotation(
}

/// <summary>
/// The type of command. The type uniquely identifies the command.
/// The name of command. The name uniquely identifies the command.
/// </summary>
public string Type { get; }
public string Name { get; }

/// <summary>
/// The display name visible in UI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ private CustomResourceSnapshot UpdateCommands(IResource resource, CustomResource

foreach (var annotation in resource.Annotations.OfType<ResourceCommandAnnotation>())
{
var existingCommand = FindByType(previousState.Commands, annotation.Type);
var existingCommand = FindByName(previousState.Commands, annotation.Name);

if (existingCommand == null)
{
Expand Down Expand Up @@ -457,11 +457,11 @@ private CustomResourceSnapshot UpdateCommands(IResource resource, CustomResource

return previousState with { Commands = builder.ToImmutable() };

static ResourceCommandSnapshot? FindByType(ImmutableArray<ResourceCommandSnapshot> commands, string type)
static ResourceCommandSnapshot? FindByName(ImmutableArray<ResourceCommandSnapshot> commands, string name)
{
for (var i = 0; i < commands.Length; i++)
{
if (commands[i].Type == type)
if (commands[i].Name == name)
{
return commands[i];
}
Expand All @@ -474,7 +474,7 @@ static ResourceCommandSnapshot CreateCommandFromAnnotation(ResourceCommandAnnota
{
var state = annotation.UpdateState(new UpdateCommandStateContext { ResourceSnapshot = previousState, ServiceProvider = serviceProvider });

return new ResourceCommandSnapshot(annotation.Type, state, annotation.DisplayName, annotation.DisplayDescription, annotation.Parameter, annotation.ConfirmationMessage, annotation.IconName, annotation.IconVariant, annotation.IsHighlighted);
return new ResourceCommandSnapshot(annotation.Name, state, annotation.DisplayName, annotation.DisplayDescription, annotation.Parameter, annotation.ConfirmationMessage, annotation.IconName, annotation.IconVariant, annotation.IsHighlighted);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Hosting/Dashboard/DashboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async Task WatchResourceConsoleLogsInternal(CancellationToken cancellationToken)

public override async Task<ResourceCommandResponse> ExecuteResourceCommand(ResourceCommandRequest request, ServerCallContext context)
{
var (result, errorMessage) = await serviceData.ExecuteCommandAsync(request.ResourceName, request.CommandType, context.CancellationToken).ConfigureAwait(false);
var (result, errorMessage) = await serviceData.ExecuteCommandAsync(request.ResourceName, request.CommandName, context.CancellationToken).ConfigureAwait(false);
var responseKind = result switch
{
DashboardServiceData.ExecuteCommandResult.Success => ResourceCommandResponseKind.Succeeded,
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Hosting/Dashboard/DashboardServiceData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public async ValueTask DisposeAsync()
logger.LogInformation("Executing command '{Type}'.", type);
if (_resourcePublisher.TryGetResource(resourceId, out _, out var resource))
{
var annotation = resource.Annotations.OfType<ResourceCommandAnnotation>().SingleOrDefault(a => a.Type == type);
var annotation = resource.Annotations.OfType<ResourceCommandAnnotation>().SingleOrDefault(a => a.Name == type);
if (annotation != null)
{
try
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Hosting/Dashboard/proto/Partials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static Resource FromSnapshot(ResourceSnapshot snapshot)

foreach (var command in snapshot.Commands)
{
resource.Commands.Add(new ResourceCommand { CommandType = command.Type, DisplayName = command.DisplayName, DisplayDescription = command.DisplayDescription ?? string.Empty, Parameter = ResourceSnapshot.ConvertToValue(command.Parameter), ConfirmationMessage = command.ConfirmationMessage ?? string.Empty, IconName = command.IconName ?? string.Empty, IconVariant = MapIconVariant(command.IconVariant), IsHighlighted = command.IsHighlighted, State = MapCommandState(command.State) });
resource.Commands.Add(new ResourceCommand { Name = command.Name, DisplayName = command.DisplayName, DisplayDescription = command.DisplayDescription ?? string.Empty, Parameter = ResourceSnapshot.ConvertToValue(command.Parameter), ConfirmationMessage = command.ConfirmationMessage ?? string.Empty, IconName = command.IconName ?? string.Empty, IconVariant = MapIconVariant(command.IconVariant), IsHighlighted = command.IsHighlighted, State = MapCommandState(command.State) });
}

if (snapshot.HealthStatus is not null)
Expand Down
4 changes: 2 additions & 2 deletions src/Aspire.Hosting/Dashboard/proto/resource_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ message ApplicationInformationResponse {
// using data from this message.
message ResourceCommand {
// Unique identifier for the command. Not intended for display.
string command_type = 1;
string name = 1;
// The display name of the command, to be shown in the UI. May be localized.
string display_name = 2;
// When present, this message must be shown to the user and their confirmation obtained
Expand Down Expand Up @@ -64,7 +64,7 @@ enum IconVariant {
message ResourceCommandRequest {
// Unique identifier for the command.
// Copied from the ResourceCommand that this request object is initialized from.
string command_type = 1;
string command_name = 1;
// The name of the resource to apply the command to. Matches Resource.name.
// Copied from the ResourceCommand that this request object is initialized from.
string resource_name = 2;
Expand Down
Loading
Loading