-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaccess_policy.go
226 lines (189 loc) · 6.67 KB
/
access_policy.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
223
224
225
226
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ AccessPolicies = (*accessPolicies)(nil)
// AccessPolicies describes all the accessPolicy related methods that the
// Scalr IACP API supports.
type AccessPolicies interface {
List(ctx context.Context, options AccessPolicyListOptions) (*AccessPolicyList, error)
Read(ctx context.Context, accessPolicyID string) (*AccessPolicy, error)
Create(ctx context.Context, options AccessPolicyCreateOptions) (*AccessPolicy, error)
Update(ctx context.Context, accessPolicyID string, options AccessPolicyUpdateOptions) (*AccessPolicy, error)
Delete(ctx context.Context, accessPolicyID string) error
}
// accessPolicies implements AccessPolicies.
type accessPolicies struct {
client *Client
}
// AccessPolicyList represents a list of accessPolicies.
type AccessPolicyList struct {
*Pagination
Items []*AccessPolicy
}
// AccessPolicy represents a Scalr accessPolicy.
type AccessPolicy struct {
ID string `jsonapi:"primary,access-policies"`
IsSystem bool `jsonapi:"attr,is-system"`
Roles []*Role `jsonapi:"relation,roles"`
User *User `jsonapi:"relation,user,omitempty"`
Team *Team `jsonapi:"relation,team,omitempty"`
ServiceAccount *ServiceAccount `jsonapi:"relation,service-account,omitempty"`
Account *Account `jsonapi:"relation,account,omitempty"`
Environment *Environment `jsonapi:"relation,environment,omitempty"`
Workspace *Workspace `jsonapi:"relation,workspace,omitempty"`
}
// AccessPolicyCreateOptions represents the options for creating a new AccessPolicy.
type AccessPolicyCreateOptions struct {
ID string `jsonapi:"primary,access-policies"`
// Relations
Roles []*Role `jsonapi:"relation,roles"`
// The subject of access policy, one of this fields must be filled
User *User `jsonapi:"relation,user,omitempty"`
Team *Team `jsonapi:"relation,team,omitempty"`
ServiceAccount *ServiceAccount `jsonapi:"relation,service-account,omitempty"`
// Scope
Account *Account `jsonapi:"relation,account,omitempty"`
Environment *Environment `jsonapi:"relation,environment,omitempty"`
Workspace *Workspace `jsonapi:"relation,workspace,omitempty"`
}
func (o AccessPolicyCreateOptions) valid() error {
if len(o.Roles) == 0 {
return errors.New("at least one role must be provided")
}
if o.Account == nil && o.Environment == nil && o.Workspace == nil {
return errors.New("one of: account,environment,workspace must be provided")
}
var scopeId, field string
if o.Account != nil {
scopeId = o.Account.ID
field = "account"
} else if o.Environment != nil {
scopeId = o.Environment.ID
field = "environment"
} else {
scopeId = o.Workspace.ID
field = "workspace"
}
if !validStringID(&scopeId) {
return fmt.Errorf("invalid value for %v ID: %v", field, scopeId)
}
if o.User == nil && o.Team == nil && o.ServiceAccount == nil {
return errors.New("one of: user,team,service_account must be provided")
}
var subjectId string
if o.User != nil {
subjectId = o.User.ID
field = "user"
} else if o.Team != nil {
subjectId = o.Team.ID
field = "team"
} else {
subjectId = o.ServiceAccount.ID
field = "service account"
}
if !validStringID(&subjectId) {
return fmt.Errorf("invalid value for %v ID: %v", field, subjectId)
}
return nil
}
// AccessPolicyListOptions represents the options for listing access policies.
type AccessPolicyListOptions struct {
ListOptions
Environment *string `url:"filter[environment],omitempty"`
Account *string `url:"filter[account],omitempty"`
Workspace *string `url:"filter[workspace],omitempty"`
User *string `url:"filter[user],omitempty"`
ServiceAccount *string `url:"filter[service-account],omitempty"`
Team *string `url:"filter[team],omitempty"`
Include string `url:"include,omitempty"`
}
// List the accessPolicies.
func (s *accessPolicies) List(ctx context.Context, options AccessPolicyListOptions) (*AccessPolicyList, error) {
req, err := s.client.newRequest("GET", "access-policies", &options)
if err != nil {
return nil, err
}
accessPolicyl := &AccessPolicyList{}
err = s.client.do(ctx, req, accessPolicyl)
if err != nil {
return nil, err
}
return accessPolicyl, nil
}
// Create is used to create a new AccessPolicy.
func (s *accessPolicies) Create(ctx context.Context, options AccessPolicyCreateOptions) (*AccessPolicy, 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", "access-policies", &options)
if err != nil {
return nil, err
}
accessPolicy := &AccessPolicy{}
err = s.client.do(ctx, req, accessPolicy)
if err != nil {
return nil, err
}
return accessPolicy, nil
}
// Read an accessPolicy by its ID.
func (s *accessPolicies) Read(ctx context.Context, accessPolicyID string) (*AccessPolicy, error) {
if !validStringID(&accessPolicyID) {
return nil, errors.New("invalid value for access policy ID")
}
u := fmt.Sprintf("access-policies/%s", url.QueryEscape(accessPolicyID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
accessPolicy := &AccessPolicy{}
err = s.client.do(ctx, req, accessPolicy)
if err != nil {
return nil, err
}
return accessPolicy, nil
}
// AccessPolicyUpdateOptions represents the options for updating an accessPolicy.
type AccessPolicyUpdateOptions struct {
// For internal use only!
ID string `jsonapi:"primary,access-policies"`
Roles []*Role `jsonapi:"relation,roles"`
}
// Update settings of an existing accessPolicy.
func (s *accessPolicies) Update(ctx context.Context, accessPolicyID string, options AccessPolicyUpdateOptions) (*AccessPolicy, error) {
// Make sure we don't send a user provided ID.
options.ID = ""
if len(options.Roles) == 0 {
return nil, errors.New("at least one role must be provided")
}
u := fmt.Sprintf("access-policies/%s", url.QueryEscape(accessPolicyID))
req, err := s.client.newRequest("PATCH", u, &options)
if err != nil {
return nil, err
}
accessPolicy := &AccessPolicy{}
err = s.client.do(ctx, req, accessPolicy)
if err != nil {
return nil, err
}
return accessPolicy, nil
}
// Delete an accessPolicy by its ID.
func (s *accessPolicies) Delete(ctx context.Context, accessPolicyID string) error {
if !validStringID(&accessPolicyID) {
return errors.New("invalid value for access policy ID")
}
u := fmt.Sprintf("access-policies/%s", url.QueryEscape(accessPolicyID))
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}