two/internal/api/agent/subnet.go
GnomeZworc 75e02bd4f7
f-21: code: separate and create route fonction
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-16 22:59:11 +02:00

41 lines
1.1 KiB
Go

package agentapi
import (
"encoding/json"
"net/http"
"strings"
)
func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
name := strings.TrimPrefix(r.URL.Path, "/subnets/")
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.getSubnet(w, r, name)
case http.MethodDelete:
s.deleteSubnet(w, r, name)
default:
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
}
}
func (s *Server) getSubnet(w http.ResponseWriter, r *http.Request, name string) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(Subnet{Name: name, State: "created"})
}
func (s *Server) deleteSubnet(w http.ResponseWriter, r *http.Request, name string) {
s.queue.Submit(func() {
destroySubnet(name)
})
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(Subnet{Name: name, State: "deleting"})
}
func destroySubnet(name string) {}