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

aws_rds_cluster: Fix Regression in serverlessv2_scaling_configuration Handling #40511

Merged
merged 4 commits into from
Dec 10, 2024
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/40511.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_rds_cluster: Fix `InvalidParameterValue: Serverless v2 maximum capacity 0.0 isn't valid. The maximum capacity must be at least 1.0.` errors when removing `serverlessv2_scaling_configuration` in an update
```
21 changes: 19 additions & 2 deletions internal/service/rds/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,22 @@ func resourceCluster() *schema.Resource {
names.AttrMaxCapacity: {
Type: schema.TypeFloat,
Required: true,
ValidateFunc: validation.FloatBetween(0, 256),
ValidateFunc: validation.FloatBetween(1, 256),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// Handles a breaking regression. On v5.79.0 and earlier,
// serverlessv2_scaling_configuration block could be removed from
// configuration. Although doing so doesn't actually remove the scaling
// configuration from AWS this dsf does allow the user to remove the block
// from their configuration without perpetual diffs.
// https://github.com/hashicorp/terraform-provider-aws/issues/40473
config := d.GetRawConfig()
raw := config.GetAttr("serverlessv2_scaling_configuration")

if raw.LengthInt() == 0 && (old != "0" && old != "") && (new == "0" || new == "") {
return true
}
return false
},
},
"min_capacity": {
Type: schema.TypeFloat,
Expand Down Expand Up @@ -2164,7 +2179,9 @@ func expandServerlessV2ScalingConfiguration(tfMap map[string]interface{}) *types

apiObject := &types.ServerlessV2ScalingConfiguration{}

if v, ok := tfMap[names.AttrMaxCapacity].(float64); ok {
// Removing the serverlessv2_scaling_configuration block from config for an update, sets max_capacity
// to 0. Sending 0 to the API causes an error.
if v, ok := tfMap[names.AttrMaxCapacity].(float64); ok && v != 0.0 {
apiObject.MaxCapacity = aws.Float64(v)
}

Expand Down
56 changes: 56 additions & 0 deletions internal/service/rds/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2123,6 +2123,43 @@ func TestAccRDSCluster_serverlessV2ScalingConfiguration(t *testing.T) {
})
}

func TestAccRDSCluster_serverlessV2ScalingRemoved(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var dbCluster types.DBCluster

rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_rds_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.RDSServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckClusterDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccClusterConfig_serverlessV2ScalingConfiguration(rName, 64.0, 2.5),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
resource.TestCheckResourceAttr(resourceName, "serverlessv2_scaling_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "serverlessv2_scaling_configuration.0.max_capacity", "64"),
resource.TestCheckResourceAttr(resourceName, "serverlessv2_scaling_configuration.0.min_capacity", "2.5"),
resource.TestCheckResourceAttrSet(resourceName, "serverlessv2_scaling_configuration.0.seconds_until_auto_pause"),
),
},
{
Config: testAccClusterConfig_serverlessV2ScalingRemoved(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
),
},
},
})
}

// Reference: https://github.com/hashicorp/terraform-provider-aws/issues/11698
func TestAccRDSCluster_Scaling_defaultMinCapacity(t *testing.T) {
ctx := acctest.Context(t)
Expand Down Expand Up @@ -5244,6 +5281,25 @@ resource "aws_rds_cluster" "test" {
`, tfrds.ClusterEngineAuroraPostgreSQL, rName, maxCapacity, minCapacity)
}

func testAccClusterConfig_serverlessV2ScalingRemoved(rName string) string {
return fmt.Sprintf(`
data "aws_rds_orderable_db_instance" "test" {
engine = %[1]q
engine_latest_version = true
preferred_instance_classes = ["db.serverless"]
}

resource "aws_rds_cluster" "test" {
cluster_identifier = %[2]q
master_password = "avoid-plaintext-passwords"
master_username = "tfacctest"
skip_final_snapshot = true
engine = data.aws_rds_orderable_db_instance.test.engine
engine_version = data.aws_rds_orderable_db_instance.test.engine_version
}
`, tfrds.ClusterEngineAuroraPostgreSQL, rName)
}

func testAccClusterConfig_serverlessV2ScalingConfigurationWithSecondsUntilAutoPause(rName string, maxCapacity, minCapacity float64, secondsUntilAutoPause int) string {
return fmt.Sprintf(`
data "aws_rds_orderable_db_instance" "test" {
Expand Down
Loading