user.go raw

   1  package govultr
   2  
   3  import (
   4  	"context"
   5  	"fmt"
   6  	"net/http"
   7  
   8  	"github.com/google/go-querystring/query"
   9  )
  10  
  11  const path = "/v2/users"
  12  
  13  // UserService is the interface to interact with the user management endpoints on the Vultr API
  14  // Link : https://www.vultr.com/api/#tag/users
  15  type UserService interface { //nolint:dupl
  16  	Create(ctx context.Context, userCreate *UserReq) (*User, *http.Response, error)
  17  	Get(ctx context.Context, userID string) (*User, *http.Response, error)
  18  	Update(ctx context.Context, userID string, userReq *UserReq) error
  19  	Delete(ctx context.Context, userID string) error
  20  	List(ctx context.Context, options *ListOptions) ([]User, *Meta, *http.Response, error)
  21  }
  22  
  23  var _ UserService = &UserServiceHandler{}
  24  
  25  // UserServiceHandler handles interaction with the user methods for the Vultr API
  26  type UserServiceHandler struct {
  27  	client *Client
  28  }
  29  
  30  // User represents an user on Vultr
  31  type User struct {
  32  	ID          string   `json:"id"`
  33  	Name        string   `json:"name"`
  34  	Email       string   `json:"email"`
  35  	APIEnabled  *bool    `json:"api_enabled"`
  36  	APIKey      string   `json:"api_key,omitempty"`
  37  	ACL         []string `json:"acls,omitempty"`
  38  	ServiceUser bool     `json:"service_user"`
  39  }
  40  
  41  // UserReq is the user struct for create and update calls
  42  type UserReq struct {
  43  	Email       string   `json:"email,omitempty"`
  44  	Name        string   `json:"name,omitempty"`
  45  	APIEnabled  *bool    `json:"api_enabled,omitempty"`
  46  	ACL         []string `json:"acls,omitempty"`
  47  	Password    string   `json:"password,omitempty"`
  48  	ServiceUser bool     `json:"service_user,omitempty"`
  49  }
  50  
  51  type usersBase struct {
  52  	Users []User `json:"users"`
  53  	Meta  *Meta  `json:"meta"`
  54  }
  55  
  56  type userBase struct {
  57  	User *User `json:"user"`
  58  }
  59  
  60  // Create will add the specified user to your Vultr account
  61  func (u *UserServiceHandler) Create(ctx context.Context, userCreate *UserReq) (*User, *http.Response, error) {
  62  	req, err := u.client.NewRequest(ctx, http.MethodPost, path, userCreate)
  63  	if err != nil {
  64  		return nil, nil, err
  65  	}
  66  
  67  	user := new(userBase)
  68  	resp, err := u.client.DoWithContext(ctx, req, user)
  69  	if err != nil {
  70  		return nil, resp, err
  71  	}
  72  
  73  	return user.User, resp, nil
  74  }
  75  
  76  // Get will retrieve a specific user account
  77  func (u *UserServiceHandler) Get(ctx context.Context, userID string) (*User, *http.Response, error) {
  78  	uri := fmt.Sprintf("%s/%s", path, userID)
  79  
  80  	req, err := u.client.NewRequest(ctx, http.MethodGet, uri, nil)
  81  	if err != nil {
  82  		return nil, nil, err
  83  	}
  84  
  85  	user := new(userBase)
  86  	resp, err := u.client.DoWithContext(ctx, req, user)
  87  	if err != nil {
  88  		return nil, resp, err
  89  	}
  90  
  91  	return user.User, resp, nil
  92  }
  93  
  94  // Update will update the given user. Empty strings will be ignored.
  95  func (u *UserServiceHandler) Update(ctx context.Context, userID string, userReq *UserReq) error {
  96  	uri := fmt.Sprintf("%s/%s", path, userID)
  97  	req, err := u.client.NewRequest(ctx, http.MethodPatch, uri, userReq)
  98  	if err != nil {
  99  		return err
 100  	}
 101  
 102  	_, err = u.client.DoWithContext(ctx, req, nil)
 103  	return err
 104  }
 105  
 106  // Delete will remove the specified user from your Vultr account
 107  func (u *UserServiceHandler) Delete(ctx context.Context, userID string) error {
 108  	uri := fmt.Sprintf("%s/%s", path, userID)
 109  
 110  	req, err := u.client.NewRequest(ctx, http.MethodDelete, uri, nil)
 111  	if err != nil {
 112  		return err
 113  	}
 114  
 115  	_, err = u.client.DoWithContext(ctx, req, nil)
 116  	return err
 117  }
 118  
 119  // List will list all the users associated with your Vultr account
 120  func (u *UserServiceHandler) List(ctx context.Context, options *ListOptions) ([]User, *Meta, *http.Response, error) { //nolint:dupl
 121  	req, err := u.client.NewRequest(ctx, http.MethodGet, path, nil)
 122  	if err != nil {
 123  		return nil, nil, nil, err
 124  	}
 125  
 126  	newValues, err := query.Values(options)
 127  	if err != nil {
 128  		return nil, nil, nil, err
 129  	}
 130  
 131  	req.URL.RawQuery = newValues.Encode()
 132  
 133  	users := new(usersBase)
 134  	resp, err := u.client.DoWithContext(ctx, req, &users)
 135  	if err != nil {
 136  		return nil, nil, resp, err
 137  	}
 138  
 139  	return users.Users, users.Meta, resp, nil
 140  }
 141