droplet create has region

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-12-31 05:48:29 -06:00
parent d2f0691744
commit c5ff4637fb
3 changed files with 68 additions and 3 deletions

36
digitalocean/api.go Normal file
View File

@ -0,0 +1,36 @@
package digitalocean
import (
"context"
"golang.org/x/oauth2"
"github.com/digitalocean/godo"
"go.wit.com/log"
)
func (d *DigitalOcean) listRegions() []godo.Region {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
client := godo.NewClient(oauthClient)
ctx := context.TODO()
// Retrieve all regions.
regions, _, err := client.Regions.List(ctx, &godo.ListOptions{})
if err != nil {
d.err = err
log.Warn(err, "digitalocean.listRegions() failed")
return nil
}
/*
// Print details of each region.
fmt.Println("Available Regions:")
for _, region := range regions {
fmt.Printf("Slug: %s, Name: %s, Available: %v\n", region.Slug, region.Name, region.Available)
}
*/
return regions
}

View File

@ -45,11 +45,12 @@ func createDroplet(token, name, region, size, image string) (*godo.Droplet, erro
} }
*/ */
func (d *DigitalOcean) Create(name string) { func (d *DigitalOcean) Create(name string, region string) {
region := "nyc1" // New York City region. // region := "nyc1" // New York City region.
size := "s-1vcpu-1gb" // Size of the droplet. size := "s-1vcpu-1gb" // Size of the droplet.
image := "ubuntu-20-04-x64" // Image slug for Ubuntu 20.04 (LTS) x64. image := "ubuntu-20-04-x64" // Image slug for Ubuntu 20.04 (LTS) x64.
return
// Create a new droplet. // Create a new droplet.
droplet, err := d.createDropletNew(name, region, size, image) droplet, err := d.createDropletNew(name, region, size, image)
if err != nil { if err != nil {
@ -126,9 +127,36 @@ func InitCreateWindow() *windowCreate {
myCreate.name = gadgets.NewBasicEntry(myCreate.grid, "Name").Set("test.wit.com") myCreate.name = gadgets.NewBasicEntry(myCreate.grid, "Name").Set("test.wit.com")
myCreate.zone = gadgets.NewBasicDropdown(myCreate.grid, "Region")
regions := myDo.listRegions()
// Print details of each region.
log.Info("Available Regions:")
for i, region := range regions {
log.Infof("i: %d, Slug: %s, Name: %s, Available: %v\n", i, region.Slug, region.Name, region.Available)
log.Spew(i, region)
myCreate.zone.Add(region.Name)
}
var zone godo.Region
myCreate.zone.Custom = func() {
s := myCreate.zone.Get()
log.Info("create droplet region changed to:", s)
for _, region := range regions {
if s == region.Name {
log.Info("Found region! slug =", region.Slug, region)
zone = region
}
}
}
myCreate.grid.NewLabel("makes a new droplet") myCreate.grid.NewLabel("makes a new droplet")
myCreate.grid.NewButton("Create", func () { myCreate.grid.NewButton("Create", func () {
myDo.Create(myCreate.name.Get()) name := myCreate.name.Get()
log.Info("create droplet name =", name, "zone =", zone.Slug)
myDo.Create(name, zone.Slug)
}) })
myCreate.ready = true myCreate.ready = true

View File

@ -49,6 +49,7 @@ type windowCreate struct {
tag *gadgets.OneLiner tag *gadgets.OneLiner
name *gadgets.BasicEntry name *gadgets.BasicEntry
zone *gadgets.BasicDropdown
} }
type ipButton struct { type ipButton struct {