Remove label from bucket (#1550)

* Remove label from bucket

* Remove debugging statement

* Fix typos
This commit is contained in:
Vincent Roseberry 2018-05-29 14:23:37 -07:00 committed by GitHub
parent 66afc153ab
commit 8c2f47ba69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 8 deletions

View File

@ -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()

View File

@ -558,14 +558,7 @@ func TestAccStorageBucket_labels(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccStorageBucketDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccStorageBucket_labels(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
testAccCheckStorageBucketHasLabel(&bucket, "my-label", "my-label-value"),
),
},
// Going from two labels
resource.TestStep{
Config: testAccStorageBucket_updateLabels(bucketName),
Check: resource.ComposeTestCheckFunc(
@ -575,6 +568,26 @@ func TestAccStorageBucket_labels(t *testing.T) {
testAccCheckStorageBucketHasLabel(&bucket, "a-new-label", "a-new-label-value"),
),
},
resource.TestStep{
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-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(
@ -583,6 +596,11 @@ func TestAccStorageBucket_labels(t *testing.T) {
testAccCheckStorageBucketHasNoLabels(&bucket),
),
},
resource.TestStep{
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
},
},
})
}