122 lines
3.6 KiB
Go
122 lines
3.6 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"cloudipvalidator/internal/db"
|
|
)
|
|
|
|
func (s *Server) handleHealthz(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(w, http.StatusOK, okResponse{OK: true})
|
|
}
|
|
|
|
func (s *Server) handleAdminStatus(w http.ResponseWriter, r *http.Request) {
|
|
ips, err := s.DB.ListIPs(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
validators, err := s.DB.ListValidators(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
byState := map[string]int{}
|
|
for _, ip := range ips {
|
|
byState[ip.State]++
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]interface{}{
|
|
"total_ips": len(ips),
|
|
"ips_by_state": byState,
|
|
"total_validators": len(validators),
|
|
})
|
|
}
|
|
|
|
func (s *Server) handleAdminIPs(w http.ResponseWriter, r *http.Request) {
|
|
ips, err := s.DB.ListIPs(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, ips)
|
|
}
|
|
|
|
func (s *Server) handleAdminIPDetail(w http.ResponseWriter, r *http.Request) {
|
|
address := r.PathValue("ip")
|
|
item, err := s.DB.GetIPByAddress(r.Context(), address)
|
|
if err != nil {
|
|
writeError(w, http.StatusNotFound, "unknown ip: "+address)
|
|
return
|
|
}
|
|
checks, err := s.DB.ListChecksForAttempt(r.Context(), item.ID, item.AttemptNumber)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
events, err := s.DB.ListEventsForIP(r.Context(), item.ID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, struct {
|
|
IP *db.IPQueueItem `json:"ip"`
|
|
Checks []db.Check `json:"checks"`
|
|
Events []db.Event `json:"events"`
|
|
}{item, checks, events})
|
|
}
|
|
|
|
func (s *Server) handleAdminValidators(w http.ResponseWriter, r *http.Request) {
|
|
validators, err := s.DB.ListValidators(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, validators)
|
|
}
|
|
|
|
// handleAdminSubmitIPs is the single entry point for both adding new
|
|
// addresses to the queue and forcing a re-check of already-finished ones —
|
|
// see db.SubmitIPs for the exact per-address rules. It's a direct DB call
|
|
// (no OpenStack interaction is needed to merely queue work), matching the
|
|
// existing admin handlers above which also bypass the orchestrator for
|
|
// reads.
|
|
func (s *Server) handleAdminSubmitIPs(w http.ResponseWriter, r *http.Request) {
|
|
var req submitIPsRequest
|
|
if err := readJSON(r, &req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid body: "+err.Error())
|
|
return
|
|
}
|
|
result, err := s.DB.SubmitIPs(r.Context(), req.Addresses)
|
|
if err != nil {
|
|
writeDBError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, submitIPsResponse{
|
|
Added: emptyIfNil(result.Added),
|
|
Requeued: emptyIfNil(result.Requeued),
|
|
Reordered: emptyIfNil(result.Reordered),
|
|
SkippedInProgress: emptyIfNil(result.SkippedInProgress),
|
|
})
|
|
}
|
|
|
|
// handleAdminCancelIP force-stops a check in progress (or still-queued) for
|
|
// the given address. Requires the orchestrator, since a floating IP may
|
|
// need to be disassociated in OpenStack.
|
|
func (s *Server) handleAdminCancelIP(w http.ResponseWriter, r *http.Request) {
|
|
address := r.PathValue("ip")
|
|
if err := s.Orch.ForceCancel(r.Context(), address); err != nil {
|
|
writeDBError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, okResponse{OK: true})
|
|
}
|
|
|
|
// emptyIfNil turns a nil slice into an empty one so these fields always
|
|
// marshal as `[]` rather than `null`.
|
|
func emptyIfNil(s []string) []string {
|
|
if s == nil {
|
|
return []string{}
|
|
}
|
|
return s
|
|
}
|