Skip to content

Commit

Permalink
suppress dataplex labels (#6518) (#12694)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Sep 30, 2022
1 parent 910c210 commit 4f6fcd6
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
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

0 comments on commit 4f6fcd6

Please sign in to comment.