forked from spaceapegames/go-wavefront
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathingestionpolicies.go
180 lines (143 loc) · 4.43 KB
/
ingestionpolicies.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
package wavefront
import (
"fmt"
)
const baseIngestionPolicyPath = "/api/v2/usage/ingestionpolicy"
type IngestionPolicyAccount struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
}
type IngestionPolicyGroup struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
Description string `json:"description,omitempty"`
}
type IngestionPolicyTag struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
type IngestionPolicyResponse struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Scope string `json:"scope,omitempty"`
Accounts []IngestionPolicyAccount `json:"accounts,omitempty"`
Groups []IngestionPolicyGroup `json:"groups,omitempty"`
Sources []string `json:"sources,omitempty"`
Namespaces []string `json:"namespaces,omitempty"`
Tags []IngestionPolicyTag `json:"pointTags,omitempty"`
}
type IngestionPolicyRequest struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Scope string `json:"scope,omitempty"`
Accounts []string `json:"accounts,omitempty"`
Groups []string `json:"groups,omitempty"`
Sources []string `json:"sources,omitempty"`
Namespaces []string `json:"namespaces,omitempty"`
// This is a slice of maps because you may have different values with the same key
// and also there is potentially an AND or OR operation assigned to them.
Tags []map[string]string `json:"pointTags,omitempty"`
}
type IngestionPolicies struct {
client Wavefronter
}
func (c *Client) IngestionPolicies() *IngestionPolicies {
return &IngestionPolicies{client: c}
}
func (p *IngestionPolicies) Find(conditions []*SearchCondition) (results []*IngestionPolicyResponse, err error) {
err = doSearch(conditions, "ingestionpolicy", p.client, &results)
return // results, err
}
func (p *IngestionPolicies) GetByID(policyID string) (*IngestionPolicyResponse, error) {
if policyID == "" {
return nil, fmt.Errorf("id must be specified")
}
ingestionPolicy := IngestionPolicyResponse{}
err := doRest(
"GET",
fmt.Sprintf("%s/%s", baseIngestionPolicyPath, policyID),
p.client,
doResponse(&ingestionPolicy),
)
if err != nil {
return nil, err
}
return &ingestionPolicy, nil
}
func (p *IngestionPolicies) Create(policy *IngestionPolicyRequest) (*IngestionPolicyResponse, error) {
if policy.Name == "" {
return nil, fmt.Errorf("ingestion policy name must be specified")
}
ingestionPolicy := IngestionPolicyResponse{}
err := doRest(
"POST",
baseIngestionPolicyPath,
p.client,
doPayload(policy),
doResponse(&ingestionPolicy),
)
if err != nil {
return nil, err
}
return &ingestionPolicy, nil
}
func (p *IngestionPolicies) Update(policy *IngestionPolicyResponse) error {
var accounts []string
for _, v := range policy.Accounts {
accounts = append(accounts, v.ID)
}
var groups []string
for _, v := range policy.Groups {
groups = append(groups, v.ID)
}
var pointTags []map[string]string
for _, v := range policy.Tags {
pointTags = append(pointTags, map[string]string{"Key": v.Key, "Value": v.Value})
}
policyRequest := &IngestionPolicyRequest{
ID: policy.ID,
Name: policy.Name,
Description: policy.Description,
Scope: policy.Scope,
Accounts: accounts,
Groups: groups,
Sources: policy.Sources,
Namespaces: policy.Namespaces,
Tags: pointTags,
}
updatedPolicy, err := p.rawUpdate(policyRequest)
if err != nil {
return err
}
*policy = *updatedPolicy
return nil
}
func (p *IngestionPolicies) rawUpdate(policy *IngestionPolicyRequest) (*IngestionPolicyResponse, error) {
if policy.ID == "" {
return nil, fmt.Errorf("id must be specified")
}
ingestionPolicy := IngestionPolicyResponse{}
err := doRest(
"PUT",
fmt.Sprintf("%s/%s", baseIngestionPolicyPath, policy.ID),
p.client,
doPayload(policy),
doResponse(&ingestionPolicy),
)
if err != nil {
return nil, err
}
return &ingestionPolicy, nil
}
func (p *IngestionPolicies) DeleteByID(policyID string) error {
if policyID == "" {
return fmt.Errorf("id must be specified")
}
return doRest(
"DELETE",
fmt.Sprintf("%s/%s", baseIngestionPolicyPath, policyID),
p.client,
)
}