-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuser.go
104 lines (86 loc) · 2.55 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
"time"
)
// Compile-time proof of interface implementation.
var _ Users = (*users)(nil)
// Users describes all the user related methods that the
// Scalr API supports.
type Users interface {
List(ctx context.Context, options UserListOptions) (*UserList, error)
Read(ctx context.Context, userID string) (*User, error)
}
// users implements Users.
type users struct {
client *Client
}
// UserStatus represents a user status.
type UserStatus string
// List of available user statuses.
const (
UserStatusActive UserStatus = "Active"
UserStatusInactive UserStatus = "Inactive"
UserStatusPending UserStatus = "Pending"
)
// User represents a Scalr IAM user.
type User struct {
ID string `jsonapi:"primary,users"`
Status UserStatus `jsonapi:"attr,status,omitempty"`
Email string `jsonapi:"attr,email,omitempty"`
Username string `jsonapi:"attr,username,omitempty"`
FullName string `jsonapi:"attr,full-name,omitempty"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
LastLoginAt time.Time `jsonapi:"attr,last-login-at,iso8601"`
// Relations
Teams []*Team `jsonapi:"relation,teams"`
IdentityProviders []*IdentityProvider `jsonapi:"relation,identity-providers"`
}
// UserList represents a list of users.
type UserList struct {
*Pagination
Items []*User
}
// UserListOptions represents the options for listing users.
type UserListOptions struct {
ListOptions
User *string `url:"filter[user],omitempty"`
Email *string `url:"filter[email],omitempty"`
IdentityProvider *string `url:"filter[identity-provider],omitempty"`
Query *string `url:"query,omitempty"`
Sort *string `url:"sort,omitempty"`
Include *string `url:"include,omitempty"`
}
// List all the users.
func (s *users) List(ctx context.Context, options UserListOptions) (*UserList, error) {
req, err := s.client.newRequest("GET", "users", &options)
if err != nil {
return nil, err
}
ul := &UserList{}
err = s.client.do(ctx, req, ul)
if err != nil {
return nil, err
}
return ul, nil
}
// Read user by its ID.
func (s *users) Read(ctx context.Context, userID string) (*User, error) {
if !validStringID(&userID) {
return nil, errors.New("invalid value for user ID")
}
u := fmt.Sprintf("users/%s", url.QueryEscape(userID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
usr := &User{}
err = s.client.do(ctx, req, usr)
if err != nil {
return nil, err
}
return usr, nil
}