forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy_set.go
477 lines (395 loc) · 14.6 KB
/
policy_set.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package tfe
import (
"context"
"fmt"
"net/url"
"time"
)
// Compile-time proof of interface implementation.
var _ PolicySets = (*policySets)(nil)
// PolicySets describes all the policy set related methods that the Terraform
// Enterprise API supports.
//
// TFE API docs: https://www.terraform.io/docs/cloud/api/policy-sets.html
type PolicySets interface {
// List all the policy sets for a given organization.
List(ctx context.Context, organization string, options *PolicySetListOptions) (*PolicySetList, error)
// Create a policy set and associate it with an organization.
Create(ctx context.Context, organization string, options PolicySetCreateOptions) (*PolicySet, error)
// Read a policy set by its ID.
Read(ctx context.Context, policySetID string) (*PolicySet, error)
// ReadWithOptions reads a policy set by its ID using the options supplied.
ReadWithOptions(ctx context.Context, policySetID string, options *PolicySetReadOptions) (*PolicySet, error)
// Update an existing policy set.
Update(ctx context.Context, policySetID string, options PolicySetUpdateOptions) (*PolicySet, error)
// Add policies to a policy set. This function can only be used when
// there is no VCS repository associated with the policy set.
AddPolicies(ctx context.Context, policySetID string, options PolicySetAddPoliciesOptions) error
// Remove policies from a policy set. This function can only be used
// when there is no VCS repository associated with the policy set.
RemovePolicies(ctx context.Context, policySetID string, options PolicySetRemovePoliciesOptions) error
// Add workspaces to a policy set.
AddWorkspaces(ctx context.Context, policySetID string, options PolicySetAddWorkspacesOptions) error
// Remove workspaces from a policy set.
RemoveWorkspaces(ctx context.Context, policySetID string, options PolicySetRemoveWorkspacesOptions) error
// Delete a policy set by its ID.
Delete(ctx context.Context, policyID string) error
}
// policySets implements PolicySets.
type policySets struct {
client *Client
}
// PolicySetList represents a list of policy sets.
type PolicySetList struct {
*Pagination
Items []*PolicySet
}
// PolicySet represents a Terraform Enterprise policy set.
type PolicySet struct {
ID string `jsonapi:"primary,policy-sets"`
Name string `jsonapi:"attr,name"`
Description string `jsonapi:"attr,description"`
Global bool `jsonapi:"attr,global"`
PoliciesPath string `jsonapi:"attr,policies-path"`
PolicyCount int `jsonapi:"attr,policy-count"`
VCSRepo *VCSRepo `jsonapi:"attr,vcs-repo"`
WorkspaceCount int `jsonapi:"attr,workspace-count"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"`
// Relations
// The organization to which the policy set belongs to.
Organization *Organization `jsonapi:"relation,organization"`
// The workspaces to which the policy set applies.
Workspaces []*Workspace `jsonapi:"relation,workspaces"`
// Individually managed policies which are associated with the policy set.
Policies []*Policy `jsonapi:"relation,policies"`
// The most recently created policy set version, regardless of status.
// Note that this relationship may include an errored and unusable version,
// and is intended to allow checking for errors.
NewestVersion *PolicySetVersion `jsonapi:"relation,newest-version"`
// The most recent successful policy set version.
CurrentVersion *PolicySetVersion `jsonapi:"relation,current-version"`
}
// PolicySetIncludeOpt represents the available options for include query params.
type PolicySetIncludeOpt string
const (
PolicySetPolicies PolicySetIncludeOpt = "policies"
PolicySetWorkspaces PolicySetIncludeOpt = "workspaces"
PolicySetCurrentVersion PolicySetIncludeOpt = "current_version"
PolicySetNewestVersion PolicySetIncludeOpt = "newest_version"
)
// PolicySetListOptions represents the options for listing policy sets.
type PolicySetListOptions struct {
ListOptions
// Optional: A search string (partial policy set name) used to filter the results.
Search string `url:"search[name],omitempty"`
}
// PolicySetReadOptions are read options.
// For a full list of relations, please see:
// https://www.terraform.io/docs/cloud/api/policy-sets.html#relationships
type PolicySetReadOptions struct {
// Optional: A list of relations to include. See available resources
// https://www.terraform.io/cloud-docs/api-docs/policy-sets#relationships
Include []PolicySetIncludeOpt `url:"include,omitempty"`
}
// PolicySetCreateOptions represents the options for creating a new policy set.
type PolicySetCreateOptions struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,policy-sets"`
// Required: The name of the policy set.
Name *string `jsonapi:"attr,name"`
// Optional: The description of the policy set.
Description *string `jsonapi:"attr,description,omitempty"`
// Optional: Whether or not the policy set is global.
Global *bool `jsonapi:"attr,global,omitempty"`
// Optional: The sub-path within the attached VCS repository to ingress. All
// files and directories outside of this sub-path will be ignored.
// This option may only be specified when a VCS repo is present.
PoliciesPath *string `jsonapi:"attr,policies-path,omitempty"`
// Optional: The initial members of the policy set.
Policies []*Policy `jsonapi:"relation,policies,omitempty"`
// Optional: VCS repository information. When present, the policies and
// configuration will be sourced from the specified VCS repository
// instead of being defined within the policy set itself. Note that
// this option is mutually exclusive with the Policies option and
// both cannot be used at the same time.
VCSRepo *VCSRepoOptions `jsonapi:"attr,vcs-repo,omitempty"`
// Optional: The initial list of workspaces for which the policy set should be enforced.
Workspaces []*Workspace `jsonapi:"relation,workspaces,omitempty"`
}
// PolicySetUpdateOptions represents the options for updating a policy set.
type PolicySetUpdateOptions struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,policy-sets"`
// Optional: The name of the policy set.
Name *string `jsonapi:"attr,name,omitempty"`
// Optional: The description of the policy set.
Description *string `jsonapi:"attr,description,omitempty"`
// Optional: Whether or not the policy set is global.
Global *bool `jsonapi:"attr,global,omitempty"`
// Optional: The sub-path within the attached VCS repository to ingress. All
// files and directories outside of this sub-path will be ignored.
// This option may only be specified when a VCS repo is present.
PoliciesPath *string `jsonapi:"attr,policies-path,omitempty"`
// Optional: VCS repository information. When present, the policies and
// configuration will be sourced from the specified VCS repository
// instead of being defined within the policy set itself. Note that
// specifying this option may only be used on policy sets with no
// directly-attached policies (*PolicySet.Policies). Specifying this
// option when policies are already present will result in an error.
VCSRepo *VCSRepoOptions `jsonapi:"attr,vcs-repo,omitempty"`
}
// PolicySetAddPoliciesOptions represents the options for adding policies
// to a policy set.
type PolicySetAddPoliciesOptions struct {
// The policies to add to the policy set.
Policies []*Policy
}
// PolicySetRemovePoliciesOptions represents the options for removing
// policies from a policy set.
type PolicySetRemovePoliciesOptions struct {
// The policies to remove from the policy set.
Policies []*Policy
}
// PolicySetAddWorkspacesOptions represents the options for adding workspaces
// to a policy set.
type PolicySetAddWorkspacesOptions struct {
// The workspaces to add to the policy set.
Workspaces []*Workspace
}
// PolicySetRemoveWorkspacesOptions represents the options for removing
// workspaces from a policy set.
type PolicySetRemoveWorkspacesOptions struct {
// The workspaces to remove from the policy set.
Workspaces []*Workspace
}
// List all the policies for a given organization.
func (s *policySets) List(ctx context.Context, organization string, options *PolicySetListOptions) (*PolicySetList, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
u := fmt.Sprintf("organizations/%s/policy-sets", url.QueryEscape(organization))
req, err := s.client.newRequest("GET", u, options)
if err != nil {
return nil, err
}
psl := &PolicySetList{}
err = s.client.do(ctx, req, psl)
if err != nil {
return nil, err
}
return psl, nil
}
// Create a policy set and associate it with an organization.
func (s *policySets) Create(ctx context.Context, organization string, options PolicySetCreateOptions) (*PolicySet, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("organizations/%s/policy-sets", url.QueryEscape(organization))
req, err := s.client.newRequest("POST", u, &options)
if err != nil {
return nil, err
}
ps := &PolicySet{}
err = s.client.do(ctx, req, ps)
if err != nil {
return nil, err
}
return ps, err
}
// Read a policy set by its ID.
func (s *policySets) Read(ctx context.Context, policySetID string) (*PolicySet, error) {
return s.ReadWithOptions(ctx, policySetID, nil)
}
// ReadWithOptions reads a policy by its ID using the options supplied.
func (s *policySets) ReadWithOptions(ctx context.Context, policySetID string, options *PolicySetReadOptions) (*PolicySet, error) {
if !validStringID(&policySetID) {
return nil, ErrInvalidPolicySetID
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("policy-sets/%s", url.QueryEscape(policySetID))
req, err := s.client.newRequest("GET", u, options)
if err != nil {
return nil, err
}
ps := &PolicySet{}
err = s.client.do(ctx, req, ps)
if err != nil {
return nil, err
}
return ps, err
}
// Update an existing policy set.
func (s *policySets) Update(ctx context.Context, policySetID string, options PolicySetUpdateOptions) (*PolicySet, error) {
if !validStringID(&policySetID) {
return nil, ErrInvalidPolicySetID
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("policy-sets/%s", url.QueryEscape(policySetID))
req, err := s.client.newRequest("PATCH", u, &options)
if err != nil {
return nil, err
}
ps := &PolicySet{}
err = s.client.do(ctx, req, ps)
if err != nil {
return nil, err
}
return ps, err
}
// AddPolicies adds policies to a policy set
func (s *policySets) AddPolicies(ctx context.Context, policySetID string, options PolicySetAddPoliciesOptions) error {
if !validStringID(&policySetID) {
return ErrInvalidPolicySetID
}
if err := options.valid(); err != nil {
return err
}
u := fmt.Sprintf("policy-sets/%s/relationships/policies", url.QueryEscape(policySetID))
req, err := s.client.newRequest("POST", u, options.Policies)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}
// RemovePolicies remove policies from a policy set
func (s *policySets) RemovePolicies(ctx context.Context, policySetID string, options PolicySetRemovePoliciesOptions) error {
if !validStringID(&policySetID) {
return ErrInvalidPolicySetID
}
if err := options.valid(); err != nil {
return err
}
u := fmt.Sprintf("policy-sets/%s/relationships/policies", url.QueryEscape(policySetID))
req, err := s.client.newRequest("DELETE", u, options.Policies)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}
// Addworkspaces adds workspaces to a policy set.
func (s *policySets) AddWorkspaces(ctx context.Context, policySetID string, options PolicySetAddWorkspacesOptions) error {
if !validStringID(&policySetID) {
return ErrInvalidPolicySetID
}
if err := options.valid(); err != nil {
return err
}
u := fmt.Sprintf("policy-sets/%s/relationships/workspaces", url.QueryEscape(policySetID))
req, err := s.client.newRequest("POST", u, options.Workspaces)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}
// RemoveWorkspaces removes workspaces from a policy set.
func (s *policySets) RemoveWorkspaces(ctx context.Context, policySetID string, options PolicySetRemoveWorkspacesOptions) error {
if !validStringID(&policySetID) {
return ErrInvalidPolicySetID
}
if err := options.valid(); err != nil {
return err
}
u := fmt.Sprintf("policy-sets/%s/relationships/workspaces", url.QueryEscape(policySetID))
req, err := s.client.newRequest("DELETE", u, options.Workspaces)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}
// Delete a policy set by its ID.
func (s *policySets) Delete(ctx context.Context, policySetID string) error {
if !validStringID(&policySetID) {
return ErrInvalidPolicySetID
}
u := fmt.Sprintf("policy-sets/%s", url.QueryEscape(policySetID))
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}
func (o PolicySetCreateOptions) valid() error {
if !validString(o.Name) {
return ErrRequiredName
}
if !validStringID(o.Name) {
return ErrInvalidName
}
return nil
}
func (o PolicySetRemoveWorkspacesOptions) valid() error {
if o.Workspaces == nil {
return ErrWorkspacesRequired
}
if len(o.Workspaces) == 0 {
return ErrWorkspaceMinLimit
}
return nil
}
func (o PolicySetUpdateOptions) valid() error {
if o.Name != nil && !validStringID(o.Name) {
return ErrInvalidName
}
return nil
}
func (o PolicySetAddPoliciesOptions) valid() error {
if o.Policies == nil {
return ErrRequiredPolicies
}
if len(o.Policies) == 0 {
return ErrInvalidPolicies
}
return nil
}
func (o PolicySetRemovePoliciesOptions) valid() error {
if o.Policies == nil {
return ErrRequiredPolicies
}
if len(o.Policies) == 0 {
return ErrInvalidPolicies
}
return nil
}
func (o PolicySetAddWorkspacesOptions) valid() error {
if o.Workspaces == nil {
return ErrWorkspacesRequired
}
if len(o.Workspaces) == 0 {
return ErrWorkspaceMinLimit
}
return nil
}
func (o *PolicySetReadOptions) valid() error {
if o == nil {
return nil // nothing to validate
}
if err := validatePolicySetIncludeParams(o.Include); err != nil {
return err
}
return nil
}
func validatePolicySetIncludeParams(params []PolicySetIncludeOpt) error {
for _, p := range params {
switch p {
case PolicySetPolicies, PolicySetWorkspaces, PolicySetCurrentVersion, PolicySetNewestVersion:
// do nothing
default:
return ErrInvalidIncludeValue
}
}
return nil
}