Compare commits

..

4 commits

Author SHA1 Message Date
7e3bf7cdd3
f-21: fix move dispatch
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-23 22:49:51 +02:00
3218b1affc
f-21: refacto: move dispatcher to agent specific files
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-23 22:38:20 +02:00
0c2dbdb525
f-21: api: make subnet get
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-23 22:30:04 +02:00
77cf180b10
f-21: api: make vpc get
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-23 22:29:43 +02:00
9 changed files with 112 additions and 14 deletions

View file

@ -7,7 +7,7 @@ import (
agentapi "git.g3e.fr/syonad/two/internal/api/agent"
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/internal/dispatcher"
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
agentmetrics "git.g3e.fr/syonad/two/internal/prometheus/agent"
"git.g3e.fr/syonad/two/pkg/db/kv"
promserver "git.g3e.fr/syonad/two/pkg/prometheus"

View file

@ -4,7 +4,7 @@ import (
"log"
"net/http"
"git.g3e.fr/syonad/two/internal/dispatcher"
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
"github.com/dgraph-io/badger/v4"
)

View file

@ -3,9 +3,11 @@ package agentapi
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"git.g3e.fr/syonad/two/internal/dispatcher"
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
"git.g3e.fr/syonad/two/pkg/db/kv"
)
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)
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) {
@ -37,4 +66,3 @@ func (s *Server) deleteSubnet(w http.ResponseWriter, r *http.Request, name strin
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(Subnet{Name: name, State: "deleting"})
}

View file

@ -3,8 +3,11 @@ package agentapi
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"git.g3e.fr/syonad/two/internal/dispatcher"
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) {
@ -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)
json.NewEncoder(w).Encode([]Subnet{})
json.NewEncoder(w).Encode(result)
}
func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {

View file

@ -5,7 +5,8 @@ import (
"net/http"
"strings"
"git.g3e.fr/syonad/two/internal/dispatcher"
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
"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) {
@ -37,4 +44,3 @@ func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string)
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(VPC{Name: name, State: "deleting"})
}

View file

@ -3,8 +3,10 @@ package agentapi
import (
"encoding/json"
"net/http"
"strings"
"git.g3e.fr/syonad/two/internal/dispatcher"
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
"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) {