This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
forked from newrelic/terraform-provider-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 5
/
resource_newrelic_alert_channel.go
304 lines (283 loc) · 9.53 KB
/
resource_newrelic_alert_channel.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
package newrelic
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/newrelic/newrelic-client-go/pkg/errors"
)
var alertChannelTypes = map[string][]string{
"email": {
"include_json_attachment",
"recipients",
},
"opsgenie": {
"api_key",
"recipients",
"tags",
"teams",
},
"pagerduty": {
"service_key",
},
"slack": {
"channel",
"url",
},
"user": {
"user_id",
},
"victorops": {
"key",
"route_key",
},
"webhook": {
"auth_password",
"auth_username",
"base_url",
"headers",
"payload_type",
"payload",
},
}
func resourceNewRelicAlertChannel() *schema.Resource {
validAlertChannelTypes := make([]string, 0, len(alertChannelTypes))
for k := range alertChannelTypes {
validAlertChannelTypes = append(validAlertChannelTypes, k)
}
return &schema.Resource{
Create: resourceNewRelicAlertChannelCreate,
Read: resourceNewRelicAlertChannelRead,
// Update: Not currently supported in API
Delete: resourceNewRelicAlertChannelDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "(Required) The name of the channel.",
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(validAlertChannelTypes, false),
Description: fmt.Sprintf("(Required) The type of channel. One of: (%s).", strings.Join(validAlertChannelTypes, ", ")),
},
"config": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Description: "The configuration block for the alert channel.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"api_key": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ForceNew: true,
Description: "The API key for integrating with OpsGenie.",
},
"auth_password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ForceNew: true,
Description: "Specifies an authentication password for use with a channel. Supported by the webhook channel type.",
},
"auth_type": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validation.StringInSlice([]string{"BASIC"}, false),
ForceNew: true,
Description: "Specifies an authentication method for use with a channel. Supported by the webhook channel type. Only HTTP basic authentication is currently supported via the value BASIC.",
},
"auth_username": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Specifies an authentication username for use with a channel. Supported by the webhook channel type.",
},
"base_url": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ForceNew: true,
Description: "The base URL of the webhook destination.",
},
"channel": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The Slack channel to send notifications to.",
},
"headers": {
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Sensitive: true,
ForceNew: true,
ConflictsWith: []string{"config.0.headers_string"},
Description: "A map of key/value pairs that represents extra HTTP headers to be sent along with the webhook payload.",
},
"headers_string": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ForceNew: true,
ConflictsWith: []string{"config.0.headers"},
Description: "Use instead of headers if the desired payload is more complex than a list of key/value pairs (e.g. a set of headers that makes use of nested objects). The value provided should be a valid JSON string with escaped double quotes. Conflicts with headers.",
// Suppress the diff shown if the differences are solely due to whitespace
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return stripWhitespace(old) == stripWhitespace(new)
},
},
"key": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ForceNew: true,
Description: "The key for integrating with VictorOps.",
},
"include_json_attachment": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "0 or 1. Flag for whether or not to attach a JSON document containing information about the associated alert to the email that is sent to recipients.",
},
"payload": {
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Sensitive: true,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"config.0.payload_string"},
Description: "A map of key/value pairs that represents the webhook payload. Must provide payload_type if setting this argument.",
},
"payload_string": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ForceNew: true,
ConflictsWith: []string{"config.0.payload"},
Description: "Use instead of payload if the desired payload is more complex than a list of key/value pairs (e.g. a payload that makes use of nested objects). The value provided should be a valid JSON string with escaped double quotes. Conflicts with payload.",
// Suppress the diff shown if the differences are solely due to whitespace
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return stripWhitespace(old) == stripWhitespace(new)
},
},
"payload_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"application/json", "application/x-www-form-urlencoded"}, false),
Description: "Can either be application/json or application/x-www-form-urlencoded. The payload_type argument is required if payload is set.",
},
"recipients": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "A set of recipients for targeting notifications. Multiple values are comma separated.",
},
"region": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"US", "EU"}, false),
ForceNew: true,
Description: "The data center region to store your data. Valid values are US and EU. Default is US.",
},
"route_key": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ForceNew: true,
Description: "The route key for integrating with VictorOps.",
},
"service_key": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ForceNew: true,
Description: "Specifies the service key for integrating with Pagerduty.",
},
"tags": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "A set of tags for targeting notifications. Multiple values are comma separated.",
},
"teams": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "A set of teams for targeting notifications. Multiple values are comma separated.",
},
"url": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ForceNew: true,
Description: "Your organization's Slack URL.",
},
"user_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The user ID for use with the user channel type.",
},
},
},
},
},
}
}
func resourceNewRelicAlertChannelCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ProviderConfig).NewClient
channel, err := expandAlertChannel(d)
if err != nil {
return err
}
log.Printf("[INFO] Creating New Relic alert channel %s", channel.Name)
channel, err = client.Alerts.CreateChannel(*channel)
if err != nil {
return err
}
d.SetId(strconv.Itoa(channel.ID))
return nil
}
func resourceNewRelicAlertChannelRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ProviderConfig).NewClient
id, err := strconv.ParseInt(d.Id(), 10, 32)
if err != nil {
return err
}
log.Printf("[INFO] Reading New Relic alert channel %v", id)
channel, err := client.Alerts.GetChannel(int(id))
if err != nil {
if _, ok := err.(*errors.NotFound); ok {
d.SetId("")
return nil
}
return err
}
return flattenAlertChannel(channel, d)
}
func resourceNewRelicAlertChannelDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ProviderConfig).NewClient
id, err := strconv.ParseInt(d.Id(), 10, 32)
if err != nil {
return err
}
log.Printf("[INFO] Deleting New Relic alert channel %v", id)
if _, err := client.Alerts.DeleteChannel(int(id)); err != nil {
return err
}
return nil
}