From 89c30b775cf8706559ed5aa89208b4634d0bacde Mon Sep 17 00:00:00 2001 From: Alex Ott Date: Wed, 31 Aug 2022 13:45:46 +0200 Subject: [PATCH] Custom suppress diff for `storage` in `databricks_pipeline` When the `storage` option isn't provided, DLT generates a temp one in the form of `dbfs:/pipelines/`. But in the sequential apply the drift was detected, and pipeline was recreated. Marking it with `computed` or `suppress_diff` doesn't help because it prevents the pipeline from the detection of change of `storage` from value to null. --- pipelines/resource_pipeline.go | 14 ++++++++++++++ pipelines/resource_pipeline_test.go | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/pipelines/resource_pipeline.go b/pipelines/resource_pipeline.go index 7573fe0ea5..f30c710579 100644 --- a/pipelines/resource_pipeline.go +++ b/pipelines/resource_pipeline.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "regexp" "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -254,6 +255,17 @@ func (a PipelinesAPI) waitForState(id string, timeout time.Duration, desiredStat }) } +func suppressStorageDiff(k, old, new string, d *schema.ResourceData) bool { + defaultStorageRegex := regexp.MustCompile( + `^dbfs:/pipelines/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`) + res := defaultStorageRegex.MatchString(old) + if new == "" && res { + log.Printf("[DEBUG] Suppressing diff for %v: platform=%#v config=%#v", k, old, new) + return true + } + return false +} + func adjustPipelineResourceSchema(m map[string]*schema.Schema) map[string]*schema.Schema { cluster, _ := m["cluster"].Elem.(*schema.Resource) clustersSchema := cluster.Schema @@ -284,6 +296,8 @@ func adjustPipelineResourceSchema(m map[string]*schema.Schema) map[string]*schem m["channel"].ValidateFunc = validation.StringInSlice([]string{"current", "preview"}, true) m["edition"].ValidateFunc = validation.StringInSlice([]string{"pro", "core", "advanced"}, true) + m["storage"].DiffSuppressFunc = suppressStorageDiff + return m } diff --git a/pipelines/resource_pipeline_test.go b/pipelines/resource_pipeline_test.go index 7a19ef6ed8..3bc9f03630 100644 --- a/pipelines/resource_pipeline_test.go +++ b/pipelines/resource_pipeline_test.go @@ -591,3 +591,11 @@ func TestListPipelinesWithFilter(t *testing.T) { require.NoError(t, err) require.Equal(t, 1, len(data)) } + +func TestStorageSuppressDiff(t *testing.T) { + k := "storage" + generated := "dbfs:/pipelines/c609bbb0-2e42-4bc8-bb4e-a1c26d6e9403" + require.True(t, suppressStorageDiff(k, generated, "", nil)) + require.False(t, suppressStorageDiff(k, generated, "/tmp/abc", nil)) + require.False(t, suppressStorageDiff(k, "/tmp/abc", "", nil)) +}