/
service_authorization.go
222 lines (183 loc) · 6.24 KB
/
service_authorization.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
210
211
212
213
214
215
216
217
218
219
220
221
222
package fastly
import (
"bytes"
"fmt"
"io"
"reflect"
"strconv"
"time"
"github.com/google/jsonapi"
)
// SAUser represents a service user account.
type SAUser struct {
ID string `jsonapi:"primary,user"`
}
// SAService represents a service.
type SAService struct {
ID string `jsonapi:"primary,service"`
}
// ServiceAuthorization is the API response model.
type ServiceAuthorization struct {
CreatedAt *time.Time `jsonapi:"attr,created_at,iso8601"`
DeletedAt *time.Time `jsonapi:"attr,deleted_at,iso8601"`
ID string `jsonapi:"primary,service_authorization"`
Permission string `jsonapi:"attr,permission,omitempty"`
Service *SAService `jsonapi:"relation,service,omitempty"`
UpdatedAt *time.Time `jsonapi:"attr,updated_at,iso8601"`
User *SAUser `jsonapi:"relation,user,omitempty"`
}
// ServiceAuthorizations is an object containing the list of ServiceAuthorization results.
type ServiceAuthorizations struct {
Info infoResponse
Items []*ServiceAuthorization
}
// saType is used for reflection because JSONAPI wants to know what it's
// decoding into.
var saType = reflect.TypeOf(new(ServiceAuthorization))
// ListServiceAuthorizationsInput is used as input to the ListServiceAuthorizations function.
type ListServiceAuthorizationsInput struct {
// PageNumber requests a specific page of service authorizations.
PageNumber int
// PageSize limits the number of returned service authorizations.
PageSize int
}
// formatFilters ensures the parameters are formatted according to how the
// JSONAPI implementation requires them.
func (i *ListServiceAuthorizationsInput) formatFilters() map[string]string {
result := map[string]string{}
pairings := map[string]int{
"page[size]": i.PageSize,
"page[number]": i.PageNumber,
}
for key, value := range pairings {
if value > 0 {
result[key] = strconv.Itoa(value)
}
}
return result
}
// ListServiceAuthorizations retrieves all resources.
func (c *Client) ListServiceAuthorizations(i *ListServiceAuthorizationsInput) (*ServiceAuthorizations, error) {
resp, err := c.Get("/service-authorizations", &RequestOptions{
Params: i.formatFilters(),
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
var buf bytes.Buffer
tee := io.TeeReader(resp.Body, &buf)
info, err := getResponseInfo(tee)
if err != nil {
return nil, err
}
data, err := jsonapi.UnmarshalManyPayload(bytes.NewReader(buf.Bytes()), saType)
if err != nil {
return nil, err
}
sas := make([]*ServiceAuthorization, len(data))
for i := range data {
typed, ok := data[i].(*ServiceAuthorization)
if !ok {
return nil, fmt.Errorf("got back a non-ServiceAuthorization response")
}
sas[i] = typed
}
return &ServiceAuthorizations{
Items: sas,
Info: info,
}, nil
}
// GetServiceAuthorizationInput is used as input to the GetServiceAuthorization function.
type GetServiceAuthorizationInput struct {
// ID of the service authorization to retrieve (required).
ID string
}
// GetServiceAuthorization retrieves the specified resource.
func (c *Client) GetServiceAuthorization(i *GetServiceAuthorizationInput) (*ServiceAuthorization, error) {
if i.ID == "" {
return nil, ErrMissingID
}
path := fmt.Sprintf("/service-authorizations/%s", i.ID)
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var sa ServiceAuthorization
if err := jsonapi.UnmarshalPayload(resp.Body, &sa); err != nil {
return nil, err
}
return &sa, nil
}
// CreateServiceAuthorizationInput is used as input to the CreateServiceAuthorization function.
type CreateServiceAuthorizationInput struct {
// ID value is ignored and should not be set, needed to make JSONAPI work correctly.
ID string `jsonapi:"primary,service_authorization"`
// Permission is the level of permissions to grant the user to the service. Valid values are "full", "read_only", "purge_select" or "purge_all".
Permission string `jsonapi:"attr,permission,omitempty"`
// Service is the ID of the service to grant permissions for.
Service *SAService `jsonapi:"relation,service,omitempty"`
// UserID is the ID of the user which should have its permissions set.
User *SAUser `jsonapi:"relation,user,omitempty"`
}
// CreateServiceAuthorization creates a new resource.
func (c *Client) CreateServiceAuthorization(i *CreateServiceAuthorizationInput) (*ServiceAuthorization, error) {
if i.Service == nil || i.Service.ID == "" {
return nil, ErrMissingServiceAuthorizationsService
}
if i.User == nil || i.User.ID == "" {
return nil, ErrMissingServiceAuthorizationsUser
}
resp, err := c.PostJSONAPI("/service-authorizations", i, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var sa ServiceAuthorization
if err := jsonapi.UnmarshalPayload(resp.Body, &sa); err != nil {
return nil, err
}
return &sa, nil
}
// UpdateServiceAuthorizationInput is used as input to the UpdateServiceAuthorization function.
type UpdateServiceAuthorizationInput struct {
// ID uniquely identifies the service authorization (service and user pair) to be updated.
ID string `jsonapi:"primary,service_authorization"`
// The permission to grant the user to the service referenced by this service authorization.
Permission string `jsonapi:"attr,permission,omitempty"`
}
// UpdateServiceAuthorization updates the specified resource.
func (c *Client) UpdateServiceAuthorization(i *UpdateServiceAuthorizationInput) (*ServiceAuthorization, error) {
if i.ID == "" {
return nil, ErrMissingID
}
if i.Permission == "" {
return nil, ErrMissingPermission
}
path := fmt.Sprintf("/service-authorizations/%s", i.ID)
resp, err := c.PatchJSONAPI(path, i, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var sa ServiceAuthorization
if err := jsonapi.UnmarshalPayload(resp.Body, &sa); err != nil {
return nil, err
}
return &sa, nil
}
// DeleteServiceAuthorizationInput is used as input to the DeleteServiceAuthorization function.
type DeleteServiceAuthorizationInput struct {
// ID of the service authorization to delete (required).
ID string
}
// DeleteServiceAuthorization deletes the specified resource.
func (c *Client) DeleteServiceAuthorization(i *DeleteServiceAuthorizationInput) error {
if i.ID == "" {
return ErrMissingID
}
path := fmt.Sprintf("/service-authorizations/%s", i.ID)
_, err := c.Delete(path, nil)
return err
}