shows the droplets and if they are on or off

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-12-30 22:41:58 -06:00
parent eb007d63d9
commit be03e85c2d
5 changed files with 98 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package digitalocean package digitalocean
import ( import (
"errors"
"github.com/digitalocean/godo" "github.com/digitalocean/godo"
"go.wit.com/log" "go.wit.com/log"
@ -10,12 +11,18 @@ import (
func (d *DigitalOcean) NewDroplet(dd *godo.Droplet) *Droplet { func (d *DigitalOcean) NewDroplet(dd *godo.Droplet) *Droplet {
if ! myDo.Ready() {return nil} 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 := new(Droplet)
droplet.ready = false droplet.ready = false
droplet.poll = dd // the information polled from the digital ocean API droplet.poll = dd // the information polled from the digital ocean API
if (d.dGrid == nil) { 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) 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() droplet.Connect()
}) })
d.dGrid.NewButton("Show", func () { droplet.edit = d.dGrid.NewButton("Edit", func () {
droplet.Show() 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 droplet.ready = true
return droplet 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() { 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} if ! d.Exists() {return}
log.Info("droplet.Connect() ssh here") log.Info("droplet.Connect() ssh here")
} }

View File

@ -13,9 +13,10 @@ var myDo *DigitalOcean
func New(p *gui.Node) *DigitalOcean { func New(p *gui.Node) *DigitalOcean {
if myDo != nil {return myDo} if myDo != nil {return myDo}
myDo = new(DigitalOcean) myDo = new(DigitalOcean)
myDo.ready = false
myDo.parent = p myDo.parent = p
myDo.ready = false myDo.dropMap = make(map[int]*Droplet)
// Your personal API token from DigitalOcean. // Your personal API token from DigitalOcean.
myDo.token = os.Getenv("DIGITALOCEAN_TOKEN") myDo.token = os.Getenv("DIGITALOCEAN_TOKEN")
@ -61,8 +62,14 @@ func (d *DigitalOcean) Update() bool {
log.Error(d.err, "Error listing droplets") log.Error(d.err, "Error listing droplets")
return false return false
} }
for _, droplet := range d.droplets { for _, droplet := range d.dpolled {
d.NewDroplet(&droplet) // 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 return true
} }

View File

@ -2,7 +2,6 @@ package digitalocean
import ( import (
"context" "context"
"fmt"
"golang.org/x/oauth2" "golang.org/x/oauth2"
@ -24,13 +23,14 @@ func (d *DigitalOcean) ListDroplets() bool {
ctx := context.TODO() ctx := context.TODO()
// List all droplets. // 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 { if d.err != nil {
return false return false
} }
// Iterate over droplets and print their details. // Iterate over droplets and print their details.
for _, droplet := range d.droplets { /*
for _, droplet := range d.polled {
fmt.Printf("Droplet: %s\n", droplet.Name) fmt.Printf("Droplet: %s\n", droplet.Name)
for _, network := range droplet.Networks.V4 { for _, network := range droplet.Networks.V4 {
if network.Type == "public" { if network.Type == "public" {
@ -44,6 +44,7 @@ func (d *DigitalOcean) ListDroplets() bool {
} }
fmt.Println("-------------------------") fmt.Println("-------------------------")
} }
*/
return true return true
} }

View File

@ -17,12 +17,14 @@ type DigitalOcean struct {
err error err error
token string // You're Digital Ocean API key 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 parent *gui.Node // should be the root of the 'gui' package binary tree
window *gui.Node // our window for displaying digital ocean droplets window *gui.Node // our window for displaying digital ocean droplets
group *gui.Node // our window for displaying digital ocean droplets group *gui.Node
grid *gui.Node // our window for displaying digital ocean droplets grid *gui.Node
dGrid *gui.Node // the grid for the droplets 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 poll *godo.Droplet // store what the digital ocean API returned
name *gui.Node 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 // a box and grid of the IPv4 addresses
box4 *gui.Node box4 *gui.Node

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
// "go.wit.com/log" "go.wit.com/log"
"go.wit.com/gui" "go.wit.com/gui"
"go.wit.com/control-panel-dns/digitalocean" "go.wit.com/control-panel-dns/digitalocean"
) )
@ -17,6 +17,11 @@ func main() {
// draw the main window // draw the main window
cloudApp(myGui) 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 // This is just a optional goroutine to watch that things are alive
gui.Watchdog() gui.Watchdog()
gui.StandardExit() gui.StandardExit()