123 lines
3.7 KiB
Go
123 lines
3.7 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"cloudipvalidator/internal/db"
|
|
"cloudipvalidator/internal/orchestrator"
|
|
)
|
|
|
|
type diagnosticAttachRequest struct {
|
|
ID string `json:"id"`
|
|
Items []struct {
|
|
Address string `json:"address"`
|
|
ValidatorID string `json:"validator_id"`
|
|
} `json:"items"`
|
|
Note string `json:"note"`
|
|
}
|
|
|
|
type diagnosticDetachRequest struct {
|
|
Addresses []string `json:"addresses"`
|
|
All bool `json:"all"`
|
|
}
|
|
|
|
type diagnosticResultsResponse struct {
|
|
Results []orchestrator.DiagnosticResult `json:"results"`
|
|
}
|
|
|
|
func (s *Server) handleAdminDiagnosticAttach(w http.ResponseWriter, r *http.Request) {
|
|
req, items, ok := readDiagnosticAttach(w, r)
|
|
if ok {
|
|
writeJSON(w, http.StatusOK, diagnosticResultsResponse{Results: s.Orch.DiagnosticAttach(r.Context(), items, req.Note)})
|
|
}
|
|
}
|
|
|
|
func readDiagnosticAttach(w http.ResponseWriter, r *http.Request) (diagnosticAttachRequest, []orchestrator.DiagnosticItem, bool) {
|
|
var req diagnosticAttachRequest
|
|
if err := readJSON(r, &req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid body: "+err.Error())
|
|
return req, nil, false
|
|
}
|
|
if len(req.Items) == 0 {
|
|
writeError(w, http.StatusBadRequest, "items must not be empty")
|
|
return req, nil, false
|
|
}
|
|
items := make([]orchestrator.DiagnosticItem, len(req.Items))
|
|
for i, it := range req.Items {
|
|
addr := strings.TrimSpace(it.Address)
|
|
if addr == "" {
|
|
writeError(w, http.StatusBadRequest, "address must not be empty")
|
|
return req, nil, false
|
|
}
|
|
items[i] = orchestrator.DiagnosticItem{Address: addr, ValidatorID: strings.TrimSpace(it.ValidatorID)}
|
|
}
|
|
return req, items, true
|
|
}
|
|
|
|
var diagnosticOperationID = regexp.MustCompile(`^[A-Za-z0-9_-]{16,80}$`)
|
|
|
|
func (s *Server) handleAdminStartDiagnosticOperation(w http.ResponseWriter, r *http.Request) {
|
|
req, items, ok := readDiagnosticAttach(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
if !diagnosticOperationID.MatchString(req.ID) {
|
|
writeError(w, http.StatusBadRequest, "invalid operation ID")
|
|
return
|
|
}
|
|
canonical, _ := json.Marshal(struct {
|
|
Items []orchestrator.DiagnosticItem
|
|
Note string
|
|
}{items, req.Note})
|
|
entries := make([]db.DiagnosticOperationItem, len(items))
|
|
for i, item := range items {
|
|
entries[i] = db.DiagnosticOperationItem{Address: item.Address, RequestedValidator: item.ValidatorID}
|
|
}
|
|
if err := s.DB.CreateDiagnosticOperation(r.Context(), req.ID, req.Note, fmt.Sprintf("%x", sha256.Sum256(canonical)), entries); err != nil {
|
|
writeDBError(w, err)
|
|
return
|
|
}
|
|
s.Orch.WakeDiagnosticOperations()
|
|
w.Header().Set("Location", "/api/v1/admin/diagnostics/operations/"+req.ID)
|
|
writeJSON(w, http.StatusAccepted, struct {
|
|
ID string `json:"id"`
|
|
}{ID: req.ID})
|
|
}
|
|
|
|
func (s *Server) handleAdminDiagnosticOperation(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
op, err := s.DB.GetDiagnosticOperation(r.Context(), r.PathValue("id"))
|
|
if err != nil {
|
|
writeDBError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, op)
|
|
}
|
|
|
|
func (s *Server) handleAdminDiagnosticDetach(w http.ResponseWriter, r *http.Request) {
|
|
var req diagnosticDetachRequest
|
|
if err := readJSON(r, &req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid body: "+err.Error())
|
|
return
|
|
}
|
|
if !req.All && len(req.Addresses) == 0 {
|
|
writeError(w, http.StatusBadRequest, "addresses or all=true is required")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, diagnosticResultsResponse{Results: s.Orch.DiagnosticDetach(r.Context(), req.Addresses, req.All)})
|
|
}
|
|
|
|
func (s *Server) handleAdminDiagnosticList(w http.ResponseWriter, r *http.Request) {
|
|
holds, err := s.Orch.DiagnosticList(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, holds)
|
|
}
|