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