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

     1  // Test the go-chef/chef chef server api /organization/:org/user and /organization/:org/association_requests
     2  // endpoints against a live server
     3  package testapi
     4  
     5  import (
     6  	"fmt"
     7  	"github.com/go-chef/chef"
     8  	"os"
     9  )
    10  
    11  // association exercise the chef server api
    12  func Association() {
    13  	client := Client(nil)
    14  
    15  	// Build stuctures to invite users
    16  	invite := chef.Request{
    17  		User: "usrinvite",
    18  	}
    19  	invite2 := chef.Request{
    20  		User: "usr2invite",
    21  	}
    22  	invitemissing := chef.Request{
    23  		User: "nouser",
    24  	}
    25  	add1 := chef.AddNow{
    26  		Username: "usradd",
    27  	}
    28  
    29  	// Invite the user to the test org
    30  	out, err := client.Associations.Invite(invite)
    31  	if err != nil {
    32  		fmt.Fprintf(os.Stderr, "Issue inviting a user %+v %+v\n", invite, err)
    33  	}
    34  	fmt.Printf("Invited user %+v %+v\n", invite, out)
    35  
    36  	// Invite a second user
    37  	out, err = client.Associations.Invite(invite2)
    38  	fmt.Printf("Invited user %+v %+v\n", invite2, out)
    39  
    40  	// fail at inviting a missing user.  Should get a 404
    41  	out, err = client.Associations.Invite(invitemissing)
    42  	if err != nil {
    43  		fmt.Fprintf(os.Stderr, "Issue inviting a user %+v %+v\n", invitemissing, err)
    44  	}
    45  	fmt.Printf("Invited user %+v %+v\n", invitemissing, out)
    46  
    47  	// Find a pending invitation by user name
    48  	id, err := client.Associations.InviteId("usr2invite")
    49  	if err != nil {
    50  		fmt.Fprintf(os.Stderr, "Issue finding an invitation for usr2invite %+v\n", err)
    51  	}
    52  	fmt.Printf("Invitation id for usr2invite %+v\n", id)
    53  
    54  	// Accept the invite for invite2
    55  	// outa, err := client.Associations.AcceptInvite(id)
    56  	// if err != nil {
    57  	// fmt.Fprintf(os.Stderr, "Issue accepting the invitation %+v\n", err)
    58  	// }
    59  	// fmt.Printf("Accept invitation %+v\n", outa)
    60  
    61  	// List the invites
    62  
    63  	outl, err := client.Associations.ListInvites()
    64  	if err != nil {
    65  		fmt.Fprintf(os.Stderr, "Issue listing the invitations %+v\n", err)
    66  	}
    67  	fmt.Printf("Invitation list %+v\n", outl)
    68  
    69  	// Delete the invitations by id
    70  	for _, in := range outl {
    71  		outd, err := client.Associations.DeleteInvite(in.Id)
    72  		if err != nil {
    73  			fmt.Fprintf(os.Stderr, "Issue deleting an invitation for %s  %+v\n", in.UserName, err)
    74  		}
    75  		fmt.Printf("Deleted invitation %s for %s %+v\n", in.Id, in.UserName, outd)
    76  	}
    77  
    78  	// Add a user to the test organization
    79  	err = client.Associations.Add(add1)
    80  	if err != nil {
    81  		fmt.Fprintf(os.Stderr, "Issue adding user usradd: %+v\n", err)
    82  	}
    83  	fmt.Printf("User added: %+v\n", add1)
    84  	// List the users
    85  	ulist, err := client.Associations.List()
    86  	if err != nil {
    87  		fmt.Fprintf(os.Stderr, "Issue listing the users: %+v\n", err)
    88  	}
    89  	fmt.Printf("Users list: %+v\n", ulist)
    90  	// Get the user details
    91  	uget, err := client.Associations.Get("usradd")
    92  	if err != nil {
    93  		fmt.Fprintf(os.Stderr, "Issue getting user details: %+v\n", err)
    94  	}
    95  	fmt.Printf("User details: %+v\n", uget)
    96  	// Delete a user from the organization
    97  	udel, err := client.Associations.Get("usradd")
    98  	if err != nil {
    99  		fmt.Fprintf(os.Stderr, "Issue deleting usradd: %+v\n", err)
   100  	}
   101  	fmt.Printf("User deleted: %+v\n", udel)
   102  }