forked from nscuro/dtrack-client
-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
policy.go
135 lines (108 loc) · 3.67 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
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
package dtrack
import (
"context"
"fmt"
"net/http"
"github.com/google/uuid"
)
type Policy struct {
UUID uuid.UUID `json:"uuid,omitempty"`
Name string `json:"name"`
Operator PolicyOperator `json:"operator"`
ViolationState PolicyViolationState `json:"violationState"`
PolicyConditions []PolicyCondition `json:"policyConditions,omitempty"`
IncludeChildren bool `json:"includeChildren,omitempty"`
Global bool `json:"global,omitempty"`
Projects []Project `json:"projects,omitempty"`
Tags []Tag `json:"tags,omitempty"`
}
type PolicyService struct {
client *Client
}
type PolicyOperator string
const (
PolicyOperatorAll PolicyOperator = "ALL"
PolicyOperatorAny PolicyOperator = "ANY"
)
type PolicyViolationState string
const (
PolicyViolationStateInfo PolicyViolationState = "INFO"
PolicyViolationStateWarn PolicyViolationState = "WARN"
PolicyViolationStateFail PolicyViolationState = "FAIL"
)
func (ps PolicyService) Get(ctx context.Context, policyUUID uuid.UUID) (p Policy, err error) {
req, err := ps.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/policy/%s", policyUUID))
if err != nil {
return
}
_, err = ps.client.doRequest(req, &p)
return
}
func (ps PolicyService) GetAll(ctx context.Context, po PageOptions) (p Page[Policy], err error) {
req, err := ps.client.newRequest(ctx, http.MethodGet, "/api/v1/policy", withPageOptions(po))
if err != nil {
return
}
res, err := ps.client.doRequest(req, &p.Items)
if err != nil {
return
}
p.TotalCount = res.TotalCount
return
}
func (ps PolicyService) Create(ctx context.Context, policy Policy) (p Policy, err error) {
req, err := ps.client.newRequest(ctx, http.MethodPut, "/api/v1/policy", withBody(policy))
if err != nil {
return
}
_, err = ps.client.doRequest(req, &p)
return
}
func (ps PolicyService) Delete(ctx context.Context, policyUUID uuid.UUID) (err error) {
req, err := ps.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/policy/%s", policyUUID))
if err != nil {
return
}
_, err = ps.client.doRequest(req, nil)
return
}
func (ps PolicyService) Update(ctx context.Context, policy Policy) (p Policy, err error) {
req, err := ps.client.newRequest(ctx, http.MethodPost, "/api/v1/policy", withBody(policy))
if err != nil {
return
}
_, err = ps.client.doRequest(req, &p)
return
}
func (ps PolicyService) AddProject(ctx context.Context, policyUUID, projectUUID uuid.UUID) (p Policy, err error) {
req, err := ps.client.newRequest(ctx, http.MethodPost, fmt.Sprintf("/api/v1/policy/%s/project/%s", policyUUID, projectUUID))
if err != nil {
return
}
_, err = ps.client.doRequest(req, &p)
return
}
func (ps PolicyService) DeleteProject(ctx context.Context, policyUUID, projectUUID uuid.UUID) (p Policy, err error) {
req, err := ps.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/policy/%s/project/%s", policyUUID, projectUUID))
if err != nil {
return
}
_, err = ps.client.doRequest(req, &p)
return
}
func (ps PolicyService) AddTag(ctx context.Context, policyUUID uuid.UUID, tagName string) (p Policy, err error) {
req, err := ps.client.newRequest(ctx, http.MethodPost, fmt.Sprintf("/api/v1/policy/%s/tag/%s", policyUUID, tagName))
if err != nil {
return
}
_, err = ps.client.doRequest(req, &p)
return
}
func (ps PolicyService) DeleteTag(ctx context.Context, policyUUID uuid.UUID, tagName string) (p Policy, err error) {
req, err := ps.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/policy/%s/tag/%s", policyUUID, tagName))
if err != nil {
return
}
_, err = ps.client.doRequest(req, &p)
return
}