Skip to content

[Fix] Adds check to ensure that the Grafana-Terraform-Provider header is set #1631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 13, 2024
20 changes: 17 additions & 3 deletions internal/resources/slo/resource_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slo

import (
"context"
"errors"
"fmt"
"regexp"

Expand Down Expand Up @@ -305,12 +306,18 @@ func resourceSloRead(ctx context.Context, d *schema.ResourceData, client *slo.AP

req := client.DefaultAPI.V1SloIdGet(ctx, sloID)
slo, _, err := req.Execute()

if err != nil {
return apiError("Unable to read SLO - API", err)
}

setTerraformState(d, *slo)
err = setTerraformState(d, *slo)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Missing Terraform provenance request header",
Detail: err.Error(),
})
}

return diags
}
Expand Down Expand Up @@ -583,7 +590,12 @@ func packAlertMetadata(metadata []interface{}) slo.SloV00AlertingMetadata {
return apiMetadata
}

func setTerraformState(d *schema.ResourceData, slo slo.SloV00Slo) {
func setTerraformState(d *schema.ResourceData, slo slo.SloV00Slo) error {
// After CREATE / UPDATE - the modified SLO is readback. Verify that the Provenance field is appropriately set
if *slo.ReadOnly.Provenance != "terraform" {
return errors.New(`provenance header missing - verify within the Grafana Terraform Provider that the 'Grafana-Terraform-Provider' request header is set to 'true'`)
}

d.Set("name", slo.Name)
d.Set("description", slo.Description)

Expand All @@ -600,6 +612,8 @@ func setTerraformState(d *schema.ResourceData, slo slo.SloV00Slo) {

retAlerting := unpackAlerting(slo.Alerting)
d.Set("alerting", retAlerting)

return nil
}

func apiError(action string, err error) diag.Diagnostics {
Expand Down
Loading