github.com/go-chef/chef@v0.30.1/testapi/group.go (about)

     1  //
     2  // Test the go-chef/chef chef server api /group endpoints against a live server
     3  //
     4  
     5  // TODO: add users and then add them to groups. Seems to fail. pivotal is maybe not a good test. adding fails silently
     6  package testapi
     7  
     8  import (
     9  	"fmt"
    10  	"github.com/go-chef/chef"
    11  	"os"
    12  )
    13  
    14  // group exercise the chef server api
    15  func Group() {
    16  	client := Client(nil)
    17  
    18  	// List the current groups
    19  	groupList, err := client.Groups.List()
    20  	if err != nil {
    21  		fmt.Fprintln(os.Stderr, "Issue printing the existing groups:", err)
    22  	}
    23  	fmt.Printf("List initial groups %+vEndInitialList\n", groupList)
    24  
    25  	// Build a stucture to define a group
    26  	group1 := chef.Group{
    27  		Name:      "group1",
    28  		GroupName: "group1",
    29  	}
    30  
    31  	// Add a new group
    32  	groupAdd, err := client.Groups.Create(group1)
    33  	if err != nil {
    34  		fmt.Fprintln(os.Stderr, "Issue adding group1:", err)
    35  	}
    36  	fmt.Println("Added group1", groupAdd)
    37  
    38  	// Add group again
    39  	groupAdd, err = client.Groups.Create(group1)
    40  	if err != nil {
    41  		fmt.Fprintln(os.Stderr, "Issue recreating group1:", err)
    42  	}
    43  	cerr, err := chef.ChefError(err)
    44  	if cerr != nil {
    45  		fmt.Fprintln(os.Stderr, "Issue recreating group1:", cerr.StatusCode())
    46  	}
    47  	fmt.Println("Recreated group1", groupAdd)
    48  
    49  	// List groups after adding
    50  	groupList, err = client.Groups.List()
    51  	if err != nil {
    52  		fmt.Fprintln(os.Stderr, "Issue printing the existing groups:", err)
    53  	}
    54  	fmt.Printf("List groups after adding group1 %+vEndAddList\n", groupList)
    55  
    56  	// Get new group
    57  	groupOut, err := client.Groups.Get("group1")
    58  	if err != nil {
    59  		fmt.Fprintln(os.Stderr, "Issue getting group1:", err)
    60  	}
    61  	fmt.Printf("Get group1 %+v\n", groupOut)
    62  
    63  	// Try to get a missing group
    64  	groupOutMissing, err := client.Groups.Get("nothere")
    65  	if err != nil {
    66  		fmt.Fprintln(os.Stderr, "Issue getting nothere:", err)
    67  	}
    68  	fmt.Println("Get nothere", groupOutMissing)
    69  
    70  	// Update a group
    71  	groupupdate := chef.GroupUpdate{}
    72  	groupupdate.Name = "group1"
    73  	groupupdate.GroupName = "group1"
    74  	groupupdate.Actors.Clients = groupOut.Clients
    75  	groupupdate.Actors.Groups = []string{}
    76  	groupupdate.Actors.Users = []string{"pivotal"}
    77  	groupUpOut, err := client.Groups.Update(groupupdate)
    78  	if err != nil {
    79  		fmt.Fprintln(os.Stderr, "Issue updating group1:", err)
    80  	}
    81  	fmt.Printf("Update group1 %+v\n", groupUpOut)
    82  
    83  	// Get new group after update
    84  	groupOut, err = client.Groups.Get("group1")
    85  	if err != nil {
    86  		fmt.Fprintln(os.Stderr, "Issue getting group1:", err)
    87  	}
    88  	fmt.Printf("Get group1 after update %+v\n", groupOut)
    89  
    90  	// Update a group add groups and change the group name
    91  	groupupdate = chef.GroupUpdate{}
    92  	groupupdate.Name = "group1"
    93  	groupupdate.GroupName = "group1-new"
    94  	groupupdate.Actors.Clients = []string{}
    95  	groupupdate.Actors.Groups = []string{"admins"}
    96  	groupupdate.Actors.Users = []string{}
    97  	groupUpOut, err = client.Groups.Update(groupupdate)
    98  	if err != nil {
    99  		fmt.Fprintln(os.Stderr, "Issue updating group1:", err)
   100  	}
   101  	fmt.Printf("Update group1 %+v\n", groupUpOut)
   102  
   103  	// Get new group after update and rename
   104  	groupOut, err = client.Groups.Get("group1-new")
   105  	if err != nil {
   106  		fmt.Fprintln(os.Stderr, "Issue getting group1-new:", err)
   107  	}
   108  	fmt.Printf("Get group1-new after update %+v\n", groupOut)
   109  
   110  	// Clean up
   111  	err = client.Groups.Delete("group1-new")
   112  	fmt.Println("Delete group1", err)
   113  
   114  	// Final list of groups
   115  	groupList, err = client.Groups.List()
   116  	if err != nil {
   117  		fmt.Fprintln(os.Stderr, "Issue listing the final groups:", err)
   118  	}
   119  	fmt.Printf("List groups after cleanup %+vEndFinalList\n", groupList)
   120  }