Skip to content

Commit

Permalink
Merge pull request #24198 from hashicorp/b-s3-bucket-website-config-r…
Browse files Browse the repository at this point in the history
…outing-rules

r/s3_bucket_website_configuration: add `routing_rules` parameter to assist in configuring rules with empty string values
  • Loading branch information
anGie44 authored May 19, 2022
2 parents 6d6b403 + 54e1e62 commit 1f5f56a
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .changelog/24198.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_s3_bucket_website_configuration: Add `routing_rules` parameter to be used instead of `routing_rule` to support configurations with empty String values
```
63 changes: 59 additions & 4 deletions internal/service/s3/bucket_website_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package s3

import (
"context"
"encoding/json"
"fmt"
"log"
"time"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -74,6 +76,7 @@ func ResourceBucketWebsiteConfiguration() *schema.Resource {
"error_document",
"index_document",
"routing_rule",
"routing_rules",
},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -90,8 +93,10 @@ func ResourceBucketWebsiteConfiguration() *schema.Resource {
},
},
"routing_rule": {
Type: schema.TypeList,
Optional: true,
Type: schema.TypeList,
Optional: true,
Computed: true,
ConflictsWith: []string{"routing_rules"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"condition": {
Expand Down Expand Up @@ -144,6 +149,17 @@ func ResourceBucketWebsiteConfiguration() *schema.Resource {
},
},
},
"routing_rules": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"routing_rule"},
ValidateFunc: validation.StringIsJSON,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
},
"website_endpoint": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -180,6 +196,14 @@ func resourceBucketWebsiteConfigurationCreate(ctx context.Context, d *schema.Res
websiteConfig.RoutingRules = expandBucketWebsiteConfigurationRoutingRules(v.([]interface{}))
}

if v, ok := d.GetOk("routing_rules"); ok {
var unmarshalledRules []*s3.RoutingRule
if err := json.Unmarshal([]byte(v.(string)), &unmarshalledRules); err != nil {
return diag.FromErr(fmt.Errorf("error creating S3 Bucket (%s) website configuration: %w", bucket, err))
}
websiteConfig.RoutingRules = unmarshalledRules
}

input := &s3.PutBucketWebsiteInput{
Bucket: aws.String(bucket),
WebsiteConfiguration: websiteConfig,
Expand Down Expand Up @@ -254,6 +278,16 @@ func resourceBucketWebsiteConfigurationRead(ctx context.Context, d *schema.Resou
return diag.FromErr(fmt.Errorf("error setting routing_rule: %w", err))
}

if output.RoutingRules != nil {
rr, err := normalizeRoutingRules(output.RoutingRules)
if err != nil {
return diag.FromErr(fmt.Errorf("error while marshaling routing rules: %w", err))
}
d.Set("routing_rules", rr)
} else {
d.Set("routing_rules", nil)
}

// Add website_endpoint and website_domain as attributes
websiteEndpoint, err := resourceBucketWebsiteConfigurationWebsiteEndpoint(ctx, meta.(*conns.AWSClient), bucket, expectedBucketOwner)
if err != nil {
Expand Down Expand Up @@ -290,8 +324,29 @@ func resourceBucketWebsiteConfigurationUpdate(ctx context.Context, d *schema.Res
websiteConfig.RedirectAllRequestsTo = expandBucketWebsiteConfigurationRedirectAllRequestsTo(v.([]interface{}))
}

if v, ok := d.GetOk("routing_rule"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
websiteConfig.RoutingRules = expandBucketWebsiteConfigurationRoutingRules(v.([]interface{}))
if d.HasChanges("routing_rule", "routing_rules") {
if d.HasChange("routing_rule") {
websiteConfig.RoutingRules = expandBucketWebsiteConfigurationRoutingRules(d.Get("routing_rule").([]interface{}))
} else {
var unmarshalledRules []*s3.RoutingRule
if err := json.Unmarshal([]byte(d.Get("routing_rules").(string)), &unmarshalledRules); err != nil {
return diag.FromErr(fmt.Errorf("error updating S3 Bucket (%s) website configuration: %w", bucket, err))
}
websiteConfig.RoutingRules = unmarshalledRules
}
} else {
// Still send the current RoutingRules configuration
if v, ok := d.GetOk("routing_rule"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
websiteConfig.RoutingRules = expandBucketWebsiteConfigurationRoutingRules(v.([]interface{}))
}

if v, ok := d.GetOk("routing_rules"); ok {
var unmarshalledRules []*s3.RoutingRule
if err := json.Unmarshal([]byte(v.(string)), &unmarshalledRules); err != nil {
return diag.FromErr(fmt.Errorf("error updating S3 Bucket (%s) website configuration: %w", bucket, err))
}
websiteConfig.RoutingRules = unmarshalledRules
}
}

input := &s3.PutBucketWebsiteInput{
Expand Down
Loading

0 comments on commit 1f5f56a

Please sign in to comment.