-
Notifications
You must be signed in to change notification settings - Fork 65
/
databag.go
116 lines (100 loc) · 3.45 KB
/
databag.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
package chef
import "fmt"
// DataBagService is the service for interacting with the chef server data endpoint
type DataBagService struct {
client *Client
}
// DataBagItem is a data bag item
type DataBagItem interface{}
// DataBag is a data bag
type DataBag struct {
Name string `json:"name"`
JsonClass string `json:"json_class"`
ChefType string `json:"chef_type"`
}
type DataBagCreateResult struct {
URI string `json:"uri"`
}
// DataBagListResult is the list of data bags returned by chef-api when listing
// https://docs.chef.io/api_chef_server/#data
type DataBagListResult map[string]string
// String makes DataBagListResult implement the string result
func (d DataBagListResult) String() (out string) {
for k, v := range d {
out += fmt.Sprintf("%s => %s\n", k, v)
}
return out
}
// List returns a list of databags on the server
//
// Chef API Docs: https://docs.chef.io/api_chef_server/#get-19
func (d *DataBagService) List() (data *DataBagListResult, err error) {
path := fmt.Sprintf("data")
err = d.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// Create adds a data bag to the server
//
// Chef API Docs: https://docs.chef.io/api_chef_server/#post-7
func (d *DataBagService) Create(databag *DataBag) (result *DataBagCreateResult, err error) {
body, err := JSONReader(databag)
if err != nil {
return
}
err = d.client.magicRequestDecoder("POST", "data", body, &result)
return
}
// Delete removes a data bag from the server
//
// Chef API Docs: https://docs.chef.io/api_chef_server/#delete-7
func (d *DataBagService) Delete(name string) (result *DataBag, err error) {
path := fmt.Sprintf("data/%s", name)
err = d.client.magicRequestDecoder("DELETE", path, nil, &result)
return
}
// ListItems gets a list of the items in a data bag.
//
// Chef API Docs: https://docs.chef.io/api_chef_server/#get-20
func (d *DataBagService) ListItems(name string) (data *DataBagListResult, err error) {
path := fmt.Sprintf("data/%s", name)
err = d.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// CreateItem adds an item to a data bag
//
// Chef API Docs: https://docs.chef.io/api_chef_server/#post-8
func (d *DataBagService) CreateItem(databagName string, databagItem DataBagItem) (err error) {
body, err := JSONReader(databagItem)
if err != nil {
return
}
path := fmt.Sprintf("data/%s", databagName)
return d.client.magicRequestDecoder("POST", path, body, nil)
}
// DeleteItem deletes an item from a data bag
//
// Chef API Docs: https://docs.chef.io/api_chef_server/#delete-8
func (d *DataBagService) DeleteItem(databagName string, databagItem string) (err error) {
path := fmt.Sprintf("data/%s/%s", databagName, databagItem)
err = d.client.magicRequestDecoder("DELETE", path, nil, nil)
return
}
// GetItem gets an item from a data bag
//
// Chef API Docs: https://docs.chef.io/api_chef_server/#get-21
func (d *DataBagService) GetItem(databagName string, databagItem string) (item DataBagItem, err error) {
path := fmt.Sprintf("data/%s/%s", databagName, databagItem)
err = d.client.magicRequestDecoder("GET", path, nil, &item)
return
}
// UpdateItem updates an item in a data bag
//
// Chef API Docs: https://docs.chef.io/api_chef_server/#put-6
func (d *DataBagService) UpdateItem(databagName string, databagItemId string, databagItem DataBagItem) (err error) {
body, err := JSONReader(databagItem)
if err != nil {
return
}
path := fmt.Sprintf("data/%s/%s", databagName, databagItemId)
return d.client.magicRequestDecoder("PUT", path, body, nil)
}