account_users.go raw

   1  package linodego
   2  
   3  import (
   4  	"context"
   5  	"encoding/json"
   6  	"time"
   7  
   8  	"github.com/linode/linodego/internal/parseabletime"
   9  )
  10  
  11  type UserType string
  12  
  13  const (
  14  	UserTypeProxy   UserType = "proxy"
  15  	UserTypeParent  UserType = "parent"
  16  	UserTypeChild   UserType = "child"
  17  	UserTypeDefault UserType = "default"
  18  )
  19  
  20  // LastLogin represents a LastLogin object
  21  type LastLogin struct {
  22  	LoginDatetime *time.Time `json:"-"`
  23  	Status        string     `json:"status"`
  24  }
  25  
  26  // User represents a User object
  27  type User struct {
  28  	Username            string     `json:"username"`
  29  	Email               string     `json:"email"`
  30  	LastLogin           *LastLogin `json:"last_login"`
  31  	UserType            UserType   `json:"user_type"`
  32  	Restricted          bool       `json:"restricted"`
  33  	TFAEnabled          bool       `json:"tfa_enabled"`
  34  	SSHKeys             []string   `json:"ssh_keys"`
  35  	PasswordCreated     *time.Time `json:"-"`
  36  	VerifiedPhoneNumber *string    `json:"verified_phone_number"`
  37  }
  38  
  39  // UserCreateOptions fields are those accepted by CreateUser
  40  type UserCreateOptions struct {
  41  	Username   string `json:"username"`
  42  	Email      string `json:"email"`
  43  	Restricted bool   `json:"restricted"`
  44  }
  45  
  46  // UserUpdateOptions fields are those accepted by UpdateUser
  47  type UserUpdateOptions struct {
  48  	Username   string `json:"username,omitempty"`
  49  	Restricted *bool  `json:"restricted,omitempty"`
  50  	Email      string `json:"email,omitempty"`
  51  }
  52  
  53  // UnmarshalJSON implements the json.Unmarshaler interface
  54  func (ll *LastLogin) UnmarshalJSON(b []byte) error {
  55  	type Mask LastLogin
  56  
  57  	p := struct {
  58  		*Mask
  59  
  60  		LoginDatetime *parseabletime.ParseableTime `json:"login_datetime"`
  61  	}{
  62  		Mask: (*Mask)(ll),
  63  	}
  64  
  65  	if err := json.Unmarshal(b, &p); err != nil {
  66  		return err
  67  	}
  68  
  69  	ll.LoginDatetime = (*time.Time)(p.LoginDatetime)
  70  
  71  	return nil
  72  }
  73  
  74  // UnmarshalJSON implements the json.Unmarshaler interface
  75  func (i *User) UnmarshalJSON(b []byte) error {
  76  	type Mask User
  77  
  78  	p := struct {
  79  		*Mask
  80  
  81  		PasswordCreated *parseabletime.ParseableTime `json:"password_created"`
  82  	}{
  83  		Mask: (*Mask)(i),
  84  	}
  85  
  86  	if err := json.Unmarshal(b, &p); err != nil {
  87  		return err
  88  	}
  89  
  90  	i.PasswordCreated = (*time.Time)(p.PasswordCreated)
  91  
  92  	return nil
  93  }
  94  
  95  // GetCreateOptions converts a User to UserCreateOptions for use in CreateUser
  96  func (i User) GetCreateOptions() (o UserCreateOptions) {
  97  	o.Username = i.Username
  98  	o.Email = i.Email
  99  	o.Restricted = i.Restricted
 100  
 101  	return o
 102  }
 103  
 104  // GetUpdateOptions converts a User to UserUpdateOptions for use in UpdateUser
 105  func (i User) GetUpdateOptions() (o UserUpdateOptions) {
 106  	o.Username = i.Username
 107  	o.Restricted = copyBool(&i.Restricted)
 108  	o.Email = i.Email
 109  
 110  	return o
 111  }
 112  
 113  // ListUsers lists Users on the account
 114  func (c *Client) ListUsers(ctx context.Context, opts *ListOptions) ([]User, error) {
 115  	return getPaginatedResults[User](ctx, c, "account/users", opts)
 116  }
 117  
 118  // GetUser gets the user with the provided ID
 119  func (c *Client) GetUser(ctx context.Context, userID string) (*User, error) {
 120  	e := formatAPIPath("account/users/%s", userID)
 121  	return doGETRequest[User](ctx, c, e)
 122  }
 123  
 124  // CreateUser creates a User.  The email address must be confirmed before the
 125  // User account can be accessed.
 126  func (c *Client) CreateUser(ctx context.Context, opts UserCreateOptions) (*User, error) {
 127  	return doPOSTRequest[User](ctx, c, "account/users", opts)
 128  }
 129  
 130  // UpdateUser updates the User with the specified id
 131  func (c *Client) UpdateUser(ctx context.Context, userID string, opts UserUpdateOptions) (*User, error) {
 132  	e := formatAPIPath("account/users/%s", userID)
 133  	return doPUTRequest[User](ctx, c, e, opts)
 134  }
 135  
 136  // DeleteUser deletes the User with the specified id
 137  func (c *Client) DeleteUser(ctx context.Context, userID string) error {
 138  	e := formatAPIPath("account/users/%s", userID)
 139  	return doDELETERequest(ctx, c, e)
 140  }
 141