Add versioning for google storage buckets (#381)

This commit is contained in:
Christopher Stelma 2017-09-15 08:36:01 -07:00 committed by Vincent Roseberry
parent b1879b2459
commit 43e749c213
3 changed files with 93 additions and 0 deletions

View File

@ -141,6 +141,21 @@ func resourceStorageBucket() *schema.Resource {
},
},
"versioning": &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
},
"website": &schema.Schema{
Type: schema.TypeList,
Optional: true,
@ -218,6 +233,10 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error
return err
}
if v, ok := d.GetOk("versioning"); ok {
sb.Versioning = expandBucketVersioning(v)
}
if v, ok := d.GetOk("website"); ok {
websites := v.([]interface{})
@ -282,6 +301,12 @@ func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error
}
}
if d.HasChange("versioning") {
if v, ok := d.GetOk("versioning"); ok {
sb.Versioning = expandBucketVersioning(v)
}
}
if d.HasChange("website") {
if v, ok := d.GetOk("website"); ok {
websites := v.([]interface{})
@ -351,6 +376,7 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error {
d.Set("storage_class", res.StorageClass)
d.Set("location", res.Location)
d.Set("cors", flattenCors(res.Cors))
d.Set("versioning", flattenBucketVersioning(res.Versioning))
d.SetId(res.Id)
return nil
}
@ -448,6 +474,32 @@ func flattenCors(corsRules []*storage.BucketCors) []map[string]interface{} {
return corsRulesSchema
}
func expandBucketVersioning(configured interface{}) *storage.BucketVersioning {
versionings := configured.([]interface{})
versioning := versionings[0].(map[string]interface{})
bucketVersioning := &storage.BucketVersioning{}
bucketVersioning.Enabled = versioning["enabled"].(bool)
bucketVersioning.ForceSendFields = append(bucketVersioning.ForceSendFields, "Enabled")
return bucketVersioning
}
func flattenBucketVersioning(bucketVersioning *storage.BucketVersioning) []map[string]interface{} {
versionings := make([]map[string]interface{}, 0, 1)
if bucketVersioning == nil {
return versionings
}
versioning := map[string]interface{}{
"enabled": bucketVersioning.Enabled,
}
versionings = append(versionings, versioning)
return versionings
}
func resourceGCSBucketLifecycleCreateOrUpdate(d *schema.ResourceData, sb *storage.Bucket) error {
if v, ok := d.GetOk("lifecycle_rule"); ok {
lifecycle_rules := v.([]interface{})

View File

@ -325,6 +325,30 @@ func TestAccStorageBucket_forceDestroy(t *testing.T) {
})
}
func TestAccStorageBucket_versioning(t *testing.T) {
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_versioning(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "versioning.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "versioning.0.enabled", "true"),
),
},
},
})
}
func TestAccStorageBucket_cors(t *testing.T) {
var bucket storage.Bucket
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
@ -590,6 +614,17 @@ resource "google_storage_bucket" "bucket" {
`, bucketName)
}
func testAccStorageBucket_versioning(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
versioning = {
enabled = "true"
}
}
`, bucketName)
}
func testAccStorageBucket_lifecycleRules(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {

View File

@ -58,6 +58,8 @@ to `google_storage_bucket_acl.predefined_acl`.
* `lifecycle_rule` - (Optional) The bucket's [Lifecycle Rules](https://cloud.google.com/storage/docs/lifecycle#configuration) configuration. Multiple blocks of this type are permitted. Structure is documented below.
* `versioning` - (Optional) The bucket's [Versioning](https://cloud.google.com/storage/docs/object-versioning) configuration.
* `website` - (Optional) Configuration if the bucket acts as a website. Structure is documented below.
* `cors` - (Optional) The bucket's [Cross-Origin Resource Sharing (CORS)](https://www.w3.org/TR/cors/) configuration. Multiple blocks of this type are permitted. Structure is documented below.
@ -86,6 +88,10 @@ The `condition` block supports the following elements, and requires at least one
* `num_newer_versions` - (Optional) Relevant only for versioned objects. The number of newer versions of an object to satisfy this condition.
The `versioning` block supports:
* `enabled` - (Optional) While set to `true`, versioning is fully enabled for this bucket.
The `website` block supports:
* `main_page_suffix` - (Optional) Behaves as the bucket's directory index where