Skip to content

resource/aws_s3_bucket_lifecycle_configuration: Correctly handles switching rule.filter attributes #42655

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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/42655.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_s3_bucket_lifecycle_configuration: Correctly handles switching child attributes of `rule.filter`.
```
127 changes: 69 additions & 58 deletions internal/service/s3/bucket_lifecycle_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int32planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
Expand Down Expand Up @@ -202,29 +201,27 @@ func (r *resourceBucketLifecycleConfiguration) Schema(ctx context.Context, reque
path.MatchRelative().AtName("tag"),
),
},
PlanModifiers: []planmodifier.Object{
emptyFilterPlanModifier(),
},
Attributes: map[string]schema.Attribute{
"object_size_greater_than": schema.Int64Attribute{
Optional: true,
Computed: true, // Because of Legacy value handling
PlanModifiers: []planmodifier.Int64{
tfint64planmodifier.NullValue(),
int64planmodifier.UseStateForUnknown(),
},
},
"object_size_less_than": schema.Int64Attribute{
Optional: true,
Computed: true, // Because of Legacy value handling
PlanModifiers: []planmodifier.Int64{
tfint64planmodifier.NullValue(),
int64planmodifier.UseStateForUnknown(),
},
},
names.AttrPrefix: schema.StringAttribute{
Optional: true,
Computed: true, // Because of Legacy value handling
PlanModifiers: []planmodifier.String{
ruleFilterPrefixForUnknown(),
},
},
},
Blocks: map[string]schema.Block{
Expand Down Expand Up @@ -1102,58 +1099,6 @@ func (m transitionDefaultMinimumObjectSizeDefaultModifier) PlanModifyString(ctx
resp.PlanValue = v
}

// ruleFilterPrefixForUnknown handles the planned value for `rule.filter.prefix`
// * If no value is set
// - If no other `filter` attributes are set, default to ""
// - Otherwise, default to `null`
func ruleFilterPrefixForUnknown() planmodifier.String {
return ruleFilterPrefixUnknownModifier{}
}

type ruleFilterPrefixUnknownModifier struct{}

func (m ruleFilterPrefixUnknownModifier) Description(_ context.Context) string {
return ""
}

func (m ruleFilterPrefixUnknownModifier) MarkdownDescription(ctx context.Context) string {
return m.Description(ctx)
}

func (m ruleFilterPrefixUnknownModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
// Nothing to do if a value is configured
if !req.ConfigValue.IsNull() {
return
}

// Do nothing if there is a known planned value.
if !req.PlanValue.IsUnknown() {
return
}

// Do nothing if there is an unknown configuration value, otherwise interpolation gets messed up.
if req.ConfigValue.IsUnknown() {
return
}

var parentConfig lifecycleRuleFilterModel
andPrefixPath := req.Path.ParentPath()
diags := req.Config.GetAttribute(ctx, andPrefixPath, &parentConfig)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}

if parentConfig.And.IsNull() &&
parentConfig.ObjectSizeGreaterThan.IsNull() &&
parentConfig.ObjectSizeLessThan.IsNull() &&
parentConfig.Tag.IsNull() {
resp.PlanValue = types.StringValue("")
} else {
resp.PlanValue = types.StringNull()
}
}

// ruleTransitionForUnknownDays handles the planned value for `rule.transition.days`
// * If no value is set
// - If `date` isn't set, default to 0
Expand Down Expand Up @@ -1265,3 +1210,69 @@ func (av warnExactlyOneOfChildrenValidator) ValidateObject(ctx context.Context,
))
}
}

func emptyFilterPlanModifier() planmodifier.Object {
return emptyFilterPlanModifier_{}
}

type emptyFilterPlanModifier_ struct{}

func (m emptyFilterPlanModifier_) Description(_ context.Context) string {
return ""
}

func (m emptyFilterPlanModifier_) MarkdownDescription(ctx context.Context) string {
return m.Description(ctx)
}

func (m emptyFilterPlanModifier_) PlanModifyObject(ctx context.Context, req planmodifier.ObjectRequest, resp *planmodifier.ObjectResponse) {
var config lifecycleRuleFilterModel
resp.Diagnostics.Append(req.ConfigValue.As(ctx, &config, basetypes.ObjectAsOptions{})...)
if resp.Diagnostics.HasError() {
return
}

// Do nothing if there is an unknown configuration value, otherwise interpolation gets messed up.
if config.And.IsUnknown() ||
config.ObjectSizeGreaterThan.IsUnknown() ||
config.ObjectSizeLessThan.IsUnknown() ||
config.Prefix.IsUnknown() ||
config.Tag.IsUnknown() {
return
}

var plan lifecycleRuleFilterModel
resp.Diagnostics.Append(req.PlanValue.As(ctx, &plan, basetypes.ObjectAsOptions{})...)
if resp.Diagnostics.HasError() {
return
}

if !config.And.IsNull() || !config.Tag.IsNull() {
plan.Prefix = types.StringNull()
p, d := fwtypes.NewObjectValueOf(ctx, &plan)
resp.Diagnostics.Append(d...)
if d.HasError() {
return
}
resp.PlanValue = p.ObjectValue
return
}

// If none are set in config, set Prefix to ""
if config.And.IsNull() &&
config.ObjectSizeGreaterThan.IsNull() &&
config.ObjectSizeLessThan.IsNull() &&
config.Prefix.IsNull() &&
config.Tag.IsNull() {
plan.Prefix = types.StringValue("")
p, d := fwtypes.NewObjectValueOf(ctx, &plan)
resp.Diagnostics.Append(d...)
if d.HasError() {
return
}
resp.PlanValue = p.ObjectValue
return
}

resp.PlanValue = req.ConfigValue
}
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,216 @@ func TestAccS3BucketLifecycleConfiguration_frameworkMigrationV0_filterWithEmptyP
})
}

func TestAccS3BucketLifecycleConfiguration_frameworkMigrationV0_Filter_objectSizeGreaterThanToPrefix(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_s3_bucket_lifecycle_configuration.test"
currTime := time.Now()
date := time.Date(currTime.Year(), currTime.Month()+1, currTime.Day(), 0, 0, 0, 0, time.UTC).Format(time.RFC3339)
dateUpdated := time.Date(currTime.Year()+1, currTime.Month(), currTime.Day(), 0, 0, 0, 0, time.UTC).Format(time.RFC3339)

var isSchemaV0 bool

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: schemaV0ErrorCheck(t, &isSchemaV0),
CheckDestroy: testAccCheckBucketLifecycleConfigurationDestroy(ctx),
Steps: []resource.TestStep{
{
PreConfig: schemaV0PreConfig(&isSchemaV0, true),
ExternalProviders: map[string]resource.ExternalProvider{
"aws": {
Source: "hashicorp/aws",
VersionConstraint: providerVersionSchemaV0,
},
"time": {
Source: "hashicorp/time",
VersionConstraint: "0.13.0",
},
},
Config: testAccBucketLifecycleConfigurationConfig_schemaV0(
testAccBucketLifecycleConfigurationConfig_filterObjectSizeGreaterThan(rName, date, 100),
),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketLifecycleConfigurationExists(ctx, resourceName),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.CompareValuePairs(resourceName, tfjsonpath.New(names.AttrBucket), "aws_s3_bucket.test", tfjsonpath.New(names.AttrBucket), compare.ValuesSame()),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrExpectedBucketOwner), knownvalue.StringExact("")),
statecheck.CompareValuePairs(resourceName, tfjsonpath.New(names.AttrID), resourceName, tfjsonpath.New(names.AttrBucket), compare.ValuesSame()),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrRule), knownvalue.ListExact([]knownvalue.Check{
knownvalue.ObjectExact(map[string]knownvalue.Check{
"abort_incomplete_multipart_upload": checkAbortIncompleteMultipartUpload_None(),
"expiration": checkExpiration_Date(date),
names.AttrFilter: checkSchemaV0Filter_GreaterThan(100),
names.AttrID: knownvalue.StringExact(rName),
"noncurrent_version_expiration": checkNoncurrentVersionExpiration_None(),
"noncurrent_version_transition": checkNoncurrentVersionTransitions(),
names.AttrPrefix: knownvalue.StringExact(""),
names.AttrStatus: knownvalue.StringExact(tfs3.LifecycleRuleStatusEnabled),
"transition": checkTransitions(),
}),
})),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("transition_default_minimum_object_size"), knownvalue.StringExact("all_storage_classes_128K")),
},
},
{
PreConfig: schemaV0PreConfig(&isSchemaV0, false),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Config: testAccBucketLifecycleConfigurationConfig_filterWithPrefix(rName, dateUpdated, "tmp/"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketLifecycleConfigurationExists(ctx, resourceName),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.CompareValuePairs(resourceName, tfjsonpath.New(names.AttrBucket), "aws_s3_bucket.test", tfjsonpath.New(names.AttrBucket), compare.ValuesSame()),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrExpectedBucketOwner), knownvalue.StringExact("")),
statecheck.CompareValuePairs(resourceName, tfjsonpath.New(names.AttrID), resourceName, tfjsonpath.New(names.AttrBucket), compare.ValuesSame()),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrRule), knownvalue.ListExact([]knownvalue.Check{
knownvalue.ObjectExact(map[string]knownvalue.Check{
"abort_incomplete_multipart_upload": checkAbortIncompleteMultipartUpload_None(),
"expiration": checkExpiration_Date(dateUpdated),
names.AttrFilter: checkFilter_Prefix("tmp/"),
names.AttrID: knownvalue.StringExact(rName),
"noncurrent_version_expiration": checkNoncurrentVersionExpiration_None(),
"noncurrent_version_transition": checkNoncurrentVersionTransitions(),
names.AttrPrefix: knownvalue.StringExact(""),
names.AttrStatus: knownvalue.StringExact(tfs3.LifecycleRuleStatusEnabled),
"transition": checkTransitions(),
}),
})),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("transition_default_minimum_object_size"), knownvalue.StringExact("all_storage_classes_128K")),
},
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
plancheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrRule), knownvalue.ListExact([]knownvalue.Check{
knownvalue.ObjectExact(map[string]knownvalue.Check{
"abort_incomplete_multipart_upload": checkAbortIncompleteMultipartUpload_None(),
"expiration": checkExpiration_Date(dateUpdated),
names.AttrFilter: checkFilter_Prefix("tmp/"),
names.AttrID: knownvalue.StringExact(rName),
"noncurrent_version_expiration": checkNoncurrentVersionExpiration_None(),
"noncurrent_version_transition": checkNoncurrentVersionTransitions(),
names.AttrPrefix: knownvalue.StringExact(""),
names.AttrStatus: knownvalue.StringExact(tfs3.LifecycleRuleStatusEnabled),
"transition": checkTransitions(),
}),
})),
},
PostApplyPreRefresh: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionNoop),
},
PostApplyPostRefresh: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionNoop),
},
},
},
},
})
}

func TestAccS3BucketLifecycleConfiguration_frameworkMigrationV0_Filter_objectSizeLessThanToPrefix(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_s3_bucket_lifecycle_configuration.test"
currTime := time.Now()
date := time.Date(currTime.Year(), currTime.Month()+1, currTime.Day(), 0, 0, 0, 0, time.UTC).Format(time.RFC3339)
dateUpdated := time.Date(currTime.Year()+1, currTime.Month(), currTime.Day(), 0, 0, 0, 0, time.UTC).Format(time.RFC3339)

var isSchemaV0 bool

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: schemaV0ErrorCheck(t, &isSchemaV0),
CheckDestroy: testAccCheckBucketLifecycleConfigurationDestroy(ctx),
Steps: []resource.TestStep{
{
PreConfig: schemaV0PreConfig(&isSchemaV0, true),
ExternalProviders: map[string]resource.ExternalProvider{
"aws": {
Source: "hashicorp/aws",
VersionConstraint: providerVersionSchemaV0,
},
"time": {
Source: "hashicorp/time",
VersionConstraint: "0.13.0",
},
},
Config: testAccBucketLifecycleConfigurationConfig_schemaV0(
testAccBucketLifecycleConfigurationConfig_filterObjectSizeLessThan(rName, date, 500),
),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketLifecycleConfigurationExists(ctx, resourceName),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.CompareValuePairs(resourceName, tfjsonpath.New(names.AttrBucket), "aws_s3_bucket.test", tfjsonpath.New(names.AttrBucket), compare.ValuesSame()),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrExpectedBucketOwner), knownvalue.StringExact("")),
statecheck.CompareValuePairs(resourceName, tfjsonpath.New(names.AttrID), resourceName, tfjsonpath.New(names.AttrBucket), compare.ValuesSame()),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrRule), knownvalue.ListExact([]knownvalue.Check{
knownvalue.ObjectExact(map[string]knownvalue.Check{
"abort_incomplete_multipart_upload": checkAbortIncompleteMultipartUpload_None(),
"expiration": checkExpiration_Date(date),
names.AttrFilter: checkSchemaV0Filter_LessThan(500),
names.AttrID: knownvalue.StringExact(rName),
"noncurrent_version_expiration": checkNoncurrentVersionExpiration_None(),
"noncurrent_version_transition": checkNoncurrentVersionTransitions(),
names.AttrPrefix: knownvalue.StringExact(""),
names.AttrStatus: knownvalue.StringExact(tfs3.LifecycleRuleStatusEnabled),
"transition": checkTransitions(),
}),
})),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("transition_default_minimum_object_size"), knownvalue.StringExact("all_storage_classes_128K")),
},
},
{
PreConfig: schemaV0PreConfig(&isSchemaV0, false),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Config: testAccBucketLifecycleConfigurationConfig_filterWithPrefix(rName, dateUpdated, "tmp/"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketLifecycleConfigurationExists(ctx, resourceName),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.CompareValuePairs(resourceName, tfjsonpath.New(names.AttrBucket), "aws_s3_bucket.test", tfjsonpath.New(names.AttrBucket), compare.ValuesSame()),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrExpectedBucketOwner), knownvalue.StringExact("")),
statecheck.CompareValuePairs(resourceName, tfjsonpath.New(names.AttrID), resourceName, tfjsonpath.New(names.AttrBucket), compare.ValuesSame()),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrRule), knownvalue.ListExact([]knownvalue.Check{
knownvalue.ObjectExact(map[string]knownvalue.Check{
"abort_incomplete_multipart_upload": checkAbortIncompleteMultipartUpload_None(),
"expiration": checkExpiration_Date(dateUpdated),
names.AttrFilter: checkFilter_Prefix("tmp/"),
names.AttrID: knownvalue.StringExact(rName),
"noncurrent_version_expiration": checkNoncurrentVersionExpiration_None(),
"noncurrent_version_transition": checkNoncurrentVersionTransitions(),
names.AttrPrefix: knownvalue.StringExact(""),
names.AttrStatus: knownvalue.StringExact(tfs3.LifecycleRuleStatusEnabled),
"transition": checkTransitions(),
}),
})),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("transition_default_minimum_object_size"), knownvalue.StringExact("all_storage_classes_128K")),
},
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
plancheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrRule), knownvalue.ListExact([]knownvalue.Check{
knownvalue.ObjectExact(map[string]knownvalue.Check{
"abort_incomplete_multipart_upload": checkAbortIncompleteMultipartUpload_None(),
"expiration": checkExpiration_Date(dateUpdated),
names.AttrFilter: checkFilter_Prefix("tmp/"),
names.AttrID: knownvalue.StringExact(rName),
"noncurrent_version_expiration": checkNoncurrentVersionExpiration_None(),
"noncurrent_version_transition": checkNoncurrentVersionTransitions(),
names.AttrPrefix: knownvalue.StringExact(""),
names.AttrStatus: knownvalue.StringExact(tfs3.LifecycleRuleStatusEnabled),
"transition": checkTransitions(),
}),
})),
},
},
},
},
})
}

func TestAccS3BucketLifecycleConfiguration_frameworkMigrationV0_Filter_ObjectSizeGreaterThan(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down
Loading
Loading