Skip to content

Commit

Permalink
Fix cloud storage bucket website params (#3977)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored and chrisst committed Jul 17, 2019
1 parent 9ae5c3d commit be55e43
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 46 deletions.
88 changes: 42 additions & 46 deletions google/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func resourceStorageBucket() *schema.Resource {
"website": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"main_page_suffix": {
Expand Down Expand Up @@ -305,23 +306,7 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error
}

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)
}
sb.Website = expandBucketWebsite(v.([]interface{}))
}

if v, ok := d.GetOk("cors"); ok {
Expand Down Expand Up @@ -388,35 +373,7 @@ func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error
}

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")
}

sb.Website = &storage.BucketWebsite{}

// Setting fields to "" to be explicit that the PATCH call will
// delete this field.
if len(websites) == 0 || websites[0] == nil {
sb.Website.NotFoundPage = ""
sb.Website.MainPageSuffix = ""
} else {
website := websites[0].(map[string]interface{})
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 = ""
}
}
}
sb.Website = expandBucketWebsite(d.Get("website"))
}

if v, ok := d.GetOk("cors"); ok {
Expand Down Expand Up @@ -524,6 +481,8 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error {
d.Set("versioning", flattenBucketVersioning(res.Versioning))
d.Set("lifecycle_rule", flattenBucketLifecycle(res.Lifecycle))
d.Set("labels", res.Labels)
d.Set("website", flattenBucketWebsite(res.Website))

if res.IamConfiguration != nil && res.IamConfiguration.BucketPolicyOnly != nil {
d.Set("bucket_policy_only", res.IamConfiguration.BucketPolicyOnly.Enabled)
} else {
Expand Down Expand Up @@ -798,6 +757,43 @@ func flattenBucketLifecycleRuleCondition(condition *storage.BucketLifecycleRuleC
return ruleCondition
}

func flattenBucketWebsite(website *storage.BucketWebsite) []map[string]interface{} {
if website == nil {
return nil
}
websites := make([]map[string]interface{}, 0, 1)
websites = append(websites, map[string]interface{}{
"main_page_suffix": website.MainPageSuffix,
"not_found_page": website.NotFoundPage,
})

return websites
}

func expandBucketWebsite(v interface{}) *storage.BucketWebsite {
if v == nil {
return nil
}
vs := v.([]interface{})

if len(vs) < 1 || vs[0] == nil {
return nil
}

website := vs[0].(map[string]interface{})
w := &storage.BucketWebsite{}

if v := website["not_found_page"]; v != "" {
w.NotFoundPage = v.(string)
}

if v := website["main_page_suffix"]; v != "" {
w.MainPageSuffix = v.(string)
}

return w
}

func expandIamConfiguration(d *schema.ResourceData) *storage.BucketIamConfiguration {
return &storage.BucketIamConfiguration{
ForceSendFields: []string{"BucketPolicyOnly"},
Expand Down
37 changes: 37 additions & 0 deletions google/resource_storage_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,28 @@ func TestAccStorageBucket_labels(t *testing.T) {
})
}

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

bucketSuffix := acctest.RandomWithPrefix("tf-website-test")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccStorageBucketDestroy,
Steps: []resource.TestStep{
{
Config: testAccStorageBucket_website(bucketSuffix),
},
{
ResourceName: "google_storage_bucket.website",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

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 @@ -1340,3 +1362,18 @@ resource "google_storage_bucket" "bucket" {
}
`, bucketName)
}

func testAccStorageBucket_website(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "website" {
name = "%s.gcp.tfacc.hashicorptest.com"
location = "US"
storage_class = "MULTI_REGIONAL"
website {
main_page_suffix = "index.html"
not_found_page = "404.html"
}
}
`, bucketName)
}

0 comments on commit be55e43

Please sign in to comment.