-
Notifications
You must be signed in to change notification settings - Fork 162
/
director.go
79 lines (60 loc) · 1.81 KB
/
director.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
package director
import (
"encoding/json"
"fmt"
"io"
"net/http"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type DirectorImpl struct {
client Client
}
func (d DirectorImpl) EnableResurrection(enabled bool) error {
return d.client.EnableResurrectionAll(enabled)
}
func (d DirectorImpl) CleanUp(all bool) error {
return d.client.CleanUp(all)
}
func (d DirectorImpl) DownloadResourceUnchecked(blobstoreID string, out io.Writer) error {
return d.client.DownloadResourceUnchecked(blobstoreID, out)
}
func (c Client) EnableResurrectionAll(enabled bool) error {
body := map[string]bool{"resurrection_paused": !enabled}
reqBody, err := json.Marshal(body)
if err != nil {
return bosherr.WrapErrorf(err, "Marshaling request body")
}
setHeaders := func(req *http.Request) {
req.Header.Add("Content-Type", "application/json")
}
_, _, err = c.clientRequest.RawPut("/resurrection", reqBody, setHeaders)
if err != nil {
return bosherr.WrapErrorf(err, "Changing VM resurrection state for all")
}
return nil
}
func (c Client) CleanUp(all bool) error {
body := map[string]interface{}{
"config": map[string]bool{"remove_all": all},
}
reqBody, err := json.Marshal(body)
if err != nil {
return bosherr.WrapErrorf(err, "Marshaling request body")
}
setHeaders := func(req *http.Request) {
req.Header.Add("Content-Type", "application/json")
}
_, err = c.taskClientRequest.PostResult("/cleanup", reqBody, setHeaders)
if err != nil {
return bosherr.WrapErrorf(err, "Cleaning up resources")
}
return nil
}
func (c Client) DownloadResourceUnchecked(blobstoreID string, out io.Writer) error {
path := fmt.Sprintf("/resources/%s", blobstoreID)
_, _, err := c.clientRequest.RawGet(path, out, nil)
if err != nil {
return bosherr.WrapErrorf(err, "Downloading resource '%s'", blobstoreID)
}
return nil
}