From 15853d55fd16ca69fa048931f6298a90726e7c34 Mon Sep 17 00:00:00 2001 From: Stefano Boriero Date: Mon, 8 Jan 2024 14:43:40 +0100 Subject: [PATCH] Force recomputation of computed values for Grafana Cloud stacks on slug 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 --- .../resources/cloud/resource_cloud_stack.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/internal/resources/cloud/resource_cloud_stack.go b/internal/resources/cloud/resource_cloud_stack.go index 259bbcd98..d7e747688 100644 --- a/internal/resources/cloud/resource_cloud_stack.go +++ b/internal/resources/cloud/resource_cloud_stack.go @@ -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" @@ -225,6 +226,23 @@ available at “https://.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") + }), + ), } }