provider/google: Log HTTP requests and responses in DEBUG mode (#14281)

This commit is contained in:
Radek Simko 2017-05-10 21:16:43 +02:00 committed by GitHub
parent 9ec0f5326d
commit f868a59ffa
2 changed files with 56 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"github.com/hashicorp/terraform/helper/logging"
"github.com/hashicorp/terraform/helper/pathorcontents" "github.com/hashicorp/terraform/helper/pathorcontents"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"golang.org/x/oauth2" "golang.org/x/oauth2"
@ -95,6 +96,8 @@ func (c *Config) loadAndValidate() error {
} }
} }
client.Transport = logging.NewTransport("Google", client.Transport)
versionString := terraform.VersionString() versionString := terraform.VersionString()
userAgent := fmt.Sprintf( userAgent := fmt.Sprintf(
"(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, versionString) "(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, versionString)

View File

@ -0,0 +1,53 @@
package logging
import (
"log"
"net/http"
"net/http/httputil"
)
type transport struct {
name string
transport http.RoundTripper
}
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
if IsDebugOrHigher() {
reqData, err := httputil.DumpRequestOut(req, true)
if err == nil {
log.Printf("[DEBUG] "+logReqMsg, t.name, string(reqData))
} else {
log.Printf("[ERROR] %s API Request error: %#v", t.name, err)
}
}
resp, err := t.transport.RoundTrip(req)
if err != nil {
return resp, err
}
if IsDebugOrHigher() {
respData, err := httputil.DumpResponse(resp, true)
if err == nil {
log.Printf("[DEBUG] "+logRespMsg, t.name, string(respData))
} else {
log.Printf("[ERROR] %s API Response error: %#v", t.name, err)
}
}
return resp, nil
}
func NewTransport(name string, t http.RoundTripper) *transport {
return &transport{name, t}
}
const logReqMsg = `%s API Request Details:
---[ REQUEST ]---------------------------------------
%s
-----------------------------------------------------`
const logRespMsg = `%s API Response Details:
---[ RESPONSE ]--------------------------------------
%s
-----------------------------------------------------`