forked from go-chef/chef
/
group.go
84 lines (72 loc) · 2.13 KB
/
group.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
//
// Test the go-chef/chef chef server api /group endpoints against a live server
//
package main
import (
"chefapi_test/testapi"
"fmt"
"github.com/go-chef/chef"
"os"
)
// main Exercise the chef server api
func main() {
client := testapi.Client()
// List the current groups
groupList, err := client.Groups.List()
if err != nil {
fmt.Fprintln(os.Stderr, "Issue printing the existing groups:", err)
}
fmt.Printf("List initial groups %+vEndInitialList\n", groupList)
// Build a stucture to define a group
group1 := chef.Group{
Name: "group1",
GroupName: "group1",
}
// Add a new group
groupAdd, err := client.Groups.Create(group1)
if err != nil {
fmt.Fprintln(os.Stderr, "Issue adding group1:", err)
}
fmt.Println("Added group1", groupAdd)
// Add group again
groupAdd, err = client.Groups.Create(group1)
if err != nil {
fmt.Fprintln(os.Stderr, "Issue recreating group1:", err)
}
fmt.Println("Recreated group1", groupAdd)
// List groups after adding
groupList, err = client.Groups.List()
if err != nil {
fmt.Fprintln(os.Stderr, "Issue printing the existing groups:", err)
}
fmt.Printf("List groups after adding group1 %+vEndAddList\n", groupList)
// Get new group
groupOut, err := client.Groups.Get("group1")
if err != nil {
fmt.Fprintln(os.Stderr, "Issue getting group1:", err)
}
fmt.Printf("Get group1 %+v\n", groupOut)
// Try to get a missing group
groupOutMissing, err := client.Groups.Get("nothere")
if err != nil {
fmt.Fprintln(os.Stderr, "Issue getting nothere:", err)
}
fmt.Println("Get nothere", groupOutMissing)
// Update a group
group1.GroupName = "group1-new"
group1.Users = append(group1.Users, "pivotal")
groupUpdate, err := client.Groups.Update(group1)
if err != nil {
fmt.Fprintln(os.Stderr, "Issue updating group1:", err)
}
fmt.Printf("Update group1 %+v\n", groupUpdate)
// Clean up
err = client.Groups.Delete("group1-new")
fmt.Println("Delete group1", err)
// Final list of groups
groupList, err = client.Groups.List()
if err != nil {
fmt.Fprintln(os.Stderr, "Issue listing the final groups:", err)
}
fmt.Printf("List groups after cleanup %+vEndFinalList\n", groupList)
}