-
Notifications
You must be signed in to change notification settings - Fork 8
/
user.go
149 lines (124 loc) · 4.1 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
package kubernetes
import (
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
terrors "github.com/tkeel-io/kit/errors"
"github.com/tkeel-io/kit/result"
tenantApi "github.com/tkeel-io/tkeel/api/tenant/v1"
"google.golang.org/protobuf/encoding/protojson"
)
const (
_listTenantUserMethodFormat = "apis/rudder/v1/tenants/%s/users"
_createTenantUserMethodFormat = "apis/rudder/v1/tenants/%s/users"
_deleteTenantUserMethodFormat = "apis/rudder/v1/tenants/%s/users/%s"
_infoTenantUserMethodFormat = "apis/rudder/v1/tenants/%s/users/%s"
)
type UserListOutPut struct {
ID string `csv:"ID"`
Username string `csv:"USERNAME"`
TenantID string `csv:"TENANT ID"`
}
type UserInfo struct {
Username string `json:"username"`
Password string `json:"password"`
}
func TenantUserCreate(tenantID, username, password string) error {
token, err := getAdminToken()
if err != nil {
return err
}
method := fmt.Sprintf(_createTenantUserMethodFormat, tenantID)
userinfo := UserInfo{Username: username, Password: password}
data, err := json.Marshal(userinfo)
if err != nil {
return errors.Wrap(err, "error marshal")
}
resp, err := InvokeByPortForward(_pluginKeel, method, data, http.MethodPost, setAuthenticate(token))
if err != nil {
return errors.Wrap(err, "invoke "+method+" error")
}
var r = &result.Http{}
if err = protojson.Unmarshal([]byte(resp), r); err != nil {
return errors.Wrap(err, "can't unmarshal'")
}
if r.Code != terrors.Success.Reason {
return errors.Wrap(errors.New(r.Msg), "error response code")
}
return nil
}
// tenant user manage.
func TenantUserDelete(tenantID, userID string) error {
token, err := getAdminToken()
if err != nil {
return err
}
method := fmt.Sprintf(_deleteTenantUserMethodFormat, tenantID, userID)
resp, err := InvokeByPortForward(_pluginKeel, method, nil, http.MethodDelete, setAuthenticate(token))
if err != nil {
return errors.Wrap(err, "invoke "+method+" error")
}
var r = &result.Http{}
if err = protojson.Unmarshal([]byte(resp), r); err != nil {
return errors.Wrap(err, "can't unmarshal'")
}
if r.Code != terrors.Success.Reason {
return errors.Wrap(errors.New(r.Msg), "error response code")
}
return nil
}
func TenantUserInfo(tenantID, userID string) ([]UserListOutPut, error) {
token, err := getAdminToken()
if err != nil {
return nil, errors.Wrap(err, "error get token")
}
method := fmt.Sprintf(_infoTenantUserMethodFormat, tenantID, userID)
resp, err := InvokeByPortForward(_pluginKeel, method, nil, http.MethodGet, setAuthenticate(token))
if err != nil {
return nil, errors.Wrap(err, "error invoke")
}
var r = &result.Http{}
if err = protojson.Unmarshal([]byte(resp), r); err != nil {
return nil, errors.Wrap(err, "error unmarshal")
}
if r.Code != terrors.Success.Reason {
return nil, errors.Wrap(errors.New(r.Msg), "error response code")
}
response := tenantApi.GetUserResponse{}
err = r.Data.UnmarshalTo(&response)
if err != nil {
return nil, errors.Wrap(err, "error unmarshal response")
}
var list = make([]UserListOutPut, 0, 1)
list = append(list, UserListOutPut{response.UserId, response.Username, response.TenantId})
return list, nil
}
func TenantUserList(tenantID string) ([]UserListOutPut, error) {
token, err := getAdminToken()
if err != nil {
return nil, errors.Wrap(err, "error get token")
}
method := fmt.Sprintf(_listTenantUserMethodFormat, tenantID)
resp, err := InvokeByPortForward(_pluginKeel, method, nil, http.MethodGet, setAuthenticate(token))
if err != nil {
return nil, errors.Wrap(err, "error invoke")
}
var r = &result.Http{}
if err = protojson.Unmarshal([]byte(resp), r); err != nil {
return nil, errors.Wrap(err, "error unmarshal")
}
if r.Code != terrors.Success.Reason {
return nil, errors.Wrap(errors.New(r.Msg), "error response code")
}
response := tenantApi.ListUserResponse{}
err = r.Data.UnmarshalTo(&response)
if err != nil {
return nil, errors.Wrap(err, "error unmarshal response")
}
var list = make([]UserListOutPut, 0, len(response.Users))
for _, user := range response.Users {
list = append(list, UserListOutPut{user.UserId, user.Username, user.TenantId})
}
return list, nil
}