github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/digitalocean/config.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"log"
     5  	"time"
     6  
     7  	"github.com/digitalocean/godo"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"golang.org/x/oauth2"
    10  )
    11  
    12  type Config struct {
    13  	Token string
    14  }
    15  
    16  // Client() returns a new client for accessing digital ocean.
    17  func (c *Config) Client() (*godo.Client, error) {
    18  	tokenSrc := oauth2.StaticTokenSource(&oauth2.Token{
    19  		AccessToken: c.Token,
    20  	})
    21  
    22  	client := godo.NewClient(oauth2.NewClient(oauth2.NoContext, tokenSrc))
    23  
    24  	log.Printf("[INFO] DigitalOcean Client configured for URL: %s", client.BaseURL.String())
    25  
    26  	return client, nil
    27  }
    28  
    29  // waitForAction waits for the action to finish using the resource.StateChangeConf.
    30  func waitForAction(client *godo.Client, action *godo.Action) error {
    31  	var (
    32  		pending   = "in-progress"
    33  		target    = "completed"
    34  		refreshfn = func() (result interface{}, state string, err error) {
    35  			a, _, err := client.Actions.Get(action.ID)
    36  			if err != nil {
    37  				return nil, "", err
    38  			}
    39  			if a.Status == "errored" {
    40  				return a, "errored", nil
    41  			}
    42  			if a.CompletedAt != nil {
    43  				return a, target, nil
    44  			}
    45  			return a, pending, nil
    46  		}
    47  	)
    48  	_, err := (&resource.StateChangeConf{
    49  		Pending: []string{pending},
    50  		Refresh: refreshfn,
    51  		Target:  []string{target},
    52  
    53  		Delay:      10 * time.Second,
    54  		Timeout:    60 * time.Minute,
    55  		MinTimeout: 3 * time.Second,
    56  
    57  		// This is a hack around DO API strangeness.
    58  		// https://github.com/hashicorp/terraform/issues/481
    59  		//
    60  		NotFoundChecks: 60,
    61  	}).WaitForState()
    62  	return err
    63  }