two/internal/api/agent/subnet.go
GnomeZworc 63a288f69e
f-21: refactor: add dispatcher layer for MQTT migration
Introduce internal/dispatcher package with a Command interface and typed
commands (CreateVPC, DeleteVPC, CreateSubnet, DeleteSubnet). The API
handlers now call dispatcher.Dispatch() instead of enqueuing closures
directly, decoupling transport (HTTP today, MQTT tomorrow) from execution.

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-21 23:36:33 +02:00

40 lines
1.1 KiB
Go

package agentapi
import (
"encoding/json"
"net/http"
"strings"
"git.g3e.fr/syonad/two/internal/dispatcher"
)
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.dispatcher.Dispatch(dispatcher.DeleteSubnetCommand{Name: name})
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(Subnet{Name: name, State: "deleting"})
}