mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-09-10 10:35:51 +00:00
478a9e933d
Preserve tenant-scoped metadata through partial URL updates and project stable URLs across runtime identities. Use a safe adjacent launch control across overview tables with desktop and mobile regression coverage.
257 lines
8.3 KiB
Go
257 lines
8.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
const (
|
|
dockerRuntimeMetadataCollectionPath = "/api/docker/runtimes/metadata"
|
|
dockerRuntimeMetadataPathPrefix = "/api/docker/runtimes/metadata/"
|
|
)
|
|
|
|
func mergeDockerMetadataPatch(
|
|
id string,
|
|
existing,
|
|
incoming *config.DockerMetadata,
|
|
fields map[string]json.RawMessage,
|
|
) *config.DockerMetadata {
|
|
result := &config.DockerMetadata{ID: id}
|
|
if existing != nil {
|
|
*result = *existing
|
|
result.Tags = cloneStringSlice(existing.Tags)
|
|
result.Notes = cloneStringSlice(existing.Notes)
|
|
}
|
|
result.ID = id
|
|
if metadataPatchHasField(fields, "customUrl") {
|
|
result.CustomURL = incoming.CustomURL
|
|
}
|
|
if metadataPatchHasField(fields, "description") {
|
|
result.Description = incoming.Description
|
|
}
|
|
if metadataPatchHasField(fields, "tags") {
|
|
result.Tags = cloneStringSlice(incoming.Tags)
|
|
}
|
|
if metadataPatchHasField(fields, "notes") {
|
|
result.Notes = cloneStringSlice(incoming.Notes)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// DockerMetadataHandler handles Docker resource metadata operations
|
|
type DockerMetadataHandler struct {
|
|
mtPersistence *config.MultiTenantPersistence
|
|
storeResolver func(context.Context) *config.DockerMetadataStore
|
|
}
|
|
|
|
// NewDockerMetadataHandler creates a new Docker metadata handler
|
|
func NewDockerMetadataHandler(mtPersistence *config.MultiTenantPersistence) *DockerMetadataHandler {
|
|
return &DockerMetadataHandler{
|
|
mtPersistence: mtPersistence,
|
|
}
|
|
}
|
|
|
|
// SetStoreResolver makes API reads and writes use the active monitor's store.
|
|
// The persistence-backed store remains the initialization/test fallback.
|
|
func (h *DockerMetadataHandler) SetStoreResolver(
|
|
resolver func(context.Context) *config.DockerMetadataStore,
|
|
) {
|
|
h.storeResolver = resolver
|
|
}
|
|
|
|
func (h *DockerMetadataHandler) getStore(ctx context.Context) *config.DockerMetadataStore {
|
|
if h != nil && h.storeResolver != nil {
|
|
if store := h.storeResolver(ctx); store != nil {
|
|
return store
|
|
}
|
|
}
|
|
orgID := "default"
|
|
if ctx != nil {
|
|
if requestOrgID := GetOrgID(ctx); requestOrgID != "" {
|
|
orgID = requestOrgID
|
|
}
|
|
}
|
|
p, _ := h.mtPersistence.GetPersistence(orgID)
|
|
return p.GetDockerMetadataStore()
|
|
}
|
|
|
|
// Store returns the underlying metadata store for default tenant
|
|
func (h *DockerMetadataHandler) Store() *config.DockerMetadataStore {
|
|
return h.getStore(context.Background())
|
|
}
|
|
|
|
// HandleGetMetadata retrieves metadata for a specific Docker resource or all resources
|
|
func (h *DockerMetadataHandler) HandleGetMetadata(w http.ResponseWriter, r *http.Request) {
|
|
handleMetadataGetRequest(w, r, "/api/docker/metadata",
|
|
func(ctx context.Context) map[string]*config.DockerMetadata { return h.getStore(ctx).GetAll() },
|
|
func(ctx context.Context, id string) *config.DockerMetadata { return h.getStore(ctx).Get(id) },
|
|
func(id string) *config.DockerMetadata { return &config.DockerMetadata{ID: id} },
|
|
)
|
|
}
|
|
|
|
// HandleUpdateMetadata updates metadata for a Docker resource
|
|
func (h *DockerMetadataHandler) HandleUpdateMetadata(w http.ResponseWriter, r *http.Request) {
|
|
handleMetadataUpdateRequest(w, r, "/api/docker/metadata",
|
|
"Resource ID required",
|
|
"resourceID",
|
|
"Failed to save Docker metadata",
|
|
"Updated Docker metadata",
|
|
func(meta *config.DockerMetadata) string { return meta.CustomURL },
|
|
func(ctx context.Context, id string) *config.DockerMetadata {
|
|
return h.getStore(ctx).Get(id)
|
|
},
|
|
mergeDockerMetadataPatch,
|
|
func(ctx context.Context, id string, meta *config.DockerMetadata) error {
|
|
return h.getStore(ctx).Set(id, meta)
|
|
},
|
|
)
|
|
}
|
|
|
|
// HandleDeleteMetadata removes metadata for a Docker resource
|
|
func (h *DockerMetadataHandler) HandleDeleteMetadata(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodDelete {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
resourceID := strings.TrimPrefix(r.URL.Path, "/api/docker/metadata/")
|
|
if resourceID == "" || resourceID == "metadata" {
|
|
http.Error(w, "Resource ID required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
store := h.getStore(r.Context())
|
|
if err := store.Delete(resourceID); err != nil {
|
|
log.Error().Err(err).Str("resourceID", resourceID).Msg("Failed to delete Docker metadata")
|
|
http.Error(w, "Failed to delete metadata", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
log.Info().Str("resourceID", resourceID).Msg("Deleted Docker metadata")
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// HandleGetRuntimeMetadata retrieves metadata for a Docker runtime or all runtimes.
|
|
func (h *DockerMetadataHandler) HandleGetRuntimeMetadata(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
// Check if requesting a specific runtime.
|
|
path := r.URL.Path
|
|
if path == dockerRuntimeMetadataCollectionPath || path == dockerRuntimeMetadataCollectionPath+"/" {
|
|
// Get all runtime metadata.
|
|
w.Header().Set("Content-Type", "application/json")
|
|
store := h.getStore(r.Context())
|
|
allMeta := store.GetAllHostMetadata()
|
|
if allMeta == nil {
|
|
// Return empty object instead of null.
|
|
json.NewEncoder(w).Encode(make(map[string]*config.DockerHostMetadata))
|
|
} else {
|
|
json.NewEncoder(w).Encode(allMeta)
|
|
}
|
|
return
|
|
}
|
|
|
|
// Get specific runtime ID from path.
|
|
runtimeID := strings.TrimPrefix(path, dockerRuntimeMetadataPathPrefix)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
if runtimeID != "" {
|
|
// Get specific runtime metadata.
|
|
store := h.getStore(r.Context())
|
|
meta := store.GetHostMetadata(runtimeID)
|
|
if meta == nil {
|
|
// Return empty metadata instead of 404.
|
|
json.NewEncoder(w).Encode(&config.DockerHostMetadata{})
|
|
} else {
|
|
json.NewEncoder(w).Encode(meta)
|
|
}
|
|
} else {
|
|
// This shouldn't happen with current routing, but handle it anyway.
|
|
http.Error(w, "Invalid request path", http.StatusBadRequest)
|
|
}
|
|
}
|
|
|
|
// HandleUpdateRuntimeMetadata updates metadata for a Docker / Podman host.
|
|
func (h *DockerMetadataHandler) HandleUpdateRuntimeMetadata(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPut && r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
runtimeID := strings.TrimPrefix(r.URL.Path, dockerRuntimeMetadataPathPrefix)
|
|
if runtimeID == "" || runtimeID == "metadata" {
|
|
http.Error(w, "Docker / Podman host ID required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var incoming config.DockerHostMetadata
|
|
fields, decoded := decodeBoundedMetadataPatch(w, r, &incoming)
|
|
if !decoded {
|
|
return
|
|
}
|
|
|
|
store := h.getStore(r.Context())
|
|
meta := store.GetHostMetadata(runtimeID)
|
|
if meta == nil {
|
|
meta = &config.DockerHostMetadata{}
|
|
}
|
|
if metadataPatchHasField(fields, "customDisplayName") {
|
|
meta.CustomDisplayName = incoming.CustomDisplayName
|
|
}
|
|
if metadataPatchHasField(fields, "customUrl") {
|
|
if errMsg := validateCustomURL(incoming.CustomURL); errMsg != "" {
|
|
http.Error(w, errMsg, http.StatusBadRequest)
|
|
return
|
|
}
|
|
meta.CustomURL = incoming.CustomURL
|
|
}
|
|
if metadataPatchHasField(fields, "notes") {
|
|
meta.Notes = cloneStringSlice(incoming.Notes)
|
|
}
|
|
|
|
if err := store.SetHostMetadata(runtimeID, meta); err != nil {
|
|
log.Error().Err(err).Str("runtimeID", runtimeID).Msg("Failed to save Docker / Podman host metadata")
|
|
http.Error(w, metadataSaveErrorMessage(err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
log.Info().Str("runtimeID", runtimeID).Str("url", meta.CustomURL).Msg("Updated Docker / Podman host metadata")
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(meta)
|
|
}
|
|
|
|
// HandleDeleteRuntimeMetadata removes metadata for a Docker / Podman host.
|
|
func (h *DockerMetadataHandler) HandleDeleteRuntimeMetadata(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodDelete {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
runtimeID := strings.TrimPrefix(r.URL.Path, dockerRuntimeMetadataPathPrefix)
|
|
if runtimeID == "" || runtimeID == "metadata" {
|
|
http.Error(w, "Docker / Podman host ID required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
store := h.getStore(r.Context())
|
|
if err := store.SetHostMetadata(runtimeID, nil); err != nil {
|
|
log.Error().Err(err).Str("runtimeID", runtimeID).Msg("Failed to delete Docker / Podman host metadata")
|
|
http.Error(w, "Failed to delete metadata", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
log.Info().Str("runtimeID", runtimeID).Msg("Deleted Docker / Podman host metadata")
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|