From 1719c2eb7f11b514387c43c82905913cccaccc4f Mon Sep 17 00:00:00 2001 From: Phillip Hoff Date: Mon, 14 Jun 2021 11:50:58 -0700 Subject: [PATCH] Add endpoint for Tye application metadata (#1079) * Sketch GET for control plane. * Add properties to control plane metadata. * Resolve linter warnings. * Switch to /application endpoint. * Change ID to Id. --- .../Model/Application.cs | 7 ++++++- .../Model/V1/V1Application.cs | 15 +++++++++++++ src/Microsoft.Tye.Hosting/TyeDashboardApi.cs | 21 ++++++++++++++++++- src/tye/ApplicationBuilderExtensions.cs | 2 +- test/Test.Infrastructure/TestHelpers.cs | 4 ++-- 5 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/Microsoft.Tye.Hosting/Model/V1/V1Application.cs diff --git a/src/Microsoft.Tye.Hosting/Model/Application.cs b/src/Microsoft.Tye.Hosting/Model/Application.cs index c91ade50..d88236cf 100644 --- a/src/Microsoft.Tye.Hosting/Model/Application.cs +++ b/src/Microsoft.Tye.Hosting/Model/Application.cs @@ -12,14 +12,19 @@ namespace Microsoft.Tye.Hosting.Model { public class Application { - public Application(FileInfo source, Dictionary services, ContainerEngine containerEngine) + public Application(string name, FileInfo source, Dictionary services, ContainerEngine containerEngine) { + Name = name; Source = source.FullName; ContextDirectory = source.DirectoryName!; Services = services; ContainerEngine = containerEngine; } + public string Id { get; } = Guid.NewGuid().ToString(); + + public string Name { get; } + public string Source { get; } public string ContextDirectory { get; } diff --git a/src/Microsoft.Tye.Hosting/Model/V1/V1Application.cs b/src/Microsoft.Tye.Hosting/Model/V1/V1Application.cs new file mode 100644 index 00000000..b646dbb3 --- /dev/null +++ b/src/Microsoft.Tye.Hosting/Model/V1/V1Application.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.Tye.Hosting.Model.V1 +{ + public class V1Application + { + public string? Id { get; set; } + + public string? Name { get; set; } + + public string? Source { get; set; } + } +} diff --git a/src/Microsoft.Tye.Hosting/TyeDashboardApi.cs b/src/Microsoft.Tye.Hosting/TyeDashboardApi.cs index 64a06f28..8c454a36 100644 --- a/src/Microsoft.Tye.Hosting/TyeDashboardApi.cs +++ b/src/Microsoft.Tye.Hosting/TyeDashboardApi.cs @@ -37,6 +37,7 @@ namespace Microsoft.Tye.Hosting public void MapRoutes(IEndpointRouteBuilder endpoints) { endpoints.MapGet("/api/v1", ServiceIndex); + endpoints.MapGet("/api/v1/application", ApplicationIndex); endpoints.MapDelete("/api/v1/control", ControlPlaneShutdown); endpoints.MapGet("/api/v1/services", Services); endpoints.MapGet("/api/v1/services/{name}", Service); @@ -50,15 +51,33 @@ namespace Microsoft.Tye.Hosting context.Response.ContentType = "application/json"; return JsonSerializer.SerializeAsync(context.Response.Body, new[] { + $"{context.Request.Scheme}://{context.Request.Host}/api/v1/application", $"{context.Request.Scheme}://{context.Request.Host}/api/v1/control", - $"{context.Request.Scheme}://{context.Request.Host}/api/v1/services", $"{context.Request.Scheme}://{context.Request.Host}/api/v1/logs/{{service}}", $"{context.Request.Scheme}://{context.Request.Host}/api/v1/metrics", $"{context.Request.Scheme}://{context.Request.Host}/api/v1/metrics/{{service}}", + $"{context.Request.Scheme}://{context.Request.Host}/api/v1/services", }, _options); } + private Task ApplicationIndex(HttpContext context) + { + var app = context.RequestServices.GetRequiredService(); + + context.Response.ContentType = "application/json"; + + return JsonSerializer.SerializeAsync( + context.Response.Body, + new V1Application + { + Id = app.Id, + Name = app.Name, + Source = app.Source + }, + _options); + } + private Task ControlPlaneShutdown(HttpContext context) { var lifetime = context.RequestServices.GetRequiredService(); diff --git a/src/tye/ApplicationBuilderExtensions.cs b/src/tye/ApplicationBuilderExtensions.cs index 3015564e..a3bab007 100644 --- a/src/tye/ApplicationBuilderExtensions.cs +++ b/src/tye/ApplicationBuilderExtensions.cs @@ -213,7 +213,7 @@ namespace Microsoft.Tye services.Add(ingress.Name, new Service(description)); } - return new Application(application.Source, services, application.ContainerEngine) { Network = application.Network }; + return new Application(application.Name, application.Source, services, application.ContainerEngine) { Network = application.Network }; } public static Tye.Hosting.Model.EnvironmentVariable ToHostingEnvironmentVariable(this EnvironmentVariableBuilder builder) diff --git a/test/Test.Infrastructure/TestHelpers.cs b/test/Test.Infrastructure/TestHelpers.cs index df520f1b..bf4d3e5d 100644 --- a/test/Test.Infrastructure/TestHelpers.cs +++ b/test/Test.Infrastructure/TestHelpers.cs @@ -251,8 +251,8 @@ namespace Test.Infrastructure var processRunner = new ProcessRunner(logger, replicaRegistry, new ProcessRunnerOptions()); var dockerRunner = new DockerRunner(logger, replicaRegistry); - await processRunner.StartAsync(new Application(new FileInfo(host.Application.Source), new Dictionary(), ContainerEngine.Default)); - await dockerRunner.StartAsync(new Application(new FileInfo(host.Application.Source), new Dictionary(), ContainerEngine.Default)); + await processRunner.StartAsync(new Application(host.Application.Name, new FileInfo(host.Application.Source), new Dictionary(), ContainerEngine.Default)); + await dockerRunner.StartAsync(new Application(host.Application.Name, new FileInfo(host.Application.Source), new Dictionary(), ContainerEngine.Default)); } await DoOperationAndWaitForReplicasToChangeState(host, ReplicaState.Stopped, replicas.Length, replicas.ToHashSet(), new HashSet(), TimeSpan.Zero, Purge);