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

@ -81,7 +81,7 @@ func TestPostSubnet_Created(t *testing.T) {
func TestPostSubnet_MissingFields(t *testing.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()
s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body)))
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) {
s, _ := newTestServer(t)
req := SubnetCreateRequest{
@ -101,8 +119,8 @@ func TestPostSubnet_VPCNotFound(t *testing.T) {
body, _ := json.Marshal(req)
w := httptest.NewRecorder()
s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body)))
if w.Code != http.StatusConflict {
t.Errorf("attendu 409, obtenu %d", w.Code)
if w.Code != http.StatusUnprocessableEntity {
t.Errorf("attendu 422, obtenu %d", w.Code)
}
}
@ -138,8 +156,8 @@ func TestPostSubnet_VPCDeleting(t *testing.T) {
body, _ := json.Marshal(req)
w := httptest.NewRecorder()
s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body)))
if w.Code != http.StatusConflict {
t.Errorf("attendu 409, obtenu %d", w.Code)
if w.Code != http.StatusUnprocessableEntity {
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"})
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)
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
}
cmd := dispatcher.CreateSubnetCommand{
@ -83,7 +83,11 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
CIDR: req.CIDR,
}
if err := s.dispatcher.Prepare(cmd); err != nil {
w.WriteHeader(http.StatusConflict)
if _, dbErr := kv.GetFromDB(s.db, "subnet/"+req.Name+"/state"); dbErr == nil {
w.WriteHeader(http.StatusConflict)
} else {
w.WriteHeader(http.StatusUnprocessableEntity)
}
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
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) {
cmd := dispatcher.DeleteVPCCommand{Name: name}
if err := s.dispatcher.Prepare(cmd); err != nil {
w.WriteHeader(http.StatusNotFound)
if _, dbErr := kv.GetFromDB(s.db, "vpc/"+name+"/state"); dbErr != nil {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusConflict)
}
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
return
}

View file

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