forked from go-chef/chef
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrole.go
More file actions
142 lines (125 loc) · 3.53 KB
/
role.go
File metadata and controls
142 lines (125 loc) · 3.53 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Test the go-chef/chef chef server api /role endpoints against a live server
//
package testapi
import (
"encoding/json"
"fmt"
"github.com/go-chef/chef"
"os"
)
// role exercise the chef server api
func Role() {
client := Client()
// The environment need to exist for the GetEnvironmentRunlist function to work
create_en1(client)
// Build a stucture to define a role
role1 := create_role1()
// Add a new role
roleAdd, err := client.Roles.Create(&role1)
if err != nil {
fmt.Fprintln(os.Stderr, "Issue adding role1:", err)
}
fmt.Printf("Added role1 %+v\n", roleAdd)
// Add role again
roleAdd, err = client.Roles.Create(&role1)
if err != nil {
fmt.Fprintln(os.Stderr, "Issue recreating role1:", err)
}
cerr, err := chef.ChefError(err)
if cerr != nil {
fmt.Fprintln(os.Stderr, "Issue recreating role1:", cerr.StatusCode())
}
fmt.Printf("Recreated role1 %+v\n", roleAdd)
// List roles after adding
roleList, err := client.Roles.List()
if err != nil {
fmt.Fprintln(os.Stderr, "Issue printing the existing roles:", err)
}
fmt.Printf("List roles after adding role1 %+v\n", roleList)
// Get new role
roleOut, err := client.Roles.Get("role1")
if err != nil {
fmt.Fprintln(os.Stderr, "Issue getting role1:", err)
}
fmt.Printf("Get role1 %+v\n", roleOut)
// Try to get a missing role
roleOutMissing, err := client.Roles.Get("nothere")
if err != nil {
fmt.Fprintln(os.Stderr, "Issue getting nothere:", err)
}
fmt.Printf("Get nothere %+v\n", roleOutMissing)
// Update a role
role1.Description = "Changed Role"
// TODO: try changing the runlists, attributes, environment run list
roleUpdate, err := client.Roles.Put(&role1)
if err != nil {
fmt.Fprintln(os.Stderr, "Issue updating role1:", err)
}
fmt.Printf("Update role1 %+v\n", roleUpdate)
envList, err := client.Roles.GetEnvironments("role1")
if err != nil {
fmt.Fprintln(os.Stderr, "Issue listing environments for role1:", err)
}
fmt.Printf("Environments for role1 %+v\n", envList)
envRunList, err := client.Roles.GetEnvironmentRunlist("role1", "en1")
if err != nil {
fmt.Fprintln(os.Stderr, "Issue listing runlist for role1::en1:", err)
}
fmt.Printf("Environments for role1 %+v\n", envRunList)
// Clean up
err = client.Roles.Delete("role1")
if err != nil {
fmt.Fprintln(os.Stderr, "Issue deleting role1", err)
}
fmt.Printf("Delete role1 %+v\n", err)
_, err = client.Environments.Delete("en1")
// Final list of roles
roleList, err = client.Roles.List()
if err != nil {
fmt.Fprintln(os.Stderr, "Issue listing the final roles:", err)
}
fmt.Printf("List roles after cleanup %+v\n", roleList)
}
func create_en1(client *chef.Client) {
en1 := chef.Environment{
Name: "en1",
Description: "Test environment",
CookbookVersions: map[string]string{
"a": "0.0.0",
},
}
_, err := client.Environments.Create(&en1)
if err != nil {
fmt.Fprintln(os.Stderr, "Issue adding en1:", err)
}
return
}
func create_role1() chef.Role {
defIn := []byte(`{
"git_repo": "here.git",
"users": ["root", "moe"]
}`)
ovrIn := []byte(`{
"env": {
"mine": "ample",
"yours": "full"
}
}`)
var defAtt interface{}
var ovrAtt interface{}
json.Unmarshal(defIn, &defAtt)
json.Unmarshal(ovrIn, &ovrAtt)
role1 := chef.Role{
Name: "role1",
DefaultAttributes: defAtt,
Description: "Test role",
EnvRunList: chef.EnvRunList{
"en1": []string{"recipe[foo1]", "recipe[foo2]"},
"en2": []string{"recipe[foo2]"},
},
RunList: []string{"recipe[foo]", "recipe[baz]", "role[banana]"},
OverrideAttributes: ovrAtt,
}
return role1
}