show droplets

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-12-30 12:20:20 -06:00
parent 7532fb5ff5
commit a117923b32
5 changed files with 115 additions and 41 deletions

69
digitalocean/droplet.go Normal file
View File

@ -0,0 +1,69 @@
package digitalocean
import (
"fmt"
"github.com/digitalocean/godo"
"go.wit.com/log"
// "go.wit.com/gui"
)
func (d *DigitalOcean) NewDroplet(dd godo.Droplet) *Droplet {
if ! myDo.Ready() {return nil}
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", 2, 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)
})
}
}
for _, network := range dd.Networks.V6 {
if network.Type == "public" {
fmt.Printf("IPv6: %s\n", network.IPAddress)
}
}
fmt.Println("-------------------------")
droplet.ready = true
return droplet
}
func (d *Droplet) Show() {
if ! myDo.Ready() {return}
log.Info("droplet.Show() window")
if d.hidden {
// my.window.Show()
}
d.hidden = false
}
func (d *Droplet) Hide() {
if ! myDo.Ready() {return}
log.Info("droplet.Hide() window")
if ! d.hidden {
// d.window.Hide()
}
d.hidden = true
}
func (d *Droplet) Exists() bool {
if ! myDo.Ready() {return false}
return true
}

View File

@ -10,9 +10,9 @@ import (
)
// ListDroplets fetches and prints out the droplets along with their IPv4 and IPv6 addresses.
func ListDroplets(token string) error {
func (d *DigitalOcean) ListDroplets() bool {
// OAuth token for authentication.
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
// OAuth2 client.
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
@ -24,13 +24,13 @@ func ListDroplets(token string) error {
ctx := context.TODO()
// List all droplets.
droplets, _, err := client.Droplets.List(ctx, &godo.ListOptions{})
if err != nil {
return err
d.droplets, _, d.err = client.Droplets.List(ctx, &godo.ListOptions{})
if d.err != nil {
return false
}
// Iterate over droplets and print their details.
for _, droplet := range droplets {
for _, droplet := range d.droplets {
fmt.Printf("Droplet: %s\n", droplet.Name)
for _, network := range droplet.Networks.V4 {
if network.Type == "public" {
@ -45,5 +45,5 @@ func ListDroplets(token string) error {
fmt.Println("-------------------------")
}
return nil
return true
}

View File

@ -27,6 +27,7 @@ func New(p *gui.Node) *DigitalOcean {
myDo.grid = myDo.group.NewGrid("grid", 2, 1).Pad()
myDo.ready = true
myDo.Hide()
return myDo
}
@ -56,10 +57,12 @@ func (d *DigitalOcean) Hide() {
func (d *DigitalOcean) Update() bool {
if ! d.Ready() {return false}
err := ListDroplets(d.token)
if err != nil {
log.Error(err, "Error listing droplets")
if ! d.ListDroplets() {
log.Error(d.err, "Error listing droplets")
return false
}
for _, droplet := range d.droplets {
d.NewDroplet(droplet)
}
return true
}

View File

@ -5,6 +5,8 @@
package digitalocean
import (
"github.com/digitalocean/godo"
"go.wit.com/gui"
"go.wit.com/gui/gadgets"
)
@ -12,17 +14,46 @@ import (
type DigitalOcean struct {
ready bool
hidden bool
err error
token string // You're Digital Ocean API key
droplets []godo.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
dGrid *gui.Node // the grid for the droplets
// Primary Directives
status *gadgets.OneLiner
summary *gadgets.OneLiner
statusIPv4 *gadgets.OneLiner
statusIPv6 *gadgets.OneLiner
}
type ipButton struct {
ip *gui.Node
c *gui.Node
}
type Droplet struct {
ready bool
hidden bool
err error
poll godo.Droplet // store what the digital ocean API returned
name *gui.Node
// a box and grid of the IPv4 addresses
box4 *gui.Node
grid4 *gui.Node
ipv4 []ipButton
// a box and grid of the IPv6 addresses
box6 *gui.Node
grid6 *gui.Node
ipv6 []ipButton
}

View File

@ -1,18 +1,5 @@
package main
import (
"context"
"fmt"
"os"
"golang.org/x/oauth2"
"go.wit.com/log"
"go.wit.com/gui"
"github.com/digitalocean/godo"
"go.wit.com/control-panel-dns/digitalocean"
)
/*
// createDroplet creates a new droplet in the specified region with the given name.
func createDroplet(token, name, region, size, image string) (*godo.Droplet, error) {
@ -48,25 +35,8 @@ func createDroplet(token, name, region, size, image string) (*godo.Droplet, erro
}
*/
/*
func oldMain() {
// Your personal API token from DigitalOcean.
token := os.Getenv("DIGITALOCEAN_TOKEN")
if token == "" {
log.Fatal("Please set your DigitalOcean API token in the DIGITALOCEAN_TOKEN environment variable")
}
// List droplets and their IP addresses.
err := digitalocean.ListDroplets(token)
if err != nil {
log.Fatalf("Error listing droplets: %s\n", err)
}
// This is just a optional goroutine to watch that things are alive
gui.Watchdog()
gui.StandardExit()
os.Exit(0)
// Parameters for the droplet you wish to create.
name := "ipv6.wit.com"
region := "nyc1" // New York City region.
@ -113,3 +83,4 @@ func createDropletNew(token, name, region, size, image string) (*godo.Droplet, e
return newDroplet, nil
}
*/