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: make object_lock_configuration.rule configurable #23984

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/23984.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_s3_bucket: Update `object_lock_configuration.rule` parameter to be configurable. Please refer to the documentation for details on drift detection and potential conflicts when configuring this parameter with the standalone `aws_s3_bucket_object_lock_configuration` resource.
```
72 changes: 49 additions & 23 deletions internal/service/s3/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,52 +628,56 @@ func ResourceBucket() *schema.Resource {
Optional: true,
Computed: true, // Can be removed when object_lock_configuration.0.object_lock_enabled is removed
ForceNew: true,
ConflictsWith: []string{"object_lock_configuration.0.object_lock_enabled"},
ConflictsWith: []string{"object_lock_configuration"},
},

"object_lock_configuration": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Deprecated: "Use the top-level parameter object_lock_enabled and the aws_s3_bucket_object_lock_configuration resource instead",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"object_lock_enabled": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(s3.ObjectLockEnabled_Values(), false),
Deprecated: "Use the top-level parameter object_lock_enabled instead",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"object_lock_enabled"},
ValidateFunc: validation.StringInSlice(s3.ObjectLockEnabled_Values(), false),
Deprecated: "Use the top-level parameter object_lock_enabled instead",
},

"rule": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Deprecated: "Use the aws_s3_bucket_object_lock_configuration resource instead",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"default_retention": {
Type: schema.TypeList,
Computed: true,
Deprecated: "Use the aws_s3_bucket_object_lock_configuration resource instead",
Type: schema.TypeList,
Required: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"mode": {
Type: schema.TypeString,
Computed: true,
Deprecated: "Use the aws_s3_bucket_object_lock_configuration resource instead",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(s3.ObjectLockRetentionMode_Values(), false),
},

"days": {
Type: schema.TypeInt,
Computed: true,
Deprecated: "Use the aws_s3_bucket_object_lock_configuration resource instead",
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(1),
},

"years": {
Type: schema.TypeInt,
Computed: true,
Deprecated: "Use the aws_s3_bucket_object_lock_configuration resource instead",
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(1),
},
},
},
Expand Down Expand Up @@ -2440,6 +2444,28 @@ func expandS3ObjectLockConfiguration(vConf []interface{}) *s3.ObjectLockConfigur
conf.ObjectLockEnabled = aws.String(vObjectLockEnabled)
}

if vRule, ok := mConf["rule"].([]interface{}); ok && len(vRule) > 0 {
mRule := vRule[0].(map[string]interface{})

if vDefaultRetention, ok := mRule["default_retention"].([]interface{}); ok && len(vDefaultRetention) > 0 && vDefaultRetention[0] != nil {
mDefaultRetention := vDefaultRetention[0].(map[string]interface{})

conf.Rule = &s3.ObjectLockRule{
DefaultRetention: &s3.DefaultRetention{},
}

if vMode, ok := mDefaultRetention["mode"].(string); ok && vMode != "" {
conf.Rule.DefaultRetention.Mode = aws.String(vMode)
}
if vDays, ok := mDefaultRetention["days"].(int); ok && vDays > 0 {
conf.Rule.DefaultRetention.Days = aws.Int64(int64(vDays))
}
if vYears, ok := mDefaultRetention["years"].(int); ok && vYears > 0 {
conf.Rule.DefaultRetention.Years = aws.Int64(int64(vYears))
}
}
}

return conf
}

Expand Down
72 changes: 72 additions & 0 deletions internal/service/s3/bucket_object_lock_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,78 @@ func TestAccS3BucketObjectLockConfiguration_update(t *testing.T) {
})
}

func TestAccS3BucketObjectLockConfiguration_migrate_noChange(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_s3_bucket_object_lock_configuration.test"
bucketResourceName := "aws_s3_bucket.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, s3.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckBucketObjectLockConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccBucketConfig_ObjectLockEnabledWithDefaultRetention(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketExists(bucketResourceName),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.#", "1"),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.0.object_lock_enabled", s3.ObjectLockEnabledEnabled),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.0.rule.#", "1"),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.0.rule.0.default_retention.0.mode", s3.ObjectLockRetentionModeCompliance),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.0.rule.0.default_retention.0.days", "3"),
),
},
{
Config: testAccBucketObjectLockConfigurationBasicConfig(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketObjectLockConfigurationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "object_lock_enabled", s3.ObjectLockEnabledEnabled),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.default_retention.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.default_retention.0.days", "3"),
resource.TestCheckResourceAttr(resourceName, "rule.0.default_retention.0.mode", s3.ObjectLockRetentionModeCompliance),
),
},
},
})
}

func TestAccS3BucketObjectLockConfiguration_migrate_withChange(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_s3_bucket_object_lock_configuration.test"
bucketResourceName := "aws_s3_bucket.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, s3.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckBucketObjectLockConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccBucketConfig_ObjectLockEnabledNoDefaultRetention(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketExists(bucketResourceName),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.#", "1"),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.0.object_lock_enabled", s3.ObjectLockEnabledEnabled),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.0.rule.#", "0"),
),
},
{
Config: testAccBucketObjectLockConfigurationBasicConfig(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketObjectLockConfigurationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "object_lock_enabled", s3.ObjectLockEnabledEnabled),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.default_retention.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.default_retention.0.days", "3"),
resource.TestCheckResourceAttr(resourceName, "rule.0.default_retention.0.mode", s3.ObjectLockRetentionModeCompliance),
),
},
},
})
}

func testAccCheckBucketObjectLockConfigurationDestroy(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).S3Conn

Expand Down
42 changes: 36 additions & 6 deletions internal/service/s3/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ func TestAccS3Bucket_Manage_objectLock(t *testing.T) {
CheckDestroy: testAccCheckBucketDestroy,
Steps: []resource.TestStep{
{
Config: testAccObjectLockEnabledNoDefaultRetention(bucketName),
Config: testAccBucketConfig_ObjectLockEnabledNoDefaultRetention(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckBucketExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "object_lock_enabled", "true"),
Expand All @@ -800,6 +800,17 @@ func TestAccS3Bucket_Manage_objectLock(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
{
Config: testAccBucketConfig_ObjectLockEnabledWithDefaultRetention(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckBucketExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "object_lock_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "object_lock_configuration.0.object_lock_enabled", "Enabled"),
resource.TestCheckResourceAttr(resourceName, "object_lock_configuration.0.rule.#", "1"),
resource.TestCheckResourceAttr(resourceName, "object_lock_configuration.0.rule.0.default_retention.0.mode", "COMPLIANCE"),
resource.TestCheckResourceAttr(resourceName, "object_lock_configuration.0.rule.0.default_retention.0.days", "3"),
),
},
},
})
}
Expand All @@ -815,7 +826,7 @@ func TestAccS3Bucket_Manage_objectLock_deprecatedEnabled(t *testing.T) {
CheckDestroy: testAccCheckBucketDestroy,
Steps: []resource.TestStep{
{
Config: testAccObjectLockEnabledNoDefaultRetention_deprecatedEnabled(bucketName),
Config: testAccBucketConfig_ObjectLockEnabledNoDefaultRetention_deprecatedEnabled(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckBucketExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "object_lock_enabled", "true"),
Expand Down Expand Up @@ -845,7 +856,7 @@ func TestAccS3Bucket_Manage_objectLock_migrate(t *testing.T) {
CheckDestroy: testAccCheckBucketDestroy,
Steps: []resource.TestStep{
{
Config: testAccObjectLockEnabledNoDefaultRetention_deprecatedEnabled(bucketName),
Config: testAccBucketConfig_ObjectLockEnabledNoDefaultRetention_deprecatedEnabled(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckBucketExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "object_lock_enabled", "true"),
Expand All @@ -854,7 +865,7 @@ func TestAccS3Bucket_Manage_objectLock_migrate(t *testing.T) {
),
},
{
Config: testAccObjectLockEnabledNoDefaultRetention(bucketName),
Config: testAccBucketConfig_ObjectLockEnabledNoDefaultRetention(bucketName),
PlanOnly: true,
},
},
Expand Down Expand Up @@ -4480,7 +4491,7 @@ resource "aws_s3_bucket_acl" "test6" {
`, randInt)
}

func testAccObjectLockEnabledNoDefaultRetention(bucketName string) string {
func testAccBucketConfig_ObjectLockEnabledNoDefaultRetention(bucketName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "test" {
bucket = %[1]q
Expand All @@ -4490,7 +4501,26 @@ resource "aws_s3_bucket" "test" {
`, bucketName)
}

func testAccObjectLockEnabledNoDefaultRetention_deprecatedEnabled(bucketName string) string {
func testAccBucketConfig_ObjectLockEnabledWithDefaultRetention(bucketName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "test" {
bucket = %[1]q

object_lock_configuration {
object_lock_enabled = "Enabled"

rule {
default_retention {
mode = "COMPLIANCE"
days = 3
}
}
}
}
`, bucketName)
}

func testAccBucketConfig_ObjectLockEnabledNoDefaultRetention_deprecatedEnabled(bucketName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "test" {
bucket = %[1]q
Expand Down
Loading