f-37: code: add state error in return nil #37

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-08-13 23:12:32 +02:00
commit fa72b6cd47
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
4 changed files with 18 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import (
"time"
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/internal/state"
"git.g3e.fr/syonad/two/pkg/worker"
"github.com/dgraph-io/badger/v4"
)
@ -13,6 +14,7 @@ import (
type Command interface {
Prepare(db *badger.DB, cfg *configuration.Config) error
Execute(db *badger.DB, cfg *configuration.Config) error
Key() string
}
type Dispatcher struct {
@ -43,6 +45,10 @@ func (d *Dispatcher) Dispatch(cmd Command) {
}
if err != nil {
d.logger.Error("command failed", append(attrs, "error", err)...)
if setErr := state.Set(d.db, cmd.Key(), state.Error); setErr != nil {
d.logger.Error("failed to mark resource as errored",
"command", cmdType, "key", cmd.Key(), "error", setErr)
}
} else {
d.logger.Info("command done", attrs...)
}

View file

@ -22,6 +22,8 @@ type CreateSubnetCommand struct {
DefaultRoute bool
}
func (c CreateSubnetCommand) Key() string { return "subnet/" + c.Name }
func (c CreateSubnetCommand) Prepare(db *badger.DB, cfg *configuration.Config) error {
if c.Mode == "" {
c.Mode = "vxlan"
@ -79,6 +81,8 @@ type DeleteSubnetCommand struct {
Name string
}
func (c DeleteSubnetCommand) Key() string { return "subnet/" + c.Name }
func (c DeleteSubnetCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
if _, err := kv.GetFromDB(db, "subnet/"+c.Name+"/state"); err != nil {
return fmt.Errorf("subnet %q not found", c.Name)

View file

@ -30,6 +30,8 @@ type StartVMCommand struct {
SSHKey string
}
func (c StartVMCommand) Key() string { return "vm/" + c.Name }
func (c StartVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
if _, err := kv.GetFromDB(db, "vm/"+c.Name+"/state"); err == nil {
return fmt.Errorf("vm %q already exists", c.Name)
@ -111,6 +113,8 @@ type StopVMCommand struct {
Name string
}
func (c StopVMCommand) Key() string { return "vm/" + c.Name }
func (c StopVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
if _, err := kv.GetFromDB(db, "vm/"+c.Name+"/state"); err != nil {
return fmt.Errorf("vm %q not found", c.Name)

View file

@ -17,6 +17,8 @@ type CreateVPCCommand struct {
CIDR string
}
func (c CreateVPCCommand) Key() string { return "vpc/" + c.Name }
func (c CreateVPCCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
if _, err := kv.GetFromDB(db, "vpc/"+c.Name+"/state"); err == nil {
return fmt.Errorf("vpc %q already exists", c.Name)
@ -38,6 +40,8 @@ type DeleteVPCCommand struct {
Name string
}
func (c DeleteVPCCommand) Key() string { return "vpc/" + c.Name }
func (c DeleteVPCCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
if _, err := kv.GetFromDB(db, "vpc/"+c.Name+"/state"); err != nil {
return fmt.Errorf("vpc %q not found", c.Name)