forked from go-chef/chef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookbook.go
166 lines (146 loc) · 6.97 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
package chef
import "fmt"
// CookbookService is the service for interacting with chef server cookbooks endpoint
type CookbookService struct {
client *Client
}
// CookbookItem represents a object of cookbook file data
type CookbookItem struct {
Url string `json:"url,omitempty"`
Path string `json:"path,omitempty"`
Name string `json:"name,omitempty"`
Checksum string `json:"checksum,omitempty"`
Specificity string `json:"specificity,omitempty"`
}
// CookbookListResult is the summary info returned by chef-api when listing
// http://docs.opscode.com/api_chef_server.html#cookbooks
type CookbookListResult map[string]CookbookVersions
// CookbookRecipesResult is the summary info returned by chef-api when listing
// http://docs.opscode.com/api_chef_server.html#cookbooks-recipes
type CookbookRecipesResult []string
// CookbookVersions is the data container returned from the chef server when listing all cookbooks
type CookbookVersions struct {
Url string `json:"url,omitempty"`
Versions []CookbookVersion `json:"versions,omitempty"`
}
// CookbookVersion is the data for a specific cookbook version
type CookbookVersion struct {
Url string `json:"url,omitempty"`
Version string `json:"version,omitempty"`
}
// CookbookMeta represents a Golang version of cookbook metadata
type CookbookMeta struct {
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
Description string `json:"description,omitempty"`
LongDescription string `json:"long_description,omitempty"`
Maintainer string `json:"maintainer,omitempty"`
MaintainerEmail string `json:"maintainer_email,omitempty"`
License string `json:"license,omitempty"`
Platforms map[string]string `json:"platforms,omitempty"`
Depends map[string]string `json:"dependencies,omitempty"`
Reccomends map[string]string `json:"recommendations,omitempty"`
Suggests map[string]string `json:"suggestions,omitempty"`
Conflicts map[string]string `json:"conflicting,omitempty"`
Provides map[string]string `json:"providing,omitempty"`
Replaces map[string]string `json:"replacing,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"` // this has a format as well that could be typed, but blargh https://github.com/lob/chef/blob/master/cookbooks/apache2/metadata.json
Groupings map[string]interface{} `json:"groupings,omitempty"` // never actually seen this used.. looks like it should be map[string]map[string]string, but not sure http://docs.opscode.com/essentials_cookbook_metadata.html
Recipes map[string]string `json:"recipes,omitempty"`
}
// CookbookAccess represents the permissions on a Cookbook
type CookbookAccess struct {
Read bool `json:"read,omitempty"`
Create bool `json:"create,omitempty"`
Grant bool `json:"grant,omitempty"`
Update bool `json:"update,omitempty"`
Delete bool `json:"delete,omitempty"`
}
// Cookbook represents the native Go version of the deserialized api cookbook
type Cookbook struct {
CookbookName string `json:"cookbook_name"`
Name string `json:"name"`
Version string `json:"version,omitempty"`
ChefType string `json:"chef_type,omitempty"`
Frozen bool `json:"frozen?,omitempty"`
JsonClass string `json:"json_class,omitempty"`
Files []CookbookItem `json:"files,omitempty"`
Templates []CookbookItem `json:"templates,omitempty"`
Attributes []CookbookItem `json:"attributes,omitempty"`
Recipes []CookbookItem `json:"recipes,omitempty"`
Definitions []CookbookItem `json:"definitions,omitempty"`
Libraries []CookbookItem `json:"libraries,omitempty"`
Providers []CookbookItem `json:"providers,omitempty"`
Resources []CookbookItem `json:"resources,omitempty"`
RootFiles []CookbookItem `json:"root_files,omitempty"`
Metadata CookbookMeta `json:"metadata,omitempty"`
Access CookbookAccess `json:"access,omitempty"`
}
// String makes CookbookListResult implement the string result
func (c CookbookListResult) String() (out string) {
for k, v := range c {
out += fmt.Sprintf("%s => %s\n", k, v.Url)
for _, i := range v.Versions {
out += fmt.Sprintf(" * %s\n", i.Version)
}
}
return out
}
// versionParams assembles a querystring for the chef api's num_versions
// This is used to restrict the number of versions returned in the reponse
func versionParams(path, numVersions string) string {
if numVersions == "0" {
numVersions = "all"
}
// need to optionally add numVersion args to the request
if len(numVersions) > 0 {
path = fmt.Sprintf("%s?num_versions=%s", path, numVersions)
}
return path
}
// Get retruns a CookbookVersion for a specific cookbook
// GET /cookbooks/name
func (c *CookbookService) Get(name string) (data CookbookVersion, err error) {
path := fmt.Sprintf("cookbooks/%s", name)
err = c.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// GetAvailable returns the versions of a coookbook available on a server
func (c *CookbookService) GetAvailableVersions(name, numVersions string) (data CookbookListResult, err error) {
path := versionParams(fmt.Sprintf("cookbooks/%s", name), numVersions)
err = c.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// GetVersion fetches a specific version of a cookbooks data from the server api
// GET /cookbook/foo/1.2.3
// GET /cookbook/foo/_latest
// Chef API docs: https://docs.chef.io/api_chef_server.html#cookbooks-name-version
func (c *CookbookService) GetVersion(name, version string) (data Cookbook, err error) {
url := fmt.Sprintf("cookbooks/%s/%s", name, version)
err = c.client.magicRequestDecoder("GET", url, nil, &data)
return
}
// ListVersions lists the cookbooks available on the server limited to numVersions
// Chef API docs: https://docs.chef.io/api_chef_server.html#cookbooks-name
func (c *CookbookService) ListAvailableVersions(numVersions string) (data CookbookListResult, err error) {
path := versionParams("cookbooks", numVersions)
err = c.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// ListAllRecipes lists the names of all recipes in the most recent cookbook versions
// Chef API docs: https://docs.chef.io/api_chef_server.html#cookbooks-recipes
func (c *CookbookService) ListAllRecipes() (data CookbookRecipesResult, err error) {
path := "cookbooks/_recipes"
err = c.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// List returns a CookbookListResult with the latest versions of cookbooks available on the server
func (c *CookbookService) List() (CookbookListResult, error) {
return c.ListAvailableVersions("")
}
// DeleteVersion removes a version of a cook from a server
func (c *CookbookService) Delete(name, version string) (err error) {
path := fmt.Sprintf("cookbooks/%s/%s", name, version)
err = c.client.magicRequestDecoder("DELETE", path, nil, nil)
return
}