-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
97 lines (78 loc) · 2.16 KB
/
users.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
package headscale
import (
"context"
"net/http"
"time"
)
type User struct {
ID string `json:"id"`
Name string `json:"name"`
CreatedAt time.Time `json:"createdAt"`
DisplayName string `json:"displayName"`
Email string `json:"email"`
ProviderID string `json:"providerId"`
Provider string `json:"provider"`
ProfilePicURL string `json:"profilePicUrl"`
}
type UserResource struct {
Client HeadscaleClientInterface
}
type UsersResponse struct {
Users []User `json:"user"`
}
func (u *UserResource) List(ctx context.Context) (UsersResponse, error) {
var users UsersResponse
url := u.Client.buildURL("user")
req, err := u.Client.buildRequest(ctx, http.MethodGet, url, requestOptions{})
if err != nil {
return users, err
}
err = u.Client.do(ctx, req, &users)
return users, err
}
type UserResponse struct {
User User `json:"user"`
}
func (u *UserResource) Get(ctx context.Context, name string) (UserResponse, error) {
var user UserResponse
url := u.Client.buildURL("user", name)
req, err := u.Client.buildRequest(ctx, http.MethodGet, url, requestOptions{})
if err != nil {
return user, err
}
err = u.Client.do(ctx, req, &user)
return user, err
}
type CreateUserRequest struct {
Name string `json:"name"`
}
func (u *UserResource) Create(ctx context.Context, name string) (User, error) {
var user User
url := u.Client.buildURL("user")
req, err := u.Client.buildRequest(ctx, http.MethodPost, url, requestOptions{
body: CreateUserRequest{
Name: name,
},
})
if err != nil {
return user, err
}
err = u.Client.do(ctx, req, &user)
return user, err
}
func (u *UserResource) Delete(ctx context.Context, name string) error {
url := u.Client.buildURL("user", name)
req, err := u.Client.buildRequest(ctx, http.MethodDelete, url, requestOptions{})
if err != nil {
return err
}
return u.Client.do(ctx, req, nil)
}
func (u *UserResource) Rename(ctx context.Context, name, newName string) error {
url := u.Client.buildURL("user", name, "rename", newName)
req, err := u.Client.buildRequest(ctx, http.MethodPost, url, requestOptions{})
if err != nil {
return err
}
return u.Client.do(ctx, req, nil)
}