f-88: api: nom obligatoire des interfaces de VM, migration nic-<i> #88
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
74e8c6220e
commit
ee7ecbf69f
15 changed files with 244 additions and 25 deletions
43
internal/migration/nicname.go
Normal file
43
internal/migration/nicname.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package migration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
func MigrateVMNICNames(db *badger.DB, log *slog.Logger) error {
|
||||
vms, err := kv.ListByPrefix(db, "vm/")
|
||||
if err != nil {
|
||||
return fmt.Errorf("list vm/: %w", err)
|
||||
}
|
||||
for _, prefix := range nicsWithoutName(vms) {
|
||||
name := "nic-" + strings.Split(prefix, "/")[3]
|
||||
if err := kv.AddInDB(db, prefix+"name", name); err != nil {
|
||||
return fmt.Errorf("store %sname: %w", prefix, err)
|
||||
}
|
||||
log.Info("vm nic name migrated", "resource", strings.TrimSuffix(prefix, "/"), "name", name, "reason", "interface name now required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func nicsWithoutName(entries map[string]string) []string {
|
||||
var prefixes []string
|
||||
for key := range entries {
|
||||
parts := strings.Split(key, "/")
|
||||
if len(parts) != 5 || parts[2] != "nic" || parts[4] != "subnet" {
|
||||
continue
|
||||
}
|
||||
prefix := strings.TrimSuffix(key, "subnet")
|
||||
if _, ok := entries[prefix+"name"]; !ok {
|
||||
prefixes = append(prefixes, prefix)
|
||||
}
|
||||
}
|
||||
sort.Strings(prefixes)
|
||||
return prefixes
|
||||
}
|
||||
52
internal/migration/nicname_test.go
Normal file
52
internal/migration/nicname_test.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package migration
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
)
|
||||
|
||||
func TestMigrateVMNICNames_NamesEveryInterfaceByItsIndex(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.0.0.10"}, 1: {"sn-b", "10.0.1.10"}})
|
||||
if err := MigrateVMNICNames(db, discardLogger()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertKey(t, db, "vm/vm-1/nic/0/name", "nic-0")
|
||||
assertKey(t, db, "vm/vm-1/nic/1/name", "nic-1")
|
||||
}
|
||||
|
||||
func TestMigrateVMNICNames_KeepsAnExistingName(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.0.0.10"}, 1: {"sn-b", "10.0.1.10"}})
|
||||
kv.AddInDB(db, "vm/vm-1/nic/0/name", "nic-a1b2")
|
||||
if err := MigrateVMNICNames(db, discardLogger()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertKey(t, db, "vm/vm-1/nic/0/name", "nic-a1b2")
|
||||
assertKey(t, db, "vm/vm-1/nic/1/name", "nic-1")
|
||||
}
|
||||
|
||||
func TestMigrateVMNICNames_IsIdempotent(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.0.0.10"}})
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := MigrateVMNICNames(db, discardLogger()); err != nil {
|
||||
t.Fatalf("passage %d : %v", i+1, err)
|
||||
}
|
||||
}
|
||||
assertKey(t, db, "vm/vm-1/nic/0/name", "nic-0")
|
||||
}
|
||||
|
||||
func TestMigrateVMNICNames_IgnoresKeysOutsideInterfaces(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
kv.AddInDB(db, "vm/vm-1/state", "running")
|
||||
kv.AddInDB(db, "vm/vm-1/disk/vda", "/data/vda.qcow2")
|
||||
if err := MigrateVMNICNames(db, discardLogger()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, _ := kv.ListByPrefix(db, "vm/vm-1/")
|
||||
if len(entries) != 2 {
|
||||
t.Errorf("clés après migration : %v", entries)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue