-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathresource_datadog_monitor.go
1430 lines (1340 loc) · 55.2 KB
/
resource_datadog_monitor.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package datadog
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"sort"
"strconv"
"strings"
"time"
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/validators"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const defaultNoDataTimeframeMinutes = 10
var retryTimeout = time.Minute
func resourceDatadogMonitor() *schema.Resource {
return &schema.Resource{
Description: "Provides a Datadog monitor resource. This can be used to create and manage Datadog monitors.",
CreateContext: resourceDatadogMonitorCreate,
ReadContext: resourceDatadogMonitorRead,
UpdateContext: resourceDatadogMonitorUpdate,
DeleteContext: resourceDatadogMonitorDelete,
CustomizeDiff: customdiff.All(resourceDatadogMonitorCustomizeDiff, tagDiff),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
SchemaFunc: func() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Description: "Name of Datadog monitor.",
Type: schema.TypeString,
Required: true,
},
"message": {
Description: "A message to include with notifications for this monitor.\n\nEmail notifications can be sent to specific users by using the same `@username` notation as events.",
Type: schema.TypeString,
Required: true,
StateFunc: func(val interface{}) string {
return strings.TrimSpace(val.(string))
},
},
"escalation_message": {
Description: "A message to include with a re-notification. Supports the `@username` notification allowed elsewhere.",
Type: schema.TypeString,
Optional: true,
StateFunc: func(val interface{}) string {
return strings.TrimSpace(val.(string))
},
},
"query": {
Description: "The monitor query to notify on. Note this is not the same query you see in the UI and the syntax is different depending on the monitor type, please see the [API Reference](https://docs.datadoghq.com/api/v1/monitors/#create-a-monitor) for details. `terraform plan` will validate query contents unless `validate` is set to `false`.\n\n**Note:** APM latency data is now available as Distribution Metrics. Existing monitors have been migrated automatically but all terraformed monitors can still use the existing metrics. We strongly recommend updating monitor definitions to query the new metrics. To learn more, or to see examples of how to update your terraform definitions to utilize the new distribution metrics, see the [detailed doc](https://docs.datadoghq.com/tracing/guide/ddsketch_trace_metrics/).",
Type: schema.TypeString,
Required: true,
StateFunc: func(val interface{}) string {
return strings.TrimSpace(val.(string))
},
},
"type": {
Description: "The type of the monitor. The mapping from these types to the types found in the Datadog Web UI can be found in the Datadog API [documentation page](https://docs.datadoghq.com/api/v1/monitors/#create-a-monitor). Note: The monitor type cannot be changed after a monitor is created.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewMonitorTypeFromValue),
// Datadog API quirk, see https://github.com/hashicorp/terraform/issues/13784
DiffSuppressFunc: func(k, oldVal, newVal string, d *schema.ResourceData) bool {
if (oldVal == "query alert" && newVal == "metric alert") ||
(oldVal == "metric alert" && newVal == "query alert") {
log.Printf("[DEBUG] Monitor '%s' got a '%s' response for an expected '%s' type. Suppressing change.", d.Get("name"), newVal, oldVal)
return true
}
return newVal == oldVal
},
},
"priority": {
Description: "Integer from 1 (high) to 5 (low) indicating alert severity.",
Type: schema.TypeString,
Optional: true,
},
// Options
"monitor_thresholds": {
Description: "Alert thresholds of the monitor.",
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ok": {
Description: "The monitor `OK` threshold. Only supported in monitor type `service check`. Must be a number.",
Type: schema.TypeString,
ValidateFunc: validators.ValidateFloatString,
Optional: true,
DiffSuppressFunc: func(k, oldVal, newVal string, d *schema.ResourceData) bool {
monitorType := d.Get("type").(string)
return monitorType != string(datadogV1.MONITORTYPE_SERVICE_CHECK)
},
},
"warning": {
Description: "The monitor `WARNING` threshold. Must be a number.",
Type: schema.TypeString,
ValidateFunc: validators.ValidateFloatString,
Optional: true,
},
"critical": {
Description: "The monitor `CRITICAL` threshold. Must be a number.",
Type: schema.TypeString,
ValidateFunc: validators.ValidateFloatString,
Optional: true,
},
"unknown": {
Description: "The monitor `UNKNOWN` threshold. Only supported in monitor type `service check`. Must be a number.",
Type: schema.TypeString,
ValidateFunc: validators.ValidateFloatString,
Optional: true,
DiffSuppressFunc: func(k, oldVal, newVal string, d *schema.ResourceData) bool {
monitorType := d.Get("type").(string)
return monitorType != string(datadogV1.MONITORTYPE_SERVICE_CHECK)
},
},
"warning_recovery": {
Description: "The monitor `WARNING` recovery threshold. Must be a number.",
Type: schema.TypeString,
ValidateFunc: validators.ValidateFloatString,
Optional: true,
},
"critical_recovery": {
Description: "The monitor `CRITICAL` recovery threshold. Must be a number.",
Type: schema.TypeString,
ValidateFunc: validators.ValidateFloatString,
Optional: true,
},
},
},
DiffSuppressFunc: suppressDataDogFloatIntDiff,
},
"monitor_threshold_windows": {
Description: "A mapping containing `recovery_window` and `trigger_window` values, e.g. `last_15m` . Can only be used for, and are required for, anomaly monitors.",
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"recovery_window": {
Description: "Describes how long an anomalous metric must be normal before the alert recovers.",
Type: schema.TypeString,
Optional: true,
},
"trigger_window": {
Description: "Describes how long a metric must be anomalous before an alert triggers.",
Type: schema.TypeString,
Optional: true,
},
},
},
},
"notify_no_data": {
Description: "A boolean indicating whether this monitor will notify when data stops reporting.",
Type: schema.TypeBool,
Optional: true,
Default: false,
ConflictsWith: []string{"on_missing_data"},
},
"on_missing_data": {
Description: "Controls how groups or monitors are treated if an evaluation does not return any data points. The default option results in different behavior depending on the monitor query type. For monitors using `Count` queries, an empty monitor evaluation is treated as 0 and is compared to the threshold conditions. For monitors using any query type other than `Count`, for example `Gauge`, `Measure`, or `Rate`, the monitor shows the last known status. This option is not available for Service Check, Composite, or SLO monitors. Valid values are: `show_no_data`, `show_and_notify_no_data`, `resolve`, and `default`.",
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"notify_no_data", "no_data_timeframe"},
},
"group_retention_duration": {
Description: "The time span after which groups with missing data are dropped from the monitor state. The minimum value is one hour, and the maximum value is 72 hours. Example values are: 60m, 1h, and 2d. This option is only available for APM Trace Analytics, Audit Trail, CI, Error Tracking, Event, Logs, and RUM monitors.",
Type: schema.TypeString,
Optional: true,
},
// We only set new_group_delay in the monitor API payload if it is nonzero
// because the SDKv2 terraform plugin API prevents unsetting new_group_delay
// in updateMonitorState, so we can't reliably distinguish between new_group_delay
// being unset (null) or set to zero.
// Note that "new_group_delay overrides new_host_delay if it is set to a nonzero value"
// refers to this terraform resource. In the API, setting new_group_delay
// to any value, including zero, causes it to override new_host_delay.
"new_group_delay": {
Description: "The time (in seconds) to skip evaluations for new groups.\n\n`new_group_delay` overrides `new_host_delay` if it is set to a nonzero value.",
Type: schema.TypeInt,
Optional: true,
},
"new_host_delay": {
// Removing the default requires removing the default in the API as well (possibly only for
// terraform user agents)
Description: "**Deprecated**. See `new_group_delay`. Time (in seconds) to allow a host to boot and applications to fully start before starting the evaluation of monitor results. Should be a non-negative integer. This value is ignored for simple monitors and monitors not grouped by host. The only case when this should be used is to override the default and set `new_host_delay` to zero for monitors grouped by host.",
Type: schema.TypeInt,
Optional: true,
Default: 300,
Deprecated: "Use `new_group_delay` except when setting `new_host_delay` to zero.",
},
"evaluation_delay": {
Description: "(Only applies to metric alert) Time (in seconds) to delay evaluation, as a non-negative integer.\n\nFor example, if the value is set to `300` (5min), the `timeframe` is set to `last_5m` and the time is 7:00, the monitor will evaluate data from 6:50 to 6:55. This is useful for AWS CloudWatch and other backfilled metrics to ensure the monitor will always have data during evaluation.",
Type: schema.TypeInt,
Computed: true,
Optional: true,
},
"no_data_timeframe": {
Description: "The number of minutes before a monitor will notify when data stops reporting.\n\nWe recommend at least 2x the monitor timeframe for metric alerts or 2 minutes for service checks.",
Type: schema.TypeInt,
Optional: true,
Default: defaultNoDataTimeframeMinutes,
DiffSuppressFunc: func(k, oldVal, newVal string, d *schema.ResourceData) bool {
if !d.Get("notify_no_data").(bool) {
if newVal != oldVal {
log.Printf("[DEBUG] Ignore the no_data_timeframe change of monitor '%s' because notify_no_data is false.", d.Get("name"))
}
return true
}
return newVal == oldVal
},
ConflictsWith: []string{"on_missing_data"},
},
"renotify_interval": {
Description: "The number of minutes after the last notification before a monitor will re-notify on the current status. It will only re-notify if it's not resolved.",
Type: schema.TypeInt,
Optional: true,
},
"renotify_occurrences": {
Description: "The number of re-notification messages that should be sent on the current status.",
Type: schema.TypeInt,
Optional: true,
},
"renotify_statuses": {
Description: "The types of statuses for which re-notification messages should be sent.",
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewMonitorRenotifyStatusTypeFromValue),
},
Optional: true,
},
"notify_audit": {
Description: "A boolean indicating whether tagged users will be notified on changes to this monitor. Defaults to `false`.",
Type: schema.TypeBool,
Optional: true,
},
"timeout_h": {
Description: "The number of hours of the monitor not reporting data before it automatically resolves from a triggered state. The minimum allowed value is 0 hours. The maximum allowed value is 24 hours.",
Type: schema.TypeInt,
Optional: true,
},
"require_full_window": {
Description: "A boolean indicating whether this monitor needs a full window of data before it's evaluated. Datadog strongly recommends you set this to `false` for sparse metrics, otherwise some evaluations may be skipped. If there's a custom_schedule set, `require_full_window` must be false and will be ignored.",
Type: schema.TypeBool,
Optional: true,
Default: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if attr, ok := d.GetOk("scheduling_options"); ok {
scheduling_options_list := attr.([]interface{})
if scheduling_options_map, ok := scheduling_options_list[0].(map[string]interface{}); ok {
custom_schedule_map, custom_schedule_found := scheduling_options_map["custom_schedule"].([]interface{})
if custom_schedule_found && len(custom_schedule_map) > 0 {
return true
}
}
}
return false
},
},
"locked": {
Description: "A boolean indicating whether changes to this monitor should be restricted to the creator or admins. Defaults to `false`.",
Type: schema.TypeBool,
Optional: true,
Deprecated: "Use `restricted_roles`.",
ConflictsWith: []string{"restricted_roles"},
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// if restricted_roles is defined, ignore locked
if _, ok := d.GetOk("restricted_roles"); ok {
return true
}
return false
},
},
"restricted_roles": {
Description: "A list of unique role identifiers to define which roles are allowed to edit the monitor. Editing a monitor includes any updates to the monitor configuration, monitor deletion, and muting of the monitor for any amount of time. Roles unique identifiers can be pulled from the [Roles API](https://docs.datadoghq.com/api/latest/roles/#list-roles) in the `data.id` field.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ConflictsWith: []string{"locked"},
},
"include_tags": {
Description: "A boolean indicating whether notifications from this monitor automatically insert its triggering tags into the title.",
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"tags": {
Description: "A list of tags to associate with your monitor. This can help you categorize and filter monitors in the manage monitors page of the UI. Note: it's not currently possible to filter by these tags when querying via the API",
// we use TypeSet to represent tags, paradoxically to be able to maintain them ordered;
// we order them explicitly in the read/create/update methods of this resource and using
// TypeSet makes Terraform ignore differences in order when creating a plan
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"groupby_simple_monitor": {
Description: "Whether or not to trigger one alert if any source breaches a threshold. This is only used by log monitors. Defaults to `false`.",
Type: schema.TypeBool,
Optional: true,
},
"notify_by": {
Description: "Controls what granularity a monitor alerts on. Only available for monitors with groupings. For instance, a monitor grouped by `cluster`, `namespace`, and `pod` can be configured to only notify on each new `cluster` violating the alert conditions by setting `notify_by` to `['cluster']`. Tags mentioned in `notify_by` must be a subset of the grouping tags in the query. For example, a query grouped by `cluster` and `namespace` cannot notify on `region`. Setting `notify_by` to `[*]` configures the monitor to notify as a simple-alert.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
// since this is only useful for "log alert" type, we don't set a default value
// if we did set it, it would be used for all types; we have to handle this manually
// throughout the code
"enable_logs_sample": {
Description: "A boolean indicating whether or not to include a list of log values which triggered the alert. This is only used by log monitors. Defaults to `false`.",
Type: schema.TypeBool,
Optional: true,
},
"enable_samples": {
Description: "Whether or not a list of samples which triggered the alert is included. This is only used by CI Test and Pipeline monitors.",
Type: schema.TypeBool,
Optional: true,
},
"force_delete": {
Description: "A boolean indicating whether this monitor can be deleted even if it’s referenced by other resources (e.g. SLO, composite monitor).",
Type: schema.TypeBool,
Optional: true,
},
"validate": {
Description: "If set to `false`, skip the validation call done during plan.",
Type: schema.TypeBool,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// This is never sent to the backend, so it should never generate a diff
return true
},
},
"variables": getMonitorFormulaQuerySchema(),
"scheduling_options": {
Description: "Configuration options for scheduling.",
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"evaluation_window": {
Description: "Configuration options for the evaluation window. If `hour_starts` is set, no other fields may be set. Otherwise, `day_starts` and `month_starts` must be set together.",
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"day_starts": {
Description: "The time of the day at which a one day cumulative evaluation window starts. Must be defined in UTC time in `HH:mm` format.",
Type: schema.TypeString,
Optional: true,
},
"month_starts": {
Description: "The day of the month at which a one month cumulative evaluation window starts. Must be a value of 1.",
Type: schema.TypeInt,
Optional: true,
},
"hour_starts": {
Description: "The minute of the hour at which a one hour cumulative evaluation window starts. Must be between 0 and 59.",
Type: schema.TypeInt,
Optional: true,
},
},
},
},
"custom_schedule": {
Description: "Configuration options for the custom schedules. If `start` is omitted, the monitor creation time will be used.",
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"recurrence": {
Description: "A list of recurrence definitions. Length must be 1.",
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"rrule": {
Description: "Must be a valid `rrule`. See API docs for supported fields",
Type: schema.TypeString,
Required: true,
},
"start": {
Description: "Time to start recurrence cycle. Similar to DTSTART. Expected format 'YYYY-MM-DDThh:mm:ss'",
Type: schema.TypeString,
Optional: true,
},
"timezone": {
Description: "'tz database' format. Example: `America/New_York` or `UTC`",
Type: schema.TypeString,
Required: true,
},
},
},
},
},
},
},
},
},
},
"notification_preset_name": {
Description: "Toggles the display of additional content sent in the monitor notification.",
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewMonitorOptionsNotificationPresetsFromValue),
},
}
},
}
}
// Monitor specific schema for formula and functions. Should be a strict
// subset of getFormulaQuerySchema with the appropriate types.
func getMonitorFormulaQuerySchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"event_query": {
Type: schema.TypeList,
Optional: true,
Description: "A timeseries formula and functions events query.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"data_source": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewMonitorFormulaAndFunctionEventsDataSourceFromValue),
Description: "The data source for event platform-based queries.",
},
"search": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
MinItems: 1,
Description: "The search options.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"query": {
Type: schema.TypeString,
Required: true,
Description: "The events search string.",
},
},
},
},
"indexes": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "An array of index names to query in the stream.",
},
"compute": {
Type: schema.TypeList,
Required: true,
Description: "The compute options.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"aggregation": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewMonitorFormulaAndFunctionEventAggregationFromValue),
Description: "The aggregation methods for event platform queries.",
},
"interval": {
Type: schema.TypeInt,
Optional: true,
Description: "A time interval in milliseconds.",
},
"metric": {
Type: schema.TypeString,
Optional: true,
Description: "The measurable attribute to compute.",
},
},
},
},
"group_by": {
Type: schema.TypeList,
Optional: true,
Description: "Group by options.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"facet": {
Type: schema.TypeString,
Required: true,
Description: "The event facet.",
},
"limit": {
Type: schema.TypeInt,
Optional: true,
Description: "The number of groups to return.",
},
"sort": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "The options for sorting group by results.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"aggregation": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewMonitorFormulaAndFunctionEventAggregationFromValue),
Description: "The aggregation methods for the event platform queries.",
},
"metric": {
Type: schema.TypeString,
Optional: true,
Description: "The metric used for sorting group by results.",
},
"order": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewQuerySortOrderFromValue),
Description: "Direction of sort.",
},
},
},
},
},
},
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of query for use in formulas.",
},
},
},
},
"cloud_cost_query": {
Type: schema.TypeList,
Optional: true,
MaxItems: 5,
Description: "The Cloud Cost query using formulas and functions.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"data_source": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewMonitorFormulaAndFunctionCostDataSourceFromValue),
Description: "The data source for cloud cost queries.",
},
"query": {
Type: schema.TypeString,
Required: true,
Description: "The cloud cost query definition.",
},
"aggregator": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewMonitorFormulaAndFunctionCostAggregatorFromValue),
Description: "The aggregation methods available for cloud cost queries.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the query for use in formulas.",
},
},
},
},
},
},
}
}
func buildMonitorStruct(d utils.Resource) (*datadogV1.Monitor, *datadogV1.MonitorUpdateRequest) {
var thresholds datadogV1.MonitorThresholds
if r, ok := d.GetOk("monitor_thresholds.0.ok"); ok {
v, _ := json.Number(r.(string)).Float64()
thresholds.SetOk(v)
}
if r, ok := d.GetOk("monitor_thresholds.0.warning"); ok {
v, _ := json.Number(r.(string)).Float64()
thresholds.SetWarning(v)
}
if r, ok := d.GetOk("monitor_thresholds.0.unknown"); ok {
v, _ := json.Number(r.(string)).Float64()
thresholds.SetUnknown(v)
}
if r, ok := d.GetOk("monitor_thresholds.0.critical"); ok {
v, _ := json.Number(r.(string)).Float64()
thresholds.SetCritical(v)
}
if r, ok := d.GetOk("monitor_thresholds.0.warning_recovery"); ok {
v, _ := json.Number(r.(string)).Float64()
thresholds.SetWarningRecovery(v)
}
if r, ok := d.GetOk("monitor_thresholds.0.critical_recovery"); ok {
v, _ := json.Number(r.(string)).Float64()
thresholds.SetCriticalRecovery(v)
}
var thresholdWindows datadogV1.MonitorThresholdWindowOptions
if r, ok := d.GetOk("monitor_threshold_windows.0.recovery_window"); ok {
thresholdWindows.SetRecoveryWindow(r.(string))
}
if r, ok := d.GetOk("monitor_threshold_windows.0.trigger_window"); ok {
thresholdWindows.SetTriggerWindow(r.(string))
}
o := datadogV1.MonitorOptions{}
hasCustomSchedule := false
if attr, ok := d.GetOk("scheduling_options"); ok {
scheduling_options_list := attr.([]interface{})
if scheduling_options_map, ok := scheduling_options_list[0].(map[string]interface{}); ok {
scheduling_options := datadogV1.NewMonitorOptionsSchedulingOptions()
evaluation_window_list, evaluation_list_found := scheduling_options_map["evaluation_window"].([]interface{})
if evaluation_list_found && len(evaluation_window_list) > 0 {
if evaluation_window_map, evaluation_window_ok := evaluation_window_list[0].(map[string]interface{}); evaluation_window_ok {
evaluation_window := datadogV1.NewMonitorOptionsSchedulingOptionsEvaluationWindow()
day_month_scheduling := false
if day_starts, ok := evaluation_window_map["day_starts"].(string); ok && day_starts != "" {
evaluation_window.SetDayStarts(day_starts)
day_month_scheduling = true
}
if month_starts, ok := evaluation_window_map["month_starts"].(int); ok && month_starts != 0 {
evaluation_window.SetMonthStarts(int32(month_starts))
day_month_scheduling = true
}
if hour_starts, ok := evaluation_window_map["hour_starts"].(int); ok && !day_month_scheduling {
evaluation_window.SetHourStarts(int32(hour_starts))
}
scheduling_options.SetEvaluationWindow(*evaluation_window)
}
}
custom_schedule_map, custom_schedule_found := scheduling_options_map["custom_schedule"].([]interface{})
if custom_schedule_found && len(custom_schedule_map) > 0 {
hasCustomSchedule = true
if recurrences, ok := custom_schedule_map[0].(map[string]interface{})["recurrence"].([]interface{}); ok {
recurrence := datadogV1.NewMonitorOptionsCustomScheduleRecurrence()
firstRecurrence := recurrences[0].(map[string]interface{})
if rrule, ok := firstRecurrence["rrule"].(string); ok {
recurrence.SetRrule(rrule)
}
if start, ok := firstRecurrence["start"].(string); ok && start != "" {
recurrence.SetStart(start)
}
if timezone, ok := firstRecurrence["timezone"].(string); ok {
recurrence.SetTimezone(timezone)
}
newRecurrences := []datadogV1.MonitorOptionsCustomScheduleRecurrence{*recurrence}
custom_schedule := datadogV1.NewMonitorOptionsCustomSchedule()
custom_schedule.SetRecurrences(newRecurrences)
scheduling_options.SetCustomSchedule(*custom_schedule)
}
}
if scheduling_options.HasCustomSchedule() || scheduling_options.HasEvaluationWindow() {
o.SetSchedulingOptions(*scheduling_options)
}
}
}
o.SetThresholds(thresholds)
o.SetIncludeTags(d.Get("include_tags").(bool))
if !hasCustomSchedule {
o.SetNotifyNoData(d.Get("notify_no_data").(bool))
o.SetRequireFullWindow(d.Get("require_full_window").(bool))
} else {
// this has to be done explicitly to override the default
o.SetRequireFullWindow(false)
}
if thresholdWindows.HasRecoveryWindow() || thresholdWindows.HasTriggerWindow() {
o.SetThresholdWindows(thresholdWindows)
}
if attr, ok := d.GetOk("group_retention_duration"); ok {
o.SetGroupRetentionDuration(attr.(string))
}
if attr, ok := d.GetOk("new_group_delay"); ok {
o.SetNewGroupDelay(int64(attr.(int)))
}
// Don't check with GetOk, doesn't work with 0 (we can't do the same for
// new_group_delay because it would always override new_host_delay).
o.SetNewHostDelay(int64(d.Get("new_host_delay").(int)))
if attr, ok := d.GetOk("evaluation_delay"); ok {
o.SetEvaluationDelay(int64(attr.(int)))
}
attr, onMissingDataOk := d.GetOk("on_missing_data")
if onMissingDataOk {
o.SetOnMissingData(datadogV1.OnMissingDataOption(attr.(string)))
}
// no_data_timeframe cannot be combined with on_missing_data. This provider
// defaults no_data_timeframe to 10, so we need this extra logic to exclude
// no_data_timeframe from the monitor definition when on_missing_data is set.
if attr, ok := d.GetOk("no_data_timeframe"); ok && !onMissingDataOk && !hasCustomSchedule {
o.SetNoDataTimeframe(int64(attr.(int)))
}
if attr, ok := d.GetOk("renotify_interval"); ok {
o.SetRenotifyInterval(int64(attr.(int)))
}
if attr, ok := d.GetOk("renotify_occurrences"); ok {
o.SetRenotifyOccurrences(int64(attr.(int)))
}
renotify_statuses := make([]datadogV1.MonitorRenotifyStatusType, 0)
if attr, ok := d.GetOk("renotify_statuses"); ok {
for _, s := range attr.(*schema.Set).List() {
renotify_statuses = append(renotify_statuses, datadogV1.MonitorRenotifyStatusType(s.(string)))
}
o.SetRenotifyStatuses(renotify_statuses)
} else {
o.SetRenotifyStatuses(nil)
}
if attr, ok := d.GetOk("notify_audit"); ok {
o.SetNotifyAudit(attr.(bool))
}
if attr, ok := d.GetOk("timeout_h"); ok {
o.SetTimeoutH(int64(attr.(int)))
}
if attr, ok := d.GetOk("escalation_message"); ok {
o.SetEscalationMessage(attr.(string))
}
if attr, ok := d.GetOk("locked"); ok {
o.SetLocked(attr.(bool))
}
if v, ok := d.GetOk("variables"); ok {
variables := v.([]interface{})
if len(variables) > 0 {
// we always have either zero or one
for _, v := range variables {
m := v.(map[string]interface{})
var monitorVariables []datadogV1.MonitorFormulaAndFunctionQueryDefinition
if query, ok := m["event_query"]; ok {
queries := query.([]interface{})
for _, q := range queries {
monitorVariables = append(monitorVariables, *buildMonitorFormulaAndFunctionEventQuery(q.(map[string]interface{})))
}
}
if query, ok := m["cloud_cost_query"]; ok {
queries := query.([]interface{})
for _, q := range queries {
monitorVariables = append(monitorVariables, *buildMonitorFormulaAndFunctionCloudCostQuery(q.(map[string]interface{})))
}
}
o.SetVariables(monitorVariables)
}
}
}
monitorType := datadogV1.MonitorType(d.Get("type").(string))
if monitorType == datadogV1.MONITORTYPE_LOG_ALERT {
if attr, ok := d.GetOk("enable_logs_sample"); ok {
o.SetEnableLogsSample(attr.(bool))
} else {
o.SetEnableLogsSample(false)
}
if attr, ok := d.GetOk("groupby_simple_monitor"); ok {
o.SetGroupbySimpleMonitor(attr.(bool))
}
}
if monitorType == datadogV1.MONITORTYPE_CI_PIPELINES_ALERT || monitorType == datadogV1.MONITORTYPE_CI_TESTS_ALERT {
if attr, ok := d.GetOk("enable_samples"); ok {
o.SetEnableSamples(attr.(bool))
} else {
o.SetEnableSamples(false)
}
}
if attr, ok := d.GetOk("notify_by"); ok {
notifyBy := make([]string, 0)
for _, s := range attr.(*schema.Set).List() {
notifyBy = append(notifyBy, s.(string))
}
sort.Strings(notifyBy)
o.SetNotifyBy(notifyBy)
}
if attr, ok := d.GetOk("notification_preset_name"); ok {
o.SetNotificationPresetName(datadogV1.MonitorOptionsNotificationPresets(attr.(string)))
}
m := datadogV1.NewMonitor(d.Get("query").(string), monitorType)
m.SetName(d.Get("name").(string))
m.SetMessage(d.Get("message").(string))
m.SetOptions(o)
u := datadogV1.NewMonitorUpdateRequest()
u.SetType(monitorType)
u.SetQuery(d.Get("query").(string))
u.SetName(d.Get("name").(string))
u.SetMessage(d.Get("message").(string))
u.SetOptions(o)
if attr, ok := d.GetOk("priority"); ok {
x, _ := strconv.ParseInt(attr.(string), 10, 64)
m.SetPriority(x)
u.SetPriority(x)
} else {
m.SetPriorityNil()
u.SetPriorityNil()
}
var roles []string
if attr, ok := d.GetOk("restricted_roles"); ok {
for _, r := range attr.(*schema.Set).List() {
roles = append(roles, r.(string))
}
sort.Strings(roles)
}
m.SetRestrictedRoles(roles)
u.SetRestrictedRoles(roles)
tags := make([]string, 0)
if attr, ok := d.GetOk("tags"); ok {
for _, s := range attr.(*schema.Set).List() {
tags = append(tags, s.(string))
}
sort.Strings(tags)
}
m.SetTags(tags)
u.SetTags(tags)
return m, u
}
func buildMonitorFormulaAndFunctionEventQuery(data map[string]interface{}) *datadogV1.MonitorFormulaAndFunctionQueryDefinition {
dataSource := datadogV1.MonitorFormulaAndFunctionEventsDataSource(data["data_source"].(string))
computeList := data["compute"].([]interface{})
computeMap := computeList[0].(map[string]interface{})
aggregation := datadogV1.MonitorFormulaAndFunctionEventAggregation(computeMap["aggregation"].(string))
compute := datadogV1.NewMonitorFormulaAndFunctionEventQueryDefinitionCompute(aggregation)
if interval, ok := computeMap["interval"].(int); ok && interval != 0 {
compute.SetInterval(int64(interval))
}
if metric, ok := computeMap["metric"].(string); ok && len(metric) > 0 {
compute.SetMetric(metric)
}
eventQuery := datadogV1.NewMonitorFormulaAndFunctionEventQueryDefinition(*compute, dataSource, data["name"].(string))
eventQueryIndexes := data["indexes"].([]interface{})
indexes := make([]string, len(eventQueryIndexes))
for i, index := range eventQueryIndexes {
indexes[i] = index.(string)
}
eventQuery.SetIndexes(indexes)
if terraformSearches, ok := data["search"].([]interface{}); ok && len(terraformSearches) > 0 {
terraformSearch := terraformSearches[0].(map[string]interface{})
eventQuery.Search = datadogV1.NewMonitorFormulaAndFunctionEventQueryDefinitionSearch(terraformSearch["query"].(string))
}
// GroupBy
if terraformGroupBys, ok := data["group_by"].([]interface{}); ok && len(terraformGroupBys) > 0 {
datadogGroupBys := make([]datadogV1.MonitorFormulaAndFunctionEventQueryGroupBy, len(terraformGroupBys))
for i, g := range terraformGroupBys {
groupBy := g.(map[string]interface{})
// Facet
datadogGroupBy := datadogV1.NewMonitorFormulaAndFunctionEventQueryGroupBy(groupBy["facet"].(string))
// Limit
if v, ok := groupBy["limit"].(int); ok && v != 0 {
datadogGroupBy.SetLimit(int64(v))
}
// Sort
if v, ok := groupBy["sort"].([]interface{}); ok && len(v) > 0 {
if v, ok := v[0].(map[string]interface{}); ok && len(v) > 0 {
sortMap := &datadogV1.MonitorFormulaAndFunctionEventQueryGroupBySort{}
if aggr, ok := v["aggregation"].(string); ok && len(aggr) > 0 {
aggregation := datadogV1.MonitorFormulaAndFunctionEventAggregation(v["aggregation"].(string))
sortMap.SetAggregation(aggregation)
}
if order, ok := v["order"].(string); ok && len(order) > 0 {
eventSort := datadogV1.QuerySortOrder(order)
sortMap.SetOrder(eventSort)
}
if metric, ok := v["metric"].(string); ok && len(metric) > 0 {
sortMap.SetMetric(metric)
}
datadogGroupBy.SetSort(*sortMap)
}
}
datadogGroupBys[i] = *datadogGroupBy
}
eventQuery.SetGroupBy(datadogGroupBys)
} else {
emptyGroupBy := make([]datadogV1.MonitorFormulaAndFunctionEventQueryGroupBy, 0)
eventQuery.SetGroupBy(emptyGroupBy)
}
definition := datadogV1.MonitorFormulaAndFunctionEventQueryDefinitionAsMonitorFormulaAndFunctionQueryDefinition(eventQuery)
return &definition
}
func buildMonitorFormulaAndFunctionCloudCostQuery(data map[string]interface{}) *datadogV1.MonitorFormulaAndFunctionQueryDefinition {
dataSource := datadogV1.MonitorFormulaAndFunctionCostDataSource(data["data_source"].(string))
cloudCostQuery := datadogV1.NewMonitorFormulaAndFunctionCostQueryDefinition(dataSource, data["name"].(string), data["query"].(string))
if v, ok := data["aggregator"].(string); ok && len(v) != 0 {
cloudCostQuery.SetAggregator(datadogV1.MonitorFormulaAndFunctionCostAggregator(v))
}
datadogV1.MonitorFormulaAndFunctionCostQueryDefinitionAsMonitorFormulaAndFunctionQueryDefinition(cloudCostQuery)
definition := datadogV1.MonitorFormulaAndFunctionCostQueryDefinitionAsMonitorFormulaAndFunctionQueryDefinition(cloudCostQuery)
return &definition
}
// Use CustomizeDiff to do monitor validation
func resourceDatadogMonitorCustomizeDiff(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
if _, ok := diff.GetOk("query"); !ok {
// If "query" depends on other resources, we can't validate as the variables may not be interpolated yet.
return nil
}
if _, ok := diff.GetOk("type"); !ok {
// Same for type
return nil
}
if validate, ok := diff.GetOkExists("validate"); ok && !validate.(bool) {
// Explicitly skip validation
return nil
}
m, _ := buildMonitorStruct(diff)
hasID := false
id, err := strconv.ParseInt(diff.Id(), 10, 64)
if err == nil {
hasID = true
}
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
return retry.RetryContext(ctx, retryTimeout, func() *retry.RetryError {
var httpresp *http.Response
if hasID {
_, httpresp, err = apiInstances.GetMonitorsApiV1().ValidateExistingMonitor(auth, id, *m)
} else {
_, httpresp, err = apiInstances.GetMonitorsApiV1().ValidateMonitor(auth, *m)
}
if err != nil {
if httpresp != nil && (httpresp.StatusCode == 502 || httpresp.StatusCode == 504) {
return retry.RetryableError(utils.TranslateClientError(err, httpresp, "error validating monitor, retrying"))
}
return retry.NonRetryableError(utils.TranslateClientError(err, httpresp, "error validating monitor"))
}
return nil
})
}
func resourceDatadogMonitorCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
m, _ := buildMonitorStruct(d)
mCreated, httpResponse, err := apiInstances.GetMonitorsApiV1().CreateMonitor(auth, *m)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpResponse, "error creating monitor")
}
if err := utils.CheckForUnparsed(m); err != nil {
return diag.FromErr(err)
}
mCreatedID := strconv.FormatInt(mCreated.GetId(), 10)
d.SetId(mCreatedID)
return updateMonitorState(d, meta, &mCreated)
}
func updateMonitorState(d *schema.ResourceData, meta interface{}, m *datadogV1.Monitor) diag.Diagnostics {
thresholds := make(map[string]string)
if v, ok := m.Options.Thresholds.GetOkOk(); ok {
thresholds["ok"] = fmt.Sprintf("%v", *v)
}