Files
pulse/internal/api/docker_metadata.go
T
rcourtman 15dbabfe9a Preserve resource URLs across runtime identity changes
Contract-Neutral: Docker URL migration touches shared monitor ingest files without changing the host deletion or re-enrollment contract.
2026-07-23 22:33:07 +01:00

231 lines
7.7 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/"
)
// 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, 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
}
// Limit request body to 16KB to prevent memory exhaustion
r.Body = http.MaxBytesReader(w, r.Body, 16*1024)
var meta config.DockerHostMetadata
if err := json.NewDecoder(r.Body).Decode(&meta); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
// Validate URL if provided
if errMsg := validateCustomURL(meta.CustomURL); errMsg != "" {
http.Error(w, errMsg, http.StatusBadRequest)
return
}
// Get existing metadata to merge with new data
store := h.getStore(r.Context())
existing := store.GetHostMetadata(runtimeID)
if existing != nil {
// Merge: only update fields that are provided
if meta.CustomDisplayName != "" || existing.CustomDisplayName != "" {
if meta.CustomDisplayName == "" {
meta.CustomDisplayName = existing.CustomDisplayName
}
}
// CustomURL can be explicitly cleared, so we don't merge it unless updating
if meta.Notes == nil && existing.Notes != nil {
meta.Notes = existing.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)
}