-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolicy_group.go
261 lines (221 loc) · 8.26 KB
/
policy_group.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
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ PolicyGroups = (*policyGroups)(nil)
// PolicyGroups describes all the policy group related methods that the
// Scalr API supports.
type PolicyGroups interface {
List(ctx context.Context, options PolicyGroupListOptions) (*PolicyGroupList, error)
Read(ctx context.Context, policyGroupID string) (*PolicyGroup, error)
Create(ctx context.Context, options PolicyGroupCreateOptions) (*PolicyGroup, error)
Update(ctx context.Context, policyGroupID string, options PolicyGroupUpdateOptions) (*PolicyGroup, error)
Delete(ctx context.Context, policyGroupID string) error
}
// policyGroups implements PolicyGroups.
type policyGroups struct {
client *Client
}
// PolicyGroupStatus represents a policy group status.
type PolicyGroupStatus string
// List of available policy group statuses.
const (
PolicyGroupStatusFetching PolicyGroupStatus = "fetching"
PolicyGroupStatusActive PolicyGroupStatus = "active"
PolicyGroupStatusErrored PolicyGroupStatus = "errored"
)
// PolicyEnforcementLevel represents enforcement level of an OPA policy.
type PolicyEnforcementLevel string
// List of available policy enforcement levels.
const (
PolicyEnforcementLevelHard = "hard-mandatory"
PolicyEnforcementLevelSoft = "soft-mandatory"
PolicyEnforcementLevelAdvisory = "advisory"
)
// Policy represents a single OPA policy.
type Policy struct {
ID string `jsonapi:"primary,policies"`
Name string `jsonapi:"attr,name"`
Enabled bool `jsonapi:"attr,enabled"`
EnforcementLevel PolicyEnforcementLevel `jsonapi:"attr,enforced-level"`
// Relations
PolicyGroup *PolicyGroup `jsonapi:"relation,policy-groups"`
}
// PolicyGroupVCSRepo contains the configuration of a VCS integration.
type PolicyGroupVCSRepo struct {
Identifier string `json:"identifier"`
Branch string `json:"branch"`
Path string `json:"path"`
}
// PolicyGroupVCSRepoOptions contains the configuration options of a VCS integration.
type PolicyGroupVCSRepoOptions struct {
Identifier *string `json:"identifier"`
Branch *string `json:"branch,omitempty"`
Path *string `json:"path,omitempty"`
}
// PolicyGroup represents a Scalr policy group.
type PolicyGroup struct {
ID string `jsonapi:"primary,policy-groups"`
Name string `jsonapi:"attr,name"`
Status PolicyGroupStatus `jsonapi:"attr,status"`
ErrorMessage string `jsonapi:"attr,error-message"`
OpaVersion string `jsonapi:"attr,opa-version"`
VCSRepo *PolicyGroupVCSRepo `jsonapi:"attr,vcs-repo"`
IsEnforced bool `jsonapi:"attr,is-enforced"`
CommonFunctionsFolder string `jsonapi:"attr,common-functions-folder"`
// Relations
Account *Account `jsonapi:"relation,account"`
VcsProvider *VcsProvider `jsonapi:"relation,vcs-provider"`
VcsRevision *VcsRevision `jsonapi:"relation,vcs-revision"`
Policies []*Policy `jsonapi:"relation,policies"`
Environments []*Environment `jsonapi:"relation,environments"`
}
// PolicyGroupList represents a list of policy groups.
type PolicyGroupList struct {
*Pagination
Items []*PolicyGroup
}
// PolicyGroupListOptions represents the options for listing policy groups.
type PolicyGroupListOptions struct {
ListOptions
Account string `url:"filter[account],omitempty"`
Environment string `url:"filter[environment],omitempty"`
Name string `url:"filter[name],omitempty"`
PolicyGroup string `url:"filter[policy-group],omitempty"`
Query string `url:"query,omitempty"`
Sort string `url:"sort,omitempty"`
Include string `url:"include,omitempty"`
}
// PolicyGroupCreateOptions represents the options for creating a new PolicyGroup.
type PolicyGroupCreateOptions struct {
ID string `jsonapi:"primary,policy-groups"`
Name *string `jsonapi:"attr,name"`
OpaVersion *string `jsonapi:"attr,opa-version,omitempty"`
VCSRepo *PolicyGroupVCSRepoOptions `jsonapi:"attr,vcs-repo"`
IsEnforced *bool `jsonapi:"attr,is-enforced,omitempty"`
CommonFunctionsFolder *string `jsonapi:"attr,common-functions-folder,omitempty"`
// Relations
Account *Account `jsonapi:"relation,account"`
VcsProvider *VcsProvider `jsonapi:"relation,vcs-provider"`
Environments []*Environment `jsonapi:"relation,environments"`
}
func (o PolicyGroupCreateOptions) valid() error {
if !validString(o.Name) {
return errors.New("name is required")
}
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.VcsProvider == nil {
return errors.New("vcs provider is required")
}
if !validStringID(&o.VcsProvider.ID) {
return errors.New("invalid value for vcs provider ID")
}
if o.VCSRepo == nil {
return errors.New("vcs repo is required")
}
return nil
}
// PolicyGroupUpdateOptions represents the options for updating a PolicyGroup.
type PolicyGroupUpdateOptions struct {
ID string `jsonapi:"primary,policy-groups"`
Name *string `jsonapi:"attr,name,omitempty"`
OpaVersion *string `jsonapi:"attr,opa-version,omitempty"`
VCSRepo *PolicyGroupVCSRepoOptions `jsonapi:"attr,vcs-repo,omitempty"`
IsEnforced *bool `jsonapi:"attr,is-enforced,omitempty"`
CommonFunctionsFolder *string `jsonapi:"attr,common-functions-folder,omitempty"`
// Relations
VcsProvider *VcsProvider `jsonapi:"relation,vcs-provider,omitempty"`
}
// List all the policy groups.
func (s *policyGroups) List(ctx context.Context, options PolicyGroupListOptions) (*PolicyGroupList, error) {
req, err := s.client.newRequest("GET", "policy-groups", &options)
if err != nil {
return nil, err
}
pgl := &PolicyGroupList{}
err = s.client.do(ctx, req, pgl)
if err != nil {
return nil, err
}
return pgl, nil
}
// Create a new policy group.
func (s *policyGroups) Create(ctx context.Context, options PolicyGroupCreateOptions) (*PolicyGroup, 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", "policy-groups", &options)
if err != nil {
return nil, err
}
pg := &PolicyGroup{}
err = s.client.do(ctx, req, pg)
if err != nil {
return nil, err
}
return pg, nil
}
// Read policy group by its ID.
func (s *policyGroups) Read(ctx context.Context, policyGroupID string) (*PolicyGroup, error) {
if !validStringID(&policyGroupID) {
return nil, errors.New("invalid value for policy group ID")
}
options := struct {
Include string `url:"include"`
}{
Include: "policies",
}
u := fmt.Sprintf("policy-groups/%s", url.QueryEscape(policyGroupID))
req, err := s.client.newRequest("GET", u, options)
if err != nil {
return nil, err
}
pg := &PolicyGroup{}
err = s.client.do(ctx, req, pg)
if err != nil {
return nil, err
}
return pg, nil
}
// Update settings of existing policy group.
func (s *policyGroups) Update(ctx context.Context, policyGroupID string, options PolicyGroupUpdateOptions) (*PolicyGroup, error) {
if !validStringID(&policyGroupID) {
return nil, errors.New("invalid value for policy group ID")
}
// Make sure we don't send a user provided ID.
options.ID = ""
u := fmt.Sprintf("policy-groups/%s", url.QueryEscape(policyGroupID))
req, err := s.client.newRequest("PATCH", u, &options)
if err != nil {
return nil, err
}
pg := &PolicyGroup{}
err = s.client.do(ctx, req, pg)
if err != nil {
return nil, err
}
return pg, nil
}
// Delete policy group by its ID.
func (s *policyGroups) Delete(ctx context.Context, policyGroupID string) error {
if !validStringID(&policyGroupID) {
return errors.New("invalid value for policy group ID")
}
u := fmt.Sprintf("policy-groups/%s", url.QueryEscape(policyGroupID))
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}