Skip to content

Commit

Permalink
Add labels to google_storage_bucket (#652)
Browse files Browse the repository at this point in the history
* revendor storage

* revendor gensupport

* revendor bigquery

* add support for storage bucket labels

* bucket label docs
  • Loading branch information
danawillow authored Oct 30, 2017
1 parent f6c1a23 commit 7b69c31
Show file tree
Hide file tree
Showing 11 changed files with 6,605 additions and 2,482 deletions.
22 changes: 20 additions & 2 deletions google/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func resourceStorageBucket() *schema.Resource {
Default: false,
},

"labels": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"location": &schema.Schema{
Type: schema.TypeString,
Default: "US",
Expand Down Expand Up @@ -223,7 +229,11 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error
location := d.Get("location").(string)

// Create a bucket, setting the acl, location and name.
sb := &storage.Bucket{Name: bucket, Location: location}
sb := &storage.Bucket{
Name: bucket,
Labels: expandLabels(d),
Location: location,
}

if v, ok := d.GetOk("storage_class"); ok {
sb.StorageClass = v.(string)
Expand Down Expand Up @@ -331,6 +341,13 @@ func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error
sb.Cors = expandCors(v.([]interface{}))
}

if d.HasChange("labels") {
sb.Labels = expandLabels(d)
if len(sb.Labels) == 0 {
sb.NullFields = append(sb.NullFields, "Labels")
}
}

res, err := config.clientStorage.Buckets.Patch(d.Get("name").(string), sb).Do()

if err != nil {
Expand Down Expand Up @@ -366,6 +383,7 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error {
d.Set("location", res.Location)
d.Set("cors", flattenCors(res.Cors))
d.Set("versioning", flattenBucketVersioning(res.Versioning))
d.Set("labels", res.Labels)
d.SetId(res.Id)
return nil
}
Expand Down Expand Up @@ -534,7 +552,7 @@ func resourceGCSBucketLifecycleCreateOrUpdate(d *schema.ResourceData, sb *storag
}

if v, ok := condition["is_live"]; ok {
target_lifecycle_rule.Condition.IsLive = v.(bool)
target_lifecycle_rule.Condition.IsLive = googleapi.Bool(v.(bool))
}

if v, ok := condition["matches_storage_class"]; ok {
Expand Down
86 changes: 86 additions & 0 deletions google/resource_storage_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,46 @@ func TestAccStorageBucket_cors(t *testing.T) {
}
}

func TestAccStorageBucket_labels(t *testing.T) {
t.Parallel()

var bucket storage.Bucket
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(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"),
),
},
resource.TestStep{
Config: testAccStorageBucket_updateLabels(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"),
),
},
resource.TestStep{
Config: testAccStorageBucket_basic(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
testAccCheckStorageBucketHasNoLabels(&bucket),
),
},
},
})
}

func testAccCheckStorageBucketExists(n string, bucketName string, bucket *storage.Bucket) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -460,6 +500,29 @@ func testAccCheckStorageBucketExists(n string, bucketName string, bucket *storag
}
}

func testAccCheckStorageBucketHasLabel(bucket *storage.Bucket, key, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
val, ok := bucket.Labels[key]
if !ok {
return fmt.Errorf("Label with key %s not found", key)
}

if val != value {
return fmt.Errorf("Label value did not match for key %s: expected %s but found %s", key, value, val)
}
return nil
}
}

func testAccCheckStorageBucketHasNoLabels(bucket *storage.Bucket) resource.TestCheckFunc {
return func(s *terraform.State) error {
if len(bucket.Labels) > 0 {
return fmt.Errorf("Expected 0 labels, found %v", bucket.Labels)
}
return nil
}
}

func testAccCheckStorageBucketPutItem(bucketName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
Expand Down Expand Up @@ -656,3 +719,26 @@ resource "google_storage_bucket" "bucket" {
}
`, bucketName)
}

func testAccStorageBucket_labels(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
labels {
my-label = "my-label-value"
}
}
`, bucketName)
}

func testAccStorageBucket_updateLabels(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
labels {
my-label = "my-updated-label-value"
a-new-label = "a-new-label-value"
}
}
`, bucketName)
}
Loading

0 comments on commit 7b69c31

Please sign in to comment.