create a basic digitalocean gui object

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-12-30 09:49:30 -06:00
parent ec5cbbe203
commit 7532fb5ff5
5 changed files with 230 additions and 111 deletions

View File

@ -1,18 +1,65 @@
package digitalocean package digitalocean
import ( import (
"log" "os"
"go.wit.com/log"
"go.wit.com/gui" "go.wit.com/gui"
) )
func MakeWindow(n *gui.Node) *gui.Node { var myDo *DigitalOcean
log.Println("digitalocean MakeWindow() START")
win := n.NewWindow("DigitalOcean Control Panel") // This is initializes the main DO object
// You can only have one of these
func New(p *gui.Node) *DigitalOcean {
if myDo != nil {return myDo}
myDo = new(DigitalOcean)
myDo.parent = p
// box := g1.NewGroup("data") myDo.ready = false
group := win.NewGroup("data")
log.Println("digitalocean MakeWindow() END", group) // Your personal API token from DigitalOcean.
return win 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("data").Pad()
myDo.grid = myDo.group.NewGrid("grid", 2, 1).Pad()
myDo.ready = true
return myDo
}
// Returns true if the status is valid
func (d *DigitalOcean) Ready() bool {
if d == nil {return false}
return d.ready
}
func (d *DigitalOcean) Show() {
if ! d.Ready() {return}
log.Info("digitalocean.Show() window")
if d.hidden {
d.window.Show()
}
d.hidden = false
}
func (d *DigitalOcean) Hide() {
if ! d.Ready() {return}
log.Info("digitalocean.Hide() window")
if ! d.hidden {
d.window.Hide()
}
d.hidden = true
}
func (d *DigitalOcean) Update() bool {
if ! d.Ready() {return false}
err := ListDroplets(d.token)
if err != nil {
log.Error(err, "Error listing droplets")
return false
}
return true
} }

28
digitalocean/structs.go Normal file
View File

@ -0,0 +1,28 @@
/*
The Digital Ocean Struct
*/
package digitalocean
import (
"go.wit.com/gui"
"go.wit.com/gui/gadgets"
)
type DigitalOcean struct {
ready bool
hidden bool
token string // You're Digital Ocean API key
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
// Primary Directives
status *gadgets.OneLiner
summary *gadgets.OneLiner
statusIPv4 *gadgets.OneLiner
statusIPv6 *gadgets.OneLiner
}

View File

@ -1,6 +1,6 @@
# export GO111MODULE="off" # export GO111MODULE="off"
run: build run: build
./control-panel-digitalocean ./control-panel-digitalocean --gui-debug
build-release: build-release:
go get -v -u -x . go get -v -u -x .

View File

@ -1,124 +1,53 @@
package main package main
import ( import (
"context" // "go.wit.com/log"
"fmt"
"os"
"golang.org/x/oauth2"
"go.wit.com/log"
"go.wit.com/gui" "go.wit.com/gui"
"github.com/digitalocean/godo"
"go.wit.com/control-panel-dns/digitalocean" "go.wit.com/control-panel-dns/digitalocean"
) )
var title string = "Digital Ocean Control Panel" var title string = "Cloud App"
var myGui *gui.Node
/* var myDo *digitalocean.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) {
// Create an OAuth2 token.
tokenSource := &oauth2.Token{
AccessToken: token,
}
// Create an OAuth2 client.
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
// Create a DigitalOcean client with the OAuth2 client.
client := godo.NewClient(oauthClient)
// Define the create request.
createRequest := &godo.DropletCreateRequest{
Name: name,
Region: region,
Size: size,
Image: godo.DropletCreateImage{
Slug: image,
},
}
// Create the droplet.
ctx := context.TODO()
newDroplet, _, err := client.Droplets.Create(ctx, createRequest)
if err != nil {
return nil, err
}
return newDroplet, nil
}
*/
func main() { func main() {
// 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)
}
// initialize a new GO GUI instance // initialize a new GO GUI instance
myGui := gui.New().Default() myGui = gui.New().Default()
// draw the cloudflare control panel window // draw the main window
win := digitalocean.MakeWindow(myGui) cloudApp(myGui)
win.SetText(title)
// 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()
os.Exit(0)
// Parameters for the droplet you wish to create.
name := "ipv6.wit.com"
region := "nyc1" // New York City region.
size := "s-1vcpu-1gb" // Size of the droplet.
image := "ubuntu-20-04-x64" // Image slug for Ubuntu 20.04 (LTS) x64.
// Create a new droplet.
droplet, err := createDropletNew(token, name, region, size, image)
if err != nil {
log.Fatalf("Something went wrong: %s\n", err)
}
fmt.Printf("Created droplet ID %d with name %s\n", droplet.ID, droplet.Name)
} }
// createDroplet creates a new droplet in the specified region with the given name. func cloudApp(n *gui.Node) *gui.Node {
func createDropletNew(token, name, region, size, image string) (*godo.Droplet, error) { win := n.NewWindow(title)
// Create an OAuth2 token.
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
// Create an OAuth2 client. // make a group label and a grid
oauthClient := oauth2.NewClient(context.Background(), tokenSource) group := win.NewGroup("data").Pad()
grid := group.NewGrid("grid", 2, 1).Pad()
// Create a DigitalOcean client with the OAuth2 client. grid.NewButton("New()", func () {
client := godo.NewClient(oauthClient) myDo = digitalocean.New(myGui)
})
grid.NewLabel("initializes the DO golang gui package")
// Define the create request. grid.NewButton("Show", func () {
createRequest := &godo.DropletCreateRequest{ myDo.Show()
Name: name, })
Region: region, grid.NewLabel("will show the DO window")
Size: size,
Image: godo.DropletCreateImage{
Slug: image,
},
IPv6: true, // Enable IPv6
}
// Create the droplet. grid.NewButton("Hide", func () {
ctx := context.TODO() myDo.Hide()
newDroplet, _, err := client.Droplets.Create(ctx, createRequest) })
if err != nil { grid.NewLabel("will hide the DO window")
return nil, err
}
return newDroplet, nil grid.NewButton("Update", func () {
myDo.Update()
})
grid.NewLabel("polls DO via the API to find the state of all your droplets")
return win
} }

View File

@ -0,0 +1,115 @@
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) {
// Create an OAuth2 token.
tokenSource := &oauth2.Token{
AccessToken: token,
}
// Create an OAuth2 client.
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
// Create a DigitalOcean client with the OAuth2 client.
client := godo.NewClient(oauthClient)
// Define the create request.
createRequest := &godo.DropletCreateRequest{
Name: name,
Region: region,
Size: size,
Image: godo.DropletCreateImage{
Slug: image,
},
}
// Create the droplet.
ctx := context.TODO()
newDroplet, _, err := client.Droplets.Create(ctx, createRequest)
if err != nil {
return nil, err
}
return newDroplet, nil
}
*/
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.
size := "s-1vcpu-1gb" // Size of the droplet.
image := "ubuntu-20-04-x64" // Image slug for Ubuntu 20.04 (LTS) x64.
// Create a new droplet.
droplet, err := createDropletNew(token, name, region, size, image)
if err != nil {
log.Fatalf("Something went wrong: %s\n", err)
}
fmt.Printf("Created droplet ID %d with name %s\n", droplet.ID, droplet.Name)
}
// createDroplet creates a new droplet in the specified region with the given name.
func createDropletNew(token, name, region, size, image string) (*godo.Droplet, error) {
// Create an OAuth2 token.
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
// Create an OAuth2 client.
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
// Create a DigitalOcean client with the OAuth2 client.
client := godo.NewClient(oauthClient)
// Define the create request.
createRequest := &godo.DropletCreateRequest{
Name: name,
Region: region,
Size: size,
Image: godo.DropletCreateImage{
Slug: image,
},
IPv6: true, // Enable IPv6
}
// Create the droplet.
ctx := context.TODO()
newDroplet, _, err := client.Droplets.Create(ctx, createRequest)
if err != nil {
return nil, err
}
return newDroplet, nil
}