github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/azure/arm/inspector.go (about) 1 package arm 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "log" 7 "net/http" 8 9 "github.com/Azure/go-autorest/autorest" 10 "github.com/Azure/go-autorest/autorest/azure" 11 "github.com/mitchellh/packer/builder/azure/common/logutil" 12 "io" 13 ) 14 15 func chop(data []byte, maxlen int64) string { 16 s := string(data) 17 if int64(len(s)) > maxlen { 18 s = s[:maxlen] + "..." 19 } 20 return s 21 } 22 23 func handleBody(body io.ReadCloser, maxlen int64) (io.ReadCloser, string) { 24 if body == nil { 25 return nil, "" 26 } 27 28 defer body.Close() 29 30 b, err := ioutil.ReadAll(body) 31 if err != nil { 32 return nil, "" 33 } 34 35 return ioutil.NopCloser(bytes.NewReader(b)), chop(b, maxlen) 36 } 37 38 func withInspection(maxlen int64) autorest.PrepareDecorator { 39 return func(p autorest.Preparer) autorest.Preparer { 40 return autorest.PreparerFunc(func(r *http.Request) (*http.Request, error) { 41 body, bodyString := handleBody(r.Body, maxlen) 42 r.Body = body 43 44 log.Print("Azure request", logutil.Fields{ 45 "method": r.Method, 46 "request": r.URL.String(), 47 "body": bodyString, 48 }) 49 return p.Prepare(r) 50 }) 51 } 52 } 53 54 func byInspecting(maxlen int64) autorest.RespondDecorator { 55 return func(r autorest.Responder) autorest.Responder { 56 return autorest.ResponderFunc(func(resp *http.Response) error { 57 body, bodyString := handleBody(resp.Body, maxlen) 58 resp.Body = body 59 60 log.Print("Azure response", logutil.Fields{ 61 "status": resp.Status, 62 "method": resp.Request.Method, 63 "request": resp.Request.URL.String(), 64 "x-ms-request-id": azure.ExtractRequestID(resp), 65 "body": bodyString, 66 }) 67 return r.Respond(resp) 68 }) 69 } 70 }