create a basic digitalocean gui object
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
ec5cbbe203
commit
7532fb5ff5
|
@ -1,18 +1,65 @@
|
|||
package digitalocean
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"os"
|
||||
"go.wit.com/log"
|
||||
"go.wit.com/gui"
|
||||
)
|
||||
|
||||
func MakeWindow(n *gui.Node) *gui.Node {
|
||||
log.Println("digitalocean MakeWindow() START")
|
||||
var myDo *DigitalOcean
|
||||
|
||||
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")
|
||||
group := win.NewGroup("data")
|
||||
log.Println("digitalocean MakeWindow() END", group)
|
||||
return win
|
||||
myDo.ready = false
|
||||
|
||||
// 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("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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
# export GO111MODULE="off"
|
||||
run: build
|
||||
./control-panel-digitalocean
|
||||
./control-panel-digitalocean --gui-debug
|
||||
|
||||
build-release:
|
||||
go get -v -u -x .
|
||||
|
|
|
@ -1,124 +1,53 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
|
||||
"go.wit.com/log"
|
||||
// "go.wit.com/log"
|
||||
"go.wit.com/gui"
|
||||
"github.com/digitalocean/godo"
|
||||
"go.wit.com/control-panel-dns/digitalocean"
|
||||
)
|
||||
|
||||
var title string = "Digital Ocean Control Panel"
|
||||
|
||||
/*
|
||||
// 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
|
||||
}
|
||||
*/
|
||||
var title string = "Cloud App"
|
||||
var myGui *gui.Node
|
||||
var myDo *digitalocean.DigitalOcean
|
||||
|
||||
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
|
||||
myGui := gui.New().Default()
|
||||
myGui = gui.New().Default()
|
||||
|
||||
// draw the cloudflare control panel window
|
||||
win := digitalocean.MakeWindow(myGui)
|
||||
win.SetText(title)
|
||||
// draw the main window
|
||||
cloudApp(myGui)
|
||||
|
||||
// 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})
|
||||
func cloudApp(n *gui.Node) *gui.Node {
|
||||
win := n.NewWindow(title)
|
||||
|
||||
// Create an OAuth2 client.
|
||||
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
|
||||
// make a group label and a grid
|
||||
group := win.NewGroup("data").Pad()
|
||||
grid := group.NewGrid("grid", 2, 1).Pad()
|
||||
|
||||
// Create a DigitalOcean client with the OAuth2 client.
|
||||
client := godo.NewClient(oauthClient)
|
||||
grid.NewButton("New()", func () {
|
||||
myDo = digitalocean.New(myGui)
|
||||
})
|
||||
grid.NewLabel("initializes the DO golang gui package")
|
||||
|
||||
// Define the create request.
|
||||
createRequest := &godo.DropletCreateRequest{
|
||||
Name: name,
|
||||
Region: region,
|
||||
Size: size,
|
||||
Image: godo.DropletCreateImage{
|
||||
Slug: image,
|
||||
},
|
||||
IPv6: true, // Enable IPv6
|
||||
}
|
||||
grid.NewButton("Show", func () {
|
||||
myDo.Show()
|
||||
})
|
||||
grid.NewLabel("will show the DO window")
|
||||
|
||||
// Create the droplet.
|
||||
ctx := context.TODO()
|
||||
newDroplet, _, err := client.Droplets.Create(ctx, createRequest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
grid.NewButton("Hide", func () {
|
||||
myDo.Hide()
|
||||
})
|
||||
grid.NewLabel("will hide the DO window")
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue