Run manual diagnostic attachments in the background
This commit is contained in:
1 parent
d845a5e4b6
commit
89ac4d5bcd
40 files changed
+1560
-58
No files matched your search
@@ -1,13 +1,19 @@
|
||||
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"`
|
||||
@@ -25,25 +31,72 @@ type diagnosticResultsResponse struct {
|
||||
}
|
||||
|
||||
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
|
||||
return req, nil, false
|
||||
}
|
||||
if len(req.Items) == 0 {
|
||||
writeError(w, http.StatusBadRequest, "items must not be empty")
|
||||
return
|
||||
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
|
||||
return req, nil, false
|
||||
}
|
||||
items[i] = orchestrator.DiagnosticItem{Address: addr, ValidatorID: strings.TrimSpace(it.ValidatorID)}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, diagnosticResultsResponse{Results: s.Orch.DiagnosticAttach(r.Context(), items, req.Note)})
|
||||
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) {
|
||||
|
||||
Reference in new issue
Block a user