forked from marpaia/chef-golang
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata.go
117 lines (107 loc) · 2.92 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
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
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
}
// chef.GetTypedDataByName accepts a reference to a type to Unmarshal the data bag
// JSON in to, and a string which represents the name of a specific databag.
//
// Returns 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:
//
// var cfg MyAppConfig
// ok, err := chef.GetTypedDataByName(&cfg, "apache")
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// if !ok {
// fmt.Println("Couldn't find that databag!")
// } else {
// fmt.Printf("%#v", cfg)
// }
func (chef *Chef) GetTypedDataByName(data interface{}, name string) (bool, error) {
resp, err := chef.Get(fmt.Sprintf("data/%s", name))
if err != nil {
return false, err
}
body, err := responseBody(resp)
if err != nil {
if strings.Contains(err.Error(), "404") {
return false, nil
}
return false, err
}
json.Unmarshal(body, &data)
return true, nil
}