Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/s3_bucket_website_configuration: add routing_rules parameter to assist in configuring rules with empty string values #24198

Merged
merged 3 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently not entirely sure if we should keep both params or deprecate routing_rule in favor of this one 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm or we can always deprecate one of these later if for example we migrate to the plugin-framework and that better supports empty string values in nested types

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