forked from louy/terraform-provider-uptimerobot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
alert_contact.go
207 lines (176 loc) · 4.81 KB
/
alert_contact.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
package uptimerobotapi
import (
"encoding/json"
"errors"
"fmt"
"net/url"
)
// maximum pagination depth to allow (10*50=500 entries)
const page_limit = 10
var alertContactType = map[string]int{
"sms": 1,
"e-mail": 2,
"twitter": 3,
"twitter-dm": 3,
"boxcar": 4,
"web-hook": 5,
"webhook": 5,
"pushbullet": 6,
"zapier": 7,
"pro-sms": 8,
"pushover": 9,
"slack": 11,
"voice-call": 14,
"splunk": 15,
"pagerduty": 16,
"opsgenie": 17,
"telegram": 18,
"ms-teams": 20,
"google-chat": 21,
"hangouts": 21,
"discord": 23,
}
var AlertContactType = mapKeys(alertContactType)
var alertContactStatus = map[string]int{
"not activated": 0,
"paused": 1,
"active": 2,
}
var AlertContactStatus = mapKeys(alertContactStatus)
type AlertContact struct {
ID string `json:"id"`
FriendlyName string `json:"friendly_name"`
Value string `json:"value"`
Type string
Status string
}
func (client UptimeRobotApiClient) GetAlertContacts() (acs []AlertContact, err error) {
data := url.Values{}
var total float64
for i := 0; i < page_limit; i++ {
body, err := client.MakeCall(
"getAlertContacts",
data.Encode(),
)
if err != nil {
return nil, err
}
alertcontacts, ok := body["alert_contacts"].([]interface{})
if !ok {
j, _ := json.Marshal(body)
err = errors.New("Unknown response from the server: " + string(j))
return nil, err
}
for _, i := range alertcontacts {
alertcontact := i.(map[string]interface{})
id := alertcontact["id"].(string)
friendlyName := alertcontact["friendly_name"].(string)
value := ""
if alertcontact["value"] != nil {
value = alertcontact["value"].(string)
}
ac := AlertContact{
id,
friendlyName,
value,
intToString(alertContactType, int(alertcontact["type"].(float64))),
intToString(alertContactStatus, int(alertcontact["status"].(float64))),
}
acs = append(acs, ac)
}
total = body["total"].(float64)
if float64(len(acs)) == total {
break
}
}
if float64(len(acs)) != total {
err = fmt.Errorf("Hitting pagination limit of: %d", page_limit)
}
return
}
func (client UptimeRobotApiClient) GetAlertContact(id string) (ac AlertContact, err error) {
ac.ID = id
data := url.Values{}
data.Add("alert_contacts", id)
body, err := client.MakeCall(
"getAlertContacts",
data.Encode(),
)
if err != nil {
return
}
alertcontacts, ok := body["alert_contacts"].([]interface{})
if !ok {
j, _ := json.Marshal(body)
err = errors.New("Unknown response from the server: " + string(j))
return
}
alertcontact := alertcontacts[0].(map[string]interface{})
ac.FriendlyName = alertcontact["friendly_name"].(string)
ac.Value = alertcontact["value"].(string)
ac.Type = intToString(alertContactType, int(alertcontact["type"].(float64)))
ac.Status = intToString(alertContactStatus, int(alertcontact["status"].(float64)))
return
}
type AlertContactCreateRequest struct {
FriendlyName string
Type string
Value string
}
func (client UptimeRobotApiClient) CreateAlertContact(req AlertContactCreateRequest) (ac AlertContact, err error) {
data := url.Values{}
data.Add("friendly_name", req.FriendlyName)
data.Add("type", fmt.Sprintf("%d", alertContactType[req.Type]))
data.Add("value", req.Value)
body, err := client.MakeCall(
"newAlertContact",
data.Encode(),
)
if err != nil {
return
}
alertcontact, ok := body["alertcontact"].(map[string]interface{})
if !ok {
j, _ := json.Marshal(body)
err = errors.New("Unknown response from the server: " + string(j))
return
}
// The alert contact ID is a string value according to API docs but is
// returned as a integer value by the newAlertContact API JSON. In other
// places the API does correctly handle it as a string value.
// The difference made by it being a string is that a zero prefix to the ID // number is preserved. A zero prefixed alert contact ID is thus far only
// been observed on the default alert contact (created at account creation).
// https://github.com/louy/terraform-provider-uptimerobot/pull/21
return client.GetAlertContact(fmt.Sprintf("%.0f", alertcontact["id"].(float64)))
}
func (client UptimeRobotApiClient) DeleteAlertContact(id string) (err error) {
data := url.Values{}
data.Add("id", id)
_, err = client.MakeCall(
"deleteAlertContact",
data.Encode(),
)
if err != nil {
return
}
return
}
type AlertContactUpdateRequest struct {
ID string
FriendlyName string
Value string
}
func (client UptimeRobotApiClient) UpdateAlertContact(req AlertContactUpdateRequest) (err error) {
data := url.Values{}
data.Add("id", req.ID)
data.Add("friendly_name", req.FriendlyName)
data.Add("value", req.Value)
_, err = client.MakeCall(
"editAlertContact",
data.Encode(),
)
if err != nil {
return
}
return
}