-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolicy_group_environments.go
122 lines (102 loc) · 3.59 KB
/
policy_group_environments.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
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ PolicyGroupEnvironments = (*policyGroupEnvironment)(nil)
// PolicyGroupEnvironments describes all the policy group environments related methods that the
// Scalr API supports.
type PolicyGroupEnvironments interface {
Create(ctx context.Context, options PolicyGroupEnvironmentsCreateOptions) error
Update(ctx context.Context, options PolicyGroupEnvironmentsUpdateOptions) error
Delete(ctx context.Context, options PolicyGroupEnvironmentDeleteOptions) error
}
// policyGroupEnvironments implements PolicyGroupEnvironments.
type policyGroupEnvironment struct {
client *Client
}
// PolicyGroupEnvironment represents a single policy group environment relation.
type PolicyGroupEnvironment struct {
ID string `jsonapi:"primary,environments"`
}
// PolicyGroupEnvironmentsCreateOptions represents options for creating new policy group environment linkage
type PolicyGroupEnvironmentsCreateOptions struct {
PolicyGroupID string
PolicyGroupEnvironments []*PolicyGroupEnvironment
}
// PolicyGroupEnvironmentsUpdateOptions represents options for updating policy group environment linkage
type PolicyGroupEnvironmentsUpdateOptions struct {
PolicyGroupID string
PolicyGroupEnvironments []*PolicyGroupEnvironment
}
type PolicyGroupEnvironmentDeleteOptions struct {
PolicyGroupID string
EnvironmentID string
}
func (o PolicyGroupEnvironmentsCreateOptions) valid() error {
if !validStringID(&o.PolicyGroupID) {
return errors.New("invalid value for policy group ID")
}
if o.PolicyGroupEnvironments == nil || len(o.PolicyGroupEnvironments) < 1 {
return errors.New("list of environments is required")
}
return nil
}
func (o PolicyGroupEnvironmentsUpdateOptions) valid() error {
if !validStringID(&o.PolicyGroupID) {
return errors.New("invalid value for policy group ID")
}
return nil
}
func (o PolicyGroupEnvironmentDeleteOptions) valid() error {
if !validStringID(&o.PolicyGroupID) {
return errors.New("invalid value for policy group ID")
}
if !validStringID(&o.EnvironmentID) {
return errors.New("invalid value for environment ID")
}
return nil
}
// Create a new policy group environments linkage.
func (s *policyGroupEnvironment) Create(ctx context.Context, options PolicyGroupEnvironmentsCreateOptions) error {
if err := options.valid(); err != nil {
return err
}
u := fmt.Sprintf("policy-groups/%s/relationships/environments", url.QueryEscape(options.PolicyGroupID))
req, err := s.client.newRequest("POST", u, options.PolicyGroupEnvironments)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}
// Update policy group environments linkage
func (s *policyGroupEnvironment) Update(ctx context.Context, options PolicyGroupEnvironmentsUpdateOptions) error {
if err := options.valid(); err != nil {
return err
}
u := fmt.Sprintf("policy-groups/%s/relationships/environments", url.QueryEscape(options.PolicyGroupID))
req, err := s.client.newRequest("PATCH", u, options.PolicyGroupEnvironments)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}
// Delete policy group environments linkage by its ID.
func (s *policyGroupEnvironment) Delete(ctx context.Context, options PolicyGroupEnvironmentDeleteOptions) error {
if err := options.valid(); err != nil {
return err
}
u := fmt.Sprintf(
"policy-groups/%s/relationships/environments/%s",
url.QueryEscape(options.PolicyGroupID),
url.QueryEscape(options.EnvironmentID),
)
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}