f-21: refacto: move dispatcher to agent specific files
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
0c2dbdb525
commit
19434b1848
9 changed files with 6 additions and 8 deletions
31
internal/dispatcher/agent/dispatcher.go
Normal file
31
internal/dispatcher/agent/dispatcher.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package dispatcher
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/pkg/worker"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
type Command interface {
|
||||
Execute(db *badger.DB, cfg *configuration.Config) error
|
||||
}
|
||||
|
||||
type Dispatcher struct {
|
||||
queue *worker.Queue
|
||||
db *badger.DB
|
||||
cfg *configuration.Config
|
||||
}
|
||||
|
||||
func New(queue *worker.Queue, db *badger.DB, cfg *configuration.Config) *Dispatcher {
|
||||
return &Dispatcher{queue: queue, db: db, cfg: cfg}
|
||||
}
|
||||
|
||||
func (d *Dispatcher) Dispatch(cmd Command) {
|
||||
d.queue.Submit(func() {
|
||||
if err := cmd.Execute(d.db, d.cfg); err != nil {
|
||||
log.Printf("command error (%T): %v", cmd, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
54
internal/dispatcher/agent/subnet_commands.go
Normal file
54
internal/dispatcher/agent/subnet_commands.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package dispatcher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/internal/subnet"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
type CreateSubnetCommand struct {
|
||||
Name string
|
||||
VPC string
|
||||
VxlanID int
|
||||
IfaceType string
|
||||
GatewayIP string
|
||||
CIDR string
|
||||
}
|
||||
|
||||
func (c CreateSubnetCommand) Execute(db *badger.DB, cfg *configuration.Config) error {
|
||||
localIface, ok := cfg.Interfaces[c.IfaceType]
|
||||
if !ok {
|
||||
localIface = cfg.DefaultInterface
|
||||
}
|
||||
kv.AddInDB(db, "subnet/"+c.Name+"/state", "creating")
|
||||
kv.AddInDB(db, "subnet/"+c.Name+"/vpc", c.VPC)
|
||||
kv.AddInDB(db, "subnet/"+c.Name+"/vxlan_id", strconv.Itoa(c.VxlanID))
|
||||
kv.AddInDB(db, "subnet/"+c.Name+"/local_iface", localIface)
|
||||
kv.AddInDB(db, "subnet/"+c.Name+"/gateway_ip", c.GatewayIP)
|
||||
kv.AddInDB(db, "subnet/"+c.Name+"/cidr", c.CIDR)
|
||||
return subnet.CreateSubnet(db, c.Name)
|
||||
}
|
||||
|
||||
type DeleteSubnetCommand struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (c DeleteSubnetCommand) Execute(db *badger.DB, _ *configuration.Config) error {
|
||||
kv.AddInDB(db, "subnet/"+c.Name+"/state", "deleting")
|
||||
if err := subnet.DeleteSubnet(db, c.Name); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if state, err := kv.GetFromDB(db, "subnet/"+c.Name+"/state"); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
} else if state == "deleted" {
|
||||
kv.DeleteInDB(db, "subnet/"+c.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
36
internal/dispatcher/agent/vpc_commands.go
Normal file
36
internal/dispatcher/agent/vpc_commands.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package dispatcher
|
||||
|
||||
import (
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"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, _ *configuration.Config) 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, _ *configuration.Config) 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