-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceaccount.go
204 lines (166 loc) · 5.73 KB
/
serviceaccount.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package api
import (
"context"
"strings"
"time"
"unicode/utf8"
"github.com/moonrhythm/validator"
)
type ServiceAccount interface {
Create(ctx context.Context, m *ServiceAccountCreate) (*Empty, error)
Get(ctx context.Context, m *ServiceAccountGet) (*ServiceAccountGetResult, error)
List(ctx context.Context, m *ServiceAccountList) (*ServiceAccountListResult, error)
Update(ctx context.Context, m *ServiceAccountUpdate) (*Empty, error)
Delete(ctx context.Context, m *ServiceAccountDelete) (*Empty, error)
CreateKey(ctx context.Context, m *ServiceAccountCreateKey) (*Empty, error)
DeleteKey(ctx context.Context, m *ServiceAccountDeleteKey) (*Empty, error)
}
type ServiceAccountCreate struct {
Project string `json:"project" yaml:"project"`
SID string `json:"sid" yaml:"sid"`
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
}
func (m *ServiceAccountCreate) Valid() error {
m.SID = strings.TrimSpace(m.SID)
m.Name = strings.TrimSpace(m.Name)
m.Description = strings.TrimSpace(m.Description)
v := validator.New()
v.Must(m.Project != "", "project required")
if v.Must(m.SID != "", "sid required") {
v.Mustf(ReValidSID.MatchString(m.SID), "sid invalid %s", ReValidSIDStr)
cnt := utf8.RuneCountInString(m.SID)
v.Must(cnt >= 3 && cnt <= 20, "sid must have length between 3-20 characters")
}
{
cnt := utf8.RuneCountInString(m.Name)
v.Mustf(cnt >= MinNameLength && cnt <= 60, "name must have length between %d-%d characters", MinNameLength, 60)
}
return WrapValidate(v)
}
type ServiceAccountUpdate struct {
Project string `json:"project" yaml:"project"`
SID string `json:"sid" yaml:"sid"`
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
}
func (m *ServiceAccountUpdate) Valid() error {
m.SID = strings.TrimSpace(m.SID)
m.Name = strings.TrimSpace(m.Name)
m.Description = strings.TrimSpace(m.Description)
v := validator.New()
v.Must(m.Project != "", "project required")
if v.Must(m.SID != "", "sid required") {
v.Mustf(ReValidSID.MatchString(m.SID), "sid invalid %s", ReValidSIDStr)
cnt := utf8.RuneCountInString(m.SID)
v.Must(cnt >= 3 && cnt <= 20, "sid must have length between 3-20 characters")
}
{
cnt := utf8.RuneCountInString(m.Name)
v.Mustf(cnt >= MinNameLength && cnt <= 60, "name must have length between %d-%d characters", MinNameLength, 60)
}
return WrapValidate(v)
}
type ServiceAccountCreateKey struct {
Project string `json:"project" yaml:"project"`
ID string `json:"id" yaml:"id"` // TODO: sid
}
func (m *ServiceAccountCreateKey) Valid() error {
v := validator.New()
v.Must(m.Project != "", "project required")
v.Must(m.ID != "", "service account id required")
return WrapValidate(v)
}
type ServiceAccountList struct {
Project string `json:"project" yaml:"project"`
}
func (m *ServiceAccountList) Valid() error {
v := validator.New()
v.Must(m.Project != "", "project required")
return WrapValidate(v)
}
type ServiceAccountListItem struct {
SID string `json:"sid" yaml:"sid"`
Email string `json:"email" yaml:"email"`
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
CreatedBy string `json:"createdBy" yaml:"createdBy"`
}
type ServiceAccountListResult struct {
Project string `json:"project" yaml:"project"`
Items []*ServiceAccountListItem `json:"items" yaml:"items"`
}
func (m *ServiceAccountListResult) Table() [][]string {
table := [][]string{
{"ID", "EMAIL", "NAME", "AGE"},
}
for _, x := range m.Items {
table = append(table, []string{
x.SID,
x.Email,
x.Name,
age(x.CreatedAt),
})
}
return table
}
type ServiceAccountGet struct {
Project string `json:"project" yaml:"project"`
ID string `json:"id" yaml:"id"` // TODO: sid
}
func (m *ServiceAccountGet) Valid() error {
v := validator.New()
v.Must(m.Project != "", "project required")
v.Must(m.ID != "", "service account id required")
return WrapValidate(v)
}
type ServiceAccountGetResult struct {
SID string `json:"sid" yaml:"sid"`
Project string `json:"project" yaml:"project"`
Email string `json:"email" yaml:"email"`
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
CreatedBy string `json:"createdBy" yaml:"createdBy"`
Keys []*ServiceAccountKey `json:"keys" yaml:"keys"`
}
func (m *ServiceAccountGetResult) Table() [][]string {
table := [][]string{
{"ID", "EMAIL", "NAME", "AGE"},
{
m.SID,
m.Email,
m.Name,
age(m.CreatedAt),
},
}
return table
}
type ServiceAccountKey struct {
Secret string `json:"secret" yaml:"secret"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
CreatedBy string `json:"createdBy" yaml:"createdBy"`
}
type ServiceAccountDeleteKey struct {
Project string `json:"project" yaml:"project"`
ID string `json:"id" yaml:"id"` // TODO: sid
Secret string `json:"secret" yaml:"secret"`
}
func (m *ServiceAccountDeleteKey) Valid() error {
v := validator.New()
v.Must(m.Project != "", "project required")
v.Must(m.ID != "", "service account id required")
v.Must(m.Secret != "", "secret required")
return WrapValidate(v)
}
type ServiceAccountDelete struct {
Project string `json:"project" yaml:"project"`
ID string `json:"id" yaml:"id"` // TODO: sid
}
func (m *ServiceAccountDelete) Valid() error {
v := validator.New()
v.Must(m.Project != "", "project required")
v.Must(m.ID != "", "service account id required")
return WrapValidate(v)
}