forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
63 lines (53 loc) · 1.45 KB
/
config.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package digitalocean
import (
"log"
"time"
"github.com/digitalocean/godo"
"github.com/hashicorp/terraform/helper/resource"
"golang.org/x/oauth2"
)
type Config struct {
Token string
}
// Client() returns a new client for accessing digital ocean.
func (c *Config) Client() (*godo.Client, error) {
tokenSrc := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: c.Token,
})
client := godo.NewClient(oauth2.NewClient(oauth2.NoContext, tokenSrc))
log.Printf("[INFO] DigitalOcean Client configured for URL: %s", client.BaseURL.String())
return client, nil
}
// waitForAction waits for the action to finish using the resource.StateChangeConf.
func waitForAction(client *godo.Client, action *godo.Action) error {
var (
pending = "in-progress"
target = "completed"
refreshfn = func() (result interface{}, state string, err error) {
a, _, err := client.Actions.Get(action.ID)
if err != nil {
return nil, "", err
}
if a.Status == "errored" {
return a, "errored", nil
}
if a.CompletedAt != nil {
return a, target, nil
}
return a, pending, nil
}
)
_, err := (&resource.StateChangeConf{
Pending: []string{pending},
Refresh: refreshfn,
Target: []string{target},
Delay: 10 * time.Second,
Timeout: 60 * time.Minute,
MinTimeout: 3 * time.Second,
// This is a hack around DO API strangeness.
// https://github.com/hashicorp/terraform/issues/481
//
NotFoundChecks: 60,
}).WaitForState()
return err
}