-
Notifications
You must be signed in to change notification settings - Fork 0
/
team.go
148 lines (108 loc) · 2.65 KB
/
team.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
package symbiosis
import (
"encoding/json"
"errors"
"fmt"
)
type TeamService interface {
GetMemberByEmail(email string) (*TeamMember, error)
GetInvitationByEmail(email string) (*TeamMember, error)
InviteMembers(emails []string, role UserRole) ([]*TeamMember, error)
DeleteMember(email string) error
ChangeRole(email string, role UserRole) error
}
type TeamMember struct {
Email string `json:"email"`
TeamId string `json:"teamId"`
Role UserRole `json:"role"`
}
type Invite struct {
Emails []string `json:"emails"`
Role UserRole `json:"role"`
}
type UserRole string
const (
// Has full access, can create teams
ROLE_OWNER UserRole = "OWNER"
// Has full access within a team, can invite other team members
ROLE_ADMIN UserRole = "ADMIN"
// Can manage resources in clusters but not create them
ROLE_MEMBER UserRole = "MEMBER"
)
type TeamServiceClient struct {
client *Client
}
func GetValidRoles() map[UserRole]bool {
return map[UserRole]bool{ROLE_OWNER: true, ROLE_ADMIN: true, ROLE_MEMBER: true}
}
func ValidateRole(role UserRole) error {
validRoles := GetValidRoles()
if _, ok := validRoles[role]; !ok {
return errors.New("Invalid role given")
}
return nil
}
func (t *TeamServiceClient) GetMemberByEmail(email string) (*TeamMember, error) {
var member *TeamMember
err := t.client.
Call(fmt.Sprintf("rest/v1/team/member/%s", email),
"Get",
&member)
if err != nil {
return nil, err
}
return member, nil
}
func (t *TeamServiceClient) GetInvitationByEmail(email string) (*TeamMember, error) {
var member *TeamMember
err := t.client.
Call(fmt.Sprintf("rest/v1/team/member/invite/%s", email),
"Get",
&member)
if err != nil {
return nil, err
}
return member, nil
}
func (t *TeamServiceClient) InviteMembers(emails []string, role UserRole) ([]*TeamMember, error) {
err := ValidateRole(role)
if err != nil {
return nil, err
}
var teamMembers []*TeamMember
body, _ := json.Marshal(Invite{emails, role})
err = t.client.
Call(fmt.Sprintf("rest/v1/team/member/invite"),
"Post",
&teamMembers,
WithBody(body))
if err != nil {
return nil, err
}
return teamMembers, nil
}
func (t *TeamServiceClient) DeleteMember(email string) error {
err := t.client.
Call(fmt.Sprintf("rest/v1/team/member/%s", email),
"Delete",
nil)
if err != nil {
return err
}
return nil
}
func (t *TeamServiceClient) ChangeRole(email string, role UserRole) error {
err := ValidateRole(role)
if err != nil {
return err
}
err = t.client.
Call(fmt.Sprintf("rest/v1/team/member/%s", email),
"Put",
nil,
WithBody([]byte(fmt.Sprintf(`{"role":"%s"}`, role))))
if err != nil {
return err
}
return nil
}