f-21: refactor: add dispatcher layer for MQTT migration
Introduce internal/dispatcher package with a Command interface and typed commands (CreateVPC, DeleteVPC, CreateSubnet, DeleteSubnet). The API handlers now call dispatcher.Dispatch() instead of enqueuing closures directly, decoupling transport (HTTP today, MQTT tomorrow) from execution. Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
7ee8527582
commit
63a288f69e
9 changed files with 114 additions and 39 deletions
29
internal/dispatcher/dispatcher.go
Normal file
29
internal/dispatcher/dispatcher.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package dispatcher
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/worker"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
type Command interface {
|
||||
Execute(db *badger.DB) error
|
||||
}
|
||||
|
||||
type Dispatcher struct {
|
||||
queue *worker.Queue
|
||||
db *badger.DB
|
||||
}
|
||||
|
||||
func New(queue *worker.Queue, db *badger.DB) *Dispatcher {
|
||||
return &Dispatcher{queue: queue, db: db}
|
||||
}
|
||||
|
||||
func (d *Dispatcher) Dispatch(cmd Command) {
|
||||
d.queue.Submit(func() {
|
||||
if err := cmd.Execute(d.db); err != nil {
|
||||
log.Printf("command error (%T): %v", cmd, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
26
internal/dispatcher/subnet_commands.go
Normal file
26
internal/dispatcher/subnet_commands.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package dispatcher
|
||||
|
||||
import "github.com/dgraph-io/badger/v4"
|
||||
|
||||
type CreateSubnetCommand struct {
|
||||
Name string
|
||||
VPC string
|
||||
VxlanID int
|
||||
LocalIP string
|
||||
GatewayIP string
|
||||
CIDR string
|
||||
}
|
||||
|
||||
func (c CreateSubnetCommand) Execute(db *badger.DB) error {
|
||||
// TODO: brancher internal/subnet/create.go
|
||||
return nil
|
||||
}
|
||||
|
||||
type DeleteSubnetCommand struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (c DeleteSubnetCommand) Execute(db *badger.DB) error {
|
||||
// TODO: brancher internal/subnet/delete.go
|
||||
return nil
|
||||
}
|
||||
35
internal/dispatcher/vpc_commands.go
Normal file
35
internal/dispatcher/vpc_commands.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package dispatcher
|
||||
|
||||
import (
|
||||
"git.g3e.fr/syonad/two/internal/vpc"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
type CreateVPCCommand struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (c CreateVPCCommand) Execute(db *badger.DB) error {
|
||||
kv.AddInDB(db, "vpc/"+c.Name+"/state", "creating")
|
||||
return vpc.CreateVPC(db, c.Name)
|
||||
}
|
||||
|
||||
type DeleteVPCCommand struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (c DeleteVPCCommand) Execute(db *badger.DB) error {
|
||||
kv.AddInDB(db, "vpc/"+c.Name+"/state", "deleting")
|
||||
if err := vpc.DeleteVPC(db, c.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
state, err := kv.GetFromDB(db, "vpc/"+c.Name+"/state")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if state == "deleted" {
|
||||
kv.DeleteInDB(db, "vpc/"+c.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue