-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathenvironment.go
135 lines (116 loc) · 4.52 KB
/
environment.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
package chef
import (
"fmt"
"sort"
)
// Environment has a Reader, hey presto
type EnvironmentService struct {
client *Client
}
type EnvironmentResult map[string]string
// Environment represents the native Go version of the deserialized Environment type
type Environment struct {
Name string `json:"name"`
Description string `json:"description"`
ChefType string `json:"chef_type,omitempty"`
Attributes interface{} `json:"attributes,omitempty"`
DefaultAttributes interface{} `json:"default_attributes,omitempty"`
OverrideAttributes interface{} `json:"override_attributes,omitempty"`
JsonClass string `json:"json_class,omitempty"`
CookbookVersions map[string]string `json:"cookbook_versions"`
}
type EnvironmentCookbookResult map[string]CookbookVersions
type EnvironmentRecipesResult []string
func strMapToStr(e map[string]string) (out string) {
keys := make([]string, len(e))
for k, _ := range e {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if k == "" {
continue
}
out += fmt.Sprintf("%s => %s\n", k, e[k])
}
return
}
// String makes EnvironmentResult implement the string result
func (e EnvironmentResult) String() (out string) {
return strMapToStr(e)
}
// List lists the environments in the Chef server.
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#environments
func (e *EnvironmentService) List() (data *EnvironmentResult, err error) {
err = e.client.magicRequestDecoder("GET", "environments", nil, &data)
return
}
// Create an environment in the Chef server.
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#environments
func (e *EnvironmentService) Create(environment *Environment) (data *EnvironmentResult, err error) {
body, err := JSONReader(environment)
if err != nil {
return
}
err = e.client.magicRequestDecoder("POST", "environments", body, &data)
return
}
// Delete an environment from the Chef server.
//
// Chef API docs: https://docs.chef.io/api_chef_server/#delete-9
func (e *EnvironmentService) Delete(name string) (data *Environment, err error) {
path := fmt.Sprintf("environments/%s", name)
err = e.client.magicRequestDecoder("DELETE", path, nil, &data)
return
}
// Get gets an environment from the Chef server.
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#environments-name
func (e *EnvironmentService) Get(name string) (data *Environment, err error) {
path := fmt.Sprintf("environments/%s", name)
err = e.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// Write an environment to the Chef server.
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#environments-name
// TODO: Fix the name restriction. The parms should be name, environment
func (e *EnvironmentService) Put(environment *Environment) (data *Environment, err error) {
path := fmt.Sprintf("environments/%s", environment.Name)
body, err := JSONReader(environment)
if err != nil {
return
}
err = e.client.magicRequestDecoder("PUT", path, body, &data)
return
}
// Get the versions of a cookbook for this environment from the Chef server.
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#environments-name-cookbooks
func (e *EnvironmentService) ListCookbooks(name string, numVersions string) (data EnvironmentCookbookResult, err error) {
path := versionParams(fmt.Sprintf("environments/%s/cookbooks", name), numVersions)
err = e.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// ListRecipes get the recipes list of recipes available to a given environment.
//
// Chef API docs: https://docs.chef.io/api_chef_server/#get-33
func (e *EnvironmentService) ListRecipes(name string) (data EnvironmentRecipesResult, err error) {
path := fmt.Sprintf("environments/%s/recipes", name)
err = e.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// Get a hash of cookbooks and cookbook versions (including all dependencies) that
// are required by the run_list array. Version constraints may be specified using
// the @ symbol after the cookbook name as a delimiter. Version constraints may also
// be present when the cookbook_versions attributes is specified for an environment
// or when dependencies are specified by a cookbook.
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#cookbooks
// TODO: Write this
// Get a list of cookbooks and cookbook versions that are available to the specified environment.
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#environments-name-cookbooks
// TODO: Write this