github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/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 defer file.Close() 112 113 body := &bytes.Buffer{} 114 115 _, err = io.Copy(body, file) 116 117 if err != nil { 118 return nil, fmt.Errorf("Error uploading file: %s", err) 119 } 120 121 request, err := http.NewRequest("PUT", url, body) 122 123 if err != nil { 124 return nil, fmt.Errorf("Error preparing upload request: %s", err) 125 } 126 127 log.Printf("Post-Processor Vagrant Cloud API Upload: %s %s", path, url) 128 129 resp, err := v.client.Do(request) 130 131 log.Printf("Post-Processor Vagrant Cloud Upload Response: \n\n%+v", resp) 132 133 return resp, err 134 } 135 136 func (v VagrantCloudClient) Post(path string, body interface{}) (*http.Response, error) { 137 params := url.Values{} 138 params.Set("access_token", v.AccessToken) 139 reqUrl := fmt.Sprintf("%s/%s?%s", v.BaseURL, path, params.Encode()) 140 141 encBody, err := encodeBody(body) 142 143 if err != nil { 144 return nil, fmt.Errorf("Error encoding body for request: %s", err) 145 } 146 147 // Scrub API key for logs 148 scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1) 149 log.Printf("Post-Processor Vagrant Cloud API POST: %s. \n\n Body: %s", scrubbedUrl, encBody) 150 151 req, err := http.NewRequest("POST", reqUrl, encBody) 152 req.Header.Add("Content-Type", "application/json") 153 154 resp, err := v.client.Do(req) 155 156 log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%+v", resp) 157 158 return resp, err 159 } 160 161 func (v VagrantCloudClient) Put(path string) (*http.Response, error) { 162 params := url.Values{} 163 params.Set("access_token", v.AccessToken) 164 reqUrl := fmt.Sprintf("%s/%s?%s", v.BaseURL, path, params.Encode()) 165 166 // Scrub API key for logs 167 scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1) 168 log.Printf("Post-Processor Vagrant Cloud API PUT: %s", scrubbedUrl) 169 170 req, err := http.NewRequest("PUT", reqUrl, nil) 171 req.Header.Add("Content-Type", "application/json") 172 173 resp, err := v.client.Do(req) 174 175 log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%+v", resp) 176 177 return resp, err 178 }