From be03e85c2def1501b6b22572d8a1d140663194ab Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 30 Dec 2023 22:41:58 -0600 Subject: [PATCH] shows the droplets and if they are on or off Signed-off-by: Jeff Carr --- digitalocean/droplet.go | 70 ++++++++++++++++++- digitalocean/main.go | 13 +++- .../{listDroplets.go => pollDroplets.go} | 7 +- digitalocean/structs.go | 14 +++- examples/control-panel-digitalocean/main.go | 7 +- 5 files changed, 98 insertions(+), 13 deletions(-) rename digitalocean/{listDroplets.go => pollDroplets.go} (89%) diff --git a/digitalocean/droplet.go b/digitalocean/droplet.go index 7238453..45deb3d 100644 --- a/digitalocean/droplet.go +++ b/digitalocean/droplet.go @@ -1,6 +1,7 @@ package digitalocean import ( + "errors" "github.com/digitalocean/godo" "go.wit.com/log" @@ -10,12 +11,18 @@ import ( func (d *DigitalOcean) NewDroplet(dd *godo.Droplet) *Droplet { if ! myDo.Ready() {return nil} + // check if the droplet ID already exists + if (d.dropMap[dd.ID] != nil) { + log.Error(errors.New("droplet.NewDroplet() already exists")) + return d.dropMap[dd.ID] + } + droplet := new(Droplet) droplet.ready = false droplet.poll = dd // the information polled from the digital ocean API if (d.dGrid == nil) { - d.dGrid = d.group.NewGrid("grid", 5, 1).Pad() + d.dGrid = d.group.NewGrid("grid", 9, 1).Pad() } droplet.name = d.dGrid.NewLabel(dd.Name) @@ -35,19 +42,76 @@ func (d *DigitalOcean) NewDroplet(dd *godo.Droplet) *Droplet { } } - d.dGrid.NewButton("Connect", func () { + droplet.status = d.dGrid.NewLabel(dd.Status) + + droplet.connect = d.dGrid.NewButton("Connect", func () { droplet.Connect() }) - d.dGrid.NewButton("Show", func () { + droplet.edit = d.dGrid.NewButton("Edit", func () { droplet.Show() }) + droplet.poweroff = d.dGrid.NewButton("Power Off", func () { + droplet.PowerOff() + }) + + droplet.poweron = d.dGrid.NewButton("Power On", func () { + droplet.PowerOn() + }) + + droplet.destroy = d.dGrid.NewButton("Destroy", func () { + droplet.Destroy() + }) + droplet.ready = true return droplet } +func (d *Droplet) Active() bool { + if ! d.Exists() {return false} + log.Info("droplet.Active() status: ", d.poll.Status) + if (d.status.S == "active") { + return true + } + return false +} + func (d *Droplet) Connect() { + if ! d.Exists() {return} + log.Info("droplet.Connect() here") +} + +func (d *Droplet) Update(dpoll *godo.Droplet) { + if ! d.Exists() {return} + d.poll = dpoll + log.Info("droplet.Update()", dpoll.Name, "dpoll.Status =", dpoll.Status) + log.Spew(dpoll) + d.status.SetText(dpoll.Status) + if d.Active() { + d.poweron.Disable() + d.destroy.Disable() + d.connect.Enable() + d.poweroff.Enable() + } else { + d.poweron.Enable() + d.destroy.Enable() + d.poweroff.Disable() + d.connect.Disable() + } +} + +func (d *Droplet) PowerOn() { + if ! d.Exists() {return} + log.Info("droplet.PowerOn() should do it here") +} + +func (d *Droplet) PowerOff() { + if ! d.Exists() {return} + log.Info("droplet.PowerOff() here") +} + +func (d *Droplet) Destroy() { if ! d.Exists() {return} log.Info("droplet.Connect() ssh here") } diff --git a/digitalocean/main.go b/digitalocean/main.go index 7b1b4e4..de96521 100644 --- a/digitalocean/main.go +++ b/digitalocean/main.go @@ -13,9 +13,10 @@ var myDo *DigitalOcean func New(p *gui.Node) *DigitalOcean { if myDo != nil {return myDo} myDo = new(DigitalOcean) + myDo.ready = false myDo.parent = p - myDo.ready = false + myDo.dropMap = make(map[int]*Droplet) // Your personal API token from DigitalOcean. myDo.token = os.Getenv("DIGITALOCEAN_TOKEN") @@ -61,8 +62,14 @@ func (d *DigitalOcean) Update() bool { log.Error(d.err, "Error listing droplets") return false } - for _, droplet := range d.droplets { - d.NewDroplet(&droplet) + for _, droplet := range d.dpolled { + // check if the droplet ID already exists + if (d.dropMap[droplet.ID] != nil) { + log.Info("droplet.Update()", droplet.ID, droplet.Name, "already exists") + d.dropMap[droplet.ID].Update(&droplet) + continue + } + d.dropMap[droplet.ID] = d.NewDroplet(&droplet) } return true } diff --git a/digitalocean/listDroplets.go b/digitalocean/pollDroplets.go similarity index 89% rename from digitalocean/listDroplets.go rename to digitalocean/pollDroplets.go index 52840e6..b150978 100644 --- a/digitalocean/listDroplets.go +++ b/digitalocean/pollDroplets.go @@ -2,7 +2,6 @@ package digitalocean import ( "context" - "fmt" "golang.org/x/oauth2" @@ -24,13 +23,14 @@ func (d *DigitalOcean) ListDroplets() bool { ctx := context.TODO() // List all droplets. - d.droplets, _, d.err = client.Droplets.List(ctx, &godo.ListOptions{}) + d.dpolled, _, d.err = client.Droplets.List(ctx, &godo.ListOptions{}) if d.err != nil { return false } // Iterate over droplets and print their details. - for _, droplet := range d.droplets { + /* + for _, droplet := range d.polled { fmt.Printf("Droplet: %s\n", droplet.Name) for _, network := range droplet.Networks.V4 { if network.Type == "public" { @@ -44,6 +44,7 @@ func (d *DigitalOcean) ListDroplets() bool { } fmt.Println("-------------------------") } + */ return true } diff --git a/digitalocean/structs.go b/digitalocean/structs.go index 3ae4f3e..6f12994 100644 --- a/digitalocean/structs.go +++ b/digitalocean/structs.go @@ -17,12 +17,14 @@ type DigitalOcean struct { err error token string // You're Digital Ocean API key - droplets []godo.Droplet + dpolled []godo.Droplet + + dropMap map[int]*Droplet parent *gui.Node // should be the root of the 'gui' package binary tree window *gui.Node // our window for displaying digital ocean droplets - group *gui.Node // our window for displaying digital ocean droplets - grid *gui.Node // our window for displaying digital ocean droplets + group *gui.Node + grid *gui.Node dGrid *gui.Node // the grid for the droplets @@ -46,6 +48,12 @@ type Droplet struct { poll *godo.Droplet // store what the digital ocean API returned name *gui.Node + status *gui.Node + destroy *gui.Node + connect *gui.Node + poweron *gui.Node + poweroff *gui.Node + edit *gui.Node // a box and grid of the IPv4 addresses box4 *gui.Node diff --git a/examples/control-panel-digitalocean/main.go b/examples/control-panel-digitalocean/main.go index b8170ca..28455f3 100644 --- a/examples/control-panel-digitalocean/main.go +++ b/examples/control-panel-digitalocean/main.go @@ -1,7 +1,7 @@ package main import ( - // "go.wit.com/log" + "go.wit.com/log" "go.wit.com/gui" "go.wit.com/control-panel-dns/digitalocean" ) @@ -17,6 +17,11 @@ func main() { // draw the main window cloudApp(myGui) + log.Sleep(1) + myDo = digitalocean.New(myGui) + myDo.Update() + myDo.Show() + // This is just a optional goroutine to watch that things are alive gui.Watchdog() gui.StandardExit()