forked from nscuro/dtrack-client
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathteam.go
131 lines (110 loc) · 3.25 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
package dtrack
import (
"context"
"fmt"
"net/http"
"github.com/google/uuid"
)
type Team struct {
UUID uuid.UUID `json:"uuid,omitempty"`
Name string `json:"name,omitempty"`
APIKeys []APIKey `json:"apiKeys,omitempty"`
Permissions []Permission `json:"permissions,omitempty"`
MappedOIDCGroups []OIDCMapping `json:"mappedOidcGroups,omitempty"`
}
type APIKey struct {
Key string `json:"key"`
Comment string `json:"comment"`
Created int `json:"created"`
LastUsed int `json:"lastUsed"`
MaskedKey string `json:"maskedKey"`
}
type TeamService struct {
client *Client
}
func (ts TeamService) Get(ctx context.Context, teamUUID uuid.UUID) (t Team, err error) {
req, err := ts.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/team/%s", teamUUID))
if err != nil {
return
}
_, err = ts.client.doRequest(req, &t)
return
}
func (ts TeamService) GetAll(ctx context.Context, po PageOptions) (p Page[Team], err error) {
req, err := ts.client.newRequest(ctx, http.MethodGet, "/api/v1/team", withPageOptions(po))
if err != nil {
return
}
res, err := ts.client.doRequest(req, &p.Items)
if err != nil {
return
}
p.TotalCount = res.TotalCount
return
}
func (ts TeamService) GenerateAPIKey(ctx context.Context, teamUUID uuid.UUID) (key string, err error) {
req, err := ts.client.newRequest(ctx, http.MethodPut, fmt.Sprintf("/api/v1/team/%s/key", teamUUID))
if err != nil {
return
}
var apiKey APIKey
_, err = ts.client.doRequest(req, &apiKey)
key = apiKey.Key
return
}
func (ts TeamService) DeleteAPIKey(ctx context.Context, key string) (err error) {
req, err := ts.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/team/key/%s", key))
if err != nil {
return
}
_, err = ts.client.doRequest(req, nil)
return
}
func (ts TeamService) UpdateAPIKeyComment(ctx context.Context, key, comment string) (commentOut string, err error) {
req, err := ts.client.newRequest(ctx, http.MethodPost, fmt.Sprintf("/api/v1/team/key/%s/comment", key), withBody(comment))
if err != nil {
return
}
var apiKey APIKey
_, err = ts.client.doRequest(req, &apiKey)
commentOut = apiKey.Comment
return
}
func (ts TeamService) GetAPIKeys(ctx context.Context, teamUUID uuid.UUID) (keys []APIKey, err error) {
keys = []APIKey{}
err = ForEach(
func(po PageOptions) (Page[Team], error) { return ts.GetAll(ctx, po) },
func(item Team) error {
if item.UUID != teamUUID {
return nil
}
keys = append(keys, item.APIKeys...)
return nil
},
)
return keys, err
}
func (ts TeamService) Create(ctx context.Context, team Team) (t Team, err error) {
req, err := ts.client.newRequest(ctx, http.MethodPut, "/api/v1/team", withBody(team))
if err != nil {
return
}
_, err = ts.client.doRequest(req, &t)
return
}
func (ts TeamService) Update(ctx context.Context, team Team) (t Team, err error) {
req, err := ts.client.newRequest(ctx, http.MethodPost, "/api/v1/team", withBody(team))
if err != nil {
return
}
_, err = ts.client.doRequest(req, &t)
return
}
func (ts TeamService) Delete(ctx context.Context, team Team) (err error) {
req, err := ts.client.newRequest(ctx, http.MethodDelete, "/api/v1/team", withBody(team))
if err != nil {
return
}
_, err = ts.client.doRequest(req, nil)
return
}