Files
Pulse/internal/api/resources_compat.go
rcourtman 876ce14ab7 Keep action refusals current on websocket rows
A Docker page open while a host's agent disconnected kept Stop and Restart
enabled, and the click then failed at plan time. After the agent came back,
the old refusal stayed until the page remounted. Only a REST read carried
refusals, and a mounted Docker page reads REST once. The websocket merge
kept that REST answer, so nothing overwrote it; it just never changed.

Every broadcast now runs the same availability check, and a refused row
carries a ref into a new actionReadinessCatalog. A row with no ref refuses
nothing, so the store and the merge take the row's own answer and a refusal
ends with the frame that ends it. A first paint from the websocket is right
too, which closes the moment after in-app navigation when refused actions
looked enabled.

Broadcast rows keep capabilities whole. The drawer's operator policy form
needs what a disconnected host offers, and a stable set means a disconnect
moves one ref per row.

A check runs per capability per resource, host and token lookups scan every
session, and each session check takes config.Mu, so one pass now answers each
lookup once. Planning and dispatch still read live. Refusals leave Detail off
the wire: it names one host's identity and would give every host its own
catalog entry.
2026-11-04 21:22:02 +00:00

171 lines
6.3 KiB
Go

package api
import (
"context"
"net/http"
"strings"
"sync/atomic"
"github.com/rcourtman/pulse-go-rewrite/internal/actionlifecycle"
"github.com/rcourtman/pulse-go-rewrite/internal/api/apicontext"
"github.com/rcourtman/pulse-go-rewrite/internal/api/resourceapi"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
unified "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
"github.com/rcourtman/pulse-go-rewrite/pkg/auth"
)
// ResourceHandlers preserves the established router and extension surface
// while composing the resource-query service with API-owned action mutations.
// QueryService owns resource registry construction, durable resource storage,
// filtering, projections, and read handlers. The fields below remain here
// because action lifecycle and operator-state mutations are separate domains.
type ResourceHandlers struct {
resourceapi.QueryService
cfg *config.Config
tenantStateProvider TenantStateProvider
actionExecutor ActionExecutor
broadcastAvailability atomic.Pointer[broadcastAvailabilityChecker]
actionCompleted func(unified.ActionAuditRecord)
actionTransition func(orgID string, record unified.ActionAuditRecord)
policyAdmission *actionlifecycle.PolicyAdmissionCoordinator
actionEmergencyStop func(orgID string) (bool, error)
actionDecisionAuthorizer actionlifecycle.DecisionAuthorizer
actionExecutionAuthorizer actionlifecycle.ExecutionAuthorizer
actionRefreshPlanner actionlifecycle.RefreshPlanner
operatorStateChanged func(orgID, resourceID string)
}
type SupplementalRecordsProvider = resourceapi.SupplementalRecordsProvider
type TenantSupplementalRecordsProvider = resourceapi.TenantSupplementalRecordsProvider
type SupplementalSnapshotSourceOwner = resourceapi.SupplementalSnapshotSourceOwner
type TenantSupplementalSnapshotSourceOwner = resourceapi.TenantSupplementalSnapshotSourceOwner
type ResourcesResponse = resourceapi.ResourcesResponse
type ResourcesMeta = resourceapi.ResourcesMeta
type StorageSummaryResponse = resourceapi.StorageSummaryResponse
type StorageSummaryIncident = resourceapi.StorageSummaryIncident
type StorageIncidentsResponse = resourceapi.StorageIncidentsResponse
type StorageIncidentSection = resourceapi.StorageIncidentSection
func EmptyResourcesResponse() ResourcesResponse { return resourceapi.EmptyResourcesResponse() }
func EmptyStorageSummaryResponse() StorageSummaryResponse {
return resourceapi.EmptyStorageSummaryResponse()
}
func EmptyStorageIncidentsResponse() StorageIncidentsResponse {
return resourceapi.EmptyStorageIncidentsResponse()
}
// NewResourceHandlers builds the compatibility composition used by Router.
func NewResourceHandlers(cfg *config.Config) *ResourceHandlers {
return &ResourceHandlers{
QueryService: *resourceapi.NewQueryService(cfg),
cfg: cfg,
policyAdmission: &actionlifecycle.PolicyAdmissionCoordinator{},
}
}
func (h *ResourceHandlers) ensureQueryService() *resourceapi.QueryService {
return &h.QueryService
}
func (h *ResourceHandlers) SetStateProvider(provider SnapshotProvider) {
h.ensureQueryService().SetStateProvider(provider)
}
func (h *ResourceHandlers) SetTenantStateProvider(provider TenantStateProvider) {
h.tenantStateProvider = provider
h.ensureQueryService().SetTenantStateProvider(provider)
}
func (h *ResourceHandlers) SetActionExecutor(executor ActionExecutor) {
h.actionExecutor = executor
checker, _ := executor.(actionlifecycle.AvailabilityChecker)
h.ensureQueryService().SetActionAvailabilityChecker(checker)
h.broadcastAvailability.Store(&broadcastAvailabilityChecker{checker: checker})
}
// broadcastAvailabilityChecker lets monitor goroutines, which can tick before
// the router finishes wiring its executors, read the checker without a race.
type broadcastAvailabilityChecker struct {
checker actionlifecycle.AvailabilityChecker
}
// AttachBroadcastActionRefusals records the server's action refusals on one
// organization's broadcast rows, so a browser learns of a refusal, or of its
// end, from the next websocket frame and not from its next REST read.
func (h *ResourceHandlers) AttachBroadcastActionRefusals(orgID string, resources []unified.Resource) {
holder := h.broadcastAvailability.Load()
if holder == nil || holder.checker == nil {
return
}
ctx := context.WithValue(context.Background(), apicontext.OrgIDContextKey, orgID)
actionlifecycle.AttachActionRefusals(ctx, holder.checker, resources)
}
func (h *ResourceHandlers) SetActionEmergencyStopChecker(checker func(orgID string) (bool, error)) {
h.actionEmergencyStop = checker
}
func (h *ResourceHandlers) SetActionAuthorizers(decision actionlifecycle.DecisionAuthorizer, execution actionlifecycle.ExecutionAuthorizer) {
h.actionDecisionAuthorizer = decision
h.actionExecutionAuthorizer = execution
}
func (h *ResourceHandlers) SetActionRefreshPlanner(planner actionlifecycle.RefreshPlanner) {
h.actionRefreshPlanner = planner
}
func (h *ResourceHandlers) SetActionCompletedPublisher(publisher func(unified.ActionAuditRecord)) {
h.actionCompleted = publisher
}
func (h *ResourceHandlers) SetActionTransitionPublisher(publisher func(orgID string, record unified.ActionAuditRecord)) {
h.actionTransition = publisher
}
func (h *ResourceHandlers) SetOperatorStateChanged(callback func(orgID, resourceID string)) {
h.operatorStateChanged = callback
}
func (h *ResourceHandlers) buildRegistry(orgID string) (*unified.ResourceRegistry, error) {
return h.ensureQueryService().BuildRegistry(orgID)
}
func (h *ResourceHandlers) getStore(orgID string) (unified.ResourceStore, error) {
return h.ensureQueryService().Store(orgID)
}
func (h *ResourceHandlers) invalidateCache(orgID string) {
h.ensureQueryService().InvalidateCache(orgID)
}
func (h *ResourceHandlers) CloseTenantStore(orgID string) error {
if h == nil {
return nil
}
return h.QueryService.CloseTenantStore(orgID)
}
func (h *ResourceHandlers) CloseStores() error {
if h == nil {
return nil
}
return h.QueryService.CloseStores()
}
// firstNonEmptyTrimmed is retained for non-resource connection projections.
func firstNonEmptyTrimmed(values ...string) string {
for _, value := range values {
if trimmed := strings.TrimSpace(value); trimmed != "" {
return trimmed
}
}
return ""
}
func getUserID(r *http.Request) string {
return auth.GetUser(r.Context())
}