summaryrefslogtreecommitdiff
path: root/api.go
blob: b1568b16f8338d98c84c6ff3082806c179f9ac37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
}