display droplets

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-12-30 20:47:14 -06:00
parent a117923b32
commit eb007d63d9
4 changed files with 78 additions and 22 deletions

View File

@ -1,14 +1,13 @@
package digitalocean
import (
"fmt"
"github.com/digitalocean/godo"
"go.wit.com/log"
// "go.wit.com/gui"
)
func (d *DigitalOcean) NewDroplet(dd godo.Droplet) *Droplet {
func (d *DigitalOcean) NewDroplet(dd *godo.Droplet) *Droplet {
if ! myDo.Ready() {return nil}
droplet := new(Droplet)
@ -16,46 +15,77 @@ func (d *DigitalOcean) NewDroplet(dd godo.Droplet) *Droplet {
droplet.poll = dd // the information polled from the digital ocean API
if (d.dGrid == nil) {
d.dGrid = d.group.NewGrid("grid", 2, 1).Pad()
d.dGrid = d.group.NewGrid("grid", 5, 1).Pad()
}
droplet.name = d.dGrid.NewLabel(dd.Name)
droplet.box4 = d.dGrid.NewBox("hBox", true)
droplet.grid4 = droplet.box4.NewGrid("grid", 2, 1).Pad()
fmt.Printf("Droplet: %s\n", dd.Name)
for _, network := range dd.Networks.V4 {
if network.Type == "public" {
fmt.Printf("IPv4: %s\n", network.IPAddress)
droplet.grid4.NewLabel(network.IPAddress)
droplet.grid4.NewButton("Connect", func () {
log.Info("ssh here", network.IPAddress)
})
}
}
droplet.box6 = d.dGrid.NewBox("hBox", true)
droplet.grid6 = droplet.box6.NewGrid("grid", 2, 1).Pad()
for _, network := range dd.Networks.V6 {
if network.Type == "public" {
fmt.Printf("IPv6: %s\n", network.IPAddress)
droplet.grid6.NewLabel(network.IPAddress)
}
}
fmt.Println("-------------------------")
d.dGrid.NewButton("Connect", func () {
droplet.Connect()
})
d.dGrid.NewButton("Show", func () {
droplet.Show()
})
droplet.ready = true
return droplet
}
func (d *Droplet) Connect() {
if ! d.Exists() {return}
log.Info("droplet.Connect() ssh here")
}
/*
type Droplet struct {
ID int `json:"id,float64,omitempty"`
Name string `json:"name,omitempty"`
Memory int `json:"memory,omitempty"`
Vcpus int `json:"vcpus,omitempty"`
Disk int `json:"disk,omitempty"`
Region *Region `json:"region,omitempty"`
Image *Image `json:"image,omitempty"`
Size *Size `json:"size,omitempty"`
SizeSlug string `json:"size_slug,omitempty"`
BackupIDs []int `json:"backup_ids,omitempty"`
NextBackupWindow *BackupWindow `json:"next_backup_window,omitempty"`
SnapshotIDs []int `json:"snapshot_ids,omitempty"`
Features []string `json:"features,omitempty"`
Locked bool `json:"locked,bool,omitempty"`
Status string `json:"status,omitempty"`
Networks *Networks `json:"networks,omitempty"`
Created string `json:"created_at,omitempty"`
Kernel *Kernel `json:"kernel,omitempty"`
Tags []string `json:"tags,omitempty"`
VolumeIDs []string `json:"volume_ids"`
VPCUUID string `json:"vpc_uuid,omitempty"`
}
*/
func (d *Droplet) Show() {
if ! myDo.Ready() {return}
log.Info("droplet.Show() window")
if d.hidden {
// my.window.Show()
}
d.hidden = false
if ! d.Exists() {return}
log.Info("droplet:", d.name.Name)
log.Info("droplet:", d.poll.ID, d.poll.Name, d.poll.Memory, d.poll.Disk, d.poll.Status)
log.Spew(d.poll)
}
func (d *Droplet) Hide() {
if ! myDo.Ready() {return}
if ! d.Exists() {return}
log.Info("droplet.Hide() window")
if ! d.hidden {
// d.window.Hide()
@ -65,5 +95,7 @@ func (d *Droplet) Hide() {
func (d *Droplet) Exists() bool {
if ! myDo.Ready() {return false}
return true
if d == nil {return false}
if d.poll == nil {return false}
return d.ready
}

24
digitalocean/json.go Normal file
View File

@ -0,0 +1,24 @@
package digitalocean
import (
"encoding/json"
)
// formatJSON takes an unformatted JSON string and returns a formatted version.
func FormatJSON(unformattedJSON string) (string, error) {
var jsonData interface{}
// Decode the JSON string into an interface
err := json.Unmarshal([]byte(unformattedJSON), &jsonData)
if err != nil {
return "", err
}
// Re-encode the JSON with indentation for formatting
formattedJSON, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
return "", err
}
return string(formattedJSON), nil
}

View File

@ -23,7 +23,7 @@ func New(p *gui.Node) *DigitalOcean {
myDo.window = p.NewWindow("DigitalOcean Control Panel")
// make a group label and a grid
myDo.group = myDo.window.NewGroup("data").Pad()
myDo.group = myDo.window.NewGroup("droplets:").Pad()
myDo.grid = myDo.group.NewGrid("grid", 2, 1).Pad()
myDo.ready = true
@ -62,7 +62,7 @@ func (d *DigitalOcean) Update() bool {
return false
}
for _, droplet := range d.droplets {
d.NewDroplet(droplet)
d.NewDroplet(&droplet)
}
return true
}

View File

@ -43,7 +43,7 @@ type Droplet struct {
hidden bool
err error
poll godo.Droplet // store what the digital ocean API returned
poll *godo.Droplet // store what the digital ocean API returned
name *gui.Node