Skip to content

Commit b172054

Browse files
author
Christopher A. Stelma
committed
add versioning for google storage buckets
1 parent 9bd35d7 commit b172054

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

google/resource_storage_bucket.go

+36
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ func resourceStorageBucket() *schema.Resource {
141141
},
142142
},
143143

144+
"versioning": &schema.Schema{
145+
Type: schema.TypeList,
146+
Optional: true,
147+
MaxItems: 1,
148+
Elem: &schema.Resource{
149+
Schema: map[string]*schema.Schema{
150+
"enabled": {
151+
Type: schema.TypeBool,
152+
Optional: true,
153+
Default: false,
154+
},
155+
},
156+
},
157+
},
158+
144159
"website": &schema.Schema{
145160
Type: schema.TypeList,
146161
Optional: true,
@@ -218,6 +233,13 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error
218233
return err
219234
}
220235

236+
if v, ok := d.GetOk("versioning"); ok {
237+
versioning := v.([]interface{})[0].(map[string]interface{})
238+
sb.Versioning = &storage.BucketVersioning{}
239+
sb.Versioning.Enabled = versioning["enabled"].(bool)
240+
sb.Versioning.ForceSendFields = append(sb.Versioning.ForceSendFields, "Enabled")
241+
}
242+
221243
if v, ok := d.GetOk("website"); ok {
222244
websites := v.([]interface{})
223245

@@ -282,6 +304,15 @@ func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error
282304
}
283305
}
284306

307+
if d.HasChange("versioning") {
308+
if v, ok := d.GetOk("versioning"); ok {
309+
versioning := v.([]interface{})[0].(map[string]interface{})
310+
sb.Versioning = &storage.BucketVersioning{}
311+
sb.Versioning.Enabled = versioning["enabled"].(bool)
312+
sb.Versioning.ForceSendFields = append(sb.Versioning.ForceSendFields, "Enabled")
313+
}
314+
}
315+
285316
if d.HasChange("website") {
286317
if v, ok := d.GetOk("website"); ok {
287318
websites := v.([]interface{})
@@ -351,6 +382,11 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error {
351382
d.Set("storage_class", res.StorageClass)
352383
d.Set("location", res.Location)
353384
d.Set("cors", flattenCors(res.Cors))
385+
versioning := make([]map[string]interface{}, 0, 1)
386+
versioning = append(versioning, map[string]interface{}{
387+
"enabled": res.Versioning.Enabled,
388+
})
389+
d.Set("versioning", versioning)
354390
d.SetId(res.Id)
355391
return nil
356392
}

website/docs/r/storage_bucket.html.markdown

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ to `google_storage_bucket_acl.predefined_acl`.
5858

5959
* `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.
6060

61+
* `versioning` - (Optional) The bucket's [Versioning](https://cloud.google.com/storage/docs/object-versioning) configuration.
62+
6163
* `website` - (Optional) Configuration if the bucket acts as a website. Structure is documented below.
6264

6365
* `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
8688

8789
* `num_newer_versions` - (Optional) Relevant only for versioned objects. The number of newer versions of an object to satisfy this condition.
8890

91+
The `versioning` block supports:
92+
93+
* `enabled` - (Optional) While set to `true`, versioning is fully enabled for this bucket.
94+
8995
The `website` block supports:
9096

9197
* `main_page_suffix` - (Optional) Behaves as the bucket's directory index where

0 commit comments

Comments
 (0)