f-88: api: ressource /floating-ips, refus et affichage sur la VM #88

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-29 21:48:12 +01:00
commit 653b1b0277
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
15 changed files with 942 additions and 12 deletions

View file

@ -44,6 +44,13 @@ func (s *Server) getVM(w http.ResponseWriter, _ *http.Request, name string) {
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to read vm"})
return
}
floating, err := kv.ListByPrefix(s.db, "floating-ip/")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to list floating ips"})
return
}
attachFloatingIPs(&vm, floating)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(vm)
}
@ -145,3 +152,18 @@ func vmFromDB(name string, entries map[string]string) (VM, error) {
return vm, nil
}
func attachFloatingIPs(vm *VM, entries map[string]string) {
for key, value := range entries {
name, ok := strings.CutSuffix(strings.TrimPrefix(key, "floating-ip/"), "/vm")
if !ok || strings.Contains(name, "/") || value != vm.Name {
continue
}
prefix := "floating-ip/" + name + "/"
for i := range vm.Interfaces {
if vm.Interfaces[i].Name == entries[prefix+"interface"] {
vm.Interfaces[i].FloatingIP = &VMFloatingIP{Name: name, IP: entries[prefix+"ip"], State: entries[prefix+"state"]}
}
}
}
}