f-21: api coherence

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-04-26 16:14:18 +02:00
commit e939467abf
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
5 changed files with 43 additions and 17 deletions

View file

@ -44,6 +44,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/VPC" $ref: "#/components/schemas/VPC"
"400":
description: Missing required field or invalid request body
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"409": "409":
description: VPC already exists description: VPC already exists
content: content:
@ -179,12 +185,6 @@ paths:
$ref: "#/components/schemas/Subnet" $ref: "#/components/schemas/Subnet"
"404": "404":
$ref: "#/components/responses/NotFound" $ref: "#/components/responses/NotFound"
"409":
description: Subnet not in a deletable state
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"500": "500":
$ref: "#/components/responses/InternalError" $ref: "#/components/responses/InternalError"

View file

@ -81,7 +81,7 @@ func TestPostSubnet_Created(t *testing.T) {
func TestPostSubnet_MissingFields(t *testing.T) { func TestPostSubnet_MissingFields(t *testing.T) {
s, _ := newTestServer(t) s, _ := newTestServer(t)
body, _ := json.Marshal(SubnetCreateRequest{Name: "sn-1"}) // vpc, iface_type, gateway_ip, cidr manquants body, _ := json.Marshal(SubnetCreateRequest{Name: "sn-1"}) // vpc, gateway_ip, cidr manquants
w := httptest.NewRecorder() w := httptest.NewRecorder()
s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body))) s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body)))
if w.Code != http.StatusBadRequest { if w.Code != http.StatusBadRequest {
@ -89,6 +89,24 @@ func TestPostSubnet_MissingFields(t *testing.T) {
} }
} }
func TestPostSubnet_IfaceTypeOptional(t *testing.T) {
s, db := newTestServer(t)
kv.AddInDB(db, "vpc/vpc-1/state", "created")
req := SubnetCreateRequest{
Name: "sn-opt",
VPC: "vpc-1",
GatewayIP: "10.0.0.1",
CIDR: "10.0.0.0/24",
// IfaceType omis — doit utiliser default_interface
}
body, _ := json.Marshal(req)
w := httptest.NewRecorder()
s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body)))
if w.Code != http.StatusAccepted {
t.Fatalf("attendu 202, obtenu %d: %s", w.Code, w.Body.String())
}
}
func TestPostSubnet_VPCNotFound(t *testing.T) { func TestPostSubnet_VPCNotFound(t *testing.T) {
s, _ := newTestServer(t) s, _ := newTestServer(t)
req := SubnetCreateRequest{ req := SubnetCreateRequest{
@ -101,8 +119,8 @@ func TestPostSubnet_VPCNotFound(t *testing.T) {
body, _ := json.Marshal(req) body, _ := json.Marshal(req)
w := httptest.NewRecorder() w := httptest.NewRecorder()
s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body))) s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body)))
if w.Code != http.StatusConflict { if w.Code != http.StatusUnprocessableEntity {
t.Errorf("attendu 409, obtenu %d", w.Code) t.Errorf("attendu 422, obtenu %d", w.Code)
} }
} }
@ -138,8 +156,8 @@ func TestPostSubnet_VPCDeleting(t *testing.T) {
body, _ := json.Marshal(req) body, _ := json.Marshal(req)
w := httptest.NewRecorder() w := httptest.NewRecorder()
s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body))) s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body)))
if w.Code != http.StatusConflict { if w.Code != http.StatusUnprocessableEntity {
t.Errorf("attendu 409, obtenu %d", w.Code) t.Errorf("attendu 422, obtenu %d", w.Code)
} }
} }

View file

@ -69,9 +69,9 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"}) json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"})
return return
} }
if req.Name == "" || req.VPC == "" || req.IfaceType == "" || req.GatewayIP == "" || req.CIDR == "" { if req.Name == "" || req.VPC == "" || req.GatewayIP == "" || req.CIDR == "" {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, iface_type, gateway_ip and cidr are required"}) json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, gateway_ip and cidr are required"})
return return
} }
cmd := dispatcher.CreateSubnetCommand{ cmd := dispatcher.CreateSubnetCommand{
@ -83,7 +83,11 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
CIDR: req.CIDR, CIDR: req.CIDR,
} }
if err := s.dispatcher.Prepare(cmd); err != nil { if err := s.dispatcher.Prepare(cmd); err != nil {
if _, dbErr := kv.GetFromDB(s.db, "subnet/"+req.Name+"/state"); dbErr == nil {
w.WriteHeader(http.StatusConflict) w.WriteHeader(http.StatusConflict)
} else {
w.WriteHeader(http.StatusUnprocessableEntity)
}
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()}) json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
return return
} }

View file

@ -42,7 +42,11 @@ func (s *Server) getVpc(w http.ResponseWriter, _ *http.Request, name string) {
func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string) { func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string) {
cmd := dispatcher.DeleteVPCCommand{Name: name} cmd := dispatcher.DeleteVPCCommand{Name: name}
if err := s.dispatcher.Prepare(cmd); err != nil { if err := s.dispatcher.Prepare(cmd); err != nil {
if _, dbErr := kv.GetFromDB(s.db, "vpc/"+name+"/state"); dbErr != nil {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusConflict)
}
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()}) json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
return return
} }

View file

@ -171,8 +171,8 @@ func TestDeleteVpc_BlockedByActiveSubnet(t *testing.T) {
req := httptest.NewRequest(http.MethodDelete, "/vpcs/vpc-busy", nil) req := httptest.NewRequest(http.MethodDelete, "/vpcs/vpc-busy", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
s.VpcByNameHandler(w, req) s.VpcByNameHandler(w, req)
if w.Code != http.StatusNotFound { if w.Code != http.StatusConflict {
t.Errorf("attendu 404 (Prepare échoue), obtenu %d: %s", w.Code, w.Body.String()) t.Errorf("attendu 409, obtenu %d: %s", w.Code, w.Body.String())
} }
} }