forked from marpaia/chef-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
77 lines (70 loc) · 1.84 KB
/
data.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
package chef
import (
"encoding/json"
"fmt"
"strings"
)
// chef.GetData returns a map of databag names to their related REST URL
// endpoint as well as an error indicating if the request was successful or not
//
// Usage:
//
// data, err := chef.GetData()
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// for d := range data {
// fmt.Println(d)
// }
func (chef *Chef) GetData() (map[string]string, error) {
resp, err := chef.Get("data")
if err != nil {
return nil, err
}
body, err := responseBody(resp)
if err != nil {
return nil, err
}
data := map[string]string{}
json.Unmarshal(body, &data)
return data, nil
}
// chef.GetDataByName accept a string which represents the name of a specific
// databag and returns a map of information about that databag, a bool
// indicating whether or not the databag was found and an error indicating if
// the request failed or not.
//
// Note that if the request is successful but no such data item existed, the
// error return value will be nil but the bool will be false
//
// Usage:
//
// data, ok, err := chef.GetDataByName("apache")
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// if !ok {
// fmt.Println("Couldn't find that databag!")
// } else {
// // do what you please with the "data" variable which is of the
// // map[string]string type
// fmt.Println(data)
// }
func (chef *Chef) GetDataByName(name string) (map[string]string, bool, error) {
resp, err := chef.Get(fmt.Sprintf("data/%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
}
data := map[string]string{}
json.Unmarshal(body, &data)
return data, true, nil
}