two/internal/api/agent/vpcs.go
GnomeZworc 6dd21a465d
All checks were successful
Pre Release Workflow / set-release-target (push) Successful in 1s
Pre Release Workflow / build (agent, amd64, linux) (push) Successful in 1m33s
Pre Release Workflow / build (db, amd64, linux) (push) Successful in 1m30s
Pre Release Workflow / build (metacli, amd64, linux) (push) Successful in 1m30s
Pre Release Workflow / build (metadata, amd64, linux) (push) Successful in 1m31s
Pre Release Workflow / build (subnet, amd64, linux) (push) Successful in 1m31s
Pre Release Workflow / prerelease (push) Successful in 13s
Pre Release Workflow / build (dhcp, amd64, linux) (push) Successful in 1m28s
Pre Release Workflow / build (vpc, amd64, linux) (push) Successful in 1m30s
Pre Release Workflow / upload-scripts (run-dnsmasq-in-netns.sh) (push) Successful in 5s
f-21: code: add vpc gestion in api
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-17 23:26:04 +02:00

49 lines
1.3 KiB
Go

package agentapi
import (
"encoding/json"
"fmt"
"net/http"
"git.g3e.fr/syonad/two/internal/vpc"
"git.g3e.fr/syonad/two/pkg/db/kv"
)
func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case http.MethodGet:
s.listVpcs(w, r)
case http.MethodPost:
s.postVpc(w, r)
default:
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
}
}
func (s *Server) listVpcs(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode([]VPC{})
}
func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) {
var req VPCCreateRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"})
return
}
if req.Name == "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{Error: "name is required"})
return
}
s.queue.Submit(func() {
kv.AddInDB(s.db, "vpc/"+req.Name+"/state", "creating")
if err := vpc.CreateVPC(s.db, req.Name); err != nil {
fmt.Println(err)
}
})
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(VPC{Name: req.Name, State: "creating"})
}