This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
alert_synthetics_conditions.go
121 lines (93 loc) · 3.23 KB
/
alert_synthetics_conditions.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
package api
import (
"fmt"
"net/url"
"strconv"
)
func (c *Client) queryAlertSyntheticsConditions(policyID int) ([]AlertSyntheticsCondition, error) {
conditions := []AlertSyntheticsCondition{}
reqURL, err := url.Parse("/alerts_synthetics_conditions.json")
if err != nil {
return nil, err
}
qs := reqURL.Query()
qs.Set("policy_id", strconv.Itoa(policyID))
reqURL.RawQuery = qs.Encode()
nextPath := reqURL.String()
for nextPath != "" {
resp := struct {
SyntheticsConditions []AlertSyntheticsCondition `json:"synthetics_conditions,omitempty"`
}{}
nextPath, err = c.Do("GET", nextPath, nil, &resp)
if err != nil {
return nil, err
}
for _, c := range resp.SyntheticsConditions {
c.PolicyID = policyID
}
conditions = append(conditions, resp.SyntheticsConditions...)
}
return conditions, nil
}
// GetAlertSyntheticsCondition gets information about a Synthetics alert condition given an ID and policy ID.
func (c *Client) GetAlertSyntheticsCondition(policyID int, id int) (*AlertSyntheticsCondition, error) {
conditions, err := c.queryAlertSyntheticsConditions(policyID)
if err != nil {
return nil, err
}
for _, condition := range conditions {
if condition.ID == id {
return &condition, nil
}
}
return nil, ErrNotFound
}
// ListAlertSyntheticsConditions returns Synthetics alert conditions for the specified policy.
func (c *Client) ListAlertSyntheticsConditions(policyID int) ([]AlertSyntheticsCondition, error) {
return c.queryAlertSyntheticsConditions(policyID)
}
// CreateAlertSyntheticsCondition creates an Synthetics alert condition given the passed configuration.
func (c *Client) CreateAlertSyntheticsCondition(condition AlertSyntheticsCondition) (*AlertSyntheticsCondition, error) {
policyID := condition.PolicyID
req := struct {
Condition AlertSyntheticsCondition `json:"synthetics_condition"`
}{
Condition: condition,
}
resp := struct {
Condition AlertSyntheticsCondition `json:"synthetics_condition,omitempty"`
}{}
u := &url.URL{Path: fmt.Sprintf("/alerts_synthetics_conditions/policies/%v.json", policyID)}
_, err := c.Do("POST", u.String(), req, &resp)
if err != nil {
return nil, err
}
resp.Condition.PolicyID = policyID
return &resp.Condition, nil
}
// UpdateAlertSyntheticsCondition updates a Synthetics alert condition with the specified changes.
func (c *Client) UpdateAlertSyntheticsCondition(condition AlertSyntheticsCondition) (*AlertSyntheticsCondition, error) {
policyID := condition.PolicyID
id := condition.ID
req := struct {
Condition AlertSyntheticsCondition `json:"synthetics_condition"`
}{
Condition: condition,
}
resp := struct {
Condition AlertSyntheticsCondition `json:"synthetics_condition,omitempty"`
}{}
u := &url.URL{Path: fmt.Sprintf("/alerts_synthetics_conditions/%v.json", id)}
_, err := c.Do("PUT", u.String(), req, &resp)
if err != nil {
return nil, err
}
resp.Condition.PolicyID = policyID
return &resp.Condition, nil
}
// DeleteAlertSyntheticsCondition removes the Synthetics alert condition given the specified ID and policy ID.
func (c *Client) DeleteAlertSyntheticsCondition(policyID int, id int) error {
u := &url.URL{Path: fmt.Sprintf("/alerts_synthetics_conditions/%v.json", id)}
_, err := c.Do("DELETE", u.String(), nil, nil)
return err
}