-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathresource_datadog_service_level_objective.go
860 lines (806 loc) · 30 KB
/
resource_datadog_service_level_objective.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
package datadog
import (
"context"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"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/schema"
)
func getTimeseriesQuerySchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Description: "A timeseries query, containing named data-source-specific queries and a formula involving the named queries.",
MaxItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"formula": {
Type: schema.TypeList,
Description: "A list that contains exactly one formula, as only a single formula may be used to define a timeseries query for a time-slice SLO.",
Required: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"formula_expression": {
Type: schema.TypeString,
Required: true,
Description: "The formula string, which is an expression involving named queries.",
},
},
},
},
"query": {
Type: schema.TypeList,
Description: "A list of data-source-specific queries that are in the formula.",
Required: true,
MinItems: 1,
Elem: &schema.Resource{
// Note this purposefully mirrors "metric_query" defined in resource_datadog_dashboard.go in the `getFormulaQuerySchema()` function.
// One difference is that we don't support the "aggregator" field here, as it's not supported by the SLO API.
// We may support "event_query" in the future, but for now we only support "metric_query".
Schema: map[string]*schema.Schema{
"metric_query": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "A timeseries formula and functions metrics query.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"data_source": {
Type: schema.TypeString,
Optional: true,
Default: "metrics",
Description: "The data source for metrics queries.",
},
"query": {
Type: schema.TypeString,
Required: true,
Description: "The metrics query definition.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the query for use in formulas.",
},
},
},
},
},
},
},
},
},
}
}
func resourceDatadogServiceLevelObjective() *schema.Resource {
return &schema.Resource{
Description: "Provides a Datadog service level objective resource. This can be used to create and manage Datadog service level objectives.",
CreateContext: resourceDatadogServiceLevelObjectiveCreate,
ReadContext: resourceDatadogServiceLevelObjectiveRead,
UpdateContext: resourceDatadogServiceLevelObjectiveUpdate,
DeleteContext: resourceDatadogServiceLevelObjectiveDelete,
CustomizeDiff: resourceDatadogServiceLevelObjectiveCustomizeDiff,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
SchemaFunc: func() map[string]*schema.Schema {
return map[string]*schema.Schema{
// Common
"name": {
Description: "Name of Datadog service level objective",
Type: schema.TypeString,
Required: true,
},
"description": {
Description: "A description of this service level objective.",
Type: schema.TypeString,
Optional: true,
StateFunc: trimStateValue,
DiffSuppressFunc: diffTrimmedValues,
},
"tags": {
// 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,
Description: "A list of tags to associate with your service level objective. This can help you categorize and filter service level objectives in the service level objectives page of the UI. Note: it's not currently possible to filter by these tags when querying via the API",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
StateFunc: func(val any) string {
return utils.NormalizeTag(val.(string))
},
},
},
"thresholds": {
Description: "A list of thresholds and targets that define the service level objectives from the provided SLIs.",
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"timeframe": {
Description: "The time frame for the objective. The mapping from these types to the types found in the Datadog Web UI can be found in the Datadog API documentation page.",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewSLOTimeframeFromValue),
},
"target": {
Description: "The objective's target in `(0,100)`.",
Type: schema.TypeFloat,
Required: true,
DiffSuppressFunc: suppressDataDogFloatIntDiff,
},
"target_display": {
Description: "A string representation of the target that indicates its precision. It uses trailing zeros to show significant decimal places (e.g. `98.00`).",
Type: schema.TypeString,
Computed: true,
},
"warning": {
Description: "The objective's warning value in `(0,100)`. This must be greater than the target value.",
Type: schema.TypeFloat,
Optional: true,
DiffSuppressFunc: suppressDataDogFloatIntDiff,
},
"warning_display": {
Description: "A string representation of the warning target (see the description of the target_display field for details).",
Type: schema.TypeString,
Computed: true,
},
},
},
},
"target_threshold": {
Description: "The objective's target in `(0,100)`. This must match the corresponding thresholds of the primary time frame.",
Type: schema.TypeFloat,
Optional: true,
Computed: true,
DiffSuppressFunc: suppressDataDogFloatIntDiff,
},
"warning_threshold": {
Description: "The objective's warning value in `(0,100)`. This must be greater than the target value and match the corresponding thresholds of the primary time frame.",
Type: schema.TypeFloat,
Optional: true,
Computed: true,
DiffSuppressFunc: suppressDataDogFloatIntDiff,
},
"timeframe": {
Description: "The primary time frame for the objective. The mapping from these types to the types found in the Datadog Web UI can be found in the Datadog API documentation page.",
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewSLOTimeframeFromValue),
},
"type": {
Description: "The type of the service level objective. 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/service-level-objectives/#create-a-slo-object).",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewSLOTypeFromValue),
},
"force_delete": {
Description: "A boolean indicating whether this monitor can be deleted even if it's referenced by other resources (for example, dashboards).",
Type: schema.TypeBool,
Optional: true,
},
// Metric-Based SLO
"query": {
// we use TypeList here because of https://github.com/hashicorp/terraform/issues/6215/
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"monitor_ids", "sli_specification", "groups"},
Description: "The metric query of good / total events",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"numerator": {
Description: "The sum of all the `good` events.",
Type: schema.TypeString,
Required: true,
StateFunc: trimStateValue,
DiffSuppressFunc: diffTrimmedValues,
},
"denominator": {
Description: "The sum of the `total` events.",
Type: schema.TypeString,
Required: true,
StateFunc: trimStateValue,
DiffSuppressFunc: diffTrimmedValues,
},
},
},
},
// Monitor-Based SLO
"monitor_ids": {
Type: schema.TypeSet,
Optional: true,
ConflictsWith: []string{"query", "sli_specification"},
Description: "A static set of monitor IDs to use as part of the SLO",
Elem: &schema.Schema{Type: schema.TypeInt, MinItems: 1},
},
// Time-Slice SLO
"sli_specification": {
Type: schema.TypeList,
MinItems: 1,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"query", "monitor_ids", "groups"},
Description: "A map of SLI specifications to use as part of the SLO.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"time_slice": {
Type: schema.TypeList,
MinItems: 1,
MaxItems: 1,
Required: true,
Description: "The time slice condition, composed of 3 parts: 1. The timeseries query, 2. The comparator, and 3. The threshold. Optionally, a fourth part, the query interval, can be provided.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"comparator": {
Type: schema.TypeString,
Required: true,
Description: "The comparator used to compare the SLI value to the threshold.",
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewSLOTimeSliceComparatorFromValue),
},
"threshold": {
Type: schema.TypeFloat,
Required: true,
Description: "The threshold value to which each SLI value will be compared.",
},
"query": getTimeseriesQuerySchema(),
"query_interval_seconds": {
Type: schema.TypeInt,
Optional: true,
Default: 300,
Description: "The interval used when querying data, which defines the size of a time slice.",
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewSLOTimeSliceIntervalFromValue),
},
},
},
},
},
},
},
"groups": {
Type: schema.TypeSet,
Optional: true,
Description: "A static set of groups to filter monitor-based SLOs",
ConflictsWith: []string{"query"},
Elem: &schema.Schema{Type: schema.TypeString, MinItems: 1},
},
"validate": {
Description: "Whether or not to validate the SLO. It checks if monitors added to a monitor SLO already exist.",
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
},
},
}
},
}
}
// ValidateServiceLevelObjectiveTypeString is a ValidateFunc that ensures the SLO is of one of the supported types
// Use CustomizeDiff to do monitor validation
func resourceDatadogServiceLevelObjectiveCustomizeDiff(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
if validate, ok := diff.GetOkExists("validate"); ok && !validate.(bool) {
// Explicitly skip validation
log.Printf("[DEBUG] Validate is %v, skipping validation", validate.(bool))
return nil
}
if val, ok := diff.GetOk("type"); ok && (val != string(datadogV1.SLOTYPE_MONITOR)) {
// If the SLO is not a Monitor type, skip the validation
log.Printf("[DEBUG] SLO type is: %v, skipping validation", val)
return nil
}
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
if attr, ok := diff.GetOk("monitor_ids"); ok {
for _, v := range attr.(*schema.Set).List() {
// Check that each monitor being added to the SLO exists
if _, httpResponse, err := apiInstances.GetMonitorsApiV1().GetMonitor(auth, int64(v.(int))); err != nil {
return utils.TranslateClientError(err, httpResponse, "error finding monitor to add to SLO")
}
}
}
return nil
}
func buildSLOTimeSliceQueryStruct(d []interface{}) *datadogV1.SLOTimeSliceQuery {
// only use the first defined query
ret := datadogV1.NewSLOTimeSliceQueryWithDefaults()
ret.Formulas = make([]datadogV1.SLOFormula, 0)
ret.Queries = make([]datadogV1.SLODataSourceQueryDefinition, 0)
if (len(d)) > 0 {
if raw, ok := d[0].(map[string]interface{}); ok {
if rawFormulas, ok := raw["formula"].([]interface{}); ok {
for _, rawFormulaEl := range rawFormulas {
if rawFormula, ok := rawFormulaEl.(map[string]interface{}); ok {
if formula, ok := rawFormula["formula_expression"].(string); ok {
ret.Formulas = append(ret.Formulas, *datadogV1.NewSLOFormula(formula))
}
}
}
}
if rawQueries, ok := raw["query"].([]interface{}); ok {
for _, rawQueryEl := range rawQueries {
rawQuery := rawQueryEl.(map[string]interface{})
rawMetricQueries := rawQuery["metric_query"].([]interface{})
if len(rawMetricQueries) >= 1 {
if rawMetricQuery, ok := rawMetricQueries[0].(map[string]interface{}); ok {
name := rawMetricQuery["name"].(string)
query := rawMetricQuery["query"].(string)
rawDataSource := rawMetricQuery["data_source"].(string)
dataSource, _ := datadogV1.NewFormulaAndFunctionMetricDataSourceFromValue(rawDataSource)
ret.Queries = append(ret.Queries,
datadogV1.FormulaAndFunctionMetricQueryDefinitionAsSLODataSourceQueryDefinition(
datadogV1.NewFormulaAndFunctionMetricQueryDefinition(*dataSource, name, query)))
}
}
}
}
}
}
return ret
}
func buildServiceLevelObjectiveStructs(d *schema.ResourceData) (*datadogV1.ServiceLevelObjective, *datadogV1.ServiceLevelObjectiveRequest, error) {
slo := datadogV1.NewServiceLevelObjectiveWithDefaults()
slo.SetName(d.Get("name").(string))
slo.SetType(datadogV1.SLOType(d.Get("type").(string)))
slo.SetId(d.Id())
slor := datadogV1.NewServiceLevelObjectiveRequestWithDefaults()
slor.SetName(d.Get("name").(string))
slor.SetType(datadogV1.SLOType(d.Get("type").(string)))
if attr, ok := d.GetOk("description"); ok {
slo.SetDescription(attr.(string))
slor.SetDescription(attr.(string))
}
switch slo.GetType() {
case datadogV1.SLOTYPE_MONITOR:
// monitor type
if attr, ok := d.GetOk("monitor_ids"); ok {
s := make([]int64, 0)
for _, v := range attr.(*schema.Set).List() {
s = append(s, int64(v.(int)))
}
slo.SetMonitorIds(s)
slor.SetMonitorIds(s)
} else {
return nil, nil, fmt.Errorf("monitor_ids is required for monitor SLOs")
}
if attr, ok := d.GetOk("groups"); ok {
s := make([]string, 0)
for _, v := range attr.(*schema.Set).List() {
s = append(s, v.(string))
}
slo.SetGroups(s)
slor.SetGroups(s)
}
case datadogV1.SLOTYPE_TIME_SLICE:
var sliSpec datadogV1.SLOSliSpec
if attr, ok := d.GetOk("sli_specification"); ok {
raw := attr.([]interface{})
if len(raw) >= 1 {
rawSliSpec := raw[0].(map[string]interface{})
if rawTimeSliceSpec, ok := rawSliSpec["time_slice"]; ok {
sliSpec.SLOTimeSliceSpec = datadogV1.NewSLOTimeSliceSpecWithDefaults()
rawTimeSliceConds := rawTimeSliceSpec.([]interface{})
if len(rawTimeSliceConds) >= 1 {
rawTimeSliceCond := rawTimeSliceConds[0].(map[string]interface{})
if rawTimeSliceQuery, ok := rawTimeSliceCond["query"].([]interface{}); ok {
sliSpec.SLOTimeSliceSpec.TimeSlice.SetQuery(*buildSLOTimeSliceQueryStruct(rawTimeSliceQuery))
}
if comparator, ok := rawTimeSliceCond["comparator"].(string); ok {
sliSpec.SLOTimeSliceSpec.TimeSlice.SetComparator(datadogV1.SLOTimeSliceComparator(comparator))
}
if threshold, ok := rawTimeSliceCond["threshold"].(float64); ok {
sliSpec.SLOTimeSliceSpec.TimeSlice.SetThreshold(threshold)
}
if queryInterval, ok := rawTimeSliceCond["query_interval_seconds"].(int); ok {
// Terraform doesn't have a way to represent an optional int, and so we
// will get a 0 value if the user doesn't specify a query_interval_seconds.
if queryInterval != 0 {
sliSpec.SLOTimeSliceSpec.TimeSlice.SetQueryIntervalSeconds(datadogV1.SLOTimeSliceInterval(queryInterval))
}
}
}
}
}
} else {
return nil, nil, fmt.Errorf("sli_specification is required for time slice SLOs")
}
slo.SetSliSpecification(sliSpec)
slor.SetSliSpecification(sliSpec)
default:
// metric type
if attr, ok := d.GetOk("query"); ok {
queries := make([]map[string]interface{}, 0)
raw := attr.([]interface{})
for _, rawQuery := range raw {
if query, ok := rawQuery.(map[string]interface{}); ok {
queries = append(queries, query)
}
}
if len(queries) >= 1 {
// only use the first defined query
slo.SetQuery(*datadogV1.NewServiceLevelObjectiveQuery(
queries[0]["denominator"].(string),
queries[0]["numerator"].(string)))
slor.SetQuery(*datadogV1.NewServiceLevelObjectiveQuery(
queries[0]["denominator"].(string),
queries[0]["numerator"].(string)))
}
} else {
return nil, nil, fmt.Errorf("query is required for metric SLOs")
}
}
if attr, ok := d.GetOk("tags"); ok {
s := make([]string, 0)
for _, v := range attr.(*schema.Set).List() {
s = append(s, v.(string))
}
slo.SetTags(s)
slor.SetTags(s)
}
if _, ok := d.GetOk("thresholds"); ok {
numThresholds := d.Get("thresholds.#").(int)
sloThresholds := make([]datadogV1.SLOThreshold, 0)
for i := 0; i < numThresholds; i++ {
prefix := fmt.Sprintf("thresholds.%d.", i)
t := datadogV1.NewSLOThresholdWithDefaults()
if tf, ok := d.GetOk(prefix + "timeframe"); ok {
t.SetTimeframe(datadogV1.SLOTimeframe(tf.(string)))
}
if targetValue, ok := d.GetOk(prefix + "target"); ok {
if f, ok := floatOk(targetValue); ok {
t.SetTarget(f)
}
}
if warningValue, ok := d.GetOk(prefix + "warning"); ok {
if f, ok := floatOk(warningValue); ok {
t.SetWarning(f)
}
}
sloThresholds = append(sloThresholds, *t)
}
if len(sloThresholds) > 0 {
slo.SetThresholds(sloThresholds)
slor.SetThresholds(sloThresholds)
}
}
plan := d.GetRawConfig()
if tf, ok := d.GetOk("timeframe"); ok {
if plan.GetAttr("timeframe").IsNull() {
d.Set("timeframe", nil)
} else {
slo.SetTimeframe(datadogV1.SLOTimeframe(tf.(string)))
slor.SetTimeframe(datadogV1.SLOTimeframe(tf.(string)))
}
}
if targetValue, ok := d.GetOk("target_threshold"); ok {
if plan.GetAttr("target_threshold").IsNull() {
d.Set("target_threshold", nil)
} else if f, ok := floatOk(targetValue); ok {
slo.SetTargetThreshold(f)
slor.SetTargetThreshold(f)
}
}
if warningValue, ok := d.GetOk("warning_threshold"); ok {
if plan.GetAttr("warning_threshold").IsNull() {
d.Set("warning_threshold", nil)
} else if f, ok := floatOk(warningValue); ok {
slo.SetWarningThreshold(f)
slor.SetWarningThreshold(f)
}
}
return slo, slor, nil
}
func floatOk(val interface{}) (float64, bool) {
switch val := val.(type) {
case float64:
return val, true
case *float64:
return *val, true
case string:
f, err := strconv.ParseFloat(val, 64)
if err == nil {
return f, true
}
case *string:
f, err := strconv.ParseFloat(*val, 64)
if err == nil {
return f, true
}
default:
return 0, false
}
return 0, false
}
func resourceDatadogServiceLevelObjectiveCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
_, slor, err := buildServiceLevelObjectiveStructs(d)
if err != nil {
return diag.FromErr(err)
}
sloResp, httpResponse, err := apiInstances.GetServiceLevelObjectivesApiV1().CreateSLO(auth, *slor)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpResponse, "error creating service level objective")
}
if err := utils.CheckForUnparsed(sloResp); err != nil {
return diag.FromErr(err)
}
slo := &sloResp.GetData()[0]
d.SetId(slo.GetId())
return updateSLOState(d, slo)
}
func resourceDatadogServiceLevelObjectiveRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
sloResp, httpresp, err := apiInstances.GetServiceLevelObjectivesApiV1().GetSLO(auth, d.Id())
if err != nil {
if httpresp != nil && httpresp.StatusCode == 404 {
d.SetId("")
return nil
}
return utils.TranslateClientErrorDiag(err, httpresp, "error getting service level objective")
}
if err := utils.CheckForUnparsed(sloResp); err != nil {
return diag.FromErr(err)
}
return updateSLOStateFromRead(d, sloResp.Data)
}
// Builds the corresponding terraform representation of the SLO's SLI specification
func buildTerraformSliSpecification(sliSpec *datadogV1.SLOSliSpec) []map[string]interface{} {
rawSliSpec := make([]map[string]interface{}, 0)
if sliSpec.SLOTimeSliceSpec != nil {
rawTimeSliceSpec := make([]map[string]interface{}, 0)
comparator := sliSpec.SLOTimeSliceSpec.TimeSlice.GetComparator()
threshold := sliSpec.SLOTimeSliceSpec.TimeSlice.GetThreshold()
query := sliSpec.SLOTimeSliceSpec.TimeSlice.GetQuery()
rawFormulas := make([]map[string]interface{}, 0)
for _, formula := range query.GetFormulas() {
rawFormula := map[string]interface{}{"formula_expression": formula.GetFormula()}
rawFormulas = append(rawFormulas, rawFormula)
}
rawQueries := make([]map[string]interface{}, 0)
for _, q := range query.GetQueries() {
rawMetricQueries := make([]map[string]interface{}, 0)
rawQuery := map[string]interface{}{
"name": q.FormulaAndFunctionMetricQueryDefinition.GetName(),
"data_source": q.FormulaAndFunctionMetricQueryDefinition.GetDataSource(),
"query": q.FormulaAndFunctionMetricQueryDefinition.GetQuery(),
}
rawMetricQueries = append(rawMetricQueries, rawQuery)
rawQueries = append(rawQueries, map[string]interface{}{"metric_query": rawMetricQueries})
}
rawQuery := make([]map[string]interface{}, 0)
rawQuery = append(rawQuery, map[string]interface{}{
"formula": rawFormulas,
"query": rawQueries,
})
rawTimeSliceCond := map[string]interface{}{
"comparator": comparator,
"threshold": threshold,
"query": rawQuery,
}
if queryInterval, ok := sliSpec.SLOTimeSliceSpec.TimeSlice.GetQueryIntervalSecondsOk(); ok {
rawTimeSliceCond["query_interval_seconds"] = *queryInterval
}
rawTimeSliceSpec = append(rawTimeSliceSpec, rawTimeSliceCond)
rawSliSpec = append(rawSliSpec, map[string]interface{}{"time_slice": rawTimeSliceSpec})
}
return rawSliSpec
}
func updateSLOState(d *schema.ResourceData, slo *datadogV1.ServiceLevelObjective) diag.Diagnostics {
thresholds := make([]map[string]interface{}, 0)
for _, threshold := range slo.GetThresholds() {
t := map[string]interface{}{
"timeframe": threshold.GetTimeframe(),
"target": threshold.GetTarget(),
}
if warning, ok := threshold.GetWarningOk(); ok {
t["warning"] = warning
}
if targetDisplay, ok := threshold.GetTargetDisplayOk(); ok {
t["target_display"] = targetDisplay
}
if warningDisplay, ok := threshold.GetWarningDisplayOk(); ok {
t["warning_display"] = warningDisplay
}
thresholds = append(thresholds, t)
}
tags := make([]string, 0)
tags = append(tags, slo.GetTags()...)
if err := d.Set("name", slo.GetName()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("description", slo.GetDescription()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("type", slo.GetType()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("tags", tags); err != nil {
return diag.FromErr(err)
}
if err := d.Set("thresholds", thresholds); err != nil {
return diag.FromErr(err)
}
if timeframe, ok := slo.GetTimeframeOk(); ok {
if err := d.Set("timeframe", timeframe); err != nil {
return diag.FromErr(err)
}
}
if target, ok := slo.GetTargetThresholdOk(); ok {
if err := d.Set("target_threshold", target); err != nil {
return diag.FromErr(err)
}
}
if warning, ok := slo.GetWarningThresholdOk(); ok {
if err := d.Set("warning_threshold", warning); err != nil {
return diag.FromErr(err)
}
}
switch slo.GetType() {
case datadogV1.SLOTYPE_MONITOR:
// monitor type
if len(slo.GetMonitorIds()) > 0 {
if err := d.Set("monitor_ids", slo.GetMonitorIds()); err != nil {
return diag.FromErr(err)
}
}
if err := d.Set("groups", slo.GetGroups()); err != nil {
return diag.FromErr(err)
}
case datadogV1.SLOTYPE_TIME_SLICE:
// time slice type
sliSpec := slo.GetSliSpecification()
tfSliSpec := buildTerraformSliSpecification(&sliSpec)
if err := d.Set("sli_specification", tfSliSpec); err != nil {
return diag.FromErr(err)
}
default:
// metric type
query := make(map[string]interface{})
q := slo.GetQuery()
query["numerator"] = q.GetNumerator()
query["denominator"] = q.GetDenominator()
if err := d.Set("query", []map[string]interface{}{query}); err != nil {
return diag.FromErr(err)
}
}
return nil
}
// This duplicates updateSLOState for the SLOResponseData structure, which has mostly the same interface
func updateSLOStateFromRead(d *schema.ResourceData, slo *datadogV1.SLOResponseData) diag.Diagnostics {
thresholds := make([]map[string]interface{}, 0)
for _, threshold := range slo.GetThresholds() {
t := map[string]interface{}{
"timeframe": threshold.GetTimeframe(),
"target": threshold.GetTarget(),
}
if warning, ok := threshold.GetWarningOk(); ok {
t["warning"] = warning
}
if targetDisplay, ok := threshold.GetTargetDisplayOk(); ok {
t["target_display"] = targetDisplay
}
if warningDisplay, ok := threshold.GetWarningDisplayOk(); ok {
t["warning_display"] = warningDisplay
}
thresholds = append(thresholds, t)
}
tags := make([]string, 0)
tags = append(tags, slo.GetTags()...)
if err := d.Set("name", slo.GetName()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("description", slo.GetDescription()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("type", slo.GetType()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("tags", tags); err != nil {
return diag.FromErr(err)
}
if err := d.Set("thresholds", thresholds); err != nil {
return diag.FromErr(err)
}
if timeframe, ok := slo.GetTimeframeOk(); ok {
if err := d.Set("timeframe", timeframe); err != nil {
return diag.FromErr(err)
}
}
if target, ok := slo.GetTargetThresholdOk(); ok {
if err := d.Set("target_threshold", target); err != nil {
return diag.FromErr(err)
}
}
if warning, ok := slo.GetWarningThresholdOk(); ok {
if err := d.Set("warning_threshold", warning); err != nil {
return diag.FromErr(err)
}
}
switch slo.GetType() {
case datadogV1.SLOTYPE_MONITOR:
// monitor type
if len(slo.GetMonitorIds()) > 0 {
if err := d.Set("monitor_ids", slo.GetMonitorIds()); err != nil {
return diag.FromErr(err)
}
}
if err := d.Set("groups", slo.GetGroups()); err != nil {
return diag.FromErr(err)
}
case datadogV1.SLOTYPE_TIME_SLICE:
// time slice type
sliSpec := slo.GetSliSpecification()
tfSliSpec := buildTerraformSliSpecification(&sliSpec)
if err := d.Set("sli_specification", tfSliSpec); err != nil {
return diag.FromErr(err)
}
default:
// metric type
query := make(map[string]interface{})
q := slo.GetQuery()
query["numerator"] = q.GetNumerator()
query["denominator"] = q.GetDenominator()
if err := d.Set("query", []map[string]interface{}{query}); err != nil {
return diag.FromErr(err)
}
}
return nil
}
func resourceDatadogServiceLevelObjectiveUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
slo, _, err := buildServiceLevelObjectiveStructs(d)
if err != nil {
return diag.FromErr(err)
}
updatedSLO, httpResponse, err := apiInstances.GetServiceLevelObjectivesApiV1().UpdateSLO(auth, d.Id(), *slo)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpResponse, "error updating service level objective")
}
if err := utils.CheckForUnparsed(updatedSLO); err != nil {
return diag.FromErr(err)
}
return updateSLOState(d, &updatedSLO.GetData()[0])
}
func resourceDatadogServiceLevelObjectiveDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
var err error
var httpResponse *http.Response
if d.Get("force_delete").(bool) {
_, httpResponse, err = apiInstances.GetServiceLevelObjectivesApiV1().DeleteSLO(auth, d.Id(),
*datadogV1.NewDeleteSLOOptionalParameters().WithForce("true"),
)
} else {
_, httpResponse, err = apiInstances.GetServiceLevelObjectivesApiV1().DeleteSLO(auth, d.Id())
}
if err != nil {
return utils.TranslateClientErrorDiag(err, httpResponse, "error deleting service level objective")
}
return nil
}
func trimStateValue(val interface{}) string {
return strings.TrimSpace(val.(string))
}
// Ignore any diff for trimmed state values.
func diffTrimmedValues(_, old, new string, _ *schema.ResourceData) bool {
return strings.TrimSpace(old) == strings.TrimSpace(new)
}