124 lines
3.2 KiB
Go
124 lines
3.2 KiB
Go
package agentapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
|
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
|
)
|
|
|
|
func (s *Server) SubnetsHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
s.listSubnets(w, r)
|
|
case http.MethodPost:
|
|
s.postSubnet(w, r)
|
|
default:
|
|
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
|
}
|
|
}
|
|
|
|
func (s *Server) listSubnets(w http.ResponseWriter, _ *http.Request) {
|
|
entries, err := kv.ListByPrefix(s.db, "subnet/")
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to list subnets"})
|
|
return
|
|
}
|
|
subnets := make(map[string]*Subnet)
|
|
for key, value := range entries {
|
|
parts := strings.Split(key, "/")
|
|
if len(parts) != 3 {
|
|
continue
|
|
}
|
|
name := parts[1]
|
|
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 "vxlan_id":
|
|
subnets[name].VxlanID, _ = strconv.Atoi(value)
|
|
case "local_iface":
|
|
subnets[name].LocalIface = value
|
|
case "gateway_ip":
|
|
subnets[name].GatewayIP = value
|
|
case "cidr":
|
|
subnets[name].CIDR = value
|
|
}
|
|
}
|
|
result := make([]Subnet, 0, len(subnets))
|
|
for _, sub := range subnets {
|
|
result = append(result, *sub)
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(result)
|
|
}
|
|
|
|
func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
|
|
var req SubnetCreateRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"})
|
|
return
|
|
}
|
|
if req.Name == "" || req.VPC == "" || req.GatewayIP == "" || req.CIDR == "" {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, gateway_ip and cidr are required"})
|
|
return
|
|
}
|
|
cmd := dispatcher.CreateSubnetCommand{
|
|
Name: req.Name,
|
|
VPC: req.VPC,
|
|
VxlanID: req.VxlanID,
|
|
IfaceType: req.IfaceType,
|
|
GatewayIP: req.GatewayIP,
|
|
CIDR: req.CIDR,
|
|
}
|
|
if err := s.dispatcher.Prepare(cmd); err != nil {
|
|
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
|
|
}
|
|
s.dispatcher.Dispatch(cmd)
|
|
entries, err := kv.ListByPrefix(s.db, "subnet/"+req.Name+"/")
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
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 "vxlan_id":
|
|
sub.VxlanID, _ = strconv.Atoi(value)
|
|
case "local_iface":
|
|
sub.LocalIface = value
|
|
case "gateway_ip":
|
|
sub.GatewayIP = value
|
|
case "cidr":
|
|
sub.CIDR = value
|
|
}
|
|
}
|
|
w.WriteHeader(http.StatusAccepted)
|
|
json.NewEncoder(w).Encode(sub)
|
|
}
|