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

Remove label from bucket #1550

Merged
merged 3 commits into from
May 29, 2018
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
9 changes: 9 additions & 0 deletions google/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,15 @@ func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error
if len(sb.Labels) == 0 {
sb.NullFields = append(sb.NullFields, "Labels")
}

// To delete a label using PATCH, we have to explicitly set its value
// to null.
old, _ := d.GetChange("labels")
for k := range old.(map[string]interface{}) {
if _, ok := sb.Labels[k]; !ok {
sb.NullFields = append(sb.NullFields, fmt.Sprintf("Labels.%s", k))
}
}
}

res, err := config.clientStorage.Buckets.Patch(d.Get("name").(string), sb).Do()
Expand Down
28 changes: 23 additions & 5 deletions google/resource_storage_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,23 +558,36 @@ func TestAccStorageBucket_labels(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccStorageBucketDestroy,
Steps: []resource.TestStep{
// Going from two labels
resource.TestStep{
Config: testAccStorageBucket_labels(bucketName),
Config: testAccStorageBucket_updateLabels(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
testAccCheckStorageBucketHasLabel(&bucket, "my-label", "my-label-value"),
testAccCheckStorageBucketHasLabel(&bucket, "my-label", "my-updated-label-value"),
testAccCheckStorageBucketHasLabel(&bucket, "a-new-label", "a-new-label-value"),
),
},
resource.TestStep{
Config: testAccStorageBucket_updateLabels(bucketName),
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
},
// Down to only one label (test single label deletion)
resource.TestStep{
Config: testAccStorageBucket_labels(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
testAccCheckStorageBucketHasLabel(&bucket, "my-label", "my-updated-label-value"),
testAccCheckStorageBucketHasLabel(&bucket, "a-new-label", "a-new-label-value"),
testAccCheckStorageBucketHasLabel(&bucket, "my-label", "my-label-value"),
),
},
resource.TestStep{
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
},
// And make sure deleting all labels work
resource.TestStep{
Config: testAccStorageBucket_basic(bucketName),
Check: resource.ComposeTestCheckFunc(
Expand All @@ -583,6 +596,11 @@ func TestAccStorageBucket_labels(t *testing.T) {
testAccCheckStorageBucketHasNoLabels(&bucket),
),
},
resource.TestStep{
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down