-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathresource_slo.go
658 lines (574 loc) · 19.3 KB
/
resource_slo.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
package slo
import (
"context"
"fmt"
"regexp"
slo "github.com/grafana/slo-openapi-client/go"
"github.com/grafana/terraform-provider-grafana/v3/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
const (
QueryTypeFreeform string = "freeform"
QueryTypeHistogram string = "histogram"
QueryTypeRatio string = "ratio"
QueryTypeThreshold string = "threshold"
)
var resourceSloID = common.NewResourceID(common.StringIDField("uuid"))
func resourceSlo() *common.Resource {
schema := &schema.Resource{
Description: `
Resource manages Grafana SLOs.
* [Official documentation](https://grafana.com/docs/grafana-cloud/alerting-and-irm/slo/)
* [API documentation](https://grafana.com/docs/grafana-cloud/alerting-and-irm/slo/api/)
* [Additional Information On Alerting Rule Annotations and Labels](https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/#templating/)
`,
CreateContext: withClient[schema.CreateContextFunc](resourceSloCreate),
ReadContext: withClient[schema.ReadContextFunc](resourceSloRead),
UpdateContext: withClient[schema.UpdateContextFunc](resourceSloUpdate),
DeleteContext: withClient[schema.DeleteContextFunc](resourceSloDelete),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: `Name should be a short description of your indicator. Consider names like "API Availability"`,
ValidateFunc: validation.StringLenBetween(0, 128),
},
"description": {
Type: schema.TypeString,
Required: true,
Description: `Description is a free-text field that can provide more context to an SLO.`,
ValidateFunc: validation.StringLenBetween(0, 1024),
},
"folder_uid": {
Type: schema.TypeString,
Optional: true,
Description: `UID for the SLO folder`,
},
"destination_datasource": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: `Destination Datasource sets the datasource defined for an SLO`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uid": {
Type: schema.TypeString,
Description: `UID for the Mimir Datasource`,
Optional: true,
},
},
},
},
"query": {
Type: schema.TypeList,
Required: true,
Description: `Query describes the indicator that will be measured against the objective. Freeform Query types are currently supported.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Description: `Query type must be one of: "freeform", "query", "ratio", or "threshold"`,
ValidateFunc: validation.StringInSlice([]string{"freeform", "query", "ratio", "threshold"}, false),
Required: true,
},
"freeform": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"query": {
Type: schema.TypeString,
Required: true,
Description: "Freeform Query Field",
},
},
},
},
"ratio": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"success_metric": {
Type: schema.TypeString,
Description: `Counter metric for success events (numerator)`,
Required: true,
},
"total_metric": {
Type: schema.TypeString,
Description: `Metric for total events (denominator)`,
Required: true,
},
"group_by_labels": {
Type: schema.TypeList,
Description: `Defines Group By Labels used for per-label alerting. These appear as variables on SLO dashboards to enable filtering and aggregation. Labels must adhere to Prometheus label name schema - "^[a-zA-Z_][a-zA-Z0-9_]*$"`,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
},
},
"label": {
Type: schema.TypeList,
Optional: true,
Description: `Additional labels that will be attached to all metrics generated from the query. These labels are useful for grouping SLOs in dashboard views that you create by hand. Labels must adhere to Prometheus label name schema - "^[a-zA-Z_][a-zA-Z0-9_]*$"`,
Elem: keyvalueSchema,
},
"objectives": {
Type: schema.TypeList,
Required: true,
Description: `Over each rolling time window, the remaining error budget will be calculated, and separate alerts can be generated for each time window based on the SLO burn rate or remaining error budget.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"value": {
Type: schema.TypeFloat,
Required: true,
ValidateFunc: validation.FloatBetween(0, 1),
Description: `Value between 0 and 1. If the value of the query is above the objective, the SLO is met.`,
},
"window": {
Type: schema.TypeString,
Required: true,
Description: `A Prometheus-parsable time duration string like 24h, 60m. This is the time window the objective is measured over.`,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^\d+(ms|s|m|h|d|w|y)$`), "Objective window must be a Prometheus-parsable time duration"),
},
},
},
},
"alerting": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: `Configures the alerting rules that will be generated for each
time window associated with the SLO. Grafana SLOs can generate
alerts when the short-term error budget burn is very high, the
long-term error budget burn rate is high, or when the remaining
error budget is below a certain threshold. Annotations and Labels support templating.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"label": {
Type: schema.TypeList,
Optional: true,
Description: `Labels will be attached to all alerts generated by any of these rules.`,
Elem: keyvalueSchema,
},
"annotation": {
Type: schema.TypeList,
Optional: true,
Description: `Annotations will be attached to all alerts generated by any of these rules.`,
Elem: keyvalueSchema,
},
"fastburn": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Alerting Rules generated for Fast Burn alerts",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"label": {
Type: schema.TypeList,
Optional: true,
Description: "Labels to attach only to Fast Burn alerts.",
Elem: keyvalueSchema,
},
"annotation": {
Type: schema.TypeList,
Optional: true,
Description: "Annotations to attach only to Fast Burn alerts.",
Elem: keyvalueSchema,
},
},
},
},
"slowburn": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Alerting Rules generated for Slow Burn alerts",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"label": {
Type: schema.TypeList,
Optional: true,
Description: "Labels to attach only to Slow Burn alerts.",
Elem: keyvalueSchema,
},
"annotation": {
Type: schema.TypeList,
Optional: true,
Description: "Annotations to attach only to Slow Burn alerts.",
Elem: keyvalueSchema,
},
},
},
},
"advanced_options": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Advanced Options for Alert Rules",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"min_failures": {
Type: schema.TypeInt,
Optional: true,
Description: "Minimum number of failed events to trigger an alert",
ValidateFunc: validation.IntAtLeast(0),
},
},
},
},
},
},
},
"search_expression": {
Type: schema.TypeString,
Optional: true,
Description: "The name of a search expression in Grafana Asserts. This is used in the SLO UI to open the Asserts RCA workbench and in alerts to link to the RCA workbench.",
},
},
}
return common.NewLegacySDKResource(
common.CategorySLO,
"grafana_slo",
resourceSloID,
schema,
).WithLister(listSlos)
}
var keyvalueSchema = &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
Description: `Key for filtering and identification`,
},
"value": {
Type: schema.TypeString,
Required: true,
Description: `Templatable value`,
},
},
}
func listSlos(ctx context.Context, client *common.Client, data any) ([]string, error) {
sloClient := client.SLOClient
if sloClient == nil {
return nil, fmt.Errorf("client not configured for SLO API")
}
slolist, _, err := sloClient.DefaultAPI.V1SloGet(ctx).Execute()
if err != nil {
return nil, err
}
var ids []string
for _, slo := range slolist.Slos {
ids = append(ids, slo.Uuid)
}
return ids, nil
}
func resourceSloCreate(ctx context.Context, d *schema.ResourceData, client *slo.APIClient) diag.Diagnostics {
var diags diag.Diagnostics
sloModel, err := packSloResource(d)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to Pack SLO",
Detail: err.Error(),
})
return diags
}
req := client.DefaultAPI.V1SloPost(ctx).SloV00Slo(sloModel)
response, _, err := req.Execute()
if err != nil {
return apiError("Unable to create SLO - API", err)
}
d.SetId(response.Uuid)
return resourceSloRead(ctx, d, client)
}
// resourceSloRead - sends a GET Request to the single SLO Endpoint
func resourceSloRead(ctx context.Context, d *schema.ResourceData, client *slo.APIClient) diag.Diagnostics {
var diags diag.Diagnostics
sloID := d.Id()
req := client.DefaultAPI.V1SloIdGet(ctx, sloID)
slo, _, err := req.Execute()
if err != nil {
return apiError("Unable to read SLO - API", err)
}
setTerraformState(d, *slo)
return diags
}
func resourceSloUpdate(ctx context.Context, d *schema.ResourceData, client *slo.APIClient) diag.Diagnostics {
var diags diag.Diagnostics
sloID := d.Id()
if d.HasChange("name") || d.HasChange("description") || d.HasChange("query") || d.HasChange("label") || d.HasChange("objectives") || d.HasChange("alerting") || d.HasChange("destination_datasource") || d.HasChange("folder_uid") {
sloV00Slo, err := packSloResource(d)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to Pack SLO",
Detail: err.Error(),
})
return diags
}
req := client.DefaultAPI.V1SloIdPut(ctx, sloID).SloV00Slo(sloV00Slo)
if _, err := req.Execute(); err != nil {
return apiError("Unable to Update SLO - API", err)
}
}
return resourceSloRead(ctx, d, client)
}
func resourceSloDelete(ctx context.Context, d *schema.ResourceData, client *slo.APIClient) diag.Diagnostics {
sloID := d.Id()
req := client.DefaultAPI.V1SloIdDelete(ctx, sloID)
_, err := req.Execute()
return apiError("Unable to Delete SLO - API", err)
}
// Fetches all the Properties defined on the Terraform SLO State Object and converts it
// to a Slo so that it can be converted to JSON and sent to the API
func packSloResource(d *schema.ResourceData) (slo.SloV00Slo, error) {
var (
tfalerting slo.SloV00Alerting
tflabels []slo.SloV00Label
tfdestinationdatasource slo.SloV00DestinationDatasource
tffolder slo.SloV00Folder
)
tfname := d.Get("name").(string)
tfdescription := d.Get("description").(string)
query := d.Get("query").([]interface{})[0].(map[string]interface{})
tfquery, err := packQuery(query)
if err != nil {
return slo.SloV00Slo{}, err
}
objectives := d.Get("objectives").([]interface{})
tfobjective := packObjectives(objectives)
labels := d.Get("label").([]interface{})
if labels != nil {
tflabels = packLabels(labels)
}
req := slo.SloV00Slo{
Uuid: d.Id(),
Name: tfname,
Description: tfdescription,
Objectives: tfobjective,
Query: tfquery,
Alerting: nil,
Labels: tflabels,
DestinationDatasource: nil,
}
// Check the Optional Search Expression Field
if searchexpression, ok := d.GetOk("search_expression"); ok && searchexpression != "" {
req.SearchExpression = common.Ref(searchexpression.(string))
}
// Check the Optional Alerting Field
if alerting, ok := d.GetOk("alerting"); ok {
alertData, ok := alerting.([]interface{})
if ok && len(alertData) > 0 {
alert, ok := alertData[0].(map[string]interface{})
if ok {
tfalerting = packAlerting(alert)
}
}
req.Alerting = &tfalerting
}
// Check the Optional Destination Datasource Field
if rawdestinationdatasource, ok := d.GetOk("destination_datasource"); ok {
destinationDatasourceData, ok := rawdestinationdatasource.([]interface{})
if ok && len(destinationDatasourceData) > 0 {
destinationdatasource := destinationDatasourceData[0].(map[string]interface{})
tfdestinationdatasource, _ = packDestinationDatasource(destinationdatasource)
}
req.DestinationDatasource = &tfdestinationdatasource
}
// Check the Optional Folder UID Field
if rawfolderuid, ok := d.GetOk("folder_uid"); ok {
folderUIDData, ok := rawfolderuid.(string)
if ok {
tffolder = packFolder(folderUIDData)
}
req.Folder = &tffolder
}
return req, nil
}
func packDestinationDatasource(destinationdatasource map[string]interface{}) (slo.SloV00DestinationDatasource, error) {
packedDestinationDatasource := slo.SloV00DestinationDatasource{}
if destinationdatasource["uid"].(string) != "" {
datasourceUID := destinationdatasource["uid"].(string)
packedDestinationDatasource.Uid = common.Ref(datasourceUID)
}
return packedDestinationDatasource, nil
}
func packFolder(folderuid string) slo.SloV00Folder {
return slo.SloV00Folder{
Uid: &folderuid,
}
}
func packQuery(query map[string]interface{}) (slo.SloV00Query, error) {
if query["type"] == "freeform" {
freeformquery := query["freeform"].([]interface{})[0].(map[string]interface{})
querystring := freeformquery["query"].(string)
sloQuery := slo.SloV00Query{
Freeform: &slo.SloV00FreeformQuery{Query: querystring},
Type: QueryTypeFreeform,
}
return sloQuery, nil
}
if query["type"] == "ratio" {
ratioquery := query["ratio"].([]interface{})[0].(map[string]interface{})
successMetric := ratioquery["success_metric"].(string)
totalMetric := ratioquery["total_metric"].(string)
groupByLabels := ratioquery["group_by_labels"].([]interface{})
var labels []string
for ind := range groupByLabels {
if groupByLabels[ind] == nil {
labels = append(labels, "")
continue
}
labels = append(labels, groupByLabels[ind].(string))
}
sloQuery := slo.SloV00Query{
Ratio: &slo.SloV00RatioQuery{
SuccessMetric: slo.SloV00MetricDef{
PrometheusMetric: successMetric,
},
TotalMetric: slo.SloV00MetricDef{
PrometheusMetric: totalMetric,
},
GroupByLabels: labels,
},
Type: QueryTypeRatio,
}
return sloQuery, nil
}
return slo.SloV00Query{}, fmt.Errorf("%s query type not implemented", query["type"])
}
func packObjectives(tfobjectives []interface{}) []slo.SloV00Objective {
objectives := []slo.SloV00Objective{}
for ind := range tfobjectives {
tfobjective := tfobjectives[ind].(map[string]interface{})
objective := slo.SloV00Objective{
Value: tfobjective["value"].(float64),
Window: tfobjective["window"].(string),
}
objectives = append(objectives, objective)
}
return objectives
}
func packLabels(tfLabels []interface{}) []slo.SloV00Label {
labelSlice := []slo.SloV00Label{}
for ind := range tfLabels {
currLabel := tfLabels[ind].(map[string]interface{})
curr := slo.SloV00Label{
Key: currLabel["key"].(string),
Value: currLabel["value"].(string),
}
labelSlice = append(labelSlice, curr)
}
return labelSlice
}
func packAlerting(tfAlerting map[string]interface{}) slo.SloV00Alerting {
var tfAnnots []slo.SloV00Label
var tfLabels []slo.SloV00Label
var tfFastBurn slo.SloV00AlertingMetadata
var tfSlowBurn slo.SloV00AlertingMetadata
var tfAdvancedOptions slo.SloV00AdvancedOptions
annots, ok := tfAlerting["annotation"].([]interface{})
if ok {
tfAnnots = packLabels(annots)
}
labels, ok := tfAlerting["label"].([]interface{})
if ok {
tfLabels = packLabels(labels)
}
fastBurn, ok := tfAlerting["fastburn"].([]interface{})
if ok {
tfFastBurn = packAlertMetadata(fastBurn)
}
slowBurn, ok := tfAlerting["slowburn"].([]interface{})
if ok {
tfSlowBurn = packAlertMetadata(slowBurn)
}
alerting := slo.SloV00Alerting{
Annotations: tfAnnots,
Labels: tfLabels,
FastBurn: &tfFastBurn,
SlowBurn: &tfSlowBurn,
}
// All options in advanced options will be optional
// Adding a second feature will need to make a better way of checking what is there
if failures := tfAlerting["advanced_options"]; failures != nil {
lf, ok := failures.([]interface{})
if ok && len(lf) > 0 {
lf2, ok := lf[0].(map[string]interface{})
if ok {
i64 := int64(lf2["min_failures"].(int))
tfAdvancedOptions = slo.SloV00AdvancedOptions{
MinFailures: &i64,
}
alerting.SetAdvancedOptions(tfAdvancedOptions)
}
}
}
return alerting
}
func packAlertMetadata(metadata []interface{}) slo.SloV00AlertingMetadata {
var tflabels []slo.SloV00Label
var tfannots []slo.SloV00Label
if len(metadata) > 0 {
meta, ok := metadata[0].(map[string]interface{})
if ok {
labels, ok := meta["label"].([]interface{})
if ok {
tflabels = packLabels(labels)
}
annots, ok := meta["annotation"].([]interface{})
if ok {
tfannots = packLabels(annots)
}
}
}
apiMetadata := slo.SloV00AlertingMetadata{
Labels: tflabels,
Annotations: tfannots,
}
return apiMetadata
}
func setTerraformState(d *schema.ResourceData, slo slo.SloV00Slo) {
d.Set("name", slo.Name)
d.Set("description", slo.Description)
d.Set("query", unpackQuery(slo.Query))
retLabels := unpackLabels(&slo.Labels)
d.Set("label", retLabels)
retDestinationDatasource := unpackDestinationDatasource(slo.DestinationDatasource)
d.Set("destination_datasource", retDestinationDatasource)
retObjectives := unpackObjectives(slo.Objectives)
d.Set("objectives", retObjectives)
retAlerting := unpackAlerting(slo.Alerting)
d.Set("alerting", retAlerting)
d.Set("search_expression", slo.SearchExpression)
}
func apiError(action string, err error) diag.Diagnostics {
if err == nil {
return nil
}
detail := err.Error()
if err, ok := err.(*slo.GenericOpenAPIError); ok {
detail += "\n" + string(err.Body())
}
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: action,
Detail: detail,
},
}
}