Merge pull request #3240 from lwander/f-gce-bucket-website

provider/gce: Implement "website features" for GCE buckets
This commit is contained in:
Dave Cunningham 2015-09-16 17:37:32 -04:00
commit 488738163b
2 changed files with 110 additions and 5 deletions

View File

@ -40,6 +40,26 @@ func resourceStorageBucket() *schema.Resource {
Optional: true, Optional: true,
Default: false, Default: false,
}, },
"website": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"main_page_suffix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"not_found_page": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
},
"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
}, },
} }
} }
@ -54,6 +74,27 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error
// Create a bucket, setting the acl, location and name. // Create a bucket, setting the acl, location and name.
sb := &storage.Bucket{Name: bucket, Location: location} sb := &storage.Bucket{Name: bucket, Location: location}
if v, ok := d.GetOk("website"); ok {
websites := v.([]interface{})
if len(websites) > 1 {
return fmt.Errorf("At most one website block is allowed")
}
sb.Website = &storage.BucketWebsite{}
website := websites[0].(map[string]interface{})
if v, ok := website["not_found_page"]; ok {
sb.Website.NotFoundPage = v.(string)
}
if v, ok := website["main_page_suffix"]; ok {
sb.Website.MainPageSuffix = v.(string)
}
}
res, err := config.clientStorage.Buckets.Insert(config.Project, sb).PredefinedAcl(acl).Do() res, err := config.clientStorage.Buckets.Insert(config.Project, sb).PredefinedAcl(acl).Do()
if err != nil { if err != nil {
@ -64,14 +105,60 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error
log.Printf("[DEBUG] Created bucket %v at location %v\n\n", res.Name, res.SelfLink) log.Printf("[DEBUG] Created bucket %v at location %v\n\n", res.Name, res.SelfLink)
// Assign the bucket ID as the resource ID // Assign the bucket ID as the resource ID
d.Set("self_link", res.SelfLink)
d.SetId(res.Id) d.SetId(res.Id)
return nil return nil
} }
func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error { func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error {
// Only thing you can currently change is force_delete (all other properties have ForceNew) config := meta.(*Config)
// which is just terraform object state change, so nothing to do here
sb := &storage.Bucket{}
if d.HasChange("website") {
if v, ok := d.GetOk("website"); ok {
websites := v.([]interface{})
if len(websites) > 1 {
return fmt.Errorf("At most one website block is allowed")
}
// Setting fields to "" to be explicit that the PATCH call will
// delete this field.
if len(websites) == 0 {
sb.Website.NotFoundPage = ""
sb.Website.MainPageSuffix = ""
} else {
website := websites[0].(map[string]interface{})
sb.Website = &storage.BucketWebsite{}
if v, ok := website["not_found_page"]; ok {
sb.Website.NotFoundPage = v.(string)
} else {
sb.Website.NotFoundPage = ""
}
if v, ok := website["main_page_suffix"]; ok {
sb.Website.MainPageSuffix = v.(string)
} else {
sb.Website.MainPageSuffix = ""
}
}
}
}
res, err := config.clientStorage.Buckets.Patch(d.Get("name").(string), sb).Do()
if err != nil {
return err
}
log.Printf("[DEBUG] Patched bucket %v at location %v\n\n", res.Name, res.SelfLink)
// Assign the bucket ID as the resource ID
d.Set("self_link", res.SelfLink)
d.SetId(res.Id)
return nil return nil
} }
@ -90,6 +177,7 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Read bucket %v at location %v\n\n", res.Name, res.SelfLink) log.Printf("[DEBUG] Read bucket %v at location %v\n\n", res.Name, res.SelfLink)
// Update the bucket ID according to the resource ID // Update the bucket ID according to the resource ID
d.Set("self_link", res.SelfLink)
d.SetId(res.Id) d.SetId(res.Id)
return nil return nil

View File

@ -20,13 +20,30 @@ resource "google_storage_bucket" "image-store" {
name = "image-store-bucket" name = "image-store-bucket"
predefined_acl = "projectPrivate" predefined_acl = "projectPrivate"
location = "EU" location = "EU"
website {
main_page_suffix = "index.html"
not_found_page = "404.html"
}
} }
``` ```
## Argument Reference ## Argument Reference
The following arguments are supported:
* `name` - (Required) The name of the bucket. * `name` - (Required) The name of the bucket.
* `predefined_acl` - (Optional, Default: 'private') The [canned GCS ACL](https://cloud.google.com/storage/docs/access-control#predefined-acl) to apply. * `predefined_acl` - (Optional, Default: 'private') The [canned GCS ACL](https://cloud.google.com/storage/docs/access-control#predefined-acl) to apply.
* `location` - (Optional, Default: 'US') The [GCS location](https://cloud.google.com/storage/docs/bucket-locations) * `location` - (Optional, Default: 'US') The [GCS location](https://cloud.google.com/storage/docs/bucket-locations)
* `force_destroy` - (Optional, Default: false) When deleting a bucket, this boolean option will delete all contained objects. If you try to delete a bucket that contains objects, Terraform will fail that run. * `force_destroy` - (Optional, Default: false) When deleting a bucket, this boolean option will delete all contained objects. If you try to delete a bucket that contains objects, Terraform will fail that run.
The optional `website` block supports:
* `main_page_suffix` - (Optional) Behaves as the bucket's directory index where missing objects are treated as potential directories.
* `not_found_page` - (Optional) The custom object to return when a requested resource is not found.
## Attributes Reference
The following attributes are exported:
* `self_link` - The URI of the created resource.