Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add diffsuppress for dataproc cluster labels #11105

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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