mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-30 01:32:00 +01:00
Reject missing selected-organization inventory with non-cacheable structured guidance, matching saved inventory reports. Preserve genuine empty fleets and populated CSV exports, and cover the existing consuming warning path. Contract-Neutral: Restores the existing organization-bound inventory contract: unavailable source data is not a successful empty fleet; schemas, CSV columns, routes, authorization and scheduling are unchanged. Change-source: pulse-maintainer
108 lines
3.8 KiB
Go
108 lines
3.8 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
|
|
"github.com/rcourtman/pulse-go-rewrite/pkg/reporting"
|
|
)
|
|
|
|
const vmInventoryUnavailableMessage = "VM inventory is unavailable. Check this organization's infrastructure connections before running again."
|
|
|
|
// HandleExportVMInventory exports the current VM inventory as spreadsheet-friendly CSV.
|
|
func (h *ReportingHandlers) HandleExportVMInventory(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
definition := reporting.DescribeVMInventoryExport()
|
|
format := reporting.ReportFormat(r.URL.Query().Get("format"))
|
|
if format == "" {
|
|
format = definition.Format
|
|
}
|
|
if !definition.SupportsFormat(format) {
|
|
writeErrorResponse(w, http.StatusBadRequest, "invalid_format", definition.InvalidFormatError(), nil)
|
|
return
|
|
}
|
|
|
|
snapshot, ok := h.getReportingEnrichmentSnapshot(r.Context(), GetOrgID(r.Context()))
|
|
if !ok {
|
|
// Match scheduled inventory reports: missing source data is not an
|
|
// observed empty fleet, and must not become a successful download.
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
writeErrorResponse(w, http.StatusServiceUnavailable, "inventory_unavailable", vmInventoryUnavailableMessage, nil)
|
|
return
|
|
}
|
|
|
|
generatedAt := time.Now().UTC()
|
|
report, err := generateVMInventoryReport(snapshot, generatedAt)
|
|
if err != nil {
|
|
writeErrorResponse(w, http.StatusInternalServerError, "generation_failed", "Failed to generate VM inventory export", nil)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", report.ContentType)
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", report.Filename))
|
|
written, writeErr := w.Write(report.Data)
|
|
h.recordReportingDownload(r, config.ReportScheduleKindVMInventory, written, writeErr, len(report.Data))
|
|
}
|
|
|
|
// Both downloads and schedules use the same current-state projection and CSV
|
|
// contract. Inventory does not use historical metric windows or resource caps.
|
|
func generateVMInventoryReport(snapshot reportingEnrichmentSnapshot, generatedAt time.Time) (generatedMultiReport, error) {
|
|
definition := reporting.DescribeVMInventoryExport()
|
|
data, err := reporting.GenerateVMInventoryCSV(reporting.VMInventoryData{
|
|
GeneratedAt: generatedAt,
|
|
Rows: buildVMInventoryRows(snapshot.Resources),
|
|
})
|
|
return generatedMultiReport{
|
|
Data: data, Format: definition.Format, ContentType: "text/csv; charset=utf-8",
|
|
Filename: definition.AttachmentFilename(generatedAt),
|
|
}, err
|
|
}
|
|
|
|
func buildVMInventoryRows(resources []unifiedresources.Resource) []reporting.VMInventoryRow {
|
|
rows := make([]reporting.VMInventoryRow, 0, len(resources))
|
|
for i := range resources {
|
|
resource := &resources[i]
|
|
if resource.Type != unifiedresources.ResourceTypeVM {
|
|
continue
|
|
}
|
|
view := unifiedresources.NewVMView(resource)
|
|
diskAllocated, diskUsed := resolveVMInventoryDiskUsage(view)
|
|
rows = append(rows, reporting.VMInventoryRow{
|
|
ResourceID: view.ID(),
|
|
Instance: view.Instance(),
|
|
Node: view.Node(),
|
|
Pool: view.Pool(),
|
|
VMID: view.VMID(),
|
|
Name: view.Name(),
|
|
Status: string(view.Status()),
|
|
CPUCores: view.CPUs(),
|
|
MemoryAllocatedBytes: view.MemoryTotal(),
|
|
DiskAllocatedBytes: diskAllocated,
|
|
DiskUsedBytes: diskUsed,
|
|
DiskStatusReason: view.DiskStatusReason(),
|
|
})
|
|
}
|
|
return rows
|
|
}
|
|
|
|
func resolveVMInventoryDiskUsage(view unifiedresources.VMView) (allocated int64, used int64) {
|
|
allocated = view.DiskTotal()
|
|
used = view.DiskUsed()
|
|
if allocated > 0 || used > 0 {
|
|
return allocated, used
|
|
}
|
|
|
|
for _, disk := range view.Disks() {
|
|
allocated += disk.Total
|
|
used += disk.Used
|
|
}
|
|
return allocated, used
|
|
}
|