f-21: api: make subnet get

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-04-23 22:30:04 +02:00
commit 0c2dbdb525
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
2 changed files with 71 additions and 4 deletions

View file

@ -3,9 +3,11 @@ package agentapi
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"git.g3e.fr/syonad/two/internal/dispatcher"
"git.g3e.fr/syonad/two/pkg/db/kv"
)
func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
@ -27,9 +29,36 @@ func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
}
}
func (s *Server) getSubnet(w http.ResponseWriter, r *http.Request, name string) {
func (s *Server) getSubnet(w http.ResponseWriter, _ *http.Request, name string) {
entries, err := kv.ListByPrefix(s.db, "subnet/"+name+"/")
if err != nil || len(entries) == 0 {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(ErrorResponse{Error: "subnet not found"})
return
}
sub := Subnet{Name: name}
for key, value := range entries {
parts := strings.Split(key, "/")
if len(parts) != 3 {
continue
}
switch parts[2] {
case "state":
sub.State = value
case "vpc":
sub.VPC = value
case "vxlan_id":
sub.VxlanID, _ = strconv.Atoi(value)
case "local_iface":
sub.LocalIface = value
case "gateway_ip":
sub.GatewayIP = value
case "cidr":
sub.CIDR = value
}
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(Subnet{Name: name, State: "created"})
json.NewEncoder(w).Encode(sub)
}
func (s *Server) deleteSubnet(w http.ResponseWriter, r *http.Request, name string) {