forked from go-chef/chef
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolicy.go
More file actions
74 lines (63 loc) · 1.97 KB
/
policy.go
File metadata and controls
74 lines (63 loc) · 1.97 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
//
// Test the go-chef/chef chef server api /policies endpoints against a live server
//
package testapi
import (
"fmt"
"github.com/go-chef/chef"
"os"
)
// policy exercise the chef server api
func Policy() {
client := Client()
// List policies
policyList, err := client.Policies.List()
if err != nil {
fmt.Fprintln(os.Stderr, "Issue printing the existing policies:", err)
}
fmt.Printf("List policies %+v\n", policyList)
policyName, policy := firstPolicy(policyList)
revisionID := firstRevision(policy)
// Get policy
policyOut, err := client.Policies.Get(policyName)
if err != nil {
fmt.Fprintf(os.Stderr, "Issue getting %+v err %+v\n", policyName, err)
}
fmt.Printf("Get %+v %+v\n", policyName, policyOut)
// Get policy revision
policyRevOut, err := client.Policies.GetRevisionDetails(policyName, revisionID)
if err != nil {
fmt.Fprintf(os.Stderr, "Issue getting %+v err %+v\n", policyName, err)
}
fmt.Printf("Get %+v revision %+v\n", policyName, policyRevOut)
// Delete a revision from a policy
revOutDel, err := client.Policies.DeleteRevision(policyName, revisionID)
if err != nil {
fmt.Fprintf(os.Stderr, "Issue deleting revision from %+v: %+v\n", policyName, err)
}
fmt.Printf("Delete revision %v from %+v %+v\n", revisionID, policyName, revOutDel)
// Try to get a missing policy
policyOutMissing, err := client.Policies.Get("nothere")
if err != nil {
fmt.Fprintf(os.Stderr, "Issue getting nothere: %+v\n", err)
}
fmt.Printf("Get nothere %+v\n", policyOutMissing)
// Delete a policy
policyOutDel, err := client.Policies.Delete("testsamp2")
if err != nil {
fmt.Fprintf(os.Stderr, "Issue deleting testsamp2: %+v\n", err)
}
fmt.Printf("Delete testsamp2 %+v\n", policyOutDel)
}
func firstPolicy(policyList chef.PoliciesGetResponse) (string, chef.Policy) {
for key, val := range policyList {
return key, val
}
return "", chef.Policy{}
}
func firstRevision(policy chef.Policy) string {
for key, _ := range policy.Revisions {
return key
}
return ""
}