Renamed MachineManager to MachineHandler. Did same for DropletManager.

This commit is contained in:
Gary Talent 2016-02-27 14:39:35 -06:00
parent a5481c055e
commit 18aa0471e9
3 changed files with 15 additions and 14 deletions

View File

@ -31,16 +31,16 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
dm := NewDropletManager(settings) dh := NewDropletHandler(settings)
ip, err := dm.SpinupMachine("minecraft") ip, err := dh.SpinupMachine("minecraft")
if err != nil { if err != nil {
log.Println("Error:", err) log.Println("Error:", err)
return return
} }
log.Println("IP: " + ip) log.Println("IP: " + ip)
if err := dm.SpindownMachine("minecraft"); err != nil { if err := dh.SpindownMachine("minecraft"); err != nil {
log.Println("Error:", err) log.Println("Error:", err)
return return
} }

View File

@ -28,13 +28,13 @@ func (t *tokenSource) Token() (*oauth2.Token, error) {
return token, nil return token, nil
} }
type DropletManager struct { type DropletHandler struct {
client *godo.Client client *godo.Client
settings Settings settings Settings
} }
func NewDropletManager(settings Settings) *DropletManager { func NewDropletHandler(settings Settings) *DropletHandler {
retval := new(DropletManager) retval := new(DropletHandler)
retval.settings = settings retval.settings = settings
// setup DO client // setup DO client
@ -48,7 +48,7 @@ func NewDropletManager(settings Settings) *DropletManager {
/* /*
Gets the Droplet if it already exists, instantiates it if it does not. Gets the Droplet if it already exists, instantiates it if it does not.
*/ */
func (me *DropletManager) SpinupMachine(name string) (string, error) { func (me *DropletHandler) SpinupMachine(name string) (string, error) {
if droplet, err := me.getDroplet(name); err == nil { if droplet, err := me.getDroplet(name); err == nil {
return droplet.PrivateIPv4() return droplet.PrivateIPv4()
} else { } else {
@ -106,7 +106,7 @@ func (me *DropletManager) SpinupMachine(name string) (string, error) {
} }
} }
func (me *DropletManager) SpindownMachine(name string) error { func (me *DropletHandler) SpindownMachine(name string) error {
droplet, err := me.getDroplet(name) droplet, err := me.getDroplet(name)
if err != nil { if err != nil {
return err return err
@ -151,7 +151,7 @@ func (me *DropletManager) SpindownMachine(name string) error {
return err return err
} }
func (me *DropletManager) getDroplet(name string) (godo.Droplet, error) { func (me *DropletHandler) getDroplet(name string) (godo.Droplet, error) {
name = DROPLET_NS + name name = DROPLET_NS + name
page := 0 page := 0
perPage := 200 perPage := 200
@ -181,7 +181,7 @@ func (me *DropletManager) getDroplet(name string) (godo.Droplet, error) {
return droplet, errors.New("Could not find droplet: " + name) return droplet, errors.New("Could not find droplet: " + name)
} }
func (me *DropletManager) getSnapshot(name string) (godo.Image, error) { func (me *DropletHandler) getSnapshot(name string) (godo.Image, error) {
name = DROPLET_NS + name name = DROPLET_NS + name
page := 0 page := 0
perPage := 200 perPage := 200
@ -216,7 +216,7 @@ func (me *DropletManager) getSnapshot(name string) (godo.Image, error) {
return image, err return image, err
} }
func (me *DropletManager) actionWait(actionId int) bool { func (me *DropletHandler) actionWait(actionId int) bool {
for { for {
a, _, err := me.client.Actions.Get(actionId) a, _, err := me.client.Actions.Get(actionId)
if err != nil { if err != nil {

View File

@ -13,9 +13,10 @@ import (
"strconv" "strconv"
) )
type MachineManager interface { type ServerHandler interface {
// Takes snapshot name, and returns the IP to connect to. // Takes snapshot name, and returns the IP to connect to.
SpinupMachine(name string) (string, error) SpinupMachine(name string) (string, error)
SpindownMachine(name string) error
} }
type service struct { type service struct {
@ -30,12 +31,12 @@ type service struct {
// Listens for clients on given ports to spin up machines for the ports // Listens for clients on given ports to spin up machines for the ports
type ServiceManager struct { type ServiceManager struct {
machineManager MachineManager machineManager ServerHandler
machineSvcCnt map[string]int machineSvcCnt map[string]int
svcConnStatus map[int]service svcConnStatus map[int]service
} }
func NewServiceHandler(mm MachineManager) *ServiceManager { func NewServiceHandler(mm ServerHandler) *ServiceManager {
sh := new(ServiceManager) sh := new(ServiceManager)
sh.machineManager = mm sh.machineManager = mm
return sh return sh