github.com/go-chef/chef@v0.30.1/testapi/policy.go (about) 1 // Test the go-chef/chef chef server api /policies endpoints against a live server 2 package testapi 3 4 import ( 5 "fmt" 6 "github.com/go-chef/chef" 7 "os" 8 ) 9 10 // policy exercise the chef server api 11 func Policy() { 12 client := Client(nil) 13 14 // List policies 15 policyList, err := client.Policies.List() 16 if err != nil { 17 fmt.Fprintln(os.Stderr, "Issue printing the existing policies:", err) 18 } 19 fmt.Printf("List policies %+v\n", policyList) 20 21 policyName, policy := firstPolicy(policyList) 22 revisionID := firstRevision(policy) 23 24 // Get policy 25 policyOut, err := client.Policies.Get(policyName) 26 if err != nil { 27 fmt.Fprintf(os.Stderr, "Issue getting %+v err %+v\n", policyName, err) 28 } 29 fmt.Printf("Get %+v %+v\n", policyName, policyOut) 30 31 // Get policy revision 32 policyRevOut, err := client.Policies.GetRevisionDetails(policyName, revisionID) 33 if err != nil { 34 fmt.Fprintf(os.Stderr, "Issue getting %+v err %+v\n", policyName, err) 35 } 36 fmt.Printf("Get %+v revision %+v\n", policyName, policyRevOut) 37 38 // Delete a revision from a policy 39 revOutDel, err := client.Policies.DeleteRevision(policyName, revisionID) 40 if err != nil { 41 fmt.Fprintf(os.Stderr, "Issue deleting revision from %+v: %+v\n", policyName, err) 42 } 43 fmt.Printf("Delete revision %v from %+v %+v\n", revisionID, policyName, revOutDel) 44 45 // Try to get a missing policy 46 policyOutMissing, err := client.Policies.Get("nothere") 47 if err != nil { 48 fmt.Fprintf(os.Stderr, "Issue getting nothere: %+v\n", err) 49 } 50 fmt.Printf("Get nothere %+v\n", policyOutMissing) 51 52 // Delete a policy 53 policyOutDel, err := client.Policies.Delete("testsamp2") 54 if err != nil { 55 fmt.Fprintf(os.Stderr, "Issue deleting testsamp2: %+v\n", err) 56 } 57 fmt.Printf("Delete testsamp2 %+v\n", policyOutDel) 58 } 59 60 func firstPolicy(policyList chef.PoliciesGetResponse) (string, chef.Policy) { 61 for key, val := range policyList { 62 return key, val 63 } 64 return "", chef.Policy{} 65 } 66 67 func firstRevision(policy chef.Policy) string { 68 for key, _ := range policy.Revisions { 69 return key 70 } 71 return "" 72 }