-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservice_account.go
210 lines (175 loc) · 6.25 KB
/
service_account.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
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
"time"
)
// Compile-time proof of interface implementation.
var _ ServiceAccounts = (*serviceAccounts)(nil)
// ServiceAccounts describes all the service account related methods that the Scalr API supports.
type ServiceAccounts interface {
// List all the service accounts.
List(ctx context.Context, options ServiceAccountListOptions) (*ServiceAccountList, error)
// Create is used to create a new service account.
Create(ctx context.Context, options ServiceAccountCreateOptions) (*ServiceAccount, error)
// Read reads a service account by its ID.
Read(ctx context.Context, serviceAccountID string) (*ServiceAccount, error)
// Update existing service account by its ID.
Update(ctx context.Context, serviceAccountID string, options ServiceAccountUpdateOptions) (*ServiceAccount, error)
// Delete service account by its ID.
Delete(ctx context.Context, serviceAccountID string) error
}
// serviceAccounts implements ServiceAccounts.
type serviceAccounts struct {
client *Client
}
// ServiceAccountList represents a list of service accounts.
type ServiceAccountList struct {
*Pagination
Items []*ServiceAccount
}
// ServiceAccountStatus represents the status of service account.
type ServiceAccountStatus string
// List of available service account statuses.
const (
ServiceAccountStatusActive ServiceAccountStatus = "Active"
ServiceAccountStatusInactive ServiceAccountStatus = "Inactive"
)
type ServiceAccount struct {
ID string `jsonapi:"primary,service-accounts"`
Name string `jsonapi:"attr,name"`
Email string `jsonapi:"attr,email"`
Description string `jsonapi:"attr,description"`
Status ServiceAccountStatus `jsonapi:"attr,status"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
// Relations
Account *Account `jsonapi:"relation,account,omitempty"`
CreatedBy *User `jsonapi:"relation,created-by,omitempty"`
Owners []*Team `jsonapi:"relation,owners"`
}
// ServiceAccountListOptions represents the options for listing service accounts.
type ServiceAccountListOptions struct {
ListOptions
Account *string `url:"filter[account],omitempty"`
Email *string `url:"filter[email],omitempty"`
ServiceAccount *string `url:"filter[service-account],omitempty"`
Query *string `url:"query,omitempty"`
Include *string `url:"include,omitempty"`
}
// ServiceAccountCreateOptions represents the options for creating a new service account.
type ServiceAccountCreateOptions struct {
// For internal use only!
ID string `jsonapi:"primary,service-accounts"`
// The name of the service account, it must be unique within the account.
Name *string `jsonapi:"attr,name"`
Description *string `jsonapi:"attr,description,omitempty"`
Status *ServiceAccountStatus `jsonapi:"attr,status,omitempty"`
Account *Account `jsonapi:"relation,account"`
Owners []*Team `jsonapi:"relation,owners"`
}
func (o ServiceAccountCreateOptions) valid() error {
if o.Account == nil {
return errors.New("account is required")
}
if !validStringID(&o.Account.ID) {
return errors.New("invalid value for account ID")
}
if o.Name == nil {
return errors.New("name is required")
}
return nil
}
// ServiceAccountUpdateOptions represents the options for updating a service account.
type ServiceAccountUpdateOptions struct {
// For internal use only!
ID string `jsonapi:"primary,service-accounts"`
Description *string `jsonapi:"attr,description,omitempty"`
Status *ServiceAccountStatus `jsonapi:"attr,status,omitempty"`
Owners []*Team `jsonapi:"relation,owners"`
}
// Read a service account by its ID.
func (s *serviceAccounts) Read(ctx context.Context, serviceAccountID string) (*ServiceAccount, error) {
if !validStringID(&serviceAccountID) {
return nil, errors.New("invalid value for service account ID")
}
options := struct {
Include string `url:"include"`
}{
Include: "created-by",
}
u := fmt.Sprintf("service-accounts/%s", url.QueryEscape(serviceAccountID))
req, err := s.client.newRequest("GET", u, options)
if err != nil {
return nil, err
}
sa := &ServiceAccount{}
err = s.client.do(ctx, req, sa)
if err != nil {
return nil, err
}
return sa, nil
}
// List all the service accounts.
func (s *serviceAccounts) List(ctx context.Context, options ServiceAccountListOptions) (*ServiceAccountList, error) {
req, err := s.client.newRequest("GET", "service-accounts", &options)
if err != nil {
return nil, err
}
sal := &ServiceAccountList{}
err = s.client.do(ctx, req, sal)
if err != nil {
return nil, err
}
return sal, nil
}
// Create is used to create a new service account.
func (s *serviceAccounts) Create(ctx context.Context, options ServiceAccountCreateOptions) (*ServiceAccount, error) {
if err := options.valid(); err != nil {
return nil, err
}
// Make sure we don't send a user provided ID.
options.ID = ""
req, err := s.client.newRequest("POST", "service-accounts", &options)
if err != nil {
return nil, err
}
sa := &ServiceAccount{}
err = s.client.do(ctx, req, sa)
if err != nil {
return nil, err
}
return sa, nil
}
// Update is used to update a service account.
func (s *serviceAccounts) Update(ctx context.Context, serviceAccountID string, options ServiceAccountUpdateOptions) (*ServiceAccount, error) {
if !validStringID(&serviceAccountID) {
return nil, errors.New("invalid value for service account ID")
}
// Make sure we don't send a user provided ID.
options.ID = ""
u := fmt.Sprintf("service-accounts/%s", url.QueryEscape(serviceAccountID))
req, err := s.client.newRequest("PATCH", u, &options)
if err != nil {
return nil, err
}
sa := &ServiceAccount{}
err = s.client.do(ctx, req, sa)
if err != nil {
return nil, err
}
return sa, nil
}
// Delete service account by its ID.
func (s *serviceAccounts) Delete(ctx context.Context, serviceAccountID string) error {
if !validStringID(&serviceAccountID) {
return errors.New("invalid value for service account ID")
}
u := fmt.Sprintf("service-accounts/%s", url.QueryEscape(serviceAccountID))
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}