f-80: api: gateway rendu par toutes les lectures de subnet, vxlan_id omis hors vxlan, spec alignée sur le code #80
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
b2c89043b2
commit
c5d7855a51
5 changed files with 108 additions and 80 deletions
|
|
@ -2,7 +2,7 @@ openapi: "3.1.0"
|
||||||
info:
|
info:
|
||||||
title: Two API
|
title: Two API
|
||||||
version: "0.1.0"
|
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:
|
servers:
|
||||||
- url: http://localhost:8080
|
- url: http://localhost:8080
|
||||||
|
|
@ -45,7 +45,7 @@ paths:
|
||||||
schema:
|
schema:
|
||||||
$ref: "#/components/schemas/VPC"
|
$ref: "#/components/schemas/VPC"
|
||||||
"400":
|
"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:
|
content:
|
||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
|
|
@ -360,7 +360,11 @@ components:
|
||||||
properties:
|
properties:
|
||||||
name:
|
name:
|
||||||
type: string
|
type: string
|
||||||
description: Unique name for the subnet
|
description: >
|
||||||
|
Unique name for the subnet. Must contain a "-": the system id (bridge br-<id>) 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
|
example: sn-00001
|
||||||
vpc:
|
vpc:
|
||||||
type: string
|
type: string
|
||||||
|
|
@ -482,6 +486,7 @@ components:
|
||||||
interfaces:
|
interfaces:
|
||||||
type: array
|
type: array
|
||||||
minItems: 1
|
minItems: 1
|
||||||
|
maxItems: 27
|
||||||
description: >
|
description: >
|
||||||
Network interfaces, in order. The position determines the PCI slot
|
Network interfaces, in order. The position determines the PCI slot
|
||||||
(0x03 + index) and therefore the interface name inside the guest.
|
(0x03 + index) and therefore the interface name inside the guest.
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ type Subnet struct {
|
||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
VPC string `json:"vpc"`
|
VPC string `json:"vpc"`
|
||||||
Mode string `json:"mode"`
|
Mode string `json:"mode"`
|
||||||
VxlanID int `json:"vxlan_id"`
|
VxlanID int `json:"vxlan_id,omitempty"`
|
||||||
LocalIface string `json:"local_iface"`
|
LocalIface string `json:"local_iface"`
|
||||||
InterfaceIP string `json:"interface_ip"`
|
InterfaceIP string `json:"interface_ip"`
|
||||||
RelayIP string `json:"relay_ip,omitempty"`
|
RelayIP string `json:"relay_ip,omitempty"`
|
||||||
|
|
|
||||||
|
|
@ -36,35 +36,7 @@ func (s *Server) getSubnet(w http.ResponseWriter, _ *http.Request, name string)
|
||||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "subnet not found"})
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "subnet not found"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sub := Subnet{Name: name}
|
sub := subnetFromEntries(name, entries)
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(sub)
|
json.NewEncoder(w).Encode(sub)
|
||||||
}
|
}
|
||||||
|
|
@ -92,3 +64,40 @@ func (s *Server) deleteSubnet(w http.ResponseWriter, _ *http.Request, name strin
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
json.NewEncoder(w).Encode(Subnet{Name: name, State: state})
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -329,6 +329,13 @@ func TestCreateSubnet_GatewayRoundTrip(t *testing.T) {
|
||||||
if w.Code != http.StatusAccepted {
|
if w.Code != http.StatusAccepted {
|
||||||
t.Fatalf("attendu 202, obtenu %d : %s", w.Code, w.Body.String())
|
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" {
|
if gw, _ := kv.GetFromDB(db, "subnet/sn-gw/gateway"); gw != "203.0.113.1" {
|
||||||
t.Errorf("gateway attendue en DB, obtenu %q", gw)
|
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" {
|
if got.Gateway != "203.0.113.1" {
|
||||||
t.Errorf("gateway absente de la réponse GET : %+v", got)
|
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 {
|
func postSubnetRequest(t *testing.T, s *Server, req SubnetCreateRequest) *httptest.ResponseRecorder {
|
||||||
|
|
|
||||||
|
|
@ -40,26 +40,7 @@ func (s *Server) listSubnets(w http.ResponseWriter, _ *http.Request) {
|
||||||
if _, ok := subnets[name]; !ok {
|
if _, ok := subnets[name]; !ok {
|
||||||
subnets[name] = &Subnet{Name: name}
|
subnets[name] = &Subnet{Name: name}
|
||||||
}
|
}
|
||||||
switch parts[2] {
|
setSubnetField(subnets[name], parts[2], value)
|
||||||
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"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
result := make([]Subnet, 0, len(subnets))
|
result := make([]Subnet, 0, len(subnets))
|
||||||
for _, sub := range 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"})
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to read subnet state"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sub := Subnet{Name: req.Name}
|
sub := subnetFromEntries(req.Name, entries)
|
||||||
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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
json.NewEncoder(w).Encode(sub)
|
json.NewEncoder(w).Encode(sub)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue