f-41: api, vpc: champ l3vni des VPC #41

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-11 20:05:20 +02:00
commit 3dd94f35dd
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
11 changed files with 295 additions and 10 deletions

View file

@ -16,11 +16,15 @@ import (
// The worker queue is buffered but has no running goroutines: Dispatch enqueues
// without blocking and Execute never runs, so DB state reflects only Prepare writes.
func newTestServer(t *testing.T) (*Server, *badger.DB) {
t.Helper()
return newTestServerWith(t, &configuration.Config{DefaultInterface: "br-test"})
}
func newTestServerWith(t *testing.T, cfg *configuration.Config) (*Server, *badger.DB) {
t.Helper()
db := kv.InitDB(kv.Config{Path: t.TempDir()}, false)
t.Cleanup(func() { db.Close() })
q := worker.New(100)
cfg := &configuration.Config{DefaultInterface: "br-test"}
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
d := dispatcher.New(q, db, cfg, logger)
return New(d, db, logger, "127.0.0.1:0"), db

View file

@ -1,14 +1,16 @@
package agentapi
type VPCCreateRequest struct {
Name string `json:"name"`
CIDR string `json:"cidr"`
Name string `json:"name"`
CIDR string `json:"cidr"`
L3VNI *uint32 `json:"l3vni,omitempty"`
}
type VPC struct {
Name string `json:"name"`
State string `json:"state"`
CIDR string `json:"cidr"`
L3VNI uint32 `json:"l3vni,omitempty"`
}
type SubnetCreateRequest struct {

View file

@ -36,8 +36,9 @@ func (s *Server) getVpc(w http.ResponseWriter, _ *http.Request, name string) {
return
}
cidr, _ := kv.GetFromDB(s.db, "vpc/"+name+"/cidr")
l3vni, _ := kv.GetFromDB(s.db, "vpc/"+name+"/l3vni")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(VPC{Name: name, State: state, CIDR: cidr})
json.NewEncoder(w).Encode(VPC{Name: name, State: state, CIDR: cidr, L3VNI: parseL3VNI(l3vni)})
}
func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string) {

View file

@ -5,9 +5,12 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
)
// --- VpcsHandler ---
@ -236,3 +239,84 @@ func TestVpcByName_InvalidMethod(t *testing.T) {
t.Errorf("attendu 405, obtenu %d", w.Code)
}
}
func newBGPTestServer(t *testing.T) (*Server, *badger.DB) {
cfg := &configuration.Config{DefaultInterface: "br-test"}
cfg.BGP.ASN = 64600
cfg.BGP.RouteReflectors = []string{"10.255.255.1"}
return newTestServerWith(t, cfg)
}
func postVpcJSON(s *Server, body string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
s.VpcsHandler(w, httptest.NewRequest(http.MethodPost, "/vpcs", strings.NewReader(body)))
return w
}
func TestPostVpc_WithL3VNI(t *testing.T) {
s, db := newBGPTestServer(t)
w := postVpcJSON(s, `{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":4001}`)
if w.Code != http.StatusAccepted {
t.Fatalf("attendu 202, obtenu %d: %s", w.Code, w.Body.String())
}
if !strings.Contains(w.Body.String(), `"l3vni":4001`) {
t.Errorf("réponse sans l3vni : %s", w.Body.String())
}
if got, _ := kv.GetFromDB(db, "vpc/vp-1/l3vni"); got != "4001" {
t.Errorf("l3vni en DB = %q", got)
}
}
func TestPostVpc_WithoutL3VNIOmitsTheField(t *testing.T) {
s, _ := newBGPTestServer(t)
w := postVpcJSON(s, `{"name":"vp-1","cidr":"10.0.0.0/16"}`)
if w.Code != http.StatusAccepted || strings.Contains(w.Body.String(), "l3vni") {
t.Errorf("%d : %s", w.Code, w.Body.String())
}
}
func TestPostVpc_RejectsAnInvalidL3VNIAsABadRequest(t *testing.T) {
s, db := newBGPTestServer(t)
for _, body := range []string{
`{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":0}`,
`{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":255}`,
`{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":16777216}`,
`{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":-1}`,
} {
if w := postVpcJSON(s, body); w.Code != http.StatusBadRequest {
t.Errorf("%s : attendu 400, obtenu %d: %s", body, w.Code, w.Body.String())
}
}
if entries, _ := kv.ListByPrefix(db, "vpc/"); len(entries) != 0 {
t.Errorf("écritures malgré le refus : %v", entries)
}
}
func TestPostVpc_L3VNIWithoutBGPIsAConflict(t *testing.T) {
s, _ := newTestServer(t)
w := postVpcJSON(s, `{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":4001}`)
if w.Code != http.StatusConflict || !strings.Contains(w.Body.String(), "requires the bgp section") {
t.Errorf("%d : %s", w.Code, w.Body.String())
}
}
func TestGetAndListVpc_ReturnTheL3VNI(t *testing.T) {
s, db := newTestServer(t)
kv.AddInDB(db, "vpc/vp-1/state", "running")
kv.AddInDB(db, "vpc/vp-1/cidr", "10.0.0.0/16")
kv.AddInDB(db, "vpc/vp-1/l3vni", "4001")
w := httptest.NewRecorder()
s.VpcByNameHandler(w, httptest.NewRequest(http.MethodGet, "/vpcs/vp-1", nil))
var got VPC
json.NewDecoder(w.Body).Decode(&got)
if got.L3VNI != 4001 {
t.Errorf("GET : l3vni = %d", got.L3VNI)
}
w = httptest.NewRecorder()
s.VpcsHandler(w, httptest.NewRequest(http.MethodGet, "/vpcs", nil))
var list []VPC
json.NewDecoder(w.Body).Decode(&list)
if len(list) != 1 || list[0].L3VNI != 4001 {
t.Errorf("liste : %+v", list)
}
}

View file

@ -4,9 +4,11 @@ import (
"encoding/json"
"net"
"net/http"
"strconv"
"strings"
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
"git.g3e.fr/syonad/two/internal/vpc"
"git.g3e.fr/syonad/two/pkg/db/kv"
)
@ -44,6 +46,8 @@ func (s *Server) listVpcs(w http.ResponseWriter, _ *http.Request) {
vpcs[name].State = value
case "cidr":
vpcs[name].CIDR = value
case "l3vni":
vpcs[name].L3VNI = parseL3VNI(value)
}
}
result := make([]VPC, 0, len(vpcs))
@ -77,6 +81,14 @@ func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) {
return
}
cmd := dispatcher.CreateVPCCommand{Name: req.Name, CIDR: req.CIDR}
if req.L3VNI != nil {
if err := vpc.ValidL3VNI(*req.L3VNI); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
return
}
cmd.L3VNI = *req.L3VNI
}
if err := s.dispatcher.Prepare(cmd); err != nil {
w.WriteHeader(http.StatusConflict)
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
@ -90,5 +102,10 @@ func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) {
return
}
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(VPC{Name: req.Name, State: state, CIDR: req.CIDR})
json.NewEncoder(w).Encode(VPC{Name: req.Name, State: state, CIDR: req.CIDR, L3VNI: cmd.L3VNI})
}
func parseL3VNI(value string) uint32 {
id, _ := strconv.ParseUint(value, 10, 32)
return uint32(id)
}