From a5c48a14e66fa9f39b169e7b4a1f2e9465aaa3c0 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sat, 4 Jan 2025 13:58:04 +0100 Subject: [PATCH 1/3] Fix removing scaling configuration --- internal/service/codebuild/fleet.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/service/codebuild/fleet.go b/internal/service/codebuild/fleet.go index 6e9fe8e9207..f0f4025495c 100644 --- a/internal/service/codebuild/fleet.go +++ b/internal/service/codebuild/fleet.go @@ -339,6 +339,8 @@ func resourceFleetUpdate(ctx context.Context, d *schema.ResourceData, meta inter if d.HasChange("scaling_configuration") { if v, ok := d.GetOk("scaling_configuration"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil { input.ScalingConfiguration = expandScalingConfiguration(v.([]interface{})[0].(map[string]interface{})) + } else { + input.ScalingConfiguration = &types.ScalingConfigurationInput{} } } From 83893c6283e4adb0a1d59f1841b35f1005c3e20c Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sat, 4 Jan 2025 13:58:25 +0100 Subject: [PATCH 2/3] Fix persistent scaling configuration diff after removal --- internal/service/codebuild/fleet.go | 18 ++++++++++-------- internal/service/codebuild/fleet_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/internal/service/codebuild/fleet.go b/internal/service/codebuild/fleet.go index f0f4025495c..67999e4d6e1 100644 --- a/internal/service/codebuild/fleet.go +++ b/internal/service/codebuild/fleet.go @@ -280,13 +280,11 @@ func resourceFleetRead(ctx context.Context, d *schema.ResourceData, meta interfa d.Set("last_modified", aws.ToTime(fleet.LastModified).Format(time.RFC3339)) d.Set(names.AttrName, fleet.Name) d.Set("overflow_behavior", fleet.OverflowBehavior) - if fleet.ScalingConfiguration != nil { - if err := d.Set("scaling_configuration", []interface{}{flattenScalingConfiguration(fleet.ScalingConfiguration)}); err != nil { - return create.AppendDiagError(diags, names.CodeBuild, create.ErrActionSetting, resNameFleet, d.Id(), err) - } - } else { - d.Set("scaling_configuration", nil) + + if err := d.Set("scaling_configuration", flattenScalingConfiguration(fleet.ScalingConfiguration)); err != nil { + return create.AppendDiagError(diags, names.CodeBuild, create.ErrActionSetting, resNameFleet, d.Id(), err) } + if fleet.Status != nil { if err := d.Set(names.AttrStatus, []interface{}{flattenStatus(fleet.Status)}); err != nil { return create.AppendDiagError(diags, names.CodeBuild, create.ErrActionSetting, resNameFleet, d.Id(), err) @@ -586,7 +584,7 @@ func expandTargetTrackingScalingConfig(tfMap map[string]interface{}) *types.Targ return apiObject } -func flattenScalingConfiguration(apiObject *types.ScalingConfigurationOutput) map[string]interface{} { +func flattenScalingConfiguration(apiObject *types.ScalingConfigurationOutput) []interface{} { if apiObject == nil { return nil } @@ -609,7 +607,11 @@ func flattenScalingConfiguration(apiObject *types.ScalingConfigurationOutput) ma tfMap["target_tracking_scaling_configs"] = flattenTargetTrackingScalingConfigs(v) } - return tfMap + if len(tfMap) == 0 { + return nil + } + + return []interface{}{tfMap} } func flattenTargetTrackingScalingConfigs(apiObjects []types.TargetTrackingScalingConfiguration) []interface{} { diff --git a/internal/service/codebuild/fleet_test.go b/internal/service/codebuild/fleet_test.go index 041c8e58f1d..005c9380988 100644 --- a/internal/service/codebuild/fleet_test.go +++ b/internal/service/codebuild/fleet_test.go @@ -251,6 +251,11 @@ func TestAccCodeBuildFleet_scalingConfiguration(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "scaling_configuration.0.target_tracking_scaling_configs.0.target_value", "97.5"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, { Config: testAccFleetConfig_scalingConfiguration2(rName), Check: resource.ComposeTestCheckFunc( @@ -263,6 +268,13 @@ func TestAccCodeBuildFleet_scalingConfiguration(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "scaling_configuration.0.target_tracking_scaling_configs.0.target_value", "90.5"), ), }, + { + Config: testAccFleetConfig_scalingConfiguration3(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckFleetExists(ctx, resourceName), + resource.TestCheckResourceAttr(resourceName, "scaling_configuration.#", "0"), + ), + }, }, }) } @@ -455,6 +467,18 @@ resource "aws_codebuild_fleet" "test" { `, rName) } +func testAccFleetConfig_scalingConfiguration3(rName string) string { + return fmt.Sprintf(` +resource "aws_codebuild_fleet" "test" { + base_capacity = 1 + compute_type = "BUILD_GENERAL1_SMALL" + environment_type = "ARM_CONTAINER" + name = %[1]q + overflow_behavior = "QUEUE" +} +`, rName) +} + func testAccFleetConfig_baseFleetServiceRole(rName string) string { return fmt.Sprintf(` resource "aws_iam_role" "test" { From 8084c967d70a2fe24886fb18d81315c94f5ea0f7 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sat, 4 Jan 2025 14:09:54 +0100 Subject: [PATCH 3/3] Add changelog --- .changelog/40773.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/40773.txt diff --git a/.changelog/40773.txt b/.changelog/40773.txt new file mode 100644 index 00000000000..77a16f6b099 --- /dev/null +++ b/.changelog/40773.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_codebuild_fleet: Allow `scaling_configuration` to be removed on Update +```