diff --git a/.changelog/5673.txt b/.changelog/5673.txt new file mode 100644 index 00000000000..f2b134a93be --- /dev/null +++ b/.changelog/5673.txt @@ -0,0 +1,3 @@ +```release-note:none +dataproc: fixed an issue where autogenerated labels would cause diffs in `google_dataproc_cluster` +``` diff --git a/google/resource_dataproc_cluster.go b/google/resource_dataproc_cluster.go index 8acbdffaad6..5fac09e18cd 100644 --- a/google/resource_dataproc_cluster.go +++ b/google/resource_dataproc_cluster.go @@ -63,6 +63,22 @@ var ( } ) +const resourceDataprocGoogleProvidedLabelPrefix = "labels.goog-dataproc" + +func resourceDataprocLabelDiffSuppress(k, old, new string, d *schema.ResourceData) bool { + if strings.HasPrefix(k, resourceDataprocGoogleProvidedLabelPrefix) && new == "" { + return true + } + + // Let diff be determined by labels (above) + if strings.HasPrefix(k, "labels.%") { + return true + } + + // For other keys, don't suppress diff. + return false +} + func resourceDataprocCluster() *schema.Resource { return &schema.Resource{ Create: resourceDataprocClusterCreate, @@ -132,11 +148,10 @@ func resourceDataprocCluster() *schema.Resource { Type: schema.TypeMap, Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, - // GCP automatically adds two labels - // 'goog-dataproc-cluster-uuid' - // 'goog-dataproc-cluster-name' - Computed: true, - Description: `The list of labels (key/value pairs) to be applied to instances in the cluster. GCP generates some itself including goog-dataproc-cluster-name which is the name of the cluster.`, + // GCP automatically adds labels + DiffSuppressFunc: resourceDataprocLabelDiffSuppress, + Computed: true, + Description: `The list of labels (key/value pairs) to be applied to instances in the cluster. GCP generates some itself including goog-dataproc-cluster-name which is the name of the cluster.`, }, "cluster_config": { diff --git a/google/resource_dataproc_cluster_test.go b/google/resource_dataproc_cluster_test.go index 3f449d33d3b..d31c1114e7b 100644 --- a/google/resource_dataproc_cluster_test.go +++ b/google/resource_dataproc_cluster_test.go @@ -589,13 +589,7 @@ func TestAccDataprocCluster_withLabels(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckDataprocClusterExists(t, "google_dataproc_cluster.with_labels", &cluster), - // We only provide one, but GCP adds three, so expect 4. This means unfortunately a - // diff will exist unless the user adds these in. An alternative approach would - // be to follow the same approach as properties, i.e. split in into labels - // and override_labels - // - // The config is currently configured with ignore_changes = ["labels"] to handle this - // + // We only provide one, but GCP adds three, so expect 4. resource.TestCheckResourceAttr("google_dataproc_cluster.with_labels", "labels.%", "4"), resource.TestCheckResourceAttr("google_dataproc_cluster.with_labels", "labels.key1", "value1"), ), @@ -1313,12 +1307,6 @@ resource "google_dataproc_cluster" "with_labels" { labels = { key1 = "value1" } - - # This is because GCP automatically adds its own labels as well. - # In this case we just want to test our newly added label is there - lifecycle { - ignore_changes = [labels] - } } `, rnd) }