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

     1  package chef
     2  
     3  // import "fmt"
     4  import "errors"
     5  
     6  type AssociationService struct {
     7  	client *Client
     8  }
     9  
    10  // Chef API docs: https://docs.chef.io/api_chef_server.html#association-requests
    11  // https://github.com/chef/chef-server/blob/master/src/oc_erchef/apps/oc_chef_wm/src/oc_chef_wm_org_invites.erl  Invitation implementation
    12  // https://github.com/chef/chef-server/blob/master/src/oc_erchef/apps/oc_chef_wm/src/oc_chef_wm_org_associations.erl user org associations
    13  
    14  // Association represents the response from creating an invitation to join an organization
    15  // POST /organization/NAME/association_requests
    16  type Association struct {
    17  	Uri              string `json:"uri"` // the last part of the uri is the invitation id
    18  	OrganizationUser struct {
    19  		UserName string `json:"username,omitempty"`
    20  	} `json:"organization_user"`
    21  	Organization struct {
    22  		Name string `json:"name,omitempty"`
    23  	} `json:"organization"`
    24  	User struct {
    25  		Email     string `json:"email,omitempty"`
    26  		FirstName string `json:"first_name,omitempty"`
    27  	} `json:"user"`
    28  }
    29  
    30  // RescindInvite respresents the response from deleting an invitation
    31  // DELETE /organization/NAME/association_requests/ID
    32  type RescindInvite struct {
    33  	Id       string `json:"id,omitempty"`
    34  	Orgname  string `json:"orgname,omitempty"`
    35  	Username string `json:"username,omitempty"`
    36  }
    37  
    38  // Invite represents an entry in the array of responses listing the outstanding invitations
    39  // GET /organization/NAME/association_requests
    40  type Invite struct {
    41  	Id       string `json:"id,omitempty"`
    42  	UserName string `json:"username,omitempty"`
    43  }
    44  
    45  // Request represents the body of the request to invite a user to an organization
    46  // POST /organization/NAME/association_requests
    47  type Request struct {
    48  	User string `json:"user"`
    49  }
    50  
    51  // AddNow represents the body of the request to add a user to an organization
    52  // POST /organization/NAME/users
    53  type AddNow struct {
    54  	Username string `json:"username"`
    55  }
    56  
    57  // Invite represents an entry in the array of responses listing the users in an organization
    58  // GET /organization/NAME/association_requests
    59  type OrgUserListEntry struct {
    60  	User struct {
    61  		Username string `json:"username,omitempty"`
    62  	} `json:"user,omitempty"`
    63  }
    64  
    65  // OrgUser represents the detailed information about a user in an organization
    66  // GET /organization/NAME/user/NAME
    67  // DELETE /organization/NAME/user/NAME
    68  type OrgUser struct {
    69  	Username    string `json:"username,omitempty"`
    70  	Email       string `json:"email,omitempty"`
    71  	DisplayName string `json:"display_name,omitempty"`
    72  	FirstName   string `json:"first_name,omitempty"`
    73  	LastName    string `json:"last_name,omitempty"`
    74  	PublicKey   string `json:"public_key,omitempty"`
    75  }
    76  
    77  // ListInvites gets a list of the pending invitations for an organization.
    78  func (e *AssociationService) ListInvites() (invitelist []Invite, err error) {
    79  	err = e.client.magicRequestDecoder("GET", "association_requests", nil, &invitelist)
    80  	return
    81  }
    82  
    83  // Invite creates an invitation for a user to join an organization on the chef server
    84  func (e *AssociationService) Invite(invite Request) (data Association, err error) {
    85  	body, err := JSONReader(invite)
    86  	if err != nil {
    87  		return
    88  	}
    89  	err = e.client.magicRequestDecoder("POST", "association_requests/", body, &data)
    90  	return
    91  }
    92  
    93  // DeleteInvite removes a pending invitation to an organization
    94  func (e *AssociationService) DeleteInvite(id string) (rescind RescindInvite, err error) {
    95  	err = e.client.magicRequestDecoder("DELETE", "association_requests/"+id, nil, &rescind)
    96  	return
    97  }
    98  
    99  // InviteID Finds an invitation id for a user
   100  func (e *AssociationService) InviteId(user string) (id string, err error) {
   101  	var invitelist []Invite
   102  	err = e.client.magicRequestDecoder("GET", "association_requests", nil, &invitelist)
   103  	if err != nil {
   104  		return
   105  	}
   106  	// Find an invite for the user or return err
   107  	for _, in := range invitelist {
   108  		if in.UserName == user {
   109  			id = in.Id
   110  		}
   111  	}
   112  	if id == "" {
   113  		err = errors.New("User request not found")
   114  	}
   115  	return
   116  }
   117  
   118  // AcceptInvite Accepts an invitation
   119  // TODO: Gets a 405, code is in knife is it part of erchef?
   120  func (e *AssociationService) AcceptInvite(id string) (data string, err error) {
   121  	body, err := JSONReader("{ \"accept\" }")
   122  	if err != nil {
   123  		return
   124  	}
   125  	err = e.client.magicRequestDecoder("PUT", "association_requests/"+id, body, &data)
   126  	return
   127  }
   128  
   129  // List gets a list of the users in an organization
   130  func (e *AssociationService) List() (data []OrgUserListEntry, err error) {
   131  	err = e.client.magicRequestDecoder("GET", "users", nil, &data)
   132  	return
   133  }
   134  
   135  // Add a user immediately
   136  func (e *AssociationService) Add(addme AddNow) (err error) {
   137  	body, err := JSONReader(addme)
   138  	if err != nil {
   139  		return
   140  	}
   141  	err = e.client.magicRequestDecoder("POST", "users", body, nil)
   142  	return
   143  }
   144  
   145  // Get the details of a user in an organization
   146  func (e *AssociationService) Get(name string) (data OrgUser, err error) {
   147  	err = e.client.magicRequestDecoder("GET", "users/"+name, nil, &data)
   148  	return
   149  }
   150  
   151  // Delete removes a user from an organization
   152  func (e *AssociationService) Delete(name string) (data OrgUser, err error) {
   153  	err = e.client.magicRequestDecoder("DELETE", "users/"+name, nil, &data)
   154  	return
   155  }