forked from marpaia/chef-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.go
More file actions
269 lines (246 loc) · 8.11 KB
/
environment.go
File metadata and controls
269 lines (246 loc) · 8.11 KB
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
package chef
import (
"encoding/json"
"fmt"
"strings"
)
// chef.Environment dinfes the relevant parameters of a Chef environment. This
// includes the name of the environment, the description strings, etc.
type Environment struct {
Name string `json:"name"`
Description string `json:"description"`
CookbookVersions map[string]string `json:"cookbook_versions"`
JSONClass string `json:"json_class"`
ChefType string `json:"chef_type"`
DefaultAttributes map[string]interface{} `json:"default_attributes"`
OverrideAttributes map[string]interface{} `json:"override_attributes"`
}
// chef.GetEnvironments returns a map of environment names to the environment's
// RESTful URL as well as an error indicating if the request was successful or
// not.
//
// Usage:
//
// environments, err := chef.GetEnvironments()
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// // do what you please with the "environments" variable which is a map of
// // environment names to environment URLs
// for environment := range environments {
// fmt.Println(environment)
// }
func (chef *Chef) GetEnvironments() (map[string]string, error) {
resp, err := chef.Get("environments")
if err != nil {
return nil, err
}
body, err := responseBody(resp)
if err != nil {
return nil, err
}
environments := map[string]string{}
json.Unmarshal(body, &environments)
return environments, nil
}
// chef.GetEnvironment accepts a string which represents the name of a Chef
// environment and returns a chef.Environment type representing that environment
// as well as a bool indicating whether or not the environment was found and an
// error indicating if the request failed or not.
//
// Note that if the request is successful but no such client existed, the error
// return value will be nil but the bool will be false.
//
// Usage:
//
// environment, ok, err := chef.GetEnvironment("production")
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// if !ok {
// fmt.Println("Couldn't find that environment!")
// } else {
// // do what you please with the "environment" variable which is of the
// // *Chef.Environment type
// fmt.Printf("%#v\n", environment)
// }
func (chef *Chef) GetEnvironment(name string) (*Environment, bool, error) {
resp, err := chef.Get(fmt.Sprintf("environments/%s", name))
if err != nil {
return nil, false, err
}
body, err := responseBody(resp)
if err != nil {
if strings.Contains(err.Error(), "404") {
return nil, false, nil
}
return nil, false, err
}
environment := new(Environment)
json.Unmarshal(body, environment)
return environment, true, nil
}
// chef.GetEnvironmentCookbooks accepts a string which represents the name of a
// Chef environment and returns a map of cookbook names to a *Chef.Cookbook type
// as well as an error indicating whether or not the request failed.
//
// Usage:
//
// cookbooks, err := chef.GetEnvironmentCookbooks("production")
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// // do what you please with the "cookbooks" variable which is a map of
// // cookbook names to chef.Cookbook types
// for name, cookbook := range cookbooks {
// fmt.Println(name, cookbook.Version[0])
// }
func (chef *Chef) GetEnvironmentCookbooks(name string) (map[string]*Cookbook, error) {
resp, err := chef.Get(fmt.Sprintf("environments/%s/cookbooks", name))
if err != nil {
return nil, err
}
body, err := responseBody(resp)
if err != nil {
return nil, err
}
cookbooks := map[string]*Cookbook{}
json.Unmarshal(body, &cookbooks)
return cookbooks, nil
}
// chef.GetEnvironmentCookbook accepts a string which represents the name of a
// Chef environment as well as a string which represent the name of a cookbook
// and returns a *Chef.Cookbook type, a bool indicating whether or not the
// cookbook was found in the given environment as well as an error indicating
// whether or not the request failed.
//
// Usage:
//
// cookbook, ok, err := chef.GetEnvironmentCookbook("production", "apache")
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// if !ok {
// fmt.Println("Couldn't find that cookbook!")
// } else {
// // do what you please with the "cookbook" variable which is of the
// // *Chef.Cookbook type
// fmt.Printf("%#v\n", cookbook)
// }
func (chef *Chef) GetEnvironmentCookbook(env, cb string) (*Cookbook, bool, error) {
resp, err := chef.Get(fmt.Sprintf("environments/%s/cookbooks/%s", env, cb))
if err != nil {
return nil, false, err
}
body, err := responseBody(resp)
if err != nil {
if strings.Contains(err.Error(), "404") {
return nil, false, nil
}
return nil, false, err
}
cookbook := map[string]*Cookbook{}
json.Unmarshal(body, &cookbook)
return cookbook[cb], true, nil
}
// chef.GetEnvironmentNodes accepts a string which represents the name of a
// Chef environment as well as a string which represent the name of a node
// and returns a map of node names to their corresponding RESTful URL, a bool
// indicating whether or not the cookbook was found in the given environment as
// well as an error indicating whether or not the request failed.
//
// Usage:
//
// nodes, err := chef.GetEnvironmentNodes("production")
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// // do what you please with the "nodes" variable which is a map of
// // node names to their corresponding RESTful URL
// for node := range nodes {
// fmt.Println(node)
// }
func (chef *Chef) GetEnvironmentNodes(name string) (map[string]string, error) {
resp, err := chef.Get(fmt.Sprintf("environments/%s/nodes", name))
if err != nil {
return nil, err
}
body, err := responseBody(resp)
if err != nil {
return nil, err
}
nodes := map[string]string{}
json.Unmarshal(body, &nodes)
return nodes, nil
}
// chef.GetEnvironmentRecipes accepts a string which represents the name of a
// Chef environment and returns a slice of recipe names as well as an error
// indicating whether or not the request failed.
//
// Usage:
//
// recipes, err := chef.GetEnvironmentRecipes("production")
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// // do what you please with the "recipes" variable which is a slice of
// // recipe names
// for recipe := range recipes {
// fmt.Println(recipe)
// }
func (chef *Chef) GetEnvironmentRecipes(name string) ([]string, error) {
resp, err := chef.Get(fmt.Sprintf("environments/%s/recipes", name))
if err != nil {
return nil, err
}
body, err := responseBody(resp)
if err != nil {
return nil, err
}
recipes := []string{}
json.Unmarshal(body, &recipes)
return recipes, nil
}
// chef.GetEnvironmentRole accepts a string which represents the name of a
// Chef environment as well as a string which represent the name of a role
// and returns a map of strings (which represent a role attribute like a
// runlist) to a slice of strings which represents the relevant information with
// regards to that attribute, a bool indicating whether or not the role was
// found in the given environment as well as an error indicating whether or not
// the request failed.
//
// Usage:
//
// role, ok, err := chef.GetEnvironmentRole("production", "base")
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// if !ok {
// fmt.Println("Couldn't find that role!")
// } else {
// // do what you please with the "role" variable
// fmt.Println(role)
// }
func (chef *Chef) GetEnvironmentRole(env, rol string) (map[string][]string, bool, error) {
resp, err := chef.Get(fmt.Sprintf("environments/%s/roles/%s", env, rol))
if err != nil {
return nil, false, err
}
body, err := responseBody(resp)
if err != nil {
if strings.Contains(err.Error(), "404") {
return nil, false, nil
}
return nil, false, err
}
role := map[string][]string{}
json.Unmarshal(body, &role)
return role, true, nil
}