forked from marpaia/chef-golang
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrole.go
89 lines (81 loc) · 2.39 KB
/
role.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
package chef
import (
"encoding/json"
"fmt"
"strings"
)
// chef.Role represents the relevant attributes of a Chef role
type Role struct {
Name string `json:"name"`
ChefType string `json:"chef_type"`
JSONClass string `json:"json_class"`
DefaultAttributes map[string]interface{} `json:"default_attributes"`
OverrideAttributes map[string]interface{} `json:"override_attributes"`
RunList []string `json:"run_list"`
}
// chef.GetRoles returns a map of role names to a string which represents the
// role's RESTful URL as well as an error indicating if the request was
// successful or not.
//
// Usgae:
//
// roles, err := chef.GetRoles()
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// // do what you please with the "roles" variable which is a map of
// // role names to their RESTful URLs
// for role := range roles {
// fmt.Println(role)
// }
func (chef *Chef) GetRoles() (map[string]string, error) {
resp, err := chef.Get("roles")
if err != nil {
return nil, err
}
body, err := responseBody(resp)
if err != nil {
return nil, err
}
roles := map[string]string{}
json.Unmarshal(body, &roles)
return roles, nil
}
// chef.GetRole returns a pointer to the chef.Role type for a given string that
// represents a role name. It also returns a bool indicating whether or not the
// client was found and an error indicating if the request failed or not.
//
// Note that if the request is successful but no such client existed, the error
// return value will be nil but the bool will be false.
//
// Usage:
//
// role, ok, err := chef.GetRole("neo4j")
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// if !ok {
// fmt.Println("Couldn't find that role!")
// } else {
// // do what you please with the "role" variable which is of the
// // *Chef.Role
// fmt.Printf("%#v\n", role)
// }
func (chef *Chef) GetRole(name string) (*Role, bool, error) {
resp, err := chef.Get(fmt.Sprintf("roles/%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
}
role := new(Role)
json.Unmarshal(body, role)
return role, true, nil
}