forked from cloudfoundry/bosh-cli
/
director.go
126 lines (99 loc) · 3.02 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
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
package director
import (
"encoding/json"
"fmt"
"io"
"net/http"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type DirectorImpl struct {
client Client
}
type OrphanedVMResponse struct {
AZName string `json:"az"`
CID string `json:"cid"`
DeploymentName string `json:"deployment_name"`
IPAddresses []string `json:"ip_addresses"`
InstanceName string `json:"instance_name"`
OrphanedAt string `json:"orphaned_at"`
}
func (d DirectorImpl) WithContext(id string) Director {
return DirectorImpl{client: d.client.WithContext(id)}
}
func (c Client) OrphanedVMs() ([]OrphanedVM, error) {
var (
orphanedVMs []OrphanedVM
resps []OrphanedVMResponse
)
err := c.clientRequest.Get("/orphaned_vms", &resps)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Finding orphaned VMs")
}
for _, r := range resps {
orphanedAt, err := TimeParser{}.Parse(r.OrphanedAt)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Converting orphaned at '%s' to time", r.OrphanedAt)
}
orphanedVMs = append(orphanedVMs, OrphanedVM{
CID: r.CID,
DeploymentName: r.DeploymentName,
InstanceName: r.InstanceName,
AZName: r.AZName,
IPAddresses: r.IPAddresses,
OrphanedAt: orphanedAt,
})
}
return orphanedVMs, nil
}
func (d DirectorImpl) OrphanedVMs() ([]OrphanedVM, error) {
return d.client.OrphanedVMs()
}
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
}