-
Notifications
You must be signed in to change notification settings - Fork 4
/
user.go
173 lines (143 loc) · 4.81 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package kosmo
import (
"context"
"sort"
"time"
wsv1alpha1 "github.com/cosmo-workspace/cosmo/api/workspace/v1alpha1"
"github.com/cosmo-workspace/cosmo/pkg/clog"
"k8s.io/apimachinery/pkg/api/equality"
apierrs "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
)
func (c *Client) GetUser(ctx context.Context, name string) (*wsv1alpha1.User, error) {
log := clog.FromContext(ctx).WithCaller()
user := wsv1alpha1.User{}
key := types.NamespacedName{Name: name}
if err := c.Get(ctx, key, &user); err != nil {
if apierrs.IsNotFound(err) {
return nil, NewNotFoundError("user is not found", err)
} else {
log.Error(err, "failed to get user", "id", name)
return nil, NewInternalServerError("failed to get user", err)
}
}
return &user, nil
}
func (c *Client) ListUsers(ctx context.Context) ([]wsv1alpha1.User, error) {
log := clog.FromContext(ctx).WithCaller()
userList := wsv1alpha1.UserList{}
if err := c.List(ctx, &userList); err != nil {
log.Error(err, "failed to list users")
return nil, NewInternalServerError("failed to list users", err)
}
users := userList.Items
sort.Slice(users, func(i, j int) bool { return users[i].Name < users[j].Name })
return users, nil
}
func (c *Client) CreateUser(ctx context.Context, userId string, displayName string,
role string, authType string, addons []wsv1alpha1.UserAddon) (*wsv1alpha1.User, error) {
log := clog.FromContext(ctx).WithCaller()
log.Info("creating user", "id", userId, "displayName", displayName, "role", role, "authType", authType, "addons", addons)
if displayName == "" {
displayName = userId
}
userrole := wsv1alpha1.UserRole(role)
if !userrole.IsValid() {
log.Info("invalid request", "id", userId, "role", userrole)
return nil, NewBadRequestError("'userrole' is invalid", nil)
}
authtype := wsv1alpha1.UserAuthType(authType)
if authtype != "" && !authtype.IsValid() {
log.Info("invalid request", "id", userId, "authtype", authtype)
return nil, NewBadRequestError("'authtype' is invalid", nil)
}
user := &wsv1alpha1.User{}
user.SetName(userId)
user.Spec = wsv1alpha1.UserSpec{
DisplayName: displayName,
Role: userrole,
AuthType: authtype,
Addons: addons,
}
log.Debug().Info("creating user object", "user", user)
err := c.Create(ctx, user)
if err != nil {
if apierrs.IsAlreadyExists(err) {
return nil, NewIsAlreadyExistsError("user already exists", err)
} else {
log.Error(err, "failed to create user", "user", userId)
return nil, NewServiceUnavailableError("failed to create user", err)
}
}
return user, nil
}
func (c *Client) GetDefaultPasswordAwait(ctx context.Context, userId string) (*string, error) {
log := clog.FromContext(ctx).WithCaller()
tk := time.NewTicker(time.Second)
defer tk.Stop()
log.Debug().Info("wait for default password creation", "user", userId)
for {
defaultPassword, err := c.GetDefaultPassword(ctx, userId)
if err == nil {
tk.Stop()
return defaultPassword, nil
}
select {
case <-ctx.Done():
tk.Stop()
log.Error(err, "reached to timeout in user creation", "user", userId)
return nil, NewInternalServerError("reached to timeout in user creation", nil)
default:
<-tk.C
}
}
}
func (c *Client) DeleteUser(ctx context.Context, userId string) (*wsv1alpha1.User, error) {
log := clog.FromContext(ctx).WithCaller()
user, err := c.GetUser(ctx, userId)
if err != nil {
return nil, err
}
if err := c.Delete(ctx, user); err != nil {
log.Error(err, "failed to delete user")
return nil, NewInternalServerError("failed to delete user", err)
}
return user, nil
}
type UpdateUserOpts struct {
DisplayName *string
UserRole *string
}
func (c *Client) UpdateUser(ctx context.Context, userId string, opts UpdateUserOpts) (*wsv1alpha1.User, error) {
logr := clog.FromContext(ctx).WithCaller()
user, err := c.GetUser(ctx, userId)
if err != nil {
return nil, err
}
before := user.DeepCopy()
if opts.DisplayName != nil && *opts.DisplayName != "" {
user.Spec.DisplayName = *opts.DisplayName
}
if opts.UserRole != nil && *opts.UserRole != "-" {
user.Spec.Role = wsv1alpha1.UserRole(*opts.UserRole)
if !user.Spec.Role.IsValid() {
logr.Debug().Info("'userrole' is invalid", "user", userId, "role", *opts.UserRole)
return nil, NewBadRequestError("'userrole' is invalid", nil)
}
}
if equality.Semantic.DeepEqual(before, user) {
logr.Debug().Info("no change", "user", before)
return nil, NewBadRequestError("no change", err)
}
if before.Spec.DisplayName != user.Spec.DisplayName {
logr.Debug().Info("name changed", "name", *opts.DisplayName)
}
if before.Spec.Role != user.Spec.Role {
logr.Debug().Info("role changed", "role", *opts.UserRole)
}
if err := c.Update(ctx, user); err != nil {
logr.Error(err, "failed to update user", "user", user)
return nil, NewInternalServerError("failed to update user", err)
}
return user, nil
}