diff --git a/digitalocean/api.go b/digitalocean/api.go new file mode 100644 index 0000000..b1568b1 --- /dev/null +++ b/digitalocean/api.go @@ -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 +} diff --git a/digitalocean/create.go b/digitalocean/create.go index 03bcf57..a64ba90 100644 --- a/digitalocean/create.go +++ b/digitalocean/create.go @@ -45,11 +45,12 @@ func createDroplet(token, name, region, size, image string) (*godo.Droplet, erro } */ -func (d *DigitalOcean) Create(name string) { - region := "nyc1" // New York City region. +func (d *DigitalOcean) Create(name string, region 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. + return // Create a new droplet. droplet, err := d.createDropletNew(name, region, size, image) if err != nil { @@ -126,9 +127,36 @@ func InitCreateWindow() *windowCreate { 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.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 diff --git a/digitalocean/structs.go b/digitalocean/structs.go index bfb607f..997c810 100644 --- a/digitalocean/structs.go +++ b/digitalocean/structs.go @@ -49,6 +49,7 @@ type windowCreate struct { tag *gadgets.OneLiner name *gadgets.BasicEntry + zone *gadgets.BasicDropdown } type ipButton struct {