-
Notifications
You must be signed in to change notification settings - Fork 23
/
policy.go
88 lines (71 loc) · 2.43 KB
/
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
package enterpriseclient
import (
"fmt"
"github.com/pkg/errors"
"github.com/replicatedhq/replicated/pkg/enterprisetypes"
)
func (c HTTPClient) ListPolicies() ([]*enterprisetypes.Policy, error) {
enterprisePolicies := []*enterprisetypes.Policy{}
err := c.doJSON("GET", "/v1/policies", 200, nil, &enterprisePolicies)
if err != nil {
return nil, errors.Wrap(err, "failed to get policies")
}
return enterprisePolicies, nil
}
func (c HTTPClient) CreatePolicy(name string, description string, policy string) (*enterprisetypes.Policy, error) {
type CreatePolicyRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Policy string `json:"policy"`
}
createPolicyRequest := CreatePolicyRequest{
Name: name,
Description: description,
Policy: policy,
}
enterprisePolicy := enterprisetypes.Policy{}
err := c.doJSON("POST", "/v1/policy", 201, createPolicyRequest, &enterprisePolicy)
if err != nil {
return nil, errors.Wrap(err, "failed to create policy")
}
return &enterprisePolicy, nil
}
func (c HTTPClient) UpdatePolicy(id string, name string, description string, policy string) (*enterprisetypes.Policy, error) {
type UpdatePolicyRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Policy string `json:"policy"`
}
updatePolicyRequest := UpdatePolicyRequest{
Name: name,
Description: description,
Policy: policy,
}
enterprisePolicy := enterprisetypes.Policy{}
err := c.doJSON("PUT", fmt.Sprintf("/v1/policy/%s", id), 200, updatePolicyRequest, &enterprisePolicy)
if err != nil {
return nil, errors.Wrap(err, "failed to update policy")
}
return &enterprisePolicy, nil
}
func (c HTTPClient) RemovePolicy(id string) error {
err := c.doJSON("DELETE", fmt.Sprintf("/v1/policy/%s", id), 204, nil, nil)
if err != nil {
return errors.Wrap(err, "failed to delete policy")
}
return nil
}
func (c HTTPClient) AssignPolicy(policyID string, channelID string) error {
err := c.doJSON("POST", fmt.Sprintf("/v1/channelpolicy/%s/%s", policyID, channelID), 204, nil, nil)
if err != nil {
return errors.Wrap(err, "failed to assign policy")
}
return nil
}
func (c HTTPClient) UnassignPolicy(policyID string, channelID string) error {
err := c.doJSON("DELETE", fmt.Sprintf("/v1/channelpolicy/%s/%s", policyID, channelID), 204, nil, nil)
if err != nil {
return errors.Wrap(err, "failed to unassign policy")
}
return nil
}