-
Notifications
You must be signed in to change notification settings - Fork 10
/
service_tokens.go
209 lines (170 loc) · 6.07 KB
/
service_tokens.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
205
206
207
208
209
package planetscale
import (
"context"
"fmt"
"net/http"
)
var _ ServiceTokenService = &serviceTokenService{}
// ServiceTokenService is an interface for communicating with the PlanetScale
// Service Token API.
type ServiceTokenService interface {
Create(context.Context, *CreateServiceTokenRequest) (*ServiceToken, error)
List(context.Context, *ListServiceTokensRequest) ([]*ServiceToken, error)
ListGrants(context.Context, *ListServiceTokenGrantsRequest) ([]*ServiceTokenGrant, error)
Delete(context.Context, *DeleteServiceTokenRequest) error
GetAccess(context.Context, *GetServiceTokenAccessRequest) ([]*ServiceTokenAccess, error)
AddAccess(context.Context, *AddServiceTokenAccessRequest) ([]*ServiceTokenAccess, error)
DeleteAccess(context.Context, *DeleteServiceTokenAccessRequest) error
}
type serviceTokenService struct {
client *Client
}
func (s *serviceTokenService) Create(ctx context.Context, createReq *CreateServiceTokenRequest) (*ServiceToken, error) {
req, err := s.client.newRequest(http.MethodPost, serviceTokensAPIPath(createReq.Organization), nil)
if err != nil {
return nil, err
}
st := &ServiceToken{}
if err := s.client.do(ctx, req, &st); err != nil {
return nil, err
}
return st, nil
}
func (s *serviceTokenService) List(ctx context.Context, listReq *ListServiceTokensRequest) ([]*ServiceToken, error) {
req, err := s.client.newRequest(http.MethodGet, serviceTokensAPIPath(listReq.Organization), nil)
if err != nil {
return nil, err
}
tokenListResponse := serviceTokensResponse{}
if err := s.client.do(ctx, req, &tokenListResponse); err != nil {
return nil, err
}
return tokenListResponse.ServiceTokens, nil
}
func (s *serviceTokenService) Delete(ctx context.Context, delReq *DeleteServiceTokenRequest) error {
req, err := s.client.newRequest(http.MethodDelete, serviceTokenAPIPath(delReq.Organization, delReq.ID), nil)
if err != nil {
return err
}
err = s.client.do(ctx, req, nil)
return err
}
func (s *serviceTokenService) GetAccess(ctx context.Context, accessReq *GetServiceTokenAccessRequest) ([]*ServiceTokenAccess, error) {
req, err := s.client.newRequest(http.MethodGet, serviceTokenAccessAPIPath(accessReq.Organization, accessReq.ID), nil)
if err != nil {
return nil, err
}
tokenAccess := serviceTokenAccessResponse{}
if err := s.client.do(ctx, req, &tokenAccess); err != nil {
return nil, err
}
return tokenAccess.ServiceTokenAccesses, nil
}
func (s *serviceTokenService) ListGrants(ctx context.Context, listReq *ListServiceTokenGrantsRequest) ([]*ServiceTokenGrant, error) {
req, err := s.client.newRequest(http.MethodGet, serviceTokenGrantsAPIPath(listReq.Organization, listReq.ID), nil)
if err != nil {
return nil, err
}
tokenGrants := serviceTokenGrantsResponse{}
if err := s.client.do(ctx, req, &tokenGrants); err != nil {
return nil, err
}
return tokenGrants.ServiceTokenGrants, nil
}
func (s *serviceTokenService) AddAccess(ctx context.Context, addReq *AddServiceTokenAccessRequest) ([]*ServiceTokenAccess, error) {
req, err := s.client.newRequest(http.MethodPost, serviceTokenAccessAPIPath(addReq.Organization, addReq.ID), addReq)
if err != nil {
return nil, err
}
tokenAccess := serviceTokenAccessResponse{}
if err := s.client.do(ctx, req, &tokenAccess); err != nil {
return nil, err
}
return tokenAccess.ServiceTokenAccesses, nil
}
func (s *serviceTokenService) DeleteAccess(ctx context.Context, delReq *DeleteServiceTokenAccessRequest) error {
req, err := s.client.newRequest(http.MethodDelete, serviceTokenAccessAPIPath(delReq.Organization, delReq.ID), delReq)
if err != nil {
return err
}
err = s.client.do(ctx, req, nil)
return err
}
type CreateServiceTokenRequest struct {
Organization string `json:"-"`
}
type ListServiceTokenGrantsRequest struct {
Organization string `json:"-"`
ID string `json:"-"`
}
type DeleteServiceTokenRequest struct {
Organization string `json:"-"`
ID string `json:"-"`
}
type ListServiceTokensRequest struct {
Organization string `json:"-"`
}
type GetServiceTokenAccessRequest struct {
Organization string `json:"-"`
ID string `json:"-"`
}
type AddServiceTokenAccessRequest struct {
Organization string `json:"-"`
ID string `json:"-"`
Database string `json:"database"`
Accesses []string `json:"access"`
}
type DeleteServiceTokenAccessRequest struct {
Organization string `json:"-"`
ID string `json:"-"`
Database string `json:"database"`
Accesses []string `json:"access"`
}
type ServiceToken struct {
ID string `json:"id"`
Type string `json:"type"`
Token string `json:"token"`
}
type ServiceTokenGrant struct {
ID string `json:"id"`
ResourceName string `json:"resource_name"`
ResourceType string `json:"resource_type"`
ResourceID string `json:"resource_id"`
Accesses []*ServiceTokenGrantAccess `json:"accesses"`
}
type ServiceTokenGrantAccess struct {
Access string `json:"access"`
Description string `json:"description"`
}
type serviceTokensResponse struct {
ServiceTokens []*ServiceToken `json:"data"`
}
type ServiceTokenAccess struct {
ID string `json:"id"`
Access string `json:"access"`
Type string `json:"type"`
Resource ServiceTokenResource `json:"resource"`
}
type ServiceTokenResource struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
}
type serviceTokenAccessResponse struct {
ServiceTokenAccesses []*ServiceTokenAccess `json:"data"`
}
type serviceTokenGrantsResponse struct {
ServiceTokenGrants []*ServiceTokenGrant `json:"data"`
}
func serviceTokenAccessAPIPath(org, id string) string {
return fmt.Sprintf("%s/%s/access", serviceTokensAPIPath(org), id)
}
func serviceTokenGrantsAPIPath(org, id string) string {
return fmt.Sprintf("%s/%s/grants", serviceTokensAPIPath(org), id)
}
func serviceTokensAPIPath(org string) string {
return fmt.Sprintf("v1/organizations/%s/service-tokens", org)
}
func serviceTokenAPIPath(org, id string) string {
return fmt.Sprintf("%s/%s", serviceTokensAPIPath(org), id)
}