From 12e74124348928e45d30982232d852dd6ef07280 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Thu, 27 Apr 2017 17:14:37 +0100 Subject: [PATCH] provider/digitalocean: Log HTTP requests & responses in DEBUG+ level (#14039) --- builtin/providers/digitalocean/config.go | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/builtin/providers/digitalocean/config.go b/builtin/providers/digitalocean/config.go index a0a9115ae..b32be5938 100644 --- a/builtin/providers/digitalocean/config.go +++ b/builtin/providers/digitalocean/config.go @@ -2,9 +2,12 @@ package digitalocean import ( "log" + "net/http" + "net/http/httputil" "time" "github.com/digitalocean/godo" + "github.com/hashicorp/terraform/helper/logging" "github.com/hashicorp/terraform/helper/resource" "golang.org/x/oauth2" ) @@ -21,11 +24,31 @@ func (c *Config) Client() (*godo.Client, error) { client := godo.NewClient(oauth2.NewClient(oauth2.NoContext, tokenSrc)) + if logging.IsDebugOrHigher() { + client.OnRequestCompleted(logRequestAndResponse) + } + log.Printf("[INFO] DigitalOcean Client configured for URL: %s", client.BaseURL.String()) return client, nil } +func logRequestAndResponse(req *http.Request, resp *http.Response) { + reqData, err := httputil.DumpRequest(req, true) + if err == nil { + log.Printf("[DEBUG] "+logReqMsg, string(reqData)) + } else { + log.Printf("[ERROR] DigitalOcean API Request error: %#v", err) + } + + respData, err := httputil.DumpResponse(resp, true) + if err == nil { + log.Printf("[DEBUG] "+logRespMsg, string(respData)) + } else { + log.Printf("[ERROR] DigitalOcean API Response error: %#v", err) + } +} + // waitForAction waits for the action to finish using the resource.StateChangeConf. func waitForAction(client *godo.Client, action *godo.Action) error { var ( @@ -61,3 +84,13 @@ func waitForAction(client *godo.Client, action *godo.Action) error { }).WaitForState() return err } + +const logReqMsg = `DigitalOcean API Request Details: +---[ REQUEST ]--------------------------------------- +%s +-----------------------------------------------------` + +const logRespMsg = `DigitalOcean API Response Details: +---[ RESPONSE ]-------------------------------------- +%s +-----------------------------------------------------`