mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-02 10:09:02 +01:00
When two Proxmox installations reuse VMIDs and guest names, three places each decided which guest owned a PBS snapshot, and they disagreed. The recovery mapper let a learned owner token break a tie between labels, which guest badges have refused since #2292, and it learned from one PBS instance at a time. The Backups table had a stricter rule of its own, so a snapshot the badge gave to a guest sat on an unmatched row while that guest's own row read Protected with no backup. models.AttributePBSBackups is now the one rule. SyncGuestBackupTimes and recovery ingestion read it, and /api/backups/pbs reports it as guestInstance so the table does no attribution. When the evidence does not settle a snapshot it stays unattributed on all three: recovery history no longer relinks a point left unsettled between live guests, and rows carried over a failed read no longer teach source inference. An unmatched row used to say it matched no monitored guest even when it matched two. It now names the guests sharing its VMID, and they point back to it. Refs #2681
117 lines
3.8 KiB
Go
117 lines
3.8 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/models"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/utils"
|
|
)
|
|
|
|
// PBSBackupsResponse is the source-specific payload for
|
|
// `GET /api/backups/pbs`. PBS backups are deduplicated server-side
|
|
// recovery artifacts, so their protection and verification state lives
|
|
// on the PBS snapshot model rather than on PVE's local vzdump file list. Each
|
|
// backup also says which guest it belongs to, the same answer guest backup badges
|
|
// and recovery points use, so clients must not work that out for themselves.
|
|
type PBSBackupsResponse struct {
|
|
Data PBSBackupsPayload `json:"data"`
|
|
Meta PBSBackupsMeta `json:"meta"`
|
|
}
|
|
|
|
type PBSBackupsPayload struct {
|
|
Backups []models.AttributedPBSBackup `json:"backups"`
|
|
}
|
|
|
|
type PBSBackupsMeta struct {
|
|
TotalBackups int `json:"totalBackups"`
|
|
}
|
|
|
|
func (r *Router) handleListPBSBackups(w http.ResponseWriter, req *http.Request) {
|
|
if req.Method != http.MethodGet {
|
|
writeErrorResponse(w, http.StatusMethodNotAllowed, "method_not_allowed",
|
|
"Only GET method is allowed", nil)
|
|
return
|
|
}
|
|
|
|
monitor := r.getTenantMonitor(req.Context())
|
|
if monitor == nil {
|
|
writeErrorResponse(w, http.StatusInternalServerError, "no_monitor",
|
|
"Monitor not available", nil)
|
|
return
|
|
}
|
|
|
|
backups := monitor.AttributedPBSBackupsSnapshot()
|
|
payload := PBSBackupsPayload{Backups: filterPBSBackups(backups, req)}
|
|
|
|
response := PBSBackupsResponse{
|
|
Data: payload,
|
|
Meta: PBSBackupsMeta{TotalBackups: len(payload.Backups)},
|
|
}
|
|
|
|
if err := utils.WriteJSONResponse(w, response); err != nil {
|
|
log.Error().Err(err).Msg("Failed to encode PBS backups response")
|
|
writeErrorResponse(w, http.StatusInternalServerError, "encoding_error",
|
|
"Failed to encode PBS backups", nil)
|
|
}
|
|
}
|
|
|
|
// filterPBSBackups narrows PBS recovery artifacts by optional query params:
|
|
//
|
|
// instance=<name> - only backups reported by a given PBS connection
|
|
// datastore=<name> - only backups in a datastore
|
|
// namespace=<name> - only backups in a namespace
|
|
// type=<vm|ct|host> - only backups of a backup type
|
|
// vmid=<id> - only backups for a guest id
|
|
func filterPBSBackups(backups []models.AttributedPBSBackup, req *http.Request) []models.AttributedPBSBackup {
|
|
query := req.URL.Query()
|
|
instance := strings.TrimSpace(query.Get("instance"))
|
|
datastore := strings.TrimSpace(query.Get("datastore"))
|
|
namespace := strings.TrimSpace(query.Get("namespace"))
|
|
backupType := strings.TrimSpace(query.Get("type"))
|
|
vmid := strings.TrimSpace(query.Get("vmid"))
|
|
|
|
if instance == "" && datastore == "" && namespace == "" && backupType == "" && vmid == "" {
|
|
return normalizePBSBackups(backups)
|
|
}
|
|
|
|
out := make([]models.AttributedPBSBackup, 0, len(backups))
|
|
for _, backup := range backups {
|
|
if instance != "" && !strings.EqualFold(backup.Instance, instance) {
|
|
continue
|
|
}
|
|
if datastore != "" && !strings.EqualFold(backup.Datastore, datastore) {
|
|
continue
|
|
}
|
|
if namespace != "" && !strings.EqualFold(backup.Namespace, namespace) {
|
|
continue
|
|
}
|
|
if backupType != "" && !strings.EqualFold(backup.BackupType, backupType) {
|
|
continue
|
|
}
|
|
if vmid != "" && backup.VMID != vmid {
|
|
continue
|
|
}
|
|
out = append(out, backup.NormalizeCollections())
|
|
}
|
|
return out
|
|
}
|
|
|
|
func normalizePBSBackups(backups []models.AttributedPBSBackup) []models.AttributedPBSBackup {
|
|
if backups == nil {
|
|
return []models.AttributedPBSBackup{}
|
|
}
|
|
out := make([]models.AttributedPBSBackup, 0, len(backups))
|
|
for _, backup := range backups {
|
|
out = append(out, backup.NormalizeCollections())
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (r *Router) registerPBSBackupsRoutes() {
|
|
r.mux.HandleFunc("/api/backups/pbs", RequireAuth(r.config, RequireScope(config.ScopeMonitoringRead, r.handleListPBSBackups)))
|
|
}
|