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

suppress dataplex labels #12694

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/6518.txt
@@ -0,0 +1,3 @@
```release-note:bug
storage: fixed a bug where user specified labels get overwritten by Dataplex auto generated labels
```
26 changes: 22 additions & 4 deletions google/resource_storage_bucket.go
Expand Up @@ -79,10 +79,12 @@ func resourceStorageBucket() *schema.Resource {
},

"labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `A set of key/value label pairs to assign to the bucket.`,
Type: schema.TypeMap,
Optional: true,
// GCP (Dataplex) automatically adds labels
DiffSuppressFunc: resourceDataplexLabelDiffSuppress,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `A set of key/value label pairs to assign to the bucket.`,
},

"location": {
Expand Down Expand Up @@ -368,6 +370,22 @@ func resourceStorageBucket() *schema.Resource {
}
}

const resourceDataplexGoogleProvidedLabelPrefix = "labels.goog-dataplex"

func resourceDataplexLabelDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
if strings.HasPrefix(k, resourceDataplexGoogleProvidedLabelPrefix) && 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
}

// Is the old bucket retention policy locked?
func isPolicyLocked(_ context.Context, old, new, _ interface{}) bool {
if old == nil || new == nil {
Expand Down
79 changes: 79 additions & 0 deletions google/resource_storage_bucket_test.go
Expand Up @@ -1022,6 +1022,85 @@ func TestAccStorageBucket_retentionPolicyLocked(t *testing.T) {
})
}

func TestLabelDiffSuppress(t *testing.T) {
cases := map[string]struct {
K, Old, New string
ExpectDiffSuppress bool
}{
"missing goog-dataplex-asset-id": {
K: "labels.goog-dataplex-asset-id",
Old: "test-bucket",
New: "",
ExpectDiffSuppress: true,
},
"explicit goog-dataplex-asset-id": {
K: "labels.goog-dataplex-asset-id",
Old: "test-bucket",
New: "test-bucket-1",
ExpectDiffSuppress: false,
},
"missing goog-dataplex-lake-id": {
K: "labels.goog-dataplex-lake-id",
Old: "test-lake",
New: "",
ExpectDiffSuppress: true,
},
"explicit goog-dataplex-lake-id": {
K: "labels.goog-dataplex-lake-id",
Old: "test-lake",
New: "test-lake-1",
ExpectDiffSuppress: false,
},
"missing goog-dataplex-project-id": {
K: "labels.goog-dataplex-project-id",
Old: "test-project-12345",
New: "",
ExpectDiffSuppress: true,
},
"explicit goog-dataplex-project-id": {
K: "labels.goog-dataplex-project-id",
Old: "test-project-12345",
New: "test-project-12345-1",
ExpectDiffSuppress: false,
},
"missing goog-dataplex-zone-id": {
K: "labels.goog-dataplex-zone-id",
Old: "test-zone1",
New: "",
ExpectDiffSuppress: true,
},
"explicit goog-dataplex-zone-id": {
K: "labels.goog-dataplex-zone-id",
Old: "test-zone1",
New: "test-zone1-1",
ExpectDiffSuppress: false,
},
"labels.%": {
K: "labels.%",
Old: "5",
New: "1",
ExpectDiffSuppress: true,
},
"deleted custom key": {
K: "labels.my-label",
Old: "my-value",
New: "",
ExpectDiffSuppress: false,
},
"added custom key": {
K: "labels.my-label",
Old: "",
New: "my-value",
ExpectDiffSuppress: false,
},
}
for tn, tc := range cases {
if resourceDataplexLabelDiffSuppress(tc.K, tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
t.Errorf("bad: %s, %q: %q => %q expect DiffSuppress to return %t", tn, tc.K, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}

func testAccCheckStorageBucketExists(t *testing.T, n string, bucketName string, bucket *storage.Bucket) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down