-
Notifications
You must be signed in to change notification settings - Fork 219
/
resource_alerting_contact_point.go
475 lines (419 loc) · 14.1 KB
/
resource_alerting_contact_point.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package grafana
import (
"context"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/go-openapi/runtime"
"github.com/grafana/grafana-openapi-client-go/client/provisioning"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/grafana/terraform-provider-grafana/v2/internal/common"
)
var (
provenanceDisabled = "disabled"
notifiers = []notifier{
alertmanagerNotifier{},
dingDingNotifier{},
discordNotifier{},
emailNotifier{},
googleChatNotifier{},
kafkaNotifier{},
lineNotifier{},
oncallNotifier{},
opsGenieNotifier{},
pagerDutyNotifier{},
pushoverNotifier{},
sensugoNotifier{},
slackNotifier{},
snsNotifier{},
teamsNotifier{},
telegramNotifier{},
threemaNotifier{},
victorOpsNotifier{},
webexNotifier{},
webhookNotifier{},
wecomNotifier{},
}
)
func resourceContactPoint() *common.Resource {
resource := &schema.Resource{
Description: `
Manages Grafana Alerting contact points.
* [Official documentation](https://grafana.com/docs/grafana/next/alerting/fundamentals/notifications/contact-points/)
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/alerting_provisioning/#contact-points)
This resource requires Grafana 9.1.0 or later.
`,
CreateContext: common.WithAlertingMutex[schema.CreateContextFunc](updateContactPoint),
ReadContext: readContactPoint,
UpdateContext: common.WithAlertingMutex[schema.UpdateContextFunc](updateContactPoint),
DeleteContext: common.WithAlertingMutex[schema.DeleteContextFunc](deleteContactPoint),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
"org_id": orgIDAttribute(),
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
Description: "The name of the contact point.",
},
"disable_provenance": {
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true, // Can't modify provenance on contact points
Description: "Allow modifying the contact point from other sources than Terraform or the Grafana API.",
},
},
}
// Build list of available notifier fields, at least one has to be specified
notifierFields := make([]string, len(notifiers))
for i, n := range notifiers {
notifierFields[i] = n.meta().field
}
for _, n := range notifiers {
resource.Schema[n.meta().field] = &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Description: n.meta().desc,
Elem: n.schema(),
AtLeastOneOf: notifierFields,
}
}
return common.NewLegacySDKResource(
"grafana_contact_point",
orgResourceIDString("name"),
resource,
)
}
// TODO: Fix lister
// .WithLister(listerFunction(listContactPoints))
// func listContactPoints(ctx context.Context, client *goapi.GrafanaHTTPAPI, data *ListerData) ([]string, error) {
// orgIDs, err := data.OrgIDs(client)
// if err != nil {
// return nil, err
// }
// idMap := map[string]bool{}
// for _, orgID := range orgIDs {
// client = client.Clone().WithOrgID(orgID)
// resp, err := client.Provisioning.GetContactpoints(provisioning.NewGetContactpointsParams())
// if err != nil {
// return nil, err
// }
// for _, contactPoint := range resp.Payload {
// idMap[MakeOrgResourceID(orgID, contactPoint.Name)] = true
// }
// }
// var ids []string
// for id := range idMap {
// ids = append(ids, id)
// }
// return ids, nil
// }
func readContactPoint(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, orgID, name := OAPIClientFromExistingOrgResource(meta, data.Id())
// First, try to fetch the contact point by name.
// If that fails, try to fetch it by the UID of its notifiers.
resp, err := client.Provisioning.GetContactpoints(provisioning.NewGetContactpointsParams().WithName(&name))
if err != nil {
return diag.FromErr(err)
}
points := resp.Payload
if len(points) == 0 && !strings.Contains(name, ":") {
// If the contact point was not found by name, try to fetch it by UID.
// This is a deprecated ID format (uid;uid2;uid3)
// TODO: Remove on the next major version
uidsMap := map[string]bool{}
for _, uid := range strings.Split(data.Id(), ";") {
uidsMap[uid] = false
}
resp, err := client.Provisioning.GetContactpoints(provisioning.NewGetContactpointsParams())
if err != nil {
return diag.FromErr(err)
}
for i, p := range resp.Payload {
if _, ok := uidsMap[p.UID]; !ok {
continue
}
uidsMap[p.UID] = true
points = append(points, p)
if i > 0 && p.Name != points[0].Name {
return diag.FromErr(fmt.Errorf("contact point with UID %s has a different name (%s) than the contact point with UID %s (%s)", p.UID, p.Name, points[0].UID, points[0].Name))
}
}
for uid, found := range uidsMap {
if !found {
// Since this is an import, all UIDs should exist
return diag.FromErr(fmt.Errorf("contact point with UID %s was not found", uid))
}
}
}
if len(points) == 0 {
return common.WarnMissing("contact point", data)
}
if err := packContactPoints(points, data); err != nil {
return diag.FromErr(err)
}
data.Set("org_id", strconv.FormatInt(orgID, 10))
data.SetId(MakeOrgResourceID(orgID, points[0].Name))
return nil
}
func updateContactPoint(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, orgID := OAPIClientFromNewOrgResource(meta, data)
ps := unpackContactPoints(data)
// Update + create notifiers
for i := range ps {
p := ps[i]
if p.deleted { // We'll handle deletions later
continue
}
var uid string
if uid = p.tfState["uid"].(string); uid != "" {
// If the contact point already has a UID, update it.
params := provisioning.NewPutContactpointParams().WithUID(uid).WithBody(p.gfState)
if data.Get("disable_provenance").(bool) {
params.SetXDisableProvenance(&provenanceDisabled)
}
if _, err := client.Provisioning.PutContactpoint(params); err != nil {
return diag.FromErr(err)
}
} else {
// If the contact point does not have a UID, create it.
// Retry if the API returns 500 because it may be that the alertmanager is not ready in the org yet.
// The alertmanager is provisioned asynchronously when the org is created.
err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
params := provisioning.NewPostContactpointsParams().WithBody(p.gfState)
if data.Get("disable_provenance").(bool) {
params.SetXDisableProvenance(&provenanceDisabled)
}
resp, err := client.Provisioning.PostContactpoints(params)
if orgID > 1 && err != nil && err.(*runtime.APIError).IsCode(500) {
return retry.RetryableError(err)
} else if err != nil {
return retry.NonRetryableError(err)
}
uid = resp.Payload.UID
return nil
})
if err != nil {
return diag.FromErr(err)
}
}
// Since this is a new resource, the proposed state won't have a UID.
// We need the UID so that we can later associate it with the config returned in the api response.
ps[i].tfState["uid"] = uid
}
// Delete notifiers
for _, p := range ps {
if !p.deleted {
continue
}
uid := p.tfState["uid"].(string)
// If the contact point is not in the proposed state, delete it.
if _, err := client.Provisioning.DeleteContactpoints(uid); err != nil {
return diag.Errorf("failed to remove contact point notifier with UID %s from contact point %s: %v", uid, data.Id(), err)
}
continue
}
data.SetId(MakeOrgResourceID(orgID, data.Get("name").(string)))
return readContactPoint(ctx, data, meta)
}
func deleteContactPoint(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, _, name := OAPIClientFromExistingOrgResource(meta, data.Id())
resp, err := client.Provisioning.GetContactpoints(provisioning.NewGetContactpointsParams().WithName(&name))
if err, shouldReturn := common.CheckReadError("contact point", data, err); shouldReturn {
return err
}
for _, cp := range resp.Payload {
if _, err := client.Provisioning.DeleteContactpoints(cp.UID); err != nil {
return diag.FromErr(err)
}
}
return nil
}
// unpackContactPoints unpacks the contact points from the Terraform state.
// It returns a slice of statePairs, which contain the Terraform state and the Grafana state for each contact point.
// It also tracks receivers that should be deleted. There are two cases where a receiver should be deleted:
// - The receiver is present in the "new" part of the diff, but all fields are zeroed out (except UID).
// - The receiver is present in the "old" part of the diff, but not in the "new" part.
func unpackContactPoints(data *schema.ResourceData) []statePair {
result := make([]statePair, 0)
name := data.Get("name").(string)
for _, n := range notifiers {
oldPoints, newPoints := data.GetChange(n.meta().field)
oldPointsList := oldPoints.(*schema.Set).List()
newPointsList := newPoints.(*schema.Set).List()
if len(oldPointsList) == 0 && len(newPointsList) == 0 {
continue
}
processedUIDs := map[string]bool{}
for _, p := range newPointsList {
// Checking if the point/receiver should be deleted
// If all fields are zeroed out, except UID, then the receiver should be deleted
deleted := false
pointMap := p.(map[string]interface{})
if uid, ok := pointMap["uid"]; ok && uid != "" {
deleted = true
processedUIDs[uid.(string)] = true
}
for fieldName, fieldSchema := range n.schema().Schema {
if !fieldSchema.Computed && fieldSchema.Required && !reflect.ValueOf(pointMap[fieldName]).IsZero() {
deleted = false
break
}
}
// Add the point/receiver to the result
// If it's not deleted, it will either be created or updated
result = append(result, statePair{
tfState: pointMap,
gfState: unpackPointConfig(n, p, name),
deleted: deleted,
})
}
// Checking if the point/receiver should be deleted
// If the point is not present in the "new" part of the diff, but is present in the "old" part, then the receiver should be deleted
for _, p := range oldPointsList {
pointMap := p.(map[string]interface{})
if uid, ok := pointMap["uid"]; ok && uid != "" && !processedUIDs[uid.(string)] {
result = append(result, statePair{
tfState: p.(map[string]interface{}),
gfState: nil,
deleted: true,
})
}
}
}
return result
}
func unpackPointConfig(n notifier, data interface{}, name string) *models.EmbeddedContactPoint {
pt := n.unpack(data, name)
settings := pt.Settings.(map[string]interface{})
// Treat settings like `omitempty`. Workaround for versions affected by https://github.com/grafana/grafana/issues/55139
for k, v := range settings {
if v == "" {
delete(settings, k)
}
}
return pt
}
func packContactPoints(ps []*models.EmbeddedContactPoint, data *schema.ResourceData) error {
pointsPerNotifier := map[notifier][]interface{}{}
disableProvenance := true
for _, p := range ps {
data.Set("name", p.Name)
if p.Provenance != "" {
disableProvenance = false
}
for _, n := range notifiers {
if *p.Type == n.meta().typeStr {
packed, err := n.pack(p, data)
if err != nil {
return err
}
pointsPerNotifier[n] = append(pointsPerNotifier[n], packed)
continue
}
}
}
data.Set("disable_provenance", disableProvenance)
for n, pts := range pointsPerNotifier {
data.Set(n.meta().field, pts)
}
return nil
}
func unpackCommonNotifierFields(raw map[string]interface{}) (string, bool, map[string]interface{}) {
return raw["uid"].(string), raw["disable_resolve_message"].(bool), raw["settings"].(map[string]interface{})
}
func packCommonNotifierFields(p *models.EmbeddedContactPoint) map[string]interface{} {
return map[string]interface{}{
"uid": p.UID,
"disable_resolve_message": p.DisableResolveMessage,
}
}
func packSettings(p *models.EmbeddedContactPoint) map[string]interface{} {
settings := map[string]interface{}{}
for k, v := range p.Settings.(map[string]interface{}) {
settings[k] = fmt.Sprintf("%s", v)
}
return settings
}
func commonNotifierResource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"uid": {
Type: schema.TypeString,
Computed: true,
Description: "The UID of the contact point.",
},
"disable_resolve_message": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether to disable sending resolve messages.",
},
"settings": {
Type: schema.TypeMap,
Optional: true,
Sensitive: true,
Default: map[string]interface{}{},
Description: "Additional custom properties to attach to the notifier.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
type notifier interface {
meta() notifierMeta
schema() *schema.Resource
pack(p *models.EmbeddedContactPoint, data *schema.ResourceData) (interface{}, error)
unpack(raw interface{}, name string) *models.EmbeddedContactPoint
}
type notifierMeta struct {
field string
typeStr string
desc string
secureFields []string
}
type statePair struct {
tfState map[string]interface{}
gfState *models.EmbeddedContactPoint
deleted bool
}
func packNotifierStringField(gfSettings, tfSettings *map[string]interface{}, gfKey, tfKey string) {
if v, ok := (*gfSettings)[gfKey]; ok && v != nil {
(*tfSettings)[tfKey] = v.(string)
delete(*gfSettings, gfKey)
}
}
func packSecureFields(tfSettings, state map[string]interface{}, secureFields []string) {
for _, tfKey := range secureFields {
if v, ok := state[tfKey]; ok && v != nil {
tfSettings[tfKey] = v.(string)
}
}
}
func unpackNotifierStringField(tfSettings, gfSettings *map[string]interface{}, tfKey, gfKey string) {
if v, ok := (*tfSettings)[tfKey]; ok && v != nil {
(*gfSettings)[gfKey] = v.(string)
}
}
func getNotifierConfigFromStateWithUID(data *schema.ResourceData, n notifier, uid string) map[string]interface{} {
if points, ok := data.GetOk(n.meta().field); ok {
for _, pt := range points.(*schema.Set).List() {
config := pt.(map[string]interface{})
if config["uid"] == uid {
return config
}
}
}
return nil
}