From c5d7855a51359d1870efe8f722c5bf81253331fd Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Fri, 23 Oct 2026 13:48:07 +0200 Subject: [PATCH] =?UTF-8?q?f-80:=20api:=20gateway=20rendu=20par=20toutes?= =?UTF-8?q?=20les=20lectures=20de=20subnet,=20vxlan=5Fid=20omis=20hors=20v?= =?UTF-8?q?xlan,=20spec=20align=C3=A9e=20sur=20le=20code=20#80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: GnomeZworc --- api/agent.yaml | 11 +++-- internal/api/agent/models.go | 2 +- internal/api/agent/subnet.go | 67 ++++++++++++++++++------------- internal/api/agent/subnet_test.go | 59 +++++++++++++++++++++++++++ internal/api/agent/subnets.go | 49 +--------------------- 5 files changed, 108 insertions(+), 80 deletions(-) diff --git a/api/agent.yaml b/api/agent.yaml index 03aad15..7e133e7 100644 --- a/api/agent.yaml +++ b/api/agent.yaml @@ -2,7 +2,7 @@ openapi: "3.1.0" info: title: Two API version: "0.1.0" - description: REST API for managing VPCs and Subnets in the Two orchestrator. + description: REST API for managing VPCs, Subnets and VMs in the Two orchestrator. servers: - url: http://localhost:8080 @@ -45,7 +45,7 @@ paths: schema: $ref: "#/components/schemas/VPC" "400": - description: Missing required field, invalid request body, or l3vni out of range + description: Missing required field, invalid request body, invalid CIDR, or l3vni out of range content: application/json: schema: @@ -360,7 +360,11 @@ components: properties: name: type: string - description: Unique name for the subnet + description: > + Unique name for the subnet. Must contain a "-": the system id (bridge br-) is + what follows the first one. Not validated by the agent (caller's responsibility): a name + without "-" makes the agent worker panic. + pattern: '^[^-]+-.+' example: sn-00001 vpc: type: string @@ -482,6 +486,7 @@ components: interfaces: type: array minItems: 1 + maxItems: 27 description: > Network interfaces, in order. The position determines the PCI slot (0x03 + index) and therefore the interface name inside the guest. diff --git a/internal/api/agent/models.go b/internal/api/agent/models.go index ff3e5f3..be152f6 100644 --- a/internal/api/agent/models.go +++ b/internal/api/agent/models.go @@ -30,7 +30,7 @@ type Subnet struct { State string `json:"state"` VPC string `json:"vpc"` Mode string `json:"mode"` - VxlanID int `json:"vxlan_id"` + VxlanID int `json:"vxlan_id,omitempty"` LocalIface string `json:"local_iface"` InterfaceIP string `json:"interface_ip"` RelayIP string `json:"relay_ip,omitempty"` diff --git a/internal/api/agent/subnet.go b/internal/api/agent/subnet.go index 99e4137..85f9e7a 100644 --- a/internal/api/agent/subnet.go +++ b/internal/api/agent/subnet.go @@ -36,35 +36,7 @@ func (s *Server) getSubnet(w http.ResponseWriter, _ *http.Request, name string) 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 "mode": - sub.Mode = value - case "vxlan_id": - sub.VxlanID, _ = strconv.Atoi(value) - case "local_iface": - sub.LocalIface = value - case "interface_ip": - sub.InterfaceIP = value - case "relay_ip": - sub.RelayIP = value - case "cidr": - sub.CIDR = value - case "default_route": - sub.DefaultRoute = value == "true" - case "gateway": - sub.Gateway = value - } - } + sub := subnetFromEntries(name, entries) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(sub) } @@ -92,3 +64,40 @@ func (s *Server) deleteSubnet(w http.ResponseWriter, _ *http.Request, name strin w.WriteHeader(http.StatusAccepted) json.NewEncoder(w).Encode(Subnet{Name: name, State: state}) } + +func subnetFromEntries(name string, entries map[string]string) Subnet { + sub := Subnet{Name: name} + for key, value := range entries { + parts := strings.Split(key, "/") + if len(parts) != 3 { + continue + } + setSubnetField(&sub, parts[2], value) + } + return sub +} + +func setSubnetField(sub *Subnet, field, value string) { + switch field { + case "state": + sub.State = value + case "vpc": + sub.VPC = value + case "mode": + sub.Mode = value + case "vxlan_id": + sub.VxlanID, _ = strconv.Atoi(value) + case "local_iface": + sub.LocalIface = value + case "interface_ip": + sub.InterfaceIP = value + case "relay_ip": + sub.RelayIP = value + case "cidr": + sub.CIDR = value + case "default_route": + sub.DefaultRoute = value == "true" + case "gateway": + sub.Gateway = value + } +} diff --git a/internal/api/agent/subnet_test.go b/internal/api/agent/subnet_test.go index bf24986..705a293 100644 --- a/internal/api/agent/subnet_test.go +++ b/internal/api/agent/subnet_test.go @@ -329,6 +329,13 @@ func TestCreateSubnet_GatewayRoundTrip(t *testing.T) { if w.Code != http.StatusAccepted { t.Fatalf("attendu 202, obtenu %d : %s", w.Code, w.Body.String()) } + var created Subnet + if err := json.Unmarshal(w.Body.Bytes(), &created); err != nil { + t.Fatalf("réponse 202 illisible : %v", err) + } + if created.Gateway != "203.0.113.1" { + t.Errorf("gateway absente de la réponse 202 : %+v", created) + } if gw, _ := kv.GetFromDB(db, "subnet/sn-gw/gateway"); gw != "203.0.113.1" { t.Errorf("gateway attendue en DB, obtenu %q", gw) @@ -343,6 +350,58 @@ func TestCreateSubnet_GatewayRoundTrip(t *testing.T) { if got.Gateway != "203.0.113.1" { t.Errorf("gateway absente de la réponse GET : %+v", got) } + + w = httptest.NewRecorder() + s.SubnetsHandler(w, httptest.NewRequest(http.MethodGet, "/subnets", nil)) + var list []Subnet + if err := json.Unmarshal(w.Body.Bytes(), &list); err != nil { + t.Fatalf("liste illisible : %v", err) + } + if len(list) != 1 || list[0].Gateway != "203.0.113.1" { + t.Errorf("gateway absente de la liste : %+v", list) + } +} + +func TestSubnet_VxlanIDOnlyForVxlan(t *testing.T) { + s, db := newTestServer(t) + kv.AddInDB(db, "vpc/vpc-1/state", "running") + vx := postSubnetRequest(t, s, SubnetCreateRequest{ + Name: "sn-vx", VPC: "vpc-1", Mode: "vxlan", VxlanID: 65537, CIDR: "10.0.0.0/24", + }) + if vx.Code != http.StatusAccepted { + t.Fatalf("vxlan : attendu 202, obtenu %d : %s", vx.Code, vx.Body.String()) + } + if !strings.Contains(vx.Body.String(), `"vxlan_id":65537`) { + t.Errorf("vxlan_id absent de la réponse 202 vxlan : %s", vx.Body.String()) + } + br := postSubnetRequest(t, s, SubnetCreateRequest{ + Name: "sn-br", VPC: "vpc-1", Mode: "bridge", VxlanID: 65538, InterfaceIP: "10.1.0.1", CIDR: "10.1.0.0/24", + }) + if br.Code != http.StatusAccepted { + t.Fatalf("bridge : attendu 202, obtenu %d : %s", br.Code, br.Body.String()) + } + if strings.Contains(br.Body.String(), "vxlan_id") { + t.Errorf("vxlan_id ne doit pas apparaître dans la réponse 202 bridge : %s", br.Body.String()) + } + + w := httptest.NewRecorder() + s.SubnetByNameHandler(w, httptest.NewRequest(http.MethodGet, "/subnets/sn-br", nil)) + if strings.Contains(w.Body.String(), "vxlan_id") { + t.Errorf("vxlan_id ne doit pas apparaître pour un subnet bridge : %s", w.Body.String()) + } + + w = httptest.NewRecorder() + s.SubnetsHandler(w, httptest.NewRequest(http.MethodGet, "/subnets", nil)) + var list []map[string]any + if err := json.Unmarshal(w.Body.Bytes(), &list); err != nil { + t.Fatalf("liste illisible : %v", err) + } + for _, sub := range list { + _, has := sub["vxlan_id"] + if want := sub["name"] == "sn-vx"; has != want { + t.Errorf("%v : vxlan_id présent=%v, attendu %v", sub["name"], has, want) + } + } } func postSubnetRequest(t *testing.T, s *Server, req SubnetCreateRequest) *httptest.ResponseRecorder { diff --git a/internal/api/agent/subnets.go b/internal/api/agent/subnets.go index 00bfb7b..5390d57 100644 --- a/internal/api/agent/subnets.go +++ b/internal/api/agent/subnets.go @@ -40,26 +40,7 @@ func (s *Server) listSubnets(w http.ResponseWriter, _ *http.Request) { if _, ok := subnets[name]; !ok { subnets[name] = &Subnet{Name: name} } - switch parts[2] { - case "state": - subnets[name].State = value - case "vpc": - subnets[name].VPC = value - case "mode": - subnets[name].Mode = value - case "vxlan_id": - subnets[name].VxlanID, _ = strconv.Atoi(value) - case "local_iface": - subnets[name].LocalIface = value - case "interface_ip": - subnets[name].InterfaceIP = value - case "relay_ip": - subnets[name].RelayIP = value - case "cidr": - subnets[name].CIDR = value - case "default_route": - subnets[name].DefaultRoute = value == "true" - } + setSubnetField(subnets[name], parts[2], value) } result := make([]Subnet, 0, len(subnets)) for _, sub := range subnets { @@ -124,33 +105,7 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to read subnet state"}) return } - sub := Subnet{Name: req.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 "mode": - sub.Mode = value - case "vxlan_id": - sub.VxlanID, _ = strconv.Atoi(value) - case "local_iface": - sub.LocalIface = value - case "interface_ip": - sub.InterfaceIP = value - case "relay_ip": - sub.RelayIP = value - case "cidr": - sub.CIDR = value - case "default_route": - sub.DefaultRoute = value == "true" - } - } + sub := subnetFromEntries(req.Name, entries) w.WriteHeader(http.StatusAccepted) json.NewEncoder(w).Encode(sub) }