2024-01-01 15:33:08 -06:00
|
|
|
package digitalocean
|
|
|
|
|
2024-01-24 22:55:34 -06:00
|
|
|
import (
|
2024-01-01 15:33:08 -06:00
|
|
|
"os"
|
2024-01-24 22:55:34 -06:00
|
|
|
|
|
|
|
"go.wit.com/gui"
|
2024-01-01 15:33:08 -06:00
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
var myDo *DigitalOcean
|
|
|
|
|
|
|
|
// This is initializes the main DO object
|
|
|
|
// You can only have one of these
|
|
|
|
func New(p *gui.Node) *DigitalOcean {
|
2024-01-24 22:55:34 -06:00
|
|
|
if myDo != nil {
|
|
|
|
return myDo
|
|
|
|
}
|
2024-01-01 15:33:08 -06:00
|
|
|
myDo = new(DigitalOcean)
|
|
|
|
myDo.ready = false
|
|
|
|
myDo.parent = p
|
|
|
|
|
|
|
|
myDo.dropMap = make(map[int]*Droplet)
|
|
|
|
|
|
|
|
// Your personal API token from DigitalOcean.
|
|
|
|
myDo.token = os.Getenv("DIGITALOCEAN_TOKEN")
|
|
|
|
|
|
|
|
myDo.window = p.NewWindow("DigitalOcean Control Panel")
|
|
|
|
|
|
|
|
// make a group label and a grid
|
|
|
|
myDo.group = myDo.window.NewGroup("droplets:").Pad()
|
|
|
|
myDo.grid = myDo.group.NewGrid("grid", 2, 1).Pad()
|
|
|
|
|
|
|
|
myDo.ready = true
|
|
|
|
myDo.Hide()
|
|
|
|
return myDo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the status is valid
|
|
|
|
func (d *DigitalOcean) Ready() bool {
|
2024-01-24 22:55:34 -06:00
|
|
|
if d == nil {
|
|
|
|
return false
|
|
|
|
}
|
2024-01-01 15:33:08 -06:00
|
|
|
return d.ready
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DigitalOcean) Show() {
|
2024-01-24 22:55:34 -06:00
|
|
|
if !d.Ready() {
|
|
|
|
return
|
|
|
|
}
|
2024-01-01 15:33:08 -06:00
|
|
|
log.Info("digitalocean.Show() window")
|
|
|
|
if d.hidden {
|
|
|
|
d.window.Show()
|
|
|
|
}
|
|
|
|
d.hidden = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DigitalOcean) Hide() {
|
2024-01-24 22:55:34 -06:00
|
|
|
if !d.Ready() {
|
|
|
|
return
|
|
|
|
}
|
2024-01-01 15:33:08 -06:00
|
|
|
log.Info("digitalocean.Hide() window")
|
2024-01-24 22:55:34 -06:00
|
|
|
if !d.hidden {
|
2024-01-01 15:33:08 -06:00
|
|
|
d.window.Hide()
|
|
|
|
}
|
|
|
|
d.hidden = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DigitalOcean) Update() bool {
|
2024-01-24 22:55:34 -06:00
|
|
|
if !d.Ready() {
|
|
|
|
return false
|
|
|
|
}
|
2024-01-01 15:33:08 -06:00
|
|
|
d.ListSSHKeyID()
|
|
|
|
if d.ListDroplets() {
|
|
|
|
for _, droplet := range d.dpolled {
|
|
|
|
// check if the droplet ID already exists
|
2024-01-24 22:55:34 -06:00
|
|
|
if d.dropMap[droplet.ID] == nil {
|
2024-01-01 15:33:08 -06:00
|
|
|
d.dropMap[droplet.ID] = d.NewDroplet(&droplet)
|
|
|
|
} else {
|
|
|
|
log.Log(POLL, "droplet.Update()", droplet.ID, droplet.Name, "already exists")
|
|
|
|
d.dropMap[droplet.ID].Update(&droplet)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Error(d.err, "Error listing droplets")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|