diff --git a/mmv1/third_party/terraform/resources/resource_dataproc_cluster.go.erb b/mmv1/third_party/terraform/resources/resource_dataproc_cluster.go.erb index 1cf7667d0799..466889209934 100644 --- a/mmv1/third_party/terraform/resources/resource_dataproc_cluster.go.erb +++ b/mmv1/third_party/terraform/resources/resource_dataproc_cluster.go.erb @@ -73,6 +73,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, @@ -138,14 +154,12 @@ func resourceDataprocCluster() *schema.Resource { Description: `The timeout duration which allows graceful decomissioning when you change the number of worker nodes directly through a terraform apply`, }, - "labels": { Type: schema.TypeMap, Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, - // GCP automatically adds two labels - // 'goog-dataproc-cluster-uuid' - // 'goog-dataproc-cluster-name' + // 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.`, }, diff --git a/mmv1/third_party/terraform/tests/resource_dataproc_cluster_test.go.erb b/mmv1/third_party/terraform/tests/resource_dataproc_cluster_test.go.erb index 4b834dbe1b58..cfe20777def9 100644 --- a/mmv1/third_party/terraform/tests/resource_dataproc_cluster_test.go.erb +++ b/mmv1/third_party/terraform/tests/resource_dataproc_cluster_test.go.erb @@ -656,13 +656,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"), ), @@ -1439,12 +1433,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) }