create takes a name. delete() droplet works
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
44730e1b91
commit
d2f0691744
|
@ -0,0 +1,161 @@
|
|||
package digitalocean
|
||||
|
||||
import (
|
||||
"context"
|
||||
"golang.org/x/oauth2"
|
||||
"github.com/digitalocean/godo"
|
||||
|
||||
"go.wit.com/log"
|
||||
"go.wit.com/gui/gadgets"
|
||||
// "go.wit.com/gui"
|
||||
)
|
||||
|
||||
/*
|
||||
// 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 (d *DigitalOcean) Create(name string) {
|
||||
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 := d.createDropletNew(name, region, size, image)
|
||||
if err != nil {
|
||||
log.Fatalf("Something went wrong: %s\n", err)
|
||||
}
|
||||
|
||||
log.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 (d *DigitalOcean) createDropletNew(name, region, size, image string) (*godo.Droplet, error) {
|
||||
// Create an OAuth2 token.
|
||||
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
|
||||
|
||||
// Create an OAuth2 client.
|
||||
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
|
||||
|
||||
// Create a DigitalOcean client with the OAuth2 client.
|
||||
client := godo.NewClient(oauthClient)
|
||||
|
||||
var sshKeys []godo.DropletCreateSSHKey
|
||||
|
||||
// Find the key by name.
|
||||
for i, key := range d.sshKeys {
|
||||
log.Info("found ssh i =", i, key.Name)
|
||||
log.Verbose("found ssh key.Name =", key.Name)
|
||||
log.Verbose("found ssh key.Fingerprint =", key.Fingerprint)
|
||||
log.Verbose("found ssh key:", key)
|
||||
/*
|
||||
sshKeys = []godo.DropletCreateSSHKey{
|
||||
{ID: key.ID},
|
||||
}
|
||||
*/
|
||||
sshKeys = append(sshKeys, godo.DropletCreateSSHKey{ID: key.ID})
|
||||
}
|
||||
|
||||
// Define the create request.
|
||||
createRequest := &godo.DropletCreateRequest{
|
||||
Name: name,
|
||||
Region: region,
|
||||
Size: size,
|
||||
Image: godo.DropletCreateImage{
|
||||
Slug: image,
|
||||
},
|
||||
IPv6: true, // Enable IPv6
|
||||
SSHKeys: sshKeys, // Add SSH key IDs here
|
||||
}
|
||||
|
||||
// Create the droplet.
|
||||
ctx := context.TODO()
|
||||
newDroplet, _, err := client.Droplets.Create(ctx, createRequest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newDroplet, nil
|
||||
}
|
||||
|
||||
var myCreate *windowCreate
|
||||
|
||||
// This is initializes the main DO object
|
||||
// You can only have one of these
|
||||
func InitCreateWindow() *windowCreate {
|
||||
if ! myDo.Ready() {return nil}
|
||||
if myCreate != nil {return myCreate}
|
||||
myCreate = new(windowCreate)
|
||||
myCreate.ready = false
|
||||
|
||||
myCreate.window = myDo.parent.NewWindow("Create Droplet")
|
||||
|
||||
// make a group label and a grid
|
||||
myCreate.group = myCreate.window.NewGroup("droplets:").Pad()
|
||||
myCreate.grid = myCreate.group.NewGrid("grid", 2, 1).Pad()
|
||||
|
||||
myCreate.name = gadgets.NewBasicEntry(myCreate.grid, "Name").Set("test.wit.com")
|
||||
|
||||
myCreate.grid.NewLabel("makes a new droplet")
|
||||
myCreate.grid.NewButton("Create", func () {
|
||||
myDo.Create(myCreate.name.Get())
|
||||
})
|
||||
|
||||
myCreate.ready = true
|
||||
myDo.create = myCreate
|
||||
return myCreate
|
||||
}
|
||||
|
||||
// Returns true if the status is valid
|
||||
func (d *windowCreate) Ready() bool {
|
||||
if d == nil {return false}
|
||||
return d.ready
|
||||
}
|
||||
|
||||
func (d *windowCreate) Show() {
|
||||
if ! d.Ready() {return}
|
||||
log.Info("digitalocean.Show() window")
|
||||
if d.hidden {
|
||||
d.window.Show()
|
||||
}
|
||||
d.hidden = false
|
||||
}
|
||||
|
||||
func (d *windowCreate) Hide() {
|
||||
if ! d.Ready() {return}
|
||||
log.Info("digitalocean.Hide() window")
|
||||
if ! d.hidden {
|
||||
d.window.Hide()
|
||||
}
|
||||
d.hidden = true
|
||||
}
|
|
@ -116,7 +116,8 @@ func (d *Droplet) PowerOff() {
|
|||
|
||||
func (d *Droplet) Destroy() {
|
||||
if ! d.Exists() {return}
|
||||
log.Info("droplet.Connect() ssh here")
|
||||
log.Info("droplet.Destroy() ID =", d.ID, "Name =", d.name)
|
||||
myDo.deleteDroplet(d)
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -2,9 +2,7 @@ package digitalocean
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
|
||||
"github.com/digitalocean/godo"
|
||||
|
||||
"go.wit.com/log"
|
||||
|
@ -22,16 +20,19 @@ func (d *DigitalOcean) ListSSHKeyID() error {
|
|||
return err
|
||||
}
|
||||
|
||||
d.sshKeys = keys
|
||||
|
||||
// Find the key by name.
|
||||
for i, key := range keys {
|
||||
log.Info("found ssh i =", i)
|
||||
log.Info("found ssh key.Name =", key.Name)
|
||||
log.Info("found ssh key.Fingerprint =", key.Fingerprint)
|
||||
log.Info("found ssh key:", key)
|
||||
// if key.Name == name {
|
||||
// return key.Fingerprint, nil
|
||||
// }
|
||||
for _, key := range keys {
|
||||
log.Info("found ssh", key.Name)
|
||||
log.Verbose("found ssh key:", key)
|
||||
}
|
||||
/*
|
||||
sshKeys := []godo.DropletCreateSSHKey{
|
||||
{ID: 22994569},
|
||||
{ID: 333},
|
||||
}
|
||||
*/
|
||||
|
||||
// return fmt.Errorf("SSH Key not found")
|
||||
return nil
|
||||
|
|
|
@ -40,6 +40,43 @@ func (d *DigitalOcean) PowerOff(dropletID int) error {
|
|||
return err
|
||||
}
|
||||
|
||||
log.Printf("Power-on signal sent to droplet with ID: %d\n", dropletID)
|
||||
log.Printf("Power-off signal sent to droplet with ID: %d\n", dropletID)
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
func (d *DigitalOcean) Destroy(dropletID int) error {
|
||||
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
|
||||
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
|
||||
client := godo.NewClient(oauthClient)
|
||||
|
||||
ctx := context.TODO()
|
||||
|
||||
// Create a request to power on the droplet.
|
||||
_, _, err := client.DropletActions.Delete(ctx, dropletID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Destroy sent to droplet with ID: %d\n", dropletID)
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
// createDroplet creates a new droplet in the specified region with the given name.
|
||||
func (d *DigitalOcean) deleteDroplet(drop *Droplet) error {
|
||||
// Create an OAuth2 token.
|
||||
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
|
||||
|
||||
// Create an OAuth2 client.
|
||||
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
|
||||
|
||||
// Create a DigitalOcean client with the OAuth2 client.
|
||||
client := godo.NewClient(oauthClient)
|
||||
|
||||
ctx := context.TODO()
|
||||
log.Warn("deleteDroplet() going to delete ID =", drop.ID, "Name =", drop.name.GetText())
|
||||
response, err := client.Droplets.Delete(ctx, drop.ID)
|
||||
log.Warn(response)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -18,8 +18,10 @@ type DigitalOcean struct {
|
|||
|
||||
token string // You're Digital Ocean API key
|
||||
dpolled []godo.Droplet
|
||||
sshKeys []godo.Key
|
||||
|
||||
dropMap map[int]*Droplet
|
||||
create *windowCreate
|
||||
|
||||
parent *gui.Node // should be the root of the 'gui' package binary tree
|
||||
window *gui.Node // our window for displaying digital ocean droplets
|
||||
|
@ -35,6 +37,20 @@ type DigitalOcean struct {
|
|||
statusIPv6 *gadgets.OneLiner
|
||||
}
|
||||
|
||||
type windowCreate struct {
|
||||
ready bool
|
||||
hidden bool
|
||||
err error
|
||||
|
||||
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
|
||||
grid *gui.Node
|
||||
|
||||
tag *gadgets.OneLiner
|
||||
name *gadgets.BasicEntry
|
||||
}
|
||||
|
||||
type ipButton struct {
|
||||
ip *gui.Node
|
||||
c *gui.Node
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
# export GO111MODULE="off"
|
||||
run: build
|
||||
reset
|
||||
./control-panel-digitalocean --gui-debug --log-debug
|
||||
|
||||
build-release:
|
||||
|
|
|
@ -54,5 +54,11 @@ func cloudApp(n *gui.Node) *gui.Node {
|
|||
})
|
||||
grid.NewLabel("polls DO via the API to find the state of all your droplets")
|
||||
|
||||
grid.NewButton("Create", func () {
|
||||
// myDo.Create("jcarr.wit.com")
|
||||
digitalocean.InitCreateWindow()
|
||||
})
|
||||
grid.NewLabel("makes a new droplet")
|
||||
|
||||
return win
|
||||
}
|
||||
|
|
|
@ -1,86 +0,0 @@
|
|||
package main
|
||||
|
||||
/*
|
||||
// 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() {
|
||||
// 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