github.com/mitchellh/packer@v1.3.2/builder/azure/arm/inspector.go (about)

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