github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/builtin/providers/digitalocean/config.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  	"net/http/httputil"
     7  	"time"
     8  
     9  	"github.com/digitalocean/godo"
    10  	"github.com/hashicorp/terraform/helper/logging"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"golang.org/x/oauth2"
    13  )
    14  
    15  type Config struct {
    16  	Token string
    17  }
    18  
    19  // Client() returns a new client for accessing digital ocean.
    20  func (c *Config) Client() (*godo.Client, error) {
    21  	tokenSrc := oauth2.StaticTokenSource(&oauth2.Token{
    22  		AccessToken: c.Token,
    23  	})
    24  
    25  	client := godo.NewClient(oauth2.NewClient(oauth2.NoContext, tokenSrc))
    26  
    27  	if logging.IsDebugOrHigher() {
    28  		client.OnRequestCompleted(logRequestAndResponse)
    29  	}
    30  
    31  	log.Printf("[INFO] DigitalOcean Client configured for URL: %s", client.BaseURL.String())
    32  
    33  	return client, nil
    34  }
    35  
    36  func logRequestAndResponse(req *http.Request, resp *http.Response) {
    37  	reqData, err := httputil.DumpRequest(req, true)
    38  	if err == nil {
    39  		log.Printf("[DEBUG] "+logReqMsg, string(reqData))
    40  	} else {
    41  		log.Printf("[ERROR] DigitalOcean API Request error: %#v", err)
    42  	}
    43  
    44  	respData, err := httputil.DumpResponse(resp, true)
    45  	if err == nil {
    46  		log.Printf("[DEBUG] "+logRespMsg, string(respData))
    47  	} else {
    48  		log.Printf("[ERROR] DigitalOcean API Response error: %#v", err)
    49  	}
    50  }
    51  
    52  // waitForAction waits for the action to finish using the resource.StateChangeConf.
    53  func waitForAction(client *godo.Client, action *godo.Action) error {
    54  	var (
    55  		pending   = "in-progress"
    56  		target    = "completed"
    57  		refreshfn = func() (result interface{}, state string, err error) {
    58  			a, _, err := client.Actions.Get(action.ID)
    59  			if err != nil {
    60  				return nil, "", err
    61  			}
    62  			if a.Status == "errored" {
    63  				return a, "errored", nil
    64  			}
    65  			if a.CompletedAt != nil {
    66  				return a, target, nil
    67  			}
    68  			return a, pending, nil
    69  		}
    70  	)
    71  	_, err := (&resource.StateChangeConf{
    72  		Pending: []string{pending},
    73  		Refresh: refreshfn,
    74  		Target:  []string{target},
    75  
    76  		Delay:      10 * time.Second,
    77  		Timeout:    60 * time.Minute,
    78  		MinTimeout: 3 * time.Second,
    79  
    80  		// This is a hack around DO API strangeness.
    81  		// https://github.com/hashicorp/terraform/issues/481
    82  		//
    83  		NotFoundChecks: 60,
    84  	}).WaitForState()
    85  	return err
    86  }
    87  
    88  const logReqMsg = `DigitalOcean API Request Details:
    89  ---[ REQUEST ]---------------------------------------
    90  %s
    91  -----------------------------------------------------`
    92  
    93  const logRespMsg = `DigitalOcean API Response Details:
    94  ---[ RESPONSE ]--------------------------------------
    95  %s
    96  -----------------------------------------------------`