Skip to content

Commit 3064948

Browse files
authored
Update SLO Resources to Accept Empty Alerting and Empty Fastburn/Slowburn Fields (#1186)
Update SLO Resources to Accept Empty Alerting and Empty Fastburn/Slowburn Fields
1 parent 1ac310f commit 3064948

File tree

10 files changed

+59
-60
lines changed

10 files changed

+59
-60
lines changed

docs/data-sources/slos.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ resource "grafana_slo" "test" {
3232
window = "30d"
3333
}
3434
destination_datasource {
35-
type = "mimir"
36-
uid = "grafanacloud-prom"
35+
uid = "grafanacloud-prom"
3736
}
3837
label {
3938
key = "custom"
@@ -177,7 +176,6 @@ Read-Only:
177176

178177
Read-Only:
179178

180-
- `type` (String)
181179
- `uid` (String)
182180

183181

docs/resources/slo.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ resource "grafana_slo" "test" {
3434
window = "30d"
3535
}
3636
destination_datasource {
37-
type = "mimir"
38-
uid = "grafanacloud-prom"
37+
uid = "grafanacloud-prom"
3938
}
4039
label {
4140
key = "slo"
@@ -86,8 +85,7 @@ resource "grafana_slo" "test" {
8685
window = "30d"
8786
}
8887
destination_datasource {
89-
type = "mimir"
90-
uid = "grafanacloud-prom"
88+
uid = "grafanacloud-prom"
9189
}
9290
label {
9391
key = "slo"
@@ -139,7 +137,7 @@ resource "grafana_slo" "test" {
139137

140138
### Optional
141139

142-
- `alerting` (Block List) Configures the alerting rules that will be generated for each
140+
- `alerting` (Block List, Max: 1) Configures the alerting rules that will be generated for each
143141
time window associated with the SLO. Grafana SLOs can generate
144142
alerts when the short-term error budget burn is very high, the
145143
long-term error budget burn rate is high, or when the remaining
@@ -200,9 +198,9 @@ Optional:
200198
Optional:
201199

202200
- `annotation` (Block List) Annotations will be attached to all alerts generated by any of these rules. (see [below for nested schema](#nestedblock--alerting--annotation))
203-
- `fastburn` (Block List) Alerting Rules generated for Fast Burn alerts (see [below for nested schema](#nestedblock--alerting--fastburn))
201+
- `fastburn` (Block List, Max: 1) Alerting Rules generated for Fast Burn alerts (see [below for nested schema](#nestedblock--alerting--fastburn))
204202
- `label` (Block List) Labels will be attached to all alerts generated by any of these rules. (see [below for nested schema](#nestedblock--alerting--label))
205-
- `slowburn` (Block List) Alerting Rules generated for Slow Burn alerts (see [below for nested schema](#nestedblock--alerting--slowburn))
203+
- `slowburn` (Block List, Max: 1) Alerting Rules generated for Slow Burn alerts (see [below for nested schema](#nestedblock--alerting--slowburn))
206204

207205
<a id="nestedblock--alerting--annotation"></a>
208206
### Nested Schema for `alerting.annotation`
@@ -282,7 +280,6 @@ Required:
282280

283281
Optional:
284282

285-
- `type` (String) Datasource Type - set to 'mimir'
286283
- `uid` (String) UID for the Mimir Datasource
287284

288285

examples/data-sources/grafana_slos/data-source.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ resource "grafana_slo" "test" {
1212
window = "30d"
1313
}
1414
destination_datasource {
15-
type = "mimir"
16-
uid = "grafanacloud-prom"
15+
uid = "grafanacloud-prom"
1716
}
1817
label {
1918
key = "custom"

examples/resources/grafana_slo/resource.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ resource "grafana_slo" "test" {
1212
window = "30d"
1313
}
1414
destination_datasource {
15-
type = "mimir"
16-
uid = "grafanacloud-prom"
15+
uid = "grafanacloud-prom"
1716
}
1817
label {
1918
key = "slo"

examples/resources/grafana_slo/resource_complex.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ resource "grafana_slo" "test" {
1414
window = "30d"
1515
}
1616
destination_datasource {
17-
type = "mimir"
18-
uid = "grafanacloud-prom"
17+
uid = "grafanacloud-prom"
1918
}
2019
label {
2120
key = "slo"

examples/resources/grafana_slo/resource_ratio.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ resource "grafana_slo" "ratio" {
1414
window = "30d"
1515
}
1616
destination_datasource {
17-
type = "mimir"
18-
uid = "grafanacloud-prom"
17+
uid = "grafanacloud-prom"
1918
}
2019
label {
2120
key = "slo"

examples/resources/grafana_slo/resource_update.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ resource "grafana_slo" "update" {
1212
window = "7d"
1313
}
1414
destination_datasource {
15-
type = "mimir"
16-
uid = "grafanacloud-prom"
15+
uid = "grafanacloud-prom"
1716
}
1817
label {
1918
key = "slo"

internal/resources/slo/data_source_slo.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ func unpackDestinationDatasource(destinationDatasource *gapi.DestinationDatasour
195195
retDestinationDatasources := []map[string]interface{}{}
196196

197197
retDestinationDatasource := make(map[string]interface{})
198-
retDestinationDatasource["type"] = destinationDatasource.Type
199198
retDestinationDatasource["uid"] = destinationDatasource.UID
200199

201200
retDestinationDatasources = append(retDestinationDatasources, retDestinationDatasource)

internal/resources/slo/resource_slo.go

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ Resource manages Grafana SLOs.
5454
Description: `UID for the Mimir Datasource`,
5555
Optional: true,
5656
},
57-
"type": &schema.Schema{
58-
Type: schema.TypeString,
59-
Description: `Datasource Type - set to 'mimir'`,
60-
Optional: true,
61-
},
6257
},
6358
},
6459
},
@@ -147,6 +142,7 @@ Resource manages Grafana SLOs.
147142
},
148143
"alerting": &schema.Schema{
149144
Type: schema.TypeList,
145+
MaxItems: 1,
150146
Optional: true,
151147
Description: `Configures the alerting rules that will be generated for each
152148
time window associated with the SLO. Grafana SLOs can generate
@@ -170,6 +166,7 @@ Resource manages Grafana SLOs.
170166
"fastburn": &schema.Schema{
171167
Type: schema.TypeList,
172168
Optional: true,
169+
MaxItems: 1,
173170
Description: "Alerting Rules generated for Fast Burn alerts",
174171
Elem: &schema.Resource{
175172
Schema: map[string]*schema.Schema{
@@ -190,6 +187,7 @@ Resource manages Grafana SLOs.
190187
},
191188
"slowburn": &schema.Schema{
192189
Type: schema.TypeList,
190+
MaxItems: 1,
193191
Optional: true,
194192
Description: "Alerting Rules generated for Slow Burn alerts",
195193
Elem: &schema.Resource{
@@ -360,25 +358,22 @@ func packSloResource(d *schema.ResourceData) (gapi.Slo, error) {
360358

361359
// Check the Optional Alerting Field
362360
if alerting, ok := d.GetOk("alerting"); ok {
363-
alertData := alerting.([]interface{})
364-
365-
// if the Alerting field is an empty block, alertData[0] has a value of nil
366-
if alertData[0] != nil {
367-
// only pack the Alerting TF fields if the user populates the Alerting field with blocks
368-
alert := alertData[0].(map[string]interface{})
369-
tfalerting = packAlerting(alert)
361+
alertData, ok := alerting.([]interface{})
362+
if ok && len(alertData) > 0 {
363+
alert, ok := alertData[0].(map[string]interface{})
364+
if ok {
365+
tfalerting = packAlerting(alert)
366+
}
370367
}
371368

372369
slo.Alerting = &tfalerting
373370
}
374371

375372
// Check the Optional Destination Datasource Field
376373
if rawdestinationdatasource, ok := d.GetOk("destination_datasource"); ok {
377-
destinationDatasourceData := rawdestinationdatasource.([]interface{})
374+
destinationDatasourceData, ok := rawdestinationdatasource.([]interface{})
378375

379-
// if the destination_datasource field is an empty block, destination[0] has a value of nil
380-
if destinationDatasourceData[0] != nil {
381-
// only pack the destinationDatasource TF fields if the user populates the Destination field with blocks
376+
if ok && len(destinationDatasourceData) > 0 {
382377
destinationdatasource := destinationDatasourceData[0].(map[string]interface{})
383378
tfdestinationdatasource, _ = packDestinationDatasource(destinationdatasource)
384379
}
@@ -392,11 +387,6 @@ func packSloResource(d *schema.ResourceData) (gapi.Slo, error) {
392387
func packDestinationDatasource(destinationdatasource map[string]interface{}) (gapi.DestinationDatasource, error) {
393388
packedDestinationDatasource := gapi.DestinationDatasource{}
394389

395-
if destinationdatasource["type"].(string) != "" {
396-
datasourceType := destinationdatasource["type"].(string)
397-
packedDestinationDatasource.Type = datasourceType
398-
}
399-
400390
if destinationdatasource["uid"].(string) != "" {
401391
datasourceUID := destinationdatasource["uid"].(string)
402392
packedDestinationDatasource.UID = datasourceUID
@@ -487,17 +477,30 @@ func packLabels(tfLabels []interface{}) []gapi.Label {
487477
}
488478

489479
func packAlerting(tfAlerting map[string]interface{}) gapi.Alerting {
490-
annots := tfAlerting["annotation"].([]interface{})
491-
tfAnnots := packLabels(annots)
480+
var tfAnnots []gapi.Label
481+
var tfLabels []gapi.Label
482+
var tfFastBurn gapi.AlertingMetadata
483+
var tfSlowBurn gapi.AlertingMetadata
484+
485+
annots, ok := tfAlerting["annotation"].([]interface{})
486+
if ok {
487+
tfAnnots = packLabels(annots)
488+
}
492489

493-
labels := tfAlerting["label"].([]interface{})
494-
tfLabels := packLabels(labels)
490+
labels, ok := tfAlerting["label"].([]interface{})
491+
if ok {
492+
tfLabels = packLabels(labels)
493+
}
495494

496-
fastBurn := tfAlerting["fastburn"].([]interface{})
497-
tfFastBurn := packAlertMetadata(fastBurn)
495+
fastBurn, ok := tfAlerting["fastburn"].([]interface{})
496+
if ok {
497+
tfFastBurn = packAlertMetadata(fastBurn)
498+
}
498499

499-
slowBurn := tfAlerting["slowburn"].([]interface{})
500-
tfSlowBurn := packAlertMetadata(slowBurn)
500+
slowBurn, ok := tfAlerting["slowburn"].([]interface{})
501+
if ok {
502+
tfSlowBurn = packAlertMetadata(slowBurn)
503+
}
501504

502505
alerting := gapi.Alerting{
503506
Annotations: tfAnnots,
@@ -510,13 +513,23 @@ func packAlerting(tfAlerting map[string]interface{}) gapi.Alerting {
510513
}
511514

512515
func packAlertMetadata(metadata []interface{}) gapi.AlertingMetadata {
513-
meta := metadata[0].(map[string]interface{})
514-
515-
labels := meta["label"].([]interface{})
516-
tflabels := packLabels(labels)
516+
var tflabels []gapi.Label
517+
var tfannots []gapi.Label
518+
519+
if len(metadata) > 0 {
520+
meta, ok := metadata[0].(map[string]interface{})
521+
if ok {
522+
labels, ok := meta["label"].([]interface{})
523+
if ok {
524+
tflabels = packLabels(labels)
525+
}
517526

518-
annots := meta["annotation"].([]interface{})
519-
tfannots := packLabels(annots)
527+
annots, ok := meta["annotation"].([]interface{})
528+
if ok {
529+
tfannots = packLabels(annots)
530+
}
531+
}
532+
}
520533

521534
apiMetadata := gapi.AlertingMetadata{
522535
Labels: tflabels,

internal/resources/slo/resource_slo_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ resource "grafana_slo" "invalid" {
158158
type = "freeform"
159159
}
160160
destination_datasource {
161-
type = "mimir"
162161
uid = "grafanacloud-prom"
163162
}
164163
objectives {
@@ -178,7 +177,6 @@ func emptyAlert(name string) string {
178177
window = "28d"
179178
}
180179
destination_datasource {
181-
type = "mimir"
182180
uid = "grafanacloud-prom"
183181
}
184182
query {
@@ -202,7 +200,6 @@ resource "grafana_slo" "no_alert" {
202200
window = "28d"
203201
}
204202
destination_datasource {
205-
type = "mimir"
206203
uid = "grafanacloud-prom"
207204
}
208205
query {

0 commit comments

Comments
 (0)