Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Centralize resource change filter parsing

rcourtman6e10d9e1fcd5
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index e00b696169..122fb17747 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -106,6 +106,11 @@ fleet-continuity ownership instead of re-deriving resource history locally.
Invalid `sourceAdapter` values are rejected at the API boundary, so the fleet
lane continues to consume only the canonical adapter set rather than
introducing a broader compatibility escape hatch.
+That same API boundary now routes the `kind`, `sourceType`, and
+`sourceAdapter` query values through the shared unified-resource change
+filter parser, so the lifecycle lane keeps the transport contract aligned
+with the canonical resource-history model instead of rebuilding filter
+normalization locally.
The router now wires the tenant resource state provider during initial setup
when a multi-tenant monitor is present, so tenant-scoped fleet pages do not
trip a missing-provider 500 before the monitor has finished initializing.
diff --git a/docs/release-control/v6/internal/subsystems/api-contracts.md b/docs/release-control/v6/internal/subsystems/api-contracts.md
index e1f7b6a77f..0f7147da99 100644
--- a/docs/release-control/v6/internal/subsystems/api-contracts.md
+++ b/docs/release-control/v6/internal/subsystems/api-contracts.md
@@ -171,6 +171,10 @@ chips.
Invalid `sourceAdapter` values are rejected at the API boundary, so the filter
contract stays aligned with the canonical adapter set rather than silently
falling back to an empty slice.
+The same resource-timeline contract now also owns canonical parsing for
+`kind`, `sourceType`, and `sourceAdapter` query values, so the HTTP handler
+stays thin and the change model remains the source of truth for timeline
+filter validation.
The same API contract now also exposes the unified-resource control-plane
history through dedicated enterprise audit reads. The action, lifecycle, and
export history endpoints live in `internal/api/activity_audit_handlers.go` and
@@ -204,6 +208,10 @@ pages.
Those relationship and timeline payloads now also carry `lastSeenAt` freshness
and optional metadata through the same owned contract, so the drawer can
preserve provenance without inventing a separate graph-detail schema.
+The same `/api/resources/{id}/timeline` filter contract now also routes its
+kinds, source types, and source adapters through the shared unified-resource
+change-filter parser, so API validation stays owned by the change model rather
+than being re-parsed separately in the HTTP handler.
The tenant-scoped unified resource API now also stays on canonical
unified-resource seeds end to end: `internal/api/resources.go`,
`internal/api/router_helpers.go`, and `internal/api/state_provider.go` no
diff --git a/docs/release-control/v6/internal/subsystems/storage-recovery.md b/docs/release-control/v6/internal/subsystems/storage-recovery.md
index 465b9a4edc..e8620bdf1a 100644
--- a/docs/release-control/v6/internal/subsystems/storage-recovery.md
+++ b/docs/release-control/v6/internal/subsystems/storage-recovery.md
@@ -257,6 +257,14 @@ That same decode path now delegates policy string and redaction normalization
to `frontend-modern/src/utils/resourcePolicyNormalization.ts`, so storage and
recovery surfaces do not reimplement sensitivity, routing, or redaction
parsing locally.
+That same shared `internal/api/` dependency now also routes resource-timeline
+filters through the unified-resource change parser, so storage and recovery
+surfaces do not inherit a second local decoder for `kind`, `sourceType`, or
+`sourceAdapter` values.
+That same shared `internal/api/` dependency now also assumes the resource
+timeline parser is owned by unified resources, so storage and recovery
+surfaces rely on one canonical change-filter contract instead of re-decoding
+timeline query values in the handler layer.
That same shared `internal/api/` dependency now also assumes canonical install
payload URLs are slash-normalized before they become response fields or helper
attachments, so recovery-adjacent links and transport surfaces cannot inherit
diff --git a/docs/release-control/v6/internal/subsystems/unified-resources.md b/docs/release-control/v6/internal/subsystems/unified-resources.md
index 183ff4b21b..83ce98d436 100644
--- a/docs/release-control/v6/internal/subsystems/unified-resources.md
+++ b/docs/release-control/v6/internal/subsystems/unified-resources.md
@@ -139,6 +139,10 @@ The canonical unified-resource change and relationship presenters now also
share the same elapsed-time and "ago" wording utilities, so `observed`,
`last seen`, and `ago` fragments stay consistent without each formatter
maintaining its own "time ago" implementation.
+The same resource-change contract now also owns the canonical filter parser
+used by `/api/resources/{id}/timeline`, so `kind`, `sourceType`, and
+`sourceAdapter` validation stays with the change model instead of being
+reconstructed in the HTTP handler.
The canonical resource policy model also owns a shared clone helper, so AI
chat and tools consumers preserve policy metadata by copying through the same
unified-resource contract instead of maintaining their own deep-copy logic.
diff --git a/internal/api/contract_test.go b/internal/api/contract_test.go
index 8ced62991b..e9c867dd2b 100644
--- a/internal/api/contract_test.go
+++ b/internal/api/contract_test.go
@@ -6,7 +6,6 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
- "net/url"
"os"
"path/filepath"
"reflect"
@@ -4256,9 +4255,7 @@ func TestContract_ResourceFacetsJSONSnapshot(t *testing.T) {
}
func TestContract_ResourceTimelineRejectsInvalidSourceAdapter(t *testing.T) {
- _, err := parseResourceChangeFilters(url.Values{
- "sourceAdapter": []string{"unsupported_adapter"},
- })
+ _, err := unifiedresources.ParseResourceChangeFilters(nil, nil, []string{"unsupported_adapter"})
if err == nil {
t.Fatal("expected invalid sourceAdapter to be rejected")
}
diff --git a/internal/api/resources.go b/internal/api/resources.go
index 0c0f78159a..288be3a7a7 100644
--- a/internal/api/resources.go
+++ b/internal/api/resources.go
@@ -3,10 +3,8 @@ package api
import (
"encoding/json"
"errors"
- "fmt"
"io"
"net/http"
- "net/url"
"sort"
"strconv"
"strings"
@@ -289,103 +287,6 @@ type resourceFacetBundleResponse struct {
Counts resourceFacetCountsResponse `json:"counts"`
}
-func parseResourceChangeFilters(values url.Values) (unified.ResourceChangeFilters, error) {
- var filters unified.ResourceChangeFilters
- if kinds, err := parseResourceChangeKinds(values["kind"]); err != nil {
- return unified.ResourceChangeFilters{}, err
- } else {
- filters.Kinds = kinds
- }
- if sourceTypes, err := parseResourceChangeSourceTypes(values["sourceType"]); err != nil {
- return unified.ResourceChangeFilters{}, err
- } else {
- filters.SourceTypes = sourceTypes
- }
- if sourceAdapters, err := parseResourceChangeSourceAdapters(values["sourceAdapter"]); err != nil {
- return unified.ResourceChangeFilters{}, err
- } else {
- filters.SourceAdapters = sourceAdapters
- }
- return filters, nil
-}
-
-func parseResourceChangeKinds(values []string) ([]unified.ChangeKind, error) {
- parsed := make([]unified.ChangeKind, 0, len(values))
- for _, value := range values {
- for _, token := range strings.Split(value, ",") {
- switch normalized := strings.TrimSpace(strings.ToLower(token)); normalized {
- case "":
- continue
- case string(unified.ChangeStateTransition):
- parsed = append(parsed, unified.ChangeStateTransition)
- case string(unified.ChangeRestart):
- parsed = append(parsed, unified.ChangeRestart)
- case string(unified.ChangeConfigUpdate):
- parsed = append(parsed, unified.ChangeConfigUpdate)
- case string(unified.ChangeAnomaly):
- parsed = append(parsed, unified.ChangeAnomaly)
- case string(unified.ChangeRelationship):
- parsed = append(parsed, unified.ChangeRelationship)
- case string(unified.ChangeCapability):
- parsed = append(parsed, unified.ChangeCapability)
- default:
- return nil, fmt.Errorf("invalid kind value %q", token)
- }
- }
- }
- return parsed, nil
-}
-
-func parseResourceChangeSourceTypes(values []string) ([]unified.ChangeSourceType, error) {
- parsed := make([]unified.ChangeSourceType, 0, len(values))
- for _, value := range values {
- for _, token := range strings.Split(value, ",") {
- switch normalized := strings.TrimSpace(strings.ToLower(token)); normalized {
- case "":
- continue
- case string(unified.SourcePlatformEvent):
- parsed = append(parsed, unified.SourcePlatformEvent)
- case string(unified.SourcePulseDiff):
- parsed = append(parsed, unified.SourcePulseDiff)
- case string(unified.SourceHeuristic):
- parsed = append(parsed, unified.SourceHeuristic)
- case string(unified.SourceUserAction):
- parsed = append(parsed, unified.SourceUserAction)
- case string(unified.SourceAgentAction):
- parsed = append(parsed, unified.SourceAgentAction)
- default:
- return nil, fmt.Errorf("invalid sourceType value %q", token)
- }
- }
- }
- return parsed, nil
-}
-
-func parseResourceChangeSourceAdapters(values []string) ([]unified.ChangeSourceAdapter, error) {
- parsed := make([]unified.ChangeSourceAdapter, 0, len(values))
- for _, value := range values {
- for _, token := range strings.Split(value, ",") {
- normalized := strings.TrimSpace(strings.ToLower(token))
- if normalized == "" {
- continue
- }
- switch normalized {
- case string(unified.AdapterDocker):
- parsed = append(parsed, unified.AdapterDocker)
- case string(unified.AdapterProxmox):
- parsed = append(parsed, unified.AdapterProxmox)
- case string(unified.AdapterTrueNAS):
- parsed = append(parsed, unified.AdapterTrueNAS)
- case string(unified.AdapterOpsAgent):
- parsed = append(parsed, unified.AdapterOpsAgent)
- default:
- return nil, fmt.Errorf("invalid sourceAdapter value %q", token)
- }
- }
- }
- return parsed, nil
-}
-
// HandleResourceRoutes dispatches nested resource routes.
func (h *ResourceHandlers) HandleResourceRoutes(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/facets") {
@@ -479,7 +380,7 @@ func (h *ResourceHandlers) HandleGetResourceFacets(w http.ResponseWriter, r *htt
}
limit = parsed
}
- filters, err := parseResourceChangeFilters(r.URL.Query())
+ filters, err := unified.ParseResourceChangeFilters(r.URL.Query()["kind"], r.URL.Query()["sourceType"], r.URL.Query()["sourceAdapter"])
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
@@ -729,7 +630,7 @@ func (h *ResourceHandlers) HandleGetResourceTimeline(w http.ResponseWriter, r *h
}
limit = parsed
}
- filters, err := parseResourceChangeFilters(r.URL.Query())
+ filters, err := unified.ParseResourceChangeFilters(r.URL.Query()["kind"], r.URL.Query()["sourceType"], r.URL.Query()["sourceAdapter"])
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
diff --git a/internal/unifiedresources/change_filters.go b/internal/unifiedresources/change_filters.go
new file mode 100644
index 0000000000..f9009b1377
--- /dev/null
+++ b/internal/unifiedresources/change_filters.go
@@ -0,0 +1,107 @@
+package unifiedresources
+
+import (
+ "fmt"
+ "strings"
+)
+
+func parseDelimitedChangeTokens(values []string) []string {
+ tokens := make([]string, 0, len(values))
+ for _, value := range values {
+ for _, token := range strings.Split(value, ",") {
+ normalized := strings.TrimSpace(strings.ToLower(token))
+ if normalized == "" {
+ continue
+ }
+ tokens = append(tokens, normalized)
+ }
+ }
+ return tokens
+}
+
+// ParseResourceChangeFilters canonicalizes timeline filter values for the
+// shared resource-change contract.
+func ParseResourceChangeFilters(kinds, sourceTypes, sourceAdapters []string) (ResourceChangeFilters, error) {
+ filters := ResourceChangeFilters{}
+
+ if parsed, err := parseResourceChangeKinds(kinds); err != nil {
+ return ResourceChangeFilters{}, err
+ } else {
+ filters.Kinds = parsed
+ }
+ if parsed, err := parseResourceChangeSourceTypes(sourceTypes); err != nil {
+ return ResourceChangeFilters{}, err
+ } else {
+ filters.SourceTypes = parsed
+ }
+ if parsed, err := parseResourceChangeSourceAdapters(sourceAdapters); err != nil {
+ return ResourceChangeFilters{}, err
+ } else {
+ filters.SourceAdapters = parsed
+ }
+
+ return filters, nil
+}
+
+func parseResourceChangeKinds(values []string) ([]ChangeKind, error) {
+ parsed := make([]ChangeKind, 0, len(values))
+ for _, normalized := range parseDelimitedChangeTokens(values) {
+ switch normalized {
+ case string(ChangeStateTransition):
+ parsed = append(parsed, ChangeStateTransition)
+ case string(ChangeRestart):
+ parsed = append(parsed, ChangeRestart)
+ case string(ChangeConfigUpdate):
+ parsed = append(parsed, ChangeConfigUpdate)
+ case string(ChangeAnomaly):
+ parsed = append(parsed, ChangeAnomaly)
+ case string(ChangeRelationship):
+ parsed = append(parsed, ChangeRelationship)
+ case string(ChangeCapability):
+ parsed = append(parsed, ChangeCapability)
+ default:
+ return nil, fmt.Errorf("invalid kind value %q", normalized)
+ }
+ }
+ return parsed, nil
+}
+
+func parseResourceChangeSourceTypes(values []string) ([]ChangeSourceType, error) {
+ parsed := make([]ChangeSourceType, 0, len(values))
+ for _, normalized := range parseDelimitedChangeTokens(values) {
+ switch normalized {
+ case string(SourcePlatformEvent):
+ parsed = append(parsed, SourcePlatformEvent)
+ case string(SourcePulseDiff):
+ parsed = append(parsed, SourcePulseDiff)
+ case string(SourceHeuristic):
+ parsed = append(parsed, SourceHeuristic)
+ case string(SourceUserAction):
+ parsed = append(parsed, SourceUserAction)
+ case string(SourceAgentAction):
+ parsed = append(parsed, SourceAgentAction)
+ default:
+ return nil, fmt.Errorf("invalid sourceType value %q", normalized)
+ }
+ }
+ return parsed, nil
+}
+
+func parseResourceChangeSourceAdapters(values []string) ([]ChangeSourceAdapter, error) {
+ parsed := make([]ChangeSourceAdapter, 0, len(values))
+ for _, normalized := range parseDelimitedChangeTokens(values) {
+ switch normalized {
+ case string(AdapterDocker):
+ parsed = append(parsed, AdapterDocker)
+ case string(AdapterProxmox):
+ parsed = append(parsed, AdapterProxmox)
+ case string(AdapterTrueNAS):
+ parsed = append(parsed, AdapterTrueNAS)
+ case string(AdapterOpsAgent):
+ parsed = append(parsed, AdapterOpsAgent)
+ default:
+ return nil, fmt.Errorf("invalid sourceAdapter value %q", normalized)
+ }
+ }
+ return parsed, nil
+}
diff --git a/internal/unifiedresources/change_filters_test.go b/internal/unifiedresources/change_filters_test.go
new file mode 100644
index 0000000000..f64f592497
--- /dev/null
+++ b/internal/unifiedresources/change_filters_test.go
@@ -0,0 +1,48 @@
+package unifiedresources
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestParseResourceChangeFilters(t *testing.T) {
+ filters, err := ParseResourceChangeFilters(
+ []string{" state_transition,RESTART ", "config_update"},
+ []string{" platform_event ", "pulse_diff, HEURISTIC"},
+ []string{" docker_adapter ", "proxmox_adapter,agent:ops-helper"},
+ )
+ if err != nil {
+ t.Fatalf("ParseResourceChangeFilters() error = %v", err)
+ }
+
+ if got, want := len(filters.Kinds), 3; got != want {
+ t.Fatalf("kinds length = %d, want %d", got, want)
+ }
+ if filters.Kinds[0] != ChangeStateTransition || filters.Kinds[1] != ChangeRestart || filters.Kinds[2] != ChangeConfigUpdate {
+ t.Fatalf("unexpected parsed kinds: %#v", filters.Kinds)
+ }
+ if got, want := len(filters.SourceTypes), 3; got != want {
+ t.Fatalf("source types length = %d, want %d", got, want)
+ }
+ if filters.SourceTypes[0] != SourcePlatformEvent || filters.SourceTypes[1] != SourcePulseDiff || filters.SourceTypes[2] != SourceHeuristic {
+ t.Fatalf("unexpected parsed source types: %#v", filters.SourceTypes)
+ }
+ if got, want := len(filters.SourceAdapters), 3; got != want {
+ t.Fatalf("source adapters length = %d, want %d", got, want)
+ }
+ if filters.SourceAdapters[0] != AdapterDocker || filters.SourceAdapters[1] != AdapterProxmox || filters.SourceAdapters[2] != AdapterOpsAgent {
+ t.Fatalf("unexpected parsed source adapters: %#v", filters.SourceAdapters)
+ }
+}
+
+func TestParseResourceChangeFiltersRejectsInvalidValues(t *testing.T) {
+ if _, err := ParseResourceChangeFilters([]string{"unknown"}, nil, nil); err == nil || !strings.Contains(err.Error(), "invalid kind value") {
+ t.Fatalf("expected invalid kind error, got %v", err)
+ }
+ if _, err := ParseResourceChangeFilters(nil, []string{"bad-source"}, nil); err == nil || !strings.Contains(err.Error(), "invalid sourceType value") {
+ t.Fatalf("expected invalid source type error, got %v", err)
+ }
+ if _, err := ParseResourceChangeFilters(nil, nil, []string{"bad-adapter"}); err == nil || !strings.Contains(err.Error(), "invalid sourceAdapter value") {
+ t.Fatalf("expected invalid source adapter error, got %v", err)
+ }
+}
diff --git a/internal/unifiedresources/code_standards_test.go b/internal/unifiedresources/code_standards_test.go
index 7a308bbc55..850b447c7a 100644
--- a/internal/unifiedresources/code_standards_test.go
+++ b/internal/unifiedresources/code_standards_test.go
@@ -245,13 +245,12 @@ func TestResourceAPIExposesDedicatedFacetReads(t *testing.T) {
"HandleGetResourceCapabilities",
"HandleGetResourceRelationships",
"HandleGetResourceTimeline",
- "parseResourceChangeFilters(r.URL.Query())",
+ "unified.ParseResourceChangeFilters(r.URL.Query()[\"kind\"], r.URL.Query()[\"sourceType\"], r.URL.Query()[\"sourceAdapter\"])",
"GetRecentChangesFiltered(resourceID, since, limit, filters)",
"CountRecentChangesFiltered(resourceID, since, filters)",
"CountRecentChangesByKindFiltered(resourceID, since, filters)",
"CountRecentChangesBySourceTypeFiltered(resourceID, since, filters)",
"sourceAdapter",
- "invalid sourceAdapter value",
"strings.HasSuffix(r.URL.Path, \"/facets\")",
"strings.HasSuffix(r.URL.Path, \"/capabilities\")",
"strings.HasSuffix(r.URL.Path, \"/relationships\")",
@@ -264,6 +263,31 @@ func TestResourceAPIExposesDedicatedFacetReads(t *testing.T) {
}
}
+func TestResourceChangeFilterParsingIsOwnedByUnifiedResources(t *testing.T) {
+ requiredSnippets := map[string][]string{
+ filepath.Join(".", "change_filters.go"): {
+ "func ParseResourceChangeFilters(kinds, sourceTypes, sourceAdapters []string) (ResourceChangeFilters, error)",
+ "func parseResourceChangeKinds(values []string) ([]ChangeKind, error)",
+ "func parseResourceChangeSourceTypes(values []string) ([]ChangeSourceType, error)",
+ "func parseResourceChangeSourceAdapters(values []string) ([]ChangeSourceAdapter, error)",
+ },
+ filepath.Join("..", "api", "resources.go"): {
+ "unified.ParseResourceChangeFilters(r.URL.Query()[\"kind\"], r.URL.Query()[\"sourceType\"], r.URL.Query()[\"sourceAdapter\"])",
+ },
+ }
+ for name, snippets := range requiredSnippets {
+ data, err := os.ReadFile(name)
+ if err != nil {
+ t.Fatalf("failed to read %s: %v", name, err)
+ }
+ for _, snippet := range snippets {
+ if !strings.Contains(string(data), snippet) {
+ t.Fatalf("%s must contain %q", name, snippet)
+ }
+ }
+ }
+}
+
func TestResourcePolicyPresentationUsesCanonicalLabels(t *testing.T) {
data, err := os.ReadFile(filepath.Join(".", "policy_presentation.go"))
if err != nil {