-
Notifications
You must be signed in to change notification settings - Fork 161
/
configs.go
204 lines (160 loc) · 4.98 KB
/
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
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
package director
import (
"encoding/json"
"fmt"
"net/http"
gourl "net/url"
"strconv"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type Config struct {
ID string
Name string
Type string
CreatedAt string `json:"created_at"`
Team string
Content string
}
type ConfigsFilter struct {
Type string
Name string
}
type UpdateConfigBody struct {
Type string `json:"type"`
Name string `json:"name"`
Content string `json:"content"`
}
func (d DirectorImpl) LatestConfig(configType string, name string) (Config, error) {
resps, err := d.client.latestConfig(configType, name)
if err != nil {
return Config{}, err
}
if len(resps) == 0 {
return Config{}, bosherr.Error("No config")
}
return resps[0], nil
}
func (d DirectorImpl) LatestConfigByID(configID string) (Config, error) {
return d.client.latestConfigByID(configID)
}
func (c Client) latestConfigByID(configID string) (Config, error) {
var config Config
path := fmt.Sprintf("/configs/%s", configID)
respBody, response, err := c.clientRequest.RawGet(path, nil, nil)
if err != nil {
if response != nil && response.StatusCode == http.StatusNotFound {
return config, bosherr.WrapErrorf(err, "No config")
}
return config, bosherr.WrapErrorf(err, "Finding config")
}
err = json.Unmarshal(respBody, &config)
if err != nil {
return config, bosherr.WrapError(err, "Unmarshaling Director response")
}
return config, nil
}
func (d DirectorImpl) ListConfigs(limit int, filter ConfigsFilter) ([]Config, error) {
return d.client.listConfigs(limit, filter)
}
func (d DirectorImpl) UpdateConfig(configType string, name string, content []byte) (Config, error) {
body, err := json.Marshal(UpdateConfigBody{configType, name, string(content)})
if err != nil {
return Config{}, bosherr.WrapError(err, "Can't marshal request body")
}
return d.client.updateConfig(body)
}
func (d DirectorImpl) DeleteConfig(configType string, name string) (bool, error) {
return d.client.deleteConfig(configType, name)
}
func (d DirectorImpl) DeleteConfigByID(configID string) (bool, error) {
return d.client.deleteConfigByID(configID)
}
func (c Client) latestConfig(configType string, name string) ([]Config, error) {
var resps []Config
query := gourl.Values{}
query.Add("type", configType)
query.Add("name", name)
query.Add("limit", "1")
query.Add("latest", "true")
path := fmt.Sprintf("/configs?%s", query.Encode())
err := c.clientRequest.Get(path, &resps)
if err != nil {
return resps, bosherr.WrapError(err, "Finding config")
}
return resps, nil
}
func (c Client) listConfigs(limit int, filter ConfigsFilter) ([]Config, error) {
var resps []Config
query := gourl.Values{}
if filter.Type != "" {
query.Add("type", filter.Type)
}
if filter.Name != "" {
query.Add("name", filter.Name)
}
query.Add("limit", fmt.Sprintf("%d", limit))
query.Add("latest", strconv.FormatBool(limit == 1))
path := fmt.Sprintf("/configs?%s", query.Encode())
err := c.clientRequest.Get(path, &resps)
if err != nil {
return resps, bosherr.WrapErrorf(err, "Listing configs")
}
return resps, nil
}
func (c Client) updateConfig(content []byte) (Config, error) {
var config Config
setHeaders := func(req *http.Request) {
req.Header.Add("Content-Type", "application/json")
}
respBody, _, err := c.clientRequest.RawPost("/configs", content, setHeaders)
if err != nil {
return config, bosherr.WrapErrorf(err, "Updating config")
}
err = json.Unmarshal(respBody, &config)
if err != nil {
return config, bosherr.WrapError(err, "Unmarshaling Director response")
}
return config, nil
}
func (c Client) deleteConfig(configType string, name string) (bool, error) {
query := gourl.Values{}
query.Add("type", configType)
query.Add("name", name)
path := fmt.Sprintf("/configs?%s", query.Encode())
_, response, err := c.clientRequest.RawDelete(path)
if err != nil {
if response != nil && response.StatusCode == http.StatusNotFound {
return false, nil
}
return false, bosherr.WrapErrorf(err, "Deleting config")
}
return true, nil
}
func (c Client) deleteConfigByID(configID string) (bool, error) {
path := fmt.Sprintf("/configs/%s", configID)
_, response, err := c.clientRequest.RawDelete(path)
if err != nil {
if response != nil && response.StatusCode == http.StatusNotFound {
return false, nil
}
return false, bosherr.WrapErrorf(err, "Deleting config")
}
return true, nil
}
func (d DirectorImpl) DiffConfig(configType string, name string, manifest []byte) (ConfigDiff, error) {
body, err := json.Marshal(UpdateConfigBody{configType, name, string(manifest)})
if err != nil {
return ConfigDiff{}, bosherr.WrapError(err, "Can't marshal request body")
}
resp, err := d.client.DiffConfig(body)
if err != nil {
return ConfigDiff{}, err
}
return NewConfigDiff(resp.Diff), nil
}
func (c Client) DiffConfig(manifest []byte) (ConfigDiffResponse, error) {
setHeaders := func(req *http.Request) {
req.Header.Add("Content-Type", "application/json")
}
return c.postConfigDiff("/configs/diff", manifest, setHeaders)
}