Skip to content

Commit

Permalink
Add diffsuppress for dataproc cluster labels (#5673) (#11105)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Feb 14, 2022
1 parent a55d3be commit eaa43d3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .changelog/5673.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none
dataproc: fixed an issue where autogenerated labels would cause diffs in `google_dataproc_cluster`
```
25 changes: 20 additions & 5 deletions google/resource_dataproc_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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": {
Expand Down
14 changes: 1 addition & 13 deletions google/resource_dataproc_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit eaa43d3

Please sign in to comment.