2014-07-19 02:20:28 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2016-07-13 16:36:37 +02:00
|
|
|
"time"
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
"github.com/digitalocean/godo"
|
2016-07-13 16:36:37 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-09-27 06:06:51 +02:00
|
|
|
"golang.org/x/oauth2"
|
2014-07-19 02:20:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2014-11-17 18:55:45 +01:00
|
|
|
Token string
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
// Client() returns a new client for accessing digital ocean.
|
2015-09-27 06:06:51 +02:00
|
|
|
func (c *Config) Client() (*godo.Client, error) {
|
|
|
|
tokenSrc := oauth2.StaticTokenSource(&oauth2.Token{
|
|
|
|
AccessToken: c.Token,
|
|
|
|
})
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
client := godo.NewClient(oauth2.NewClient(oauth2.NoContext, tokenSrc))
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
log.Printf("[INFO] DigitalOcean Client configured for URL: %s", client.BaseURL.String())
|
2014-07-19 02:20:28 +02:00
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
2016-07-13 16:36:37 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|