Skip to content

Commit

Permalink
Force recomputation of computed values for Grafana Cloud stacks on sl…
Browse files Browse the repository at this point in the history
…ug update (#1269)

* Force recomputation of computed values for Grafana Cloud stacks on slug update

Names of some attributes of the Grafana Cloud stack depend on the slug
of the stack. While the current state of the provider does update these
attributes on the resource in the state, if these attributes are
referenced in an output clause, the absence of a change in the generated
plan renders the change unnoticed and stale information is outputted on
a first apply. By using https://pkg.go.dev/github.com/hashicorp/terraform-plugin-sdk/helper/customdiff
it's possible to mark these values to be computed again if the slug
changes, thus being reflected as changes in the plan and generated an
up to date output.

Fixes #1191

* Adjust code formatting with go fmt

---------

Co-authored-by: Stefano Boriero <stefano.boriero@ocado.com>
  • Loading branch information
stefanoboriero and Stefano Boriero authored Jan 8, 2024
1 parent 06964e4 commit 15853d5
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions internal/resources/cloud/resource_cloud_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -225,6 +226,23 @@ available at “https://<stack_slug>.grafana.net".`,
Computed: true,
},
},
CustomizeDiff: customdiff.All(
customdiff.ComputedIf("url", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("slug")
}),
customdiff.ComputedIf("alertmanager_name", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("slug")
}),
customdiff.ComputedIf("logs_name", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("slug")
}),
customdiff.ComputedIf("traces_name", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("slug")
}),
customdiff.ComputedIf("prometheus_name", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("slug")
}),
),
}
}

Expand Down

0 comments on commit 15853d5

Please sign in to comment.