-
Notifications
You must be signed in to change notification settings - Fork 89
/
policies.go
410 lines (330 loc) · 11.8 KB
/
policies.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package alerts
import (
"context"
"fmt"
"github.com/newrelic/newrelic-client-go/pkg/errors"
"github.com/newrelic/newrelic-client-go/internal/http"
"github.com/newrelic/newrelic-client-go/internal/serialization"
)
// IncidentPreferenceType specifies rollup settings for alert policies.
type IncidentPreferenceType string
var (
// IncidentPreferenceTypes specifies the possible incident preferenece types for an alert policy.
IncidentPreferenceTypes = struct {
PerPolicy IncidentPreferenceType
PerCondition IncidentPreferenceType
PerConditionAndTarget IncidentPreferenceType
}{
PerPolicy: "PER_POLICY",
PerCondition: "PER_CONDITION",
PerConditionAndTarget: "PER_CONDITION_AND_TARGET",
}
)
// Policy represents a New Relic alert policy.
type Policy struct {
ID int `json:"id,omitempty"`
IncidentPreference IncidentPreferenceType `json:"incident_preference,omitempty"`
Name string `json:"name,omitempty"`
CreatedAt *serialization.EpochTime `json:"created_at,omitempty"`
UpdatedAt *serialization.EpochTime `json:"updated_at,omitempty"`
}
// ListPoliciesParams represents a set of filters to be used when querying New
// Relic alert policies.
type ListPoliciesParams struct {
Name string `url:"filter[name],omitempty"`
}
// ListPolicies returns a list of Alert Policies for a given account.
func (a *Alerts) ListPolicies(params *ListPoliciesParams) ([]Policy, error) {
return a.ListPoliciesWithContext(context.Background(), params)
}
// ListPoliciesWithContext returns a list of Alert Policies for a given account.
func (a *Alerts) ListPoliciesWithContext(ctx context.Context, params *ListPoliciesParams) ([]Policy, error) {
alertPolicies := []Policy{}
nextURL := a.config.Region().RestURL("/alerts_policies.json")
for nextURL != "" {
response := alertPoliciesResponse{}
resp, err := a.client.GetWithContext(ctx, nextURL, ¶ms, &response)
if err != nil {
return nil, err
}
alertPolicies = append(alertPolicies, response.Policies...)
paging := a.pager.Parse(resp)
nextURL = paging.Next
}
return alertPolicies, nil
}
// GetPolicy returns a specific alert policy by ID for a given account.
func (a *Alerts) GetPolicy(id int) (*Policy, error) {
return a.GetPolicyWithContext(context.Background(), id)
}
// GetPolicyWithContext returns a specific alert policy by ID for a given account.
func (a *Alerts) GetPolicyWithContext(ctx context.Context, id int) (*Policy, error) {
policies, err := a.ListPoliciesWithContext(ctx, nil)
if err != nil {
return nil, err
}
for _, policy := range policies {
if policy.ID == id {
return &policy, nil
}
}
return nil, errors.NewNotFoundf("no alert policy found for id %d", id)
}
// CreatePolicy creates a new alert policy for a given account.
func (a *Alerts) CreatePolicy(policy Policy) (*Policy, error) {
return a.CreatePolicyWithContext(context.Background(), policy)
}
// CreatePolicyWithContext creates a new alert policy for a given account.
func (a *Alerts) CreatePolicyWithContext(ctx context.Context, policy Policy) (*Policy, error) {
reqBody := alertPolicyRequestBody{
Policy: policy,
}
resp := alertPolicyResponse{}
_, err := a.client.PostWithContext(ctx, a.config.Region().RestURL("/alerts_policies.json"), nil, &reqBody, &resp)
if err != nil {
return nil, err
}
return &resp.Policy, nil
}
// UpdatePolicy update an alert policy for a given account.
func (a *Alerts) UpdatePolicy(policy Policy) (*Policy, error) {
return a.UpdatePolicyWithContext(context.Background(), policy)
}
// UpdatePolicyWithContext update an alert policy for a given account.
func (a *Alerts) UpdatePolicyWithContext(ctx context.Context, policy Policy) (*Policy, error) {
reqBody := alertPolicyRequestBody{
Policy: policy,
}
resp := alertPolicyResponse{}
url := fmt.Sprintf("/alerts_policies/%d.json", policy.ID)
_, err := a.client.PutWithContext(ctx, a.config.Region().RestURL(url), nil, &reqBody, &resp)
if err != nil {
return nil, err
}
return &resp.Policy, nil
}
// DeletePolicy deletes an existing alert policy for a given account.
func (a *Alerts) DeletePolicy(id int) (*Policy, error) {
return a.DeletePolicyWithContext(context.Background(), id)
}
// DeletePolicyWithContext deletes an existing alert policy for a given account.
func (a *Alerts) DeletePolicyWithContext(ctx context.Context, id int) (*Policy, error) {
resp := alertPolicyResponse{}
url := fmt.Sprintf("/alerts_policies/%d.json", id)
_, err := a.client.DeleteWithContext(ctx, a.config.Region().RestURL(url), nil, &resp)
if err != nil {
return nil, err
}
return &resp.Policy, nil
}
func (a *Alerts) CreatePolicyMutation(accountID int, policy AlertsPolicyInput) (*AlertsPolicy, error) {
return a.CreatePolicyMutationWithContext(context.Background(), accountID, policy)
}
func (a *Alerts) CreatePolicyMutationWithContext(ctx context.Context, accountID int, policy AlertsPolicyInput) (*AlertsPolicy, error) {
vars := map[string]interface{}{
"accountID": accountID,
"policy": policy,
}
resp := alertQueryPolicyCreateResponse{}
if err := a.client.NerdGraphQueryWithContext(ctx, alertsPolicyCreatePolicy, vars, &resp); err != nil {
return nil, err
}
return &resp.AlertsPolicy, nil
}
func (a *Alerts) UpdatePolicyMutation(accountID int, policyID string, policy AlertsPolicyUpdateInput) (*AlertsPolicy, error) {
return a.UpdatePolicyMutationWithContext(context.Background(), accountID, policyID, policy)
}
func (a *Alerts) UpdatePolicyMutationWithContext(ctx context.Context, accountID int, policyID string, policy AlertsPolicyUpdateInput) (*AlertsPolicy, error) {
vars := map[string]interface{}{
"accountID": accountID,
"policyID": policyID,
"policy": policy,
}
resp := alertQueryPolicyUpdateResponse{}
if err := a.client.NerdGraphQueryWithContext(ctx, alertsPolicyUpdatePolicy, vars, &resp); err != nil {
return nil, err
}
return &resp.AlertsPolicy, nil
}
// QueryPolicy queries NerdGraph for a policy matching the given account ID and
// policy ID.
func (a *Alerts) QueryPolicy(accountID int, id string) (*AlertsPolicy, error) {
return a.QueryPolicyWithContext(context.Background(), accountID, id)
}
// QueryPolicyWithContext queries NerdGraph for a policy matching the given account ID and
// policy ID.
func (a *Alerts) QueryPolicyWithContext(ctx context.Context, accountID int, id string) (*AlertsPolicy, error) {
resp := alertQueryPolicyResponse{}
vars := map[string]interface{}{
"accountID": accountID,
"policyID": id,
}
req, err := a.client.NewNerdGraphRequest(alertPolicyQueryPolicy, vars, &resp)
if err != nil {
return nil, err
}
req.WithContext(ctx)
var errorResponse alertPoliciesErrorResponse
req.SetErrorValue(&errorResponse)
if _, err := a.client.Do(req); err != nil {
return nil, err
}
return &resp.Actor.Account.Alerts.Policy, nil
}
// QueryPolicySearch searches NerdGraph for policies.
func (a *Alerts) QueryPolicySearch(accountID int, params AlertsPoliciesSearchCriteriaInput) ([]*AlertsPolicy, error) {
return a.QueryPolicySearchWithContext(context.Background(), accountID, params)
}
// QueryPolicySearchWithContext searches NerdGraph for policies.
func (a *Alerts) QueryPolicySearchWithContext(ctx context.Context, accountID int, params AlertsPoliciesSearchCriteriaInput) ([]*AlertsPolicy, error) {
policies := []*AlertsPolicy{}
var nextCursor *string
for ok := true; ok; ok = nextCursor != nil {
resp := alertQueryPolicySearchResponse{}
vars := map[string]interface{}{
"accountID": accountID,
"cursor": nextCursor,
"searchCriteria": params,
}
if err := a.client.NerdGraphQueryWithContext(ctx, alertsPolicyQuerySearch, vars, &resp); err != nil {
return nil, err
}
policies = append(policies, resp.Actor.Account.Alerts.PoliciesSearch.Policies...)
nextCursor = resp.Actor.Account.Alerts.PoliciesSearch.NextCursor
}
return policies, nil
}
// DeletePolicyMutation is the NerdGraph mutation to delete a policy given the
// account ID and the policy ID.
func (a *Alerts) DeletePolicyMutation(accountID int, id string) (*AlertsPolicy, error) {
return a.DeletePolicyMutationWithContext(context.Background(), accountID, id)
}
// DeletePolicyMutationWithContext is the NerdGraph mutation to delete a policy given the
// account ID and the policy ID.
func (a *Alerts) DeletePolicyMutationWithContext(ctx context.Context, accountID int, id string) (*AlertsPolicy, error) {
policy := &AlertsPolicy{}
resp := alertQueryPolicyDeleteRespose{}
vars := map[string]interface{}{
"accountID": accountID,
"policyID": id,
}
if err := a.client.NerdGraphQueryWithContext(ctx, alertPolicyDeletePolicy, vars, &resp); err != nil {
return nil, err
}
return policy, nil
}
type alertPoliciesErrorResponse struct {
http.GraphQLErrorResponse
}
func (r *alertPoliciesErrorResponse) IsNotFound() bool {
if len(r.Errors) == 0 {
return false
}
for _, err := range r.Errors {
if err.Message == "Not Found" &&
// TODO: When the alerts API begins using `errorClass`
// instead of `code` to specify error type, the conditional
// checking the `code` field can be removed.
//
// https://newrelic.atlassian.net/browse/AINTER-7746
(err.Extensions.Code == "BAD_USER_INPUT" || err.Extensions.ErrorClass == "BAD_USER_INPUT") {
return true
}
}
return false
}
func (r *alertPoliciesErrorResponse) Error() string {
return r.GraphQLErrorResponse.Error()
}
func (r *alertPoliciesErrorResponse) New() http.ErrorResponse {
return &alertPoliciesErrorResponse{}
}
type alertPoliciesResponse struct {
Policies []Policy `json:"policies,omitempty"`
}
type alertPolicyResponse struct {
Policy Policy `json:"policy,omitempty"`
}
type alertPolicyRequestBody struct {
Policy Policy `json:"policy"`
}
type alertQueryPolicySearchResponse struct {
Actor struct {
Account struct {
Alerts struct {
PoliciesSearch struct {
NextCursor *string `json:"nextCursor"`
Policies []*AlertsPolicy `json:"policies"`
TotalCount int `json:"totalCount"`
} `json:"policiesSearch"`
} `json:"alerts"`
} `json:"account"`
} `json:"actor"`
}
type alertQueryPolicyCreateResponse struct {
AlertsPolicy AlertsPolicy `json:"alertsPolicyCreate"`
}
type alertQueryPolicyUpdateResponse struct {
AlertsPolicy AlertsPolicy `json:"alertsPolicyUpdate"`
}
type alertQueryPolicyResponse struct {
Actor struct {
Account struct {
Alerts struct {
Policy AlertsPolicy `json:"policy"`
} `json:"alerts"`
} `json:"account"`
} `json:"actor"`
}
type alertQueryPolicyDeleteRespose struct {
AlertsPolicyDelete struct {
ID int `json:"id,string"`
} `json:"alertsPolicyDelete"`
}
const (
graphqlAlertPolicyFields = `
id
name
incidentPreference
accountId
`
alertPolicyQueryPolicy = `query($accountID: Int!, $policyID: ID!) {
actor {
account(id: $accountID) {
alerts {
policy(id: $policyID) {` + graphqlAlertPolicyFields + `
}
}
}
}
}`
alertsPolicyQuerySearch = `query($accountID: Int!, $cursor: String, $criteria: AlertsPoliciesSearchCriteriaInput) {
actor {
account(id: $accountID) {
alerts {
policiesSearch(cursor: $cursor, searchCriteria: $criteria) {
nextCursor
totalCount
policies {
accountId
id
incidentPreference
name
}
}
}
}
}
}`
alertsPolicyCreatePolicy = `mutation CreatePolicy($accountID: Int!, $policy: AlertsPolicyInput!){
alertsPolicyCreate(accountId: $accountID, policy: $policy) {` + graphqlAlertPolicyFields + `
} }`
alertsPolicyUpdatePolicy = `mutation UpdatePolicy($accountID: Int!, $policyID: ID!, $policy: AlertsPolicyUpdateInput!){
alertsPolicyUpdate(accountId: $accountID, id: $policyID, policy: $policy) {` + graphqlAlertPolicyFields + `
}
}`
alertPolicyDeletePolicy = `mutation DeletePolicy($accountID: Int!, $policyID: ID!){
alertsPolicyDelete(accountId: $accountID, id: $policyID) {
id
} }`
)