github.com/aiven/aiven-go-client@v1.36.0/project_user.go (about)

     1  package aiven
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  type (
     8  	// ProjectUser represents a user who has accepted membership in a project
     9  	ProjectUser struct {
    10  		Email          string   `json:"user_email"`
    11  		RealName       string   `json:"real_name"`
    12  		MemberType     string   `json:"member_type"`
    13  		TeamId         string   `json:"team_id"`
    14  		TeamName       string   `json:"team_name"`
    15  		BillingContact bool     `json:"billing_contact"`
    16  		AuthMethods    []string `json:"auth"`
    17  		CreateTime     string   `json:"create_time"`
    18  	}
    19  
    20  	// ProjectInvitation represents a user who has been invited to join a project but has
    21  	// not yet accepted the invitation
    22  	ProjectInvitation struct {
    23  		UserEmail         string `json:"invited_user_email"`
    24  		InvitingUserEmail string `json:"inviting_user_email"`
    25  		MemberType        string `json:"member_type"`
    26  		InviteTime        string `json:"invite_time"`
    27  	}
    28  
    29  	// ProjectUsersHandler is the client that interacts with project User and
    30  	// Invitation API endpoints on Aiven
    31  	ProjectUsersHandler struct {
    32  		client *Client
    33  	}
    34  
    35  	// CreateProjectInvitationRequest are the parameters to invite a user to a project
    36  	CreateProjectInvitationRequest struct {
    37  		UserEmail  string `json:"user_email"`
    38  		MemberType string `json:"member_type"`
    39  	}
    40  
    41  	// UpdateProjectUserOrInvitationRequest are the parameters to update project user or invitation
    42  	UpdateProjectUserOrInvitationRequest struct {
    43  		MemberType string `json:"member_type"`
    44  	}
    45  
    46  	// ProjectInvitationsAndUsersListResponse represents the response from Aiven for
    47  	// listing project invitations and members.
    48  	ProjectInvitationsAndUsersListResponse struct {
    49  		APIResponse
    50  		ProjectInvitations []*ProjectInvitation `json:"invitations"`
    51  		ProjectUsers       []*ProjectUser       `json:"users"`
    52  	}
    53  )
    54  
    55  // Invite user to join a project on Aiven.
    56  func (h *ProjectUsersHandler) Invite(project string, req CreateProjectInvitationRequest) error {
    57  	path := buildPath("project", project, "invite")
    58  	_, err := h.client.doPostRequest(path, req)
    59  	return err
    60  }
    61  
    62  // Get a specific project user or project invitation.
    63  func (h *ProjectUsersHandler) Get(project, email string) (*ProjectUser, *ProjectInvitation, error) {
    64  	// There's no API for getting integration endpoint by ID. List all endpoints
    65  	// and pick the correct one instead. (There shouldn't ever be many endpoints.)
    66  	users, invitations, err := h.List(project)
    67  	if err != nil {
    68  		return nil, nil, err
    69  	}
    70  
    71  	for _, user := range users {
    72  		if user.Email == email && user.TeamId == "" {
    73  			return user, nil, nil
    74  		}
    75  	}
    76  
    77  	for _, invitation := range invitations {
    78  		if invitation.UserEmail == email {
    79  			return nil, invitation, nil
    80  		}
    81  	}
    82  
    83  	err = Error{Message: fmt.Sprintf("User / invitation with email %v not found", email), Status: 404}
    84  	return nil, nil, err
    85  }
    86  
    87  // UpdateUser updates the given project user with the given parameters.
    88  func (h *ProjectUsersHandler) UpdateUser(
    89  	project string,
    90  	email string,
    91  	req UpdateProjectUserOrInvitationRequest,
    92  ) error {
    93  	path := buildPath("project", project, "user", email)
    94  	_, err := h.client.doPutRequest(path, req)
    95  	return err
    96  }
    97  
    98  // UpdateInvitation updates the given project member with the given parameters.
    99  // NB: The server does not support updating invitations so this is implemented as delete + create
   100  func (h *ProjectUsersHandler) UpdateInvitation(
   101  	project string,
   102  	email string,
   103  	req UpdateProjectUserOrInvitationRequest,
   104  ) error {
   105  	err := h.DeleteInvitation(project, email)
   106  	if err != nil {
   107  		return err
   108  	}
   109  	return h.Invite(project, CreateProjectInvitationRequest{UserEmail: email, MemberType: req.MemberType})
   110  }
   111  
   112  // UpdateUserOrInvitation updates either a user if the given email address is associated with a
   113  // project member or project invitation if it isn't
   114  func (h *ProjectUsersHandler) UpdateUserOrInvitation(
   115  	project string,
   116  	email string,
   117  	req UpdateProjectUserOrInvitationRequest,
   118  ) error {
   119  	err := h.UpdateUser(project, email, req)
   120  	if err == nil {
   121  		return nil
   122  	}
   123  
   124  	if IsNotFound(err) {
   125  		return h.UpdateInvitation(project, email, req)
   126  	}
   127  
   128  	return err
   129  }
   130  
   131  // DeleteInvitation deletes the given project invitation from Aiven.
   132  func (h *ProjectUsersHandler) DeleteInvitation(project, email string) error {
   133  	path := buildPath("project", project, "invite", email)
   134  	bts, err := h.client.doDeleteRequest(path, nil)
   135  	if err != nil {
   136  		return err
   137  	}
   138  
   139  	return checkAPIResponse(bts, nil)
   140  }
   141  
   142  // DeleteUser deletes the given project user from Aiven.
   143  func (h *ProjectUsersHandler) DeleteUser(project, email string) error {
   144  	path := buildPath("project", project, "user", email)
   145  	bts, err := h.client.doDeleteRequest(path, nil)
   146  	if err != nil {
   147  		return err
   148  	}
   149  
   150  	return checkAPIResponse(bts, nil)
   151  }
   152  
   153  // DeleteUserOrInvitation deletes a user or a project invitation, whichever the email
   154  // address is associated with
   155  func (h *ProjectUsersHandler) DeleteUserOrInvitation(project, email string) error {
   156  	err := h.DeleteUser(project, email)
   157  	if err == nil {
   158  		return nil
   159  	}
   160  
   161  	if IsNotFound(err) {
   162  		return h.DeleteInvitation(project, email)
   163  	}
   164  
   165  	return err
   166  }
   167  
   168  // List all users and invitations for a given project.
   169  func (h *ProjectUsersHandler) List(project string) ([]*ProjectUser, []*ProjectInvitation, error) {
   170  	path := buildPath("project", project, "users")
   171  	rsp, err := h.client.doGetRequest(path, nil)
   172  	if err != nil {
   173  		return nil, nil, err
   174  	}
   175  
   176  	var r ProjectInvitationsAndUsersListResponse
   177  	errR := checkAPIResponse(rsp, &r)
   178  
   179  	return r.ProjectUsers, r.ProjectInvitations, errR
   180  }