forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_configs.go
103 lines (79 loc) · 2.28 KB
/
cloud_configs.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
package director
import (
"net/http"
"encoding/json"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type CloudConfigDiffResponse struct {
Diff [][]interface{} `json:"diff"`
}
type CloudConfig struct {
Properties string
}
type CloudConfigDiff struct {
Diff [][]interface{}
}
func NewCloudConfigDiff(diff [][]interface{}) CloudConfigDiff {
return CloudConfigDiff{
Diff: diff,
}
}
func (d DirectorImpl) LatestCloudConfig() (CloudConfig, error) {
resps, err := d.client.CloudConfigs()
if err != nil {
return CloudConfig{}, err
}
if len(resps) == 0 {
return CloudConfig{}, bosherr.Error("No cloud config")
}
return resps[0], nil
}
func (d DirectorImpl) UpdateCloudConfig(manifest []byte) error {
return d.client.UpdateCloudConfig(manifest)
}
func (c Client) CloudConfigs() ([]CloudConfig, error) {
var resps []CloudConfig
err := c.clientRequest.Get("/cloud_configs?limit=1", &resps)
if err != nil {
return resps, bosherr.WrapErrorf(err, "Finding cloud configs")
}
return resps, nil
}
func (c Client) UpdateCloudConfig(manifest []byte) error {
path := "/cloud_configs"
setHeaders := func(req *http.Request) {
req.Header.Add("Content-Type", "text/yaml")
}
_, _, err := c.clientRequest.RawPost(path, manifest, setHeaders)
if err != nil {
return bosherr.WrapErrorf(err, "Updating cloud config")
}
return nil
}
func (d DirectorImpl) DiffCloudConfig(manifest []byte) (CloudConfigDiff, error) {
resp, err := d.client.DiffCloudConfig(manifest)
if err != nil {
return CloudConfigDiff{}, err
}
return NewCloudConfigDiff(resp.Diff), nil
}
func (c Client) DiffCloudConfig(manifest []byte) (CloudConfigDiffResponse, error) {
setHeaders := func(req *http.Request) {
req.Header.Add("Content-Type", "text/yaml")
}
var resp CloudConfigDiffResponse
respBody, response, err := c.clientRequest.RawPost("/cloud_configs/diff", manifest, setHeaders)
if err != nil {
if response != nil && response.StatusCode == http.StatusNotFound {
// return empty diff, just for compatibility with directors which don't have the endpoint
return resp, nil
} else {
return resp, bosherr.WrapErrorf(err, "Fetching diff result")
}
}
err = json.Unmarshal(respBody, &resp)
if err != nil {
return resp, bosherr.WrapError(err, "Unmarshaling Director response")
}
return resp, nil
}