Skip to content

Commit

Permalink
Update SLO Resources to Accept Empty Alerting and Empty Fastburn/Slow…
Browse files Browse the repository at this point in the history
…burn Fields (#1186)

Update SLO Resources to Accept Empty Alerting and Empty Fastburn/Slowburn Fields
  • Loading branch information
elainevuong authored Nov 29, 2023
1 parent 1ac310f commit 3064948
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 60 deletions.
4 changes: 1 addition & 3 deletions docs/data-sources/slos.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ resource "grafana_slo" "test" {
window = "30d"
}
destination_datasource {
type = "mimir"
uid = "grafanacloud-prom"
uid = "grafanacloud-prom"
}
label {
key = "custom"
Expand Down Expand Up @@ -177,7 +176,6 @@ Read-Only:

Read-Only:

- `type` (String)
- `uid` (String)


Expand Down
13 changes: 5 additions & 8 deletions docs/resources/slo.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ resource "grafana_slo" "test" {
window = "30d"
}
destination_datasource {
type = "mimir"
uid = "grafanacloud-prom"
uid = "grafanacloud-prom"
}
label {
key = "slo"
Expand Down Expand Up @@ -86,8 +85,7 @@ resource "grafana_slo" "test" {
window = "30d"
}
destination_datasource {
type = "mimir"
uid = "grafanacloud-prom"
uid = "grafanacloud-prom"
}
label {
key = "slo"
Expand Down Expand Up @@ -139,7 +137,7 @@ resource "grafana_slo" "test" {

### Optional

- `alerting` (Block List) Configures the alerting rules that will be generated for each
- `alerting` (Block List, Max: 1) 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
Expand Down Expand Up @@ -200,9 +198,9 @@ Optional:
Optional:

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

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

Optional:

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


Expand Down
3 changes: 1 addition & 2 deletions examples/data-sources/grafana_slos/data-source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ resource "grafana_slo" "test" {
window = "30d"
}
destination_datasource {
type = "mimir"
uid = "grafanacloud-prom"
uid = "grafanacloud-prom"
}
label {
key = "custom"
Expand Down
3 changes: 1 addition & 2 deletions examples/resources/grafana_slo/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ resource "grafana_slo" "test" {
window = "30d"
}
destination_datasource {
type = "mimir"
uid = "grafanacloud-prom"
uid = "grafanacloud-prom"
}
label {
key = "slo"
Expand Down
3 changes: 1 addition & 2 deletions examples/resources/grafana_slo/resource_complex.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ resource "grafana_slo" "test" {
window = "30d"
}
destination_datasource {
type = "mimir"
uid = "grafanacloud-prom"
uid = "grafanacloud-prom"
}
label {
key = "slo"
Expand Down
3 changes: 1 addition & 2 deletions examples/resources/grafana_slo/resource_ratio.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ resource "grafana_slo" "ratio" {
window = "30d"
}
destination_datasource {
type = "mimir"
uid = "grafanacloud-prom"
uid = "grafanacloud-prom"
}
label {
key = "slo"
Expand Down
3 changes: 1 addition & 2 deletions examples/resources/grafana_slo/resource_update.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ resource "grafana_slo" "update" {
window = "7d"
}
destination_datasource {
type = "mimir"
uid = "grafanacloud-prom"
uid = "grafanacloud-prom"
}
label {
key = "slo"
Expand Down
1 change: 0 additions & 1 deletion internal/resources/slo/data_source_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ func unpackDestinationDatasource(destinationDatasource *gapi.DestinationDatasour
retDestinationDatasources := []map[string]interface{}{}

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

retDestinationDatasources = append(retDestinationDatasources, retDestinationDatasource)
Expand Down
83 changes: 48 additions & 35 deletions internal/resources/slo/resource_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ Resource manages Grafana SLOs.
Description: `UID for the Mimir Datasource`,
Optional: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Description: `Datasource Type - set to 'mimir'`,
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -147,6 +142,7 @@ Resource manages Grafana SLOs.
},
"alerting": &schema.Schema{
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
Expand All @@ -170,6 +166,7 @@ Resource manages Grafana SLOs.
"fastburn": &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Alerting Rules generated for Fast Burn alerts",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -190,6 +187,7 @@ Resource manages Grafana SLOs.
},
"slowburn": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Alerting Rules generated for Slow Burn alerts",
Elem: &schema.Resource{
Expand Down Expand Up @@ -360,25 +358,22 @@ func packSloResource(d *schema.ResourceData) (gapi.Slo, error) {

// Check the Optional Alerting Field
if alerting, ok := d.GetOk("alerting"); ok {
alertData := alerting.([]interface{})

// if the Alerting field is an empty block, alertData[0] has a value of nil
if alertData[0] != nil {
// only pack the Alerting TF fields if the user populates the Alerting field with blocks
alert := alertData[0].(map[string]interface{})
tfalerting = packAlerting(alert)
alertData, ok := alerting.([]interface{})
if ok && len(alertData) > 0 {
alert, ok := alertData[0].(map[string]interface{})
if ok {
tfalerting = packAlerting(alert)
}
}

slo.Alerting = &tfalerting
}

// Check the Optional Destination Datasource Field
if rawdestinationdatasource, ok := d.GetOk("destination_datasource"); ok {
destinationDatasourceData := rawdestinationdatasource.([]interface{})
destinationDatasourceData, ok := rawdestinationdatasource.([]interface{})

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

if destinationdatasource["type"].(string) != "" {
datasourceType := destinationdatasource["type"].(string)
packedDestinationDatasource.Type = datasourceType
}

if destinationdatasource["uid"].(string) != "" {
datasourceUID := destinationdatasource["uid"].(string)
packedDestinationDatasource.UID = datasourceUID
Expand Down Expand Up @@ -487,17 +477,30 @@ func packLabels(tfLabels []interface{}) []gapi.Label {
}

func packAlerting(tfAlerting map[string]interface{}) gapi.Alerting {
annots := tfAlerting["annotation"].([]interface{})
tfAnnots := packLabels(annots)
var tfAnnots []gapi.Label
var tfLabels []gapi.Label
var tfFastBurn gapi.AlertingMetadata
var tfSlowBurn gapi.AlertingMetadata

annots, ok := tfAlerting["annotation"].([]interface{})
if ok {
tfAnnots = packLabels(annots)
}

labels := tfAlerting["label"].([]interface{})
tfLabels := packLabels(labels)
labels, ok := tfAlerting["label"].([]interface{})
if ok {
tfLabels = packLabels(labels)
}

fastBurn := tfAlerting["fastburn"].([]interface{})
tfFastBurn := packAlertMetadata(fastBurn)
fastBurn, ok := tfAlerting["fastburn"].([]interface{})
if ok {
tfFastBurn = packAlertMetadata(fastBurn)
}

slowBurn := tfAlerting["slowburn"].([]interface{})
tfSlowBurn := packAlertMetadata(slowBurn)
slowBurn, ok := tfAlerting["slowburn"].([]interface{})
if ok {
tfSlowBurn = packAlertMetadata(slowBurn)
}

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

func packAlertMetadata(metadata []interface{}) gapi.AlertingMetadata {
meta := metadata[0].(map[string]interface{})

labels := meta["label"].([]interface{})
tflabels := packLabels(labels)
var tflabels []gapi.Label
var tfannots []gapi.Label

if len(metadata) > 0 {
meta, ok := metadata[0].(map[string]interface{})
if ok {
labels, ok := meta["label"].([]interface{})
if ok {
tflabels = packLabels(labels)
}

annots := meta["annotation"].([]interface{})
tfannots := packLabels(annots)
annots, ok := meta["annotation"].([]interface{})
if ok {
tfannots = packLabels(annots)
}
}
}

apiMetadata := gapi.AlertingMetadata{
Labels: tflabels,
Expand Down
3 changes: 0 additions & 3 deletions internal/resources/slo/resource_slo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ resource "grafana_slo" "invalid" {
type = "freeform"
}
destination_datasource {
type = "mimir"
uid = "grafanacloud-prom"
}
objectives {
Expand All @@ -178,7 +177,6 @@ func emptyAlert(name string) string {
window = "28d"
}
destination_datasource {
type = "mimir"
uid = "grafanacloud-prom"
}
query {
Expand All @@ -202,7 +200,6 @@ resource "grafana_slo" "no_alert" {
window = "28d"
}
destination_datasource {
type = "mimir"
uid = "grafanacloud-prom"
}
query {
Expand Down

0 comments on commit 3064948

Please sign in to comment.