Compare commits
1 commit
b0c73b2952
...
53ca41e635
| Author | SHA1 | Date | |
|---|---|---|---|
|
53ca41e635 |
9 changed files with 36 additions and 146 deletions
BIN
agent
Executable file
BIN
agent
Executable file
Binary file not shown.
|
|
@ -23,7 +23,7 @@ func main() {
|
|||
log.Fatalf("failed to load config: %v", err)
|
||||
}
|
||||
|
||||
db := kv.InitDB(kv.Config{Path: cfg.Database.Path}, false)
|
||||
db := kv.InitDB(kv.Config{Path: cfg.Database.Path}, true)
|
||||
defer db.Close()
|
||||
|
||||
q := worker.New(cfg.Worker.BufferSize)
|
||||
|
|
|
|||
|
|
@ -1,33 +0,0 @@
|
|||
package agentapi
|
||||
|
||||
type VPCCreateRequest struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type VPC struct {
|
||||
Name string `json:"name"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
type SubnetCreateRequest struct {
|
||||
Name string `json:"name"`
|
||||
VPC string `json:"vpc"`
|
||||
VxlanID int `json:"vxlan_id"`
|
||||
LocalIP string `json:"local_ip"`
|
||||
GatewayIP string `json:"gateway_ip"`
|
||||
CIDR string `json:"cidr"`
|
||||
}
|
||||
|
||||
type Subnet struct {
|
||||
Name string `json:"name"`
|
||||
State string `json:"state"`
|
||||
VPC string `json:"vpc"`
|
||||
VxlanID int `json:"vxlan_id"`
|
||||
LocalIP string `json:"local_ip"`
|
||||
GatewayIP string `json:"gateway_ip"`
|
||||
CIDR string `json:"cidr"`
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
|
@ -22,12 +22,5 @@ func (s *Server) Start(address string) {
|
|||
mux.HandleFunc("/subnets", s.SubnetsHandler)
|
||||
mux.HandleFunc("/subnets/", s.SubnetByNameHandler)
|
||||
log.Printf("API server listening on %s", address)
|
||||
log.Fatal(http.ListenAndServe(address, logMiddleware(mux)))
|
||||
}
|
||||
|
||||
func logMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL.Path)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
log.Fatal(http.ListenAndServe(address, mux))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,33 +9,22 @@ import (
|
|||
func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||
name := strings.TrimPrefix(r.URL.Path, "/subnets/")
|
||||
if name == "" {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "resource not found"})
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
s.getSubnet(w, r, name)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{"name": name})
|
||||
case http.MethodDelete:
|
||||
s.deleteSubnet(w, r, name)
|
||||
s.queue.Submit(func() {
|
||||
deleteSubnet(name)
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
default:
|
||||
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) getSubnet(w http.ResponseWriter, r *http.Request, name string) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(Subnet{Name: name, State: "created"})
|
||||
}
|
||||
|
||||
func (s *Server) deleteSubnet(w http.ResponseWriter, r *http.Request, name string) {
|
||||
s.queue.Submit(func() {
|
||||
destroySubnet(name)
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(Subnet{Name: name, State: "deleting"})
|
||||
}
|
||||
|
||||
func destroySubnet(name string) {}
|
||||
func deleteSubnet(name string) {}
|
||||
|
|
|
|||
|
|
@ -9,44 +9,16 @@ 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)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode([]interface{}{})
|
||||
case http.MethodPost:
|
||||
s.postSubnet(w, r)
|
||||
s.queue.Submit(func() {
|
||||
createSubnet()
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
default:
|
||||
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) listSubnets(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode([]Subnet{})
|
||||
}
|
||||
|
||||
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.LocalIP == "" || req.GatewayIP == "" || req.CIDR == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, local_ip, gateway_ip and cidr are required"})
|
||||
return
|
||||
}
|
||||
s.queue.Submit(func() {
|
||||
createSubnet(req)
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(Subnet{
|
||||
Name: req.Name,
|
||||
State: "creating",
|
||||
VPC: req.VPC,
|
||||
VxlanID: req.VxlanID,
|
||||
LocalIP: req.LocalIP,
|
||||
GatewayIP: req.GatewayIP,
|
||||
CIDR: req.CIDR,
|
||||
})
|
||||
}
|
||||
|
||||
func createSubnet(req SubnetCreateRequest) {}
|
||||
func createSubnet() {}
|
||||
|
|
|
|||
|
|
@ -9,33 +9,22 @@ import (
|
|||
func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||
name := strings.TrimPrefix(r.URL.Path, "/vpcs/")
|
||||
if name == "" {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "resource not found"})
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
s.getVpc(w, r, name)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{"name": name})
|
||||
case http.MethodDelete:
|
||||
s.deleteVpc(w, r, name)
|
||||
s.queue.Submit(func() {
|
||||
deleteVpc(name)
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
default:
|
||||
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) getVpc(w http.ResponseWriter, r *http.Request, name string) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(VPC{Name: name, State: "created"})
|
||||
}
|
||||
|
||||
func (s *Server) deleteVpc(w http.ResponseWriter, r *http.Request, name string) {
|
||||
s.queue.Submit(func() {
|
||||
destroyVpc(name)
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(VPC{Name: name, State: "deleting"})
|
||||
}
|
||||
|
||||
func destroyVpc(name string) {}
|
||||
func deleteVpc(name string) {}
|
||||
|
|
|
|||
|
|
@ -9,36 +9,16 @@ func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) {
|
|||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
s.listVpcs(w, r)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode([]interface{}{})
|
||||
case http.MethodPost:
|
||||
s.postVpc(w, r)
|
||||
s.queue.Submit(func() {
|
||||
createVpc()
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
default:
|
||||
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) listVpcs(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode([]VPC{})
|
||||
}
|
||||
|
||||
func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) {
|
||||
var req VPCCreateRequest
|
||||
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 == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "name is required"})
|
||||
return
|
||||
}
|
||||
s.queue.Submit(func() {
|
||||
createVpc(req.Name)
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(VPC{Name: req.Name, State: "creating"})
|
||||
}
|
||||
|
||||
func createVpc(name string) {}
|
||||
func createVpc() {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue