forked from marpaia/chef-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookbook.go
194 lines (183 loc) · 5.83 KB
/
cookbook.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
package chef
import (
"encoding/json"
"fmt"
"strings"
)
// chef.Cookbook defines the relevant parameters of a Chef cookbook. This
// includes the RESTful URL of a cookbook and a slice of all of the cookbooks
// versions. The versions each have two attributes: Url, which represents the
// RESTful URL of the cookbook version and Version, which represents the version
// number (identifier) of the cookbook version
type Cookbook struct {
Url string `json:"url"`
Versions []struct {
Url string `json:"url"`
Version string `json:"version"`
} `json:"versions"`
}
// chef.CookbookVersion defines the relevant parameters of a specific Chef
// cookbook version. This includes, but is not limited to, information about
// recipes, files, etc, various pieces of metadata about the cookbook at that
// point in time, such as the name of the cookbook, the description, the
// license, etc.
type CookbookVersion struct {
Files []struct {
CookbookItem
} `json:"files"`
Definitions []struct {
CookbookItem
} `json:"definitions"`
Libraries []struct {
CookbookItem
} `json:"libraries"`
Attributes []struct {
CookbookItem
} `json:"attributes"`
Recipes []struct {
CookbookItem
} `json:"recipes"`
Providers []struct {
CookbookItem
} `json:"providers"`
Resources []struct {
CookbookItem
} `json:"resources"`
Templates []struct {
CookbookItem
} `json:"templates"`
RootFiles []struct {
CookbookItem
} `json:"root_file"`
Metadata struct {
Name string `json:"name"`
Description string `json:"description"`
LongDescription string `json:"long_description"`
Maintainer string `json:"maintainer"`
MaintainerEmail string `json:"maintainer_email"`
License string `json:"license"`
Providing map[string]string `json:"providing"`
Dependencies map[string]string `json:"dependencies"`
} `json:"metadata"`
Name string `json:"cookbook_name"`
Version string `json:"version"`
FullName string `json:"name"`
Frozen bool `json:"frozen?"`
ChefType string `json:"chef_type"`
JSONClass string `json:"json_class"`
}
// chef.CookbookItem defines the relevant parameters of various items that are
// found in a chef Cookbook such as the name, checksum, etc. This type is
// embedded in the chef.CookVersion type to reduce code repetition.
type CookbookItem struct {
Name string `json:"name"`
Path string `json:"path"`
Checksum string `json:"checksum"`
Specificity string `json:"specificity"`
Url string `json:"url"`
}
// chef.GetCookbooks returns a map of cookbook names to a pointer to the
// chef.Cookbook type as well as an error indicating if the request was
// successful or not.
//
// Usgae:
//
// cookbooks, err := chef.GetCookbooks()
// 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) GetCookbooks() (map[string]*Cookbook, error) {
resp, err := chef.Get("cookbooks")
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.GetCookbook returns a pointer to the chef.Cookbook type for a given
// string that represents a cookbook name. It also returns a bool indicating
// whether or not the client 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:
//
// cookbook, ok, err := chef.GetCookbook("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) GetCookbook(name string) (*Cookbook, bool, error) {
resp, err := chef.Get(fmt.Sprintf("cookbooks/%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
}
cookbook := map[string]*Cookbook{}
json.Unmarshal(body, &cookbook)
return cookbook[name], true, nil
}
// chef.GetCookbookVersion returns a pointer to the chef.CookbookVersion type
// for a given string that represents a cookbook version. It also returns a bool
// indicating whether or not the client 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:
//
// cookbook, ok, err := chef.GetCookbookVersion("apache", "1.0.0")
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// if !ok {
// fmt.Println("Couldn't find that cookbook version!")
// } else {
// // do what you please with the "cookbook" variable which is of the
// // *Chef.CookbookVersion type
// fmt.Printf("%#v\n", cookbook)
// }
func (chef *Chef) GetCookbookVersion(name, version string) (*CookbookVersion, bool, error) {
resp, err := chef.Get(fmt.Sprintf("cookbooks/%s/%s", name, version))
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 := new(CookbookVersion)
json.Unmarshal(body, &cookbook)
return cookbook, true, nil
}