f-41: subnet: export du préfixe dans la VRF, suppression refusée tant qu'une VM l'utilise #41
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
3c6852dc18
commit
fe27d45262
12 changed files with 253 additions and 2 deletions
|
|
@ -2,7 +2,9 @@ package dispatcher
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
|
|
@ -101,9 +103,36 @@ func (c DeleteSubnetCommand) Prepare(db *badger.DB, _ *configuration.Config) err
|
|||
if !state.CanDelete(current) {
|
||||
return fmt.Errorf("subnet %q cannot be deleted while %s", c.Name, current)
|
||||
}
|
||||
vms, err := vmsUsingSubnet(db, c.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(vms) > 0 {
|
||||
return fmt.Errorf("subnet %q is still used by vms %s", c.Name, strings.Join(vms, ", "))
|
||||
}
|
||||
return state.Set(db, c.Key(), state.Deleting)
|
||||
}
|
||||
|
||||
func vmsUsingSubnet(db *badger.DB, subnetName string) ([]string, error) {
|
||||
entries, err := kv.ListByPrefix(db, "vm/")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list vms: %w", err)
|
||||
}
|
||||
var users []string
|
||||
for key, value := range entries {
|
||||
parts := strings.Split(key, "/")
|
||||
if len(parts) != 5 || parts[2] != "nic" || parts[4] != "subnet" || value != subnetName {
|
||||
continue
|
||||
}
|
||||
if s, err := state.Get(db, "vm/"+parts[1]); err == nil && s == state.Deleted {
|
||||
continue
|
||||
}
|
||||
users = append(users, parts[1])
|
||||
}
|
||||
sort.Strings(users)
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (c DeleteSubnetCommand) Execute(db *badger.DB, cfg *configuration.Config) error {
|
||||
if err := subnet.DeleteSubnet(db, c.Name, cfg); err != nil {
|
||||
return err
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue