github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/post-processor/vagrant-cloud/client.go (about) 1 package vagrantcloud 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io" 8 "log" 9 "net/http" 10 "net/url" 11 "os" 12 "strings" 13 ) 14 15 type VagrantCloudClient struct { 16 // The http client for communicating 17 client *http.Client 18 19 // The base URL of the API 20 BaseURL string 21 22 // Access token 23 AccessToken string 24 } 25 26 type VagrantCloudErrors struct { 27 Errors map[string][]string `json:"errors"` 28 } 29 30 func (v VagrantCloudErrors) FormatErrors() string { 31 errs := make([]string, 0) 32 for e := range v.Errors { 33 msg := fmt.Sprintf("%s %s", e, strings.Join(v.Errors[e], ",")) 34 errs = append(errs, msg) 35 } 36 return strings.Join(errs, ". ") 37 } 38 39 func (v VagrantCloudClient) New(baseUrl string, token string) *VagrantCloudClient { 40 c := &VagrantCloudClient{ 41 client: &http.Client{ 42 Transport: &http.Transport{ 43 Proxy: http.ProxyFromEnvironment, 44 }, 45 }, 46 BaseURL: baseUrl, 47 AccessToken: token, 48 } 49 return c 50 } 51 52 func decodeBody(resp *http.Response, out interface{}) error { 53 defer resp.Body.Close() 54 dec := json.NewDecoder(resp.Body) 55 return dec.Decode(out) 56 } 57 58 // encodeBody is used to encode a request body 59 func encodeBody(obj interface{}) (io.Reader, error) { 60 buf := bytes.NewBuffer(nil) 61 enc := json.NewEncoder(buf) 62 if err := enc.Encode(obj); err != nil { 63 return nil, err 64 } 65 return buf, nil 66 } 67 68 func (v VagrantCloudClient) Get(path string) (*http.Response, error) { 69 params := url.Values{} 70 params.Set("access_token", v.AccessToken) 71 reqUrl := fmt.Sprintf("%s/%s?%s", v.BaseURL, path, params.Encode()) 72 73 // Scrub API key for logs 74 scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1) 75 log.Printf("Post-Processor Vagrant Cloud API GET: %s", scrubbedUrl) 76 77 req, err := http.NewRequest("GET", reqUrl, nil) 78 req.Header.Add("Content-Type", "application/json") 79 resp, err := v.client.Do(req) 80 81 log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%+v", resp) 82 83 return resp, err 84 } 85 86 func (v VagrantCloudClient) Delete(path string) (*http.Response, error) { 87 params := url.Values{} 88 params.Set("access_token", v.AccessToken) 89 reqUrl := fmt.Sprintf("%s/%s?%s", v.BaseURL, path, params.Encode()) 90 91 // Scrub API key for logs 92 scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1) 93 log.Printf("Post-Processor Vagrant Cloud API DELETE: %s", scrubbedUrl) 94 95 req, err := http.NewRequest("DELETE", reqUrl, nil) 96 req.Header.Add("Content-Type", "application/json") 97 resp, err := v.client.Do(req) 98 99 log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%+v", resp) 100 101 return resp, err 102 } 103 104 func (v VagrantCloudClient) Upload(path string, url string) (*http.Response, error) { 105 file, err := os.Open(path) 106 107 if err != nil { 108 return nil, fmt.Errorf("Error opening file for upload: %s", err) 109 } 110 111 fi, err := file.Stat() 112 113 if err != nil { 114 return nil, fmt.Errorf("Error stating file for upload: %s", err) 115 } 116 117 defer file.Close() 118 119 request, err := http.NewRequest("PUT", url, file) 120 121 if err != nil { 122 return nil, fmt.Errorf("Error preparing upload request: %s", err) 123 } 124 125 log.Printf("Post-Processor Vagrant Cloud API Upload: %s %s", path, url) 126 127 request.ContentLength = fi.Size() 128 resp, err := v.client.Do(request) 129 130 log.Printf("Post-Processor Vagrant Cloud Upload Response: \n\n%+v", resp) 131 132 return resp, err 133 } 134 135 func (v VagrantCloudClient) Post(path string, body interface{}) (*http.Response, error) { 136 params := url.Values{} 137 params.Set("access_token", v.AccessToken) 138 reqUrl := fmt.Sprintf("%s/%s?%s", v.BaseURL, path, params.Encode()) 139 140 encBody, err := encodeBody(body) 141 142 if err != nil { 143 return nil, fmt.Errorf("Error encoding body for request: %s", err) 144 } 145 146 // Scrub API key for logs 147 scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1) 148 log.Printf("Post-Processor Vagrant Cloud API POST: %s. \n\n Body: %s", scrubbedUrl, encBody) 149 150 req, err := http.NewRequest("POST", reqUrl, encBody) 151 req.Header.Add("Content-Type", "application/json") 152 153 resp, err := v.client.Do(req) 154 155 log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%+v", resp) 156 157 return resp, err 158 } 159 160 func (v VagrantCloudClient) Put(path string) (*http.Response, error) { 161 params := url.Values{} 162 params.Set("access_token", v.AccessToken) 163 reqUrl := fmt.Sprintf("%s/%s?%s", v.BaseURL, path, params.Encode()) 164 165 // Scrub API key for logs 166 scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1) 167 log.Printf("Post-Processor Vagrant Cloud API PUT: %s", scrubbedUrl) 168 169 req, err := http.NewRequest("PUT", reqUrl, nil) 170 req.Header.Add("Content-Type", "application/json") 171 172 resp, err := v.client.Do(req) 173 174 log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%+v", resp) 175 176 return resp, err 177 }