f-21: api: make subnet get
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
77cf180b10
commit
0c2dbdb525
2 changed files with 71 additions and 4 deletions
|
|
@ -3,9 +3,11 @@ package agentapi
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.g3e.fr/syonad/two/internal/dispatcher"
|
"git.g3e.fr/syonad/two/internal/dispatcher"
|
||||||
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
@ -27,9 +29,36 @@ func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) getSubnet(w http.ResponseWriter, r *http.Request, name string) {
|
func (s *Server) getSubnet(w http.ResponseWriter, _ *http.Request, name string) {
|
||||||
|
entries, err := kv.ListByPrefix(s.db, "subnet/"+name+"/")
|
||||||
|
if err != nil || len(entries) == 0 {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "subnet not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sub := Subnet{Name: 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.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(Subnet{Name: name, State: "created"})
|
json.NewEncoder(w).Encode(sub)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) deleteSubnet(w http.ResponseWriter, r *http.Request, name string) {
|
func (s *Server) deleteSubnet(w http.ResponseWriter, r *http.Request, name string) {
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,11 @@ package agentapi
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.g3e.fr/syonad/two/internal/dispatcher"
|
"git.g3e.fr/syonad/two/internal/dispatcher"
|
||||||
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) SubnetsHandler(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) SubnetsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
@ -19,9 +22,44 @@ func (s *Server) SubnetsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) listSubnets(w http.ResponseWriter, r *http.Request) {
|
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)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode([]Subnet{})
|
json.NewEncoder(w).Encode(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue