f-21: api: make vpc get
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
0f70b32076
commit
77cf180b10
2 changed files with 35 additions and 2 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/dispatcher"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
)
|
||||
|
||||
func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -28,8 +29,14 @@ func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (s *Server) getVpc(w http.ResponseWriter, _ *http.Request, name string) {
|
||||
state, err := kv.GetFromDB(s.db, "vpc/"+name+"/state")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "vpc not found"})
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(VPC{Name: name, State: "created"})
|
||||
json.NewEncoder(w).Encode(VPC{Name: name, State: state})
|
||||
}
|
||||
|
||||
func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string) {
|
||||
|
|
|
|||
|
|
@ -3,8 +3,10 @@ package agentapi
|
|||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/dispatcher"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
)
|
||||
|
||||
func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -20,8 +22,32 @@ func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (s *Server) listVpcs(w http.ResponseWriter, _ *http.Request) {
|
||||
entries, err := kv.ListByPrefix(s.db, "vpc/")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to list vpcs"})
|
||||
return
|
||||
}
|
||||
vpcs := make(map[string]*VPC)
|
||||
for key, value := range entries {
|
||||
parts := strings.Split(key, "/")
|
||||
if len(parts) != 3 {
|
||||
continue
|
||||
}
|
||||
name := parts[1]
|
||||
if _, ok := vpcs[name]; !ok {
|
||||
vpcs[name] = &VPC{Name: name}
|
||||
}
|
||||
if parts[2] == "state" {
|
||||
vpcs[name].State = value
|
||||
}
|
||||
}
|
||||
result := make([]VPC, 0, len(vpcs))
|
||||
for _, v := range vpcs {
|
||||
result = append(result, *v)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode([]VPC{})
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
|
||||
func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue