two/internal/api/agent/vpc.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

55 lines
1.4 KiB
Go

package agentapi
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"git.g3e.fr/syonad/two/internal/vpc"
"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) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(VPC{Name: name, State: "created"})
}
func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string) {
s.queue.Submit(func() {
kv.AddInDB(s.db, "vpc/"+name+"/state", "deleting")
if err := vpc.DeleteVPC(s.db, name); err != nil {
fmt.Println(err)
}
if state, err := kv.GetFromDB(s.db, "vpc/"+name+"/state"); err != nil {
fmt.Println(err)
os.Exit(1)
} else if state == "deleted" {
kv.DeleteInDB(s.db, "vpc/"+name)
}
})
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(VPC{Name: name, State: "deleting"})
}
func destroyVpc(name string) {}