-
Notifications
You must be signed in to change notification settings - Fork 16
/
api.go
396 lines (350 loc) · 10.8 KB
/
api.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package gogobosh
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"
)
// GetStemcells from given BOSH
func (c *Client) GetStemcells() ([]Stemcell, error) {
r := c.NewRequest("GET", "/stemcells")
var stemcells []Stemcell
err := c.DoRequestAndUnmarshal(r, &stemcells)
if err != nil {
return []Stemcell{}, fmt.Errorf("error getting stemcells: %w", err)
}
return stemcells, nil
}
// UploadStemcell to the given BOSH
func (c *Client) UploadStemcell(url, sha1 string) (Task, error) {
r := c.NewRequest("POST", "/stemcells")
in := struct {
Location string `json:"location"`
SHA1 string `json:"sha1"`
}{
Location: url,
SHA1: sha1,
}
b, err := json.Marshal(&in)
if err != nil {
return Task{}, fmt.Errorf("error marshalling upload request: %w", err)
}
r.body = bytes.NewBuffer(b)
r.header["Content-Type"] = "application/json"
var task Task
err = c.DoRequestAndUnmarshal(r, &task)
if err != nil {
return Task{}, fmt.Errorf("error uploading stemcell %s: %w", url, err)
}
return task, nil
}
// GetReleases from the given BOSH
func (c *Client) GetReleases() ([]Release, error) {
r := c.NewRequest("GET", "/releases")
var releases []Release
err := c.DoRequestAndUnmarshal(r, &releases)
if err != nil {
return []Release{}, fmt.Errorf("error requesting releases: %w", err)
}
return releases, nil
}
// UploadRelease to the given BOSH
func (c *Client) UploadRelease(url, sha1 string) (Task, error) {
r := c.NewRequest("POST", "/releases")
in := struct {
Location string `json:"location"`
SHA1 string `json:"sha1"`
}{
Location: url,
SHA1: sha1,
}
b, err := json.Marshal(&in)
if err != nil {
return Task{}, fmt.Errorf("error marshalling upload release request: %w", err)
}
r.body = bytes.NewBuffer(b)
r.header["Content-Type"] = "application/json"
var task Task
err = c.DoRequestAndUnmarshal(r, &task)
if err != nil {
return Task{}, fmt.Errorf("error uploading release: %w", err)
}
return task, nil
}
// GetDeployments returns all deployments from the given BOSH
func (c *Client) GetDeployments() ([]Deployment, error) {
r := c.NewRequest("GET", "/deployments")
var deployments []Deployment
err := c.DoRequestAndUnmarshal(r, &deployments)
if err != nil {
return []Deployment{}, fmt.Errorf("error requesting deployments: %w", err)
}
return deployments, nil
}
// GetDeployment returns a specific deployment by name from the given BOSH
func (c *Client) GetDeployment(name string) (Manifest, error) {
r := c.NewRequest("GET", "/deployments/"+name)
var manifest Manifest
err := c.DoRequestAndUnmarshal(r, &manifest)
if err != nil {
return Manifest{}, fmt.Errorf("error requesting deployment manifest: %w", err)
}
return manifest, nil
}
// DeleteDeployment from given BOSH
func (c *Client) DeleteDeployment(name string) (Task, error) {
r := c.NewRequest("DELETE", "/deployments/"+name)
var task Task
err := c.DoRequestAndUnmarshal(r, &task)
if err != nil {
return Task{}, fmt.Errorf("error deleting deployment %s: %w", name, err)
}
return task, nil
}
// CreateDeployment deploys the given deployment manifest
func (c *Client) CreateDeployment(manifest string) (Task, error) {
r := c.NewRequest("POST", "/deployments")
buffer := bytes.NewBufferString(manifest)
r.body = buffer
r.header["Content-Type"] = "text/yaml"
var task Task
err := c.DoRequestAndUnmarshal(r, &task)
if err != nil {
return Task{}, fmt.Errorf("error creating deployment: %w", err)
}
return task, nil
}
// GetDeploymentVMs returns all the VMs that make up the specified deployment
func (c *Client) GetDeploymentVMs(name string) ([]VM, error) {
r := c.NewRequest("GET", "/deployments/"+name+"/vms?format=full")
var task Task
err := c.DoRequestAndUnmarshal(r, &task)
if err != nil {
return []VM{}, fmt.Errorf("error requesting deployment %s VMs: %w", name, err)
}
task, err = c.WaitUntilDone(task, time.Minute*5)
if err != nil {
return []VM{}, fmt.Errorf("error waiting for deployment %s VM task to complete: %w", name, err)
}
var vms []VM
output, err := c.GetTaskResult(task.ID)
if err != nil {
return []VM{}, fmt.Errorf("error getting deployment %s VMs task result: %w", name, err)
}
for _, value := range output {
if len(value) > 0 {
var vm VM
err = json.Unmarshal([]byte(value), &vm)
if err != nil {
return []VM{}, fmt.Errorf("error unmarshalling deployment %s VMs response: %w", name, err)
}
vms = append(vms, vm)
}
}
return vms, nil
}
// GetTasksByQuery from given BOSH
func (c *Client) GetTasksByQuery(query url.Values) ([]Task, error) {
requestUrl := "/tasks?" + query.Encode()
r := c.NewRequest("GET", requestUrl)
var tasks []Task
err := c.DoRequestAndUnmarshal(r, &tasks)
if err != nil {
return []Task{}, fmt.Errorf("error requesting tasks by query %s: %w", query.Encode(), err)
}
return tasks, nil
}
// GetTasks returns all BOSH tasks
func (c *Client) GetTasks() ([]Task, error) {
return c.GetTasksByQuery(nil)
}
// GetTask returns the specified task from BOSH
func (c *Client) GetTask(id int) (Task, error) {
stringID := strconv.Itoa(id)
r := c.NewRequest("GET", "/tasks/"+stringID)
var task Task
err := c.DoRequestAndUnmarshal(r, &task)
if err != nil {
return Task{}, fmt.Errorf("error getting task %s: %w", stringID, err)
}
return task, nil
}
// GetTaskOutput returns the completed tasks output
func (c *Client) GetTaskOutput(id int, typ string) ([]string, error) {
r := c.NewRequest("GET", "/tasks/"+strconv.Itoa(id)+"/output?type="+typ)
res, err := c.DoRequest(r)
if err != nil {
return []string{}, fmt.Errorf("error requesting task output: %w", err)
}
defer func() { _ = res.Body.Close() }()
b, err := io.ReadAll(res.Body)
if err != nil {
return []string{}, fmt.Errorf("error reading task output response: %w", err)
}
return strings.Split(strings.TrimSuffix(string(b), "\n"), "\n"), nil
}
// GetTaskResult returns the tasks result
func (c *Client) GetTaskResult(id int) ([]string, error) {
return c.GetTaskOutput(id, "result")
}
// GetTaskEvents retrieves the events for the specified task
func (c *Client) GetTaskEvents(id int) ([]TaskEvent, error) {
raw, err := c.GetTaskOutput(id, "event")
if err != nil {
return []TaskEvent{}, fmt.Errorf("error getting the task events: %w", err)
}
events := make([]TaskEvent, len(raw))
for i := range raw {
err = json.Unmarshal([]byte(raw[i]), &events[i])
if err != nil {
return []TaskEvent{}, fmt.Errorf("error unmarshalling the task events: %w", err)
}
}
return events, nil
}
// GetCloudConfig from given BOSH
func (c *Client) GetCloudConfig(latest bool) ([]Cfg, error) {
qs := "?latest=true"
if !latest {
qs = "?latest=false"
}
r := c.NewRequest("GET", "/configs"+qs)
var cfg []Cfg
err := c.DoRequestAndUnmarshal(r, &cfg)
if err != nil {
return []Cfg{}, fmt.Errorf("error cloud config: %w", err)
}
return cfg, nil
}
// UpdateCloudConfig updates the cloud config with the specified config
func (c *Client) UpdateCloudConfig(config string) error {
r := c.NewRequest("POST", "/configs")
in := struct {
Name string `json:"name"`
Type string `json:"type"`
Content string `json:"content"`
}{
Name: "default",
Type: "cloud",
Content: config,
}
b, err := json.Marshal(&in)
if err != nil {
return fmt.Errorf("error marshalling the cloud config update: %w", err)
}
r.body = bytes.NewBuffer(b)
r.header["Content-Type"] = "application/json"
resp, err := c.DoRequest(r)
if err != nil {
return fmt.Errorf("error updating the cloud config: %w", err)
}
defer func() { _ = resp.Body.Close() }()
return nil
}
// Cleanup will post to the cleanup endpoint of bosh, passing along the removeAll flag passed in as a bool
func (c *Client) Cleanup(removeAll bool) (Task, error) {
r := c.NewRequest("POST", "/cleanup")
var requestBody struct {
Config struct {
RemoveAll bool `json:"remove_all"`
} `json:"config"`
}
requestBody.Config.RemoveAll = removeAll
b, err := json.Marshal(&requestBody)
if err != nil {
return Task{}, fmt.Errorf("error marshalling the cleanup request: %w", err)
}
r.body = bytes.NewBuffer(b)
r.header["Content-Type"] = "application/json"
var task Task
err = c.DoRequestAndUnmarshal(r, &task)
if err != nil {
return Task{}, fmt.Errorf("error cleaning up BOSH: %w", err)
}
return task, nil
}
func (c *Client) Restart(deployment, instanceGroup, instanceID string) (Task, error) {
return c.vmAction("restart", deployment, instanceGroup, instanceID, true)
}
func (c *Client) RestartNoConverge(deployment, instanceGroup, instanceID string) (Task, error) {
return c.vmAction("restart", deployment, instanceGroup, instanceID, false)
}
func (c *Client) Stop(deployment, instanceGroup, instanceID string) (Task, error) {
return c.vmAction("stopped", deployment, instanceGroup, instanceID, true)
}
func (c *Client) StopNoConverge(deployment, instanceGroup, instanceID string) (Task, error) {
return c.vmAction("stopped", deployment, instanceGroup, instanceID, false)
}
func (c *Client) Start(deployment, instanceGroup, instanceID string) (Task, error) {
return c.vmAction("started", deployment, instanceGroup, instanceID, true)
}
func (c *Client) StartNoConverge(deployment, instanceGroup, instanceID string) (Task, error) {
return c.vmAction("started", deployment, instanceGroup, instanceID, false)
}
func (c *Client) vmAction(action, deployment, instanceGroup, instanceID string, converge bool) (Task, error) {
var p string
if converge {
p = fmt.Sprintf("/deployments/%s/jobs/%s/%s?state=%s",
deployment, instanceGroup, instanceID, action)
} else {
p = fmt.Sprintf("/deployments/%s/instance_groups/%s/%s/actions/%s",
deployment, instanceGroup, instanceID, action)
}
return c.executeVMAction(action, p)
}
func (c *Client) executeVMAction(action, actionPath string) (Task, error) {
var task Task
r := c.NewRequest("PUT", actionPath)
r.header["Content-Type"] = "text/yaml"
err := c.DoRequestAndUnmarshal(r, &task)
if err != nil {
return Task{}, fmt.Errorf("error creating VM %s task: %w", action, err)
}
return task, nil
}
func (c *Client) WaitUntilDone(task Task, timeout time.Duration) (Task, error) {
type Result struct {
Task Task
Error error
}
doneCh := make(chan Result)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
go func(taskID int) {
for range ticker.C {
curTask, err := c.GetTask(taskID)
if err != nil {
doneCh <- Result{
Task: Task{},
Error: fmt.Errorf("error getting task %d status: %w", curTask.ID, err),
}
return
}
switch curTask.State {
case "done":
doneCh <- Result{
Task: curTask,
}
return
case "error":
doneCh <- Result{
Task: curTask,
Error: fmt.Errorf("task %d failed: %s", curTask.ID, curTask.Result),
}
return
}
}
}(task.ID)
for {
select {
case result := <-doneCh:
return result.Task, result.Error
case <-time.After(timeout):
return task, fmt.Errorf("timed out waiting for task %d to complete", task.ID)
}
}
}