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

NO MERGE: Open browser tab from command #6601

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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: 4 additions & 0 deletions src/Aspire.Dashboard/Components/Pages/Resources.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ private async Task ExecuteResourceCommandAsync(ResourceViewModel resource, Comma
toastParameters.Intent = ToastIntent.Success;
toastParameters.Icon = GetIntentIcon(ToastIntent.Success);
}
else if (response.Kind == ResourceCommandResponseKind.Action && response.ActionKind == ResourceCommandResponseActionKind.OpenExternal)
{
await JS.InvokeVoidAsync("window.open", response.Url);
}
else
{
toastParameters.Title = string.Format(CultureInfo.InvariantCulture, Loc[nameof(Dashboard.Resources.Resources.ResourceCommandFailed)], messageResourceName, command.DisplayName);
Expand Down
13 changes: 12 additions & 1 deletion src/Aspire.Dashboard/Model/ResourceCommandResponseViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace Aspire.Dashboard.Model;
public class ResourceCommandResponseViewModel
{
public required ResourceCommandResponseKind Kind { get; init; }

public ResourceCommandResponseActionKind? ActionKind { get; init; }

public string? Url { get; init; }

public string? ErrorMessage { get; init; }
}

Expand All @@ -15,5 +20,11 @@ public enum ResourceCommandResponseKind
Undefined = 0,
Succeeded = 1,
Failed = 2,
Cancelled = 3
Cancelled = 3,
Action = 4
}

public enum ResourceCommandResponseActionKind
{
OpenExternal = 0
}
10 changes: 6 additions & 4 deletions src/Aspire.Dashboard/ResourceService/Partials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ partial class ResourceCommandResponse
public ResourceCommandResponseViewModel ToViewModel()
{
return new ResourceCommandResponseViewModel()
{
ErrorMessage = ErrorMessage,
Kind = (Dashboard.Model.ResourceCommandResponseKind)Kind
};
{
ActionKind = (Dashboard.Model.ResourceCommandResponseActionKind)ActionKind,
ErrorMessage = ErrorMessage,
Kind = (Dashboard.Model.ResourceCommandResponseKind)Kind,
Url = Url ?? string.Empty
};
}
}
25 changes: 25 additions & 0 deletions src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ public static IResourceBuilder<T> WithPgAdmin<T>(this IResourceBuilder<T> builde
{
var builderForExistingResource = builder.ApplicationBuilder.CreateResourceBuilder(existingPgAdminResource);
configureContainer?.Invoke(builderForExistingResource);

builder.WithCommand("open-pgadmin", "Open PgAdmin", (context) => {
var endpoint = builderForExistingResource.GetEndpoint("http");

var result = new OpenExternalExecuteCommandResult()
{
Success = true,
Url = endpoint.Url
};

return Task.FromResult((ExecuteCommandResult)result);
});

return builder;
}
else
Expand All @@ -155,6 +168,18 @@ public static IResourceBuilder<T> WithPgAdmin<T>(this IResourceBuilder<T> builde
.WithHttpHealthCheck("/browser")
.ExcludeFromManifest();

builder.WithCommand("open-pgadmin", "Open PgAdmin", (context) => {
var endpoint = pgAdminContainerBuilder.GetEndpoint("http");

var result = new OpenExternalExecuteCommandResult()
{
Success = true,
Url = endpoint.Url
};

return Task.FromResult((ExecuteCommandResult)result);
});

builder.ApplicationBuilder.Eventing.Subscribe<AfterEndpointsAllocatedEvent>((e, ct) =>
{
var serverFileMount = pgAdminContainer.Annotations.OfType<ContainerMountAnnotation>().Single(v => v.Target == "/pgadmin4/servers.json");
Expand Down
20 changes: 19 additions & 1 deletion src/Aspire.Hosting/ApplicationModel/ResourceCommandAnnotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static class CommandResults
/// <summary>
/// The result of executing a command. Returned from <see cref="ResourceCommandAnnotation.ExecuteCommand"/>.
/// </summary>
public sealed class ExecuteCommandResult
public class ExecuteCommandResult
{
/// <summary>
/// A flag that indicates whether the command was successful.
Expand All @@ -141,6 +141,24 @@ public sealed class ExecuteCommandResult
public string? ErrorMessage { get; init; }
}

/// <summary>
/// TODO:
/// </summary>
public sealed class OpenExternalExecuteCommandResult : ExecuteCommandResult
{
/// <summary>
/// TODO
/// </summary>
public required string Url { get; init; }
}

/// <summary>
/// TODO:
/// </summary>
public sealed class CancelledExecuteCommandResult : ExecuteCommandResult
{
}

/// <summary>
/// Context for <see cref="ResourceCommandAnnotation.UpdateState"/>.
/// </summary>
Expand Down
24 changes: 10 additions & 14 deletions src/Aspire.Hosting/Dashboard/DashboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.RegularExpressions;
using Aspire.Hosting.ApplicationModel;
using Aspire.ResourceService.Proto.V1;
using Grpc.Core;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -68,7 +69,7 @@ async Task WatchResourcesInternal(CancellationToken cancellationToken)

foreach (var resource in initialData)
{
data.Resources.Add(Resource.FromSnapshot(resource));
data.Resources.Add(Aspire.ResourceService.Proto.V1.Resource.FromSnapshot(resource));
}

await responseStream.WriteAsync(new() { InitialData = data }, cancellationToken).ConfigureAwait(false);
Expand All @@ -83,7 +84,7 @@ async Task WatchResourcesInternal(CancellationToken cancellationToken)

if (update.ChangeType is ResourceSnapshotChangeType.Upsert)
{
change.Upsert = Resource.FromSnapshot(update.Resource);
change.Upsert = Aspire.ResourceService.Proto.V1.Resource.FromSnapshot(update.Resource);
}
else if (update.ChangeType is ResourceSnapshotChangeType.Delete)
{
Expand Down Expand Up @@ -160,19 +161,14 @@ async Task WatchResourceConsoleLogsInternal(CancellationToken cancellationToken)

public override async Task<ResourceCommandResponse> ExecuteResourceCommand(ResourceCommandRequest request, ServerCallContext context)
{
var (result, errorMessage) = await serviceData.ExecuteCommandAsync(request.ResourceName, request.CommandName, context.CancellationToken).ConfigureAwait(false);
var responseKind = result switch
var result = await serviceData.ExecuteCommandAsync(request.ResourceName, request.CommandName, context.CancellationToken).ConfigureAwait(false);
return result switch
{
DashboardServiceData.ExecuteCommandResult.Success => ResourceCommandResponseKind.Succeeded,
DashboardServiceData.ExecuteCommandResult.Canceled => ResourceCommandResponseKind.Cancelled,
DashboardServiceData.ExecuteCommandResult.Failure => ResourceCommandResponseKind.Failed,
_ => ResourceCommandResponseKind.Undefined
};

return new ResourceCommandResponse
{
Kind = responseKind,
ErrorMessage = errorMessage ?? string.Empty
OpenExternalExecuteCommandResult openExternal => new ResourceCommandResponse { Kind = ResourceCommandResponseKind.Action, ActionKind = ResourceCommandResponseActionKind.OpenExternal, Url = openExternal.Url },
CancelledExecuteCommandResult => new ResourceCommandResponse { Kind = ResourceCommandResponseKind.Cancelled },
{ Success: true } => new ResourceCommandResponse { Kind = ResourceCommandResponseKind.Succeeded },
{ Success: false } => new ResourceCommandResponse { Kind = ResourceCommandResponseKind.Failed, ErrorMessage = result.ErrorMessage ?? string.Empty },
_ => new ResourceCommandResponse { Kind = ResourceCommandResponseKind.Undefined }
};
}

Expand Down
24 changes: 12 additions & 12 deletions src/Aspire.Hosting/Dashboard/DashboardServiceData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void Dispose()
_cts.Dispose();
}

internal async Task<(ExecuteCommandResult result, string? errorMessage)> ExecuteCommandAsync(string resourceId, string type, CancellationToken cancellationToken)
internal async Task<ExecuteCommandResult> ExecuteCommandAsync(string resourceId, string type, CancellationToken cancellationToken)
{
var logger = _resourceLoggerService.GetLogger(resourceId);

Expand All @@ -100,31 +100,31 @@ public void Dispose()
if (result.Success)
{
logger.LogInformation("Successfully executed command '{Type}'.", type);
return (ExecuteCommandResult.Success, null);
return result;
}
else
{
logger.LogInformation("Failure executed command '{Type}'. Error message: {ErrorMessage}", type, result.ErrorMessage);
return (ExecuteCommandResult.Failure, result.ErrorMessage);
return result;
}
}
catch (Exception ex)
{
logger.LogError(ex, "Error executing command '{Type}'.", type);
return (ExecuteCommandResult.Failure, "Unhandled exception thrown.");
return new ExecuteCommandResult
{
Success = false,
ErrorMessage = ex.Message
};
}
}
}

logger.LogInformation("Command '{Type}' not available.", type);
return (ExecuteCommandResult.Canceled, null);
}

internal enum ExecuteCommandResult
{
Success,
Failure,
Canceled
return new CancelledExecuteCommandResult
{
Success = false
};
}

internal ResourceSnapshotSubscription SubscribeResources()
Expand Down
7 changes: 7 additions & 0 deletions src/Aspire.Hosting/Dashboard/proto/resource_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,18 @@ enum ResourceCommandResponseKind {
SUCCEEDED = 1;
FAILED = 2;
CANCELLED = 3;
ACTION = 4;
}

enum ResourceCommandResponseActionKind {
OPEN_EXTERNAL = 0;
}

message ResourceCommandResponse {
ResourceCommandResponseKind kind = 1;
optional string error_message = 2;
optional ResourceCommandResponseActionKind actionKind = 3;
optional string url = 4;
}

////////////////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions src/Aspire.Hosting/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#nullable enable
Aspire.Hosting.ApplicationModel.CancelledExecuteCommandResult
Aspire.Hosting.ApplicationModel.CancelledExecuteCommandResult.CancelledExecuteCommandResult() -> void
Aspire.Hosting.ApplicationModel.ContainerLifetime.Session = 0 -> Aspire.Hosting.ApplicationModel.ContainerLifetime
Aspire.Hosting.ApplicationModel.CustomResourceSnapshot.HealthStatus.get -> Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus?
Aspire.Hosting.ApplicationModel.DockerfileBuildAnnotation
Expand Down Expand Up @@ -84,6 +86,10 @@ Aspire.Hosting.ApplicationModel.HealthCheckAnnotation
Aspire.Hosting.ApplicationModel.HealthCheckAnnotation.HealthCheckAnnotation(string! key) -> void
Aspire.Hosting.ApplicationModel.HealthCheckAnnotation.Key.get -> string!
Aspire.Hosting.ApplicationModel.IResourceWithWaitSupport
Aspire.Hosting.ApplicationModel.OpenExternalExecuteCommandResult
Aspire.Hosting.ApplicationModel.OpenExternalExecuteCommandResult.OpenExternalExecuteCommandResult() -> void
Aspire.Hosting.ApplicationModel.OpenExternalExecuteCommandResult.Url.get -> string!
Aspire.Hosting.ApplicationModel.OpenExternalExecuteCommandResult.Url.init -> void
Aspire.Hosting.ApplicationModel.ResourceCommandAnnotation.ConfirmationMessage.get -> string?
Aspire.Hosting.ApplicationModel.ResourceCommandAnnotation.DisplayDescription.get -> string?
Aspire.Hosting.ApplicationModel.ResourceCommandAnnotation.Name.get -> string!
Expand Down