File: databag.go

package info (click to toggle)
golang-github-go-chef-chef 0.0.1%2Bgit20161023.60.deb8c38-1.2~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 372 kB
  • sloc: sh: 14; makefile: 7
file content (108 lines) | stat: -rw-r--r-- 3,517 bytes parent folder | download | duplicates (3)
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
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
// http://docs.getchef.com/api_chef_server.html#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: http://docs.getchef.com/api_chef_server.html#id18
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: http://docs.getchef.com/api_chef_server.html#id19
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: ????????????????
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: http://docs.getchef.com/api_chef_server.html#id20
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: http://docs.getchef.com/api_chef_server.html#id21
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: http://docs.getchef.com/api_chef_server.html#id22
func (d *DataBagService) DeleteItem(databagName, 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: http://docs.getchef.com/api_chef_server.html#id23
func (d *DataBagService) GetItem(databagName, 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: http://docs.getchef.com/api_chef_server.html#id24
func (d *DataBagService) UpdateItem(databagName, 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)
}