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 #5673

Merged
merged 2 commits into from
Feb 14, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.`,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
Expand Down Expand Up @@ -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)
}
Expand Down