f-21: dispatch: add a prepare step to dispatch
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
19434b1848
commit
1d86c45ba4
7 changed files with 102 additions and 21 deletions
|
|
@ -61,8 +61,15 @@ func (s *Server) getSubnet(w http.ResponseWriter, _ *http.Request, name string)
|
|||
json.NewEncoder(w).Encode(sub)
|
||||
}
|
||||
|
||||
func (s *Server) deleteSubnet(w http.ResponseWriter, r *http.Request, name string) {
|
||||
s.dispatcher.Dispatch(dispatcher.DeleteSubnetCommand{Name: name})
|
||||
func (s *Server) deleteSubnet(w http.ResponseWriter, _ *http.Request, name string) {
|
||||
cmd := dispatcher.DeleteSubnetCommand{Name: name}
|
||||
if err := s.dispatcher.Prepare(cmd); err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
s.dispatcher.Dispatch(cmd)
|
||||
state, _ := kv.GetFromDB(s.db, "subnet/"+name+"/state")
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(Subnet{Name: name, State: "deleting"})
|
||||
json.NewEncoder(w).Encode(Subnet{Name: name, State: state})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,21 +74,42 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
|
|||
json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, iface_type, gateway_ip and cidr are required"})
|
||||
return
|
||||
}
|
||||
s.dispatcher.Dispatch(dispatcher.CreateSubnetCommand{
|
||||
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 {
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
s.dispatcher.Dispatch(cmd)
|
||||
entries, _ := kv.ListByPrefix(s.db, "subnet/"+req.Name+"/")
|
||||
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(Subnet{
|
||||
Name: req.Name,
|
||||
State: "creating",
|
||||
VPC: req.VPC,
|
||||
VxlanID: req.VxlanID,
|
||||
GatewayIP: req.GatewayIP,
|
||||
CIDR: req.CIDR,
|
||||
})
|
||||
json.NewEncoder(w).Encode(sub)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,7 +40,14 @@ func (s *Server) getVpc(w http.ResponseWriter, _ *http.Request, name string) {
|
|||
}
|
||||
|
||||
func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string) {
|
||||
s.dispatcher.Dispatch(dispatcher.DeleteVPCCommand{Name: name})
|
||||
cmd := dispatcher.DeleteVPCCommand{Name: name}
|
||||
if err := s.dispatcher.Prepare(cmd); err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
s.dispatcher.Dispatch(cmd)
|
||||
state, _ := kv.GetFromDB(s.db, "vpc/"+name+"/state")
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(VPC{Name: name, State: "deleting"})
|
||||
json.NewEncoder(w).Encode(VPC{Name: name, State: state})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,7 +62,14 @@ func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) {
|
|||
json.NewEncoder(w).Encode(ErrorResponse{Error: "name is required"})
|
||||
return
|
||||
}
|
||||
s.dispatcher.Dispatch(dispatcher.CreateVPCCommand{Name: req.Name})
|
||||
cmd := dispatcher.CreateVPCCommand{Name: req.Name}
|
||||
if err := s.dispatcher.Prepare(cmd); err != nil {
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
s.dispatcher.Dispatch(cmd)
|
||||
state, _ := kv.GetFromDB(s.db, "vpc/"+req.Name+"/state")
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(VPC{Name: req.Name, State: "creating"})
|
||||
json.NewEncoder(w).Encode(VPC{Name: req.Name, State: state})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue