two/internal/api/agent/vpc.go
GnomeZworc 19434b1848
f-21: refacto: move dispatcher to agent specific files
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-23 22:50:12 +02:00

46 lines
1.3 KiB
Go

package agentapi
import (
"encoding/json"
"net/http"
"strings"
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
"git.g3e.fr/syonad/two/pkg/db/kv"
)
func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
name := strings.TrimPrefix(r.URL.Path, "/vpcs/")
if name == "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(ErrorResponse{Error: "resource not found"})
return
}
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case http.MethodGet:
s.getVpc(w, r, name)
case http.MethodDelete:
s.deleteVpc(w, r, name)
default:
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
}
}
func (s *Server) getVpc(w http.ResponseWriter, _ *http.Request, name string) {
state, err := kv.GetFromDB(s.db, "vpc/"+name+"/state")
if err != nil {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(ErrorResponse{Error: "vpc not found"})
return
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(VPC{Name: name, State: state})
}
func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string) {
s.dispatcher.Dispatch(dispatcher.DeleteVPCCommand{Name: name})
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(VPC{Name: name, State: "deleting"})
}