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

Initialize start time and end time value avoid null override #33713

Merged
Show file tree
Hide file tree
Changes from 3 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/33713.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_appautoscaling_scheduled_action: Avoid missing `StartTime` and `EndTime` when update scheduled action
```
18 changes: 18 additions & 0 deletions internal/service/appautoscaling/scheduled_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ func resourceScheduledActionPut(ctx context.Context, d *schema.ResourceData, met
ScalableDimension: aws.String(d.Get("scalable_dimension").(string)),
}

currentStartTime := getTimeIfExist(d, "start_time")
if currentStartTime != nil {
input.StartTime = aws.Time(currentStartTime.(time.Time))
}
currentEndTime := getTimeIfExist(d, "end_time")
if currentEndTime != nil {
input.EndTime = aws.Time(currentEndTime.(time.Time))
}
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved

needsPut := true
if d.IsNewResource() {
scheduledActionPopulateInputForCreate(input, d)
Expand Down Expand Up @@ -296,3 +305,12 @@ func flattenScalableTargetAction(cfg *applicationautoscaling.ScalableTargetActio

return []interface{}{m}
}

func getTimeIfExist(d *schema.ResourceData, timeKey string) interface{} {
val, ok := d.GetOk(timeKey)
if !ok {
return nil
}
timeVal, _ := time.Parse(time.RFC3339, val.(string))
return timeVal
}
272 changes: 272 additions & 0 deletions internal/service/appautoscaling/scheduled_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,177 @@ import (
tfappautoscaling "github.com/hashicorp/terraform-provider-aws/internal/service/appautoscaling"
)

func TestAccAppAutoScalingScheduledAction_UpdateScheduleRetainStartAndEndTime_ecs(t *testing.T) {
ctx := acctest.Context(t)
var sa applicationautoscaling.ScheduledAction
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
ts := time.Now().AddDate(0, 0, 1).Format("2006-01-02T15:04:05")
tsUpdate := time.Now().AddDate(0, 0, 2).Format("2006-01-02T15:04:05")
resourceName := "aws_appautoscaling_scheduled_action.test"
autoscalingTargetResourceName := "aws_appautoscaling_target.test"
startTime := time.Now().AddDate(0, 0, 2).Format("2006-01-02T15:04:05Z")
endTime := time.Now().AddDate(0, 0, 8).Format("2006-01-02T15:04:05Z")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, applicationautoscaling.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckScheduledActionDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccScheduledActionConfigWithStartAndEndTime_ecs(rName, ts, startTime, endTime),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckScheduledActionExistsAndContainStartTimeEndTime(ctx, resourceName, &sa, startTime, endTime),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrPair(resourceName, "service_namespace", autoscalingTargetResourceName, "service_namespace"),
resource.TestCheckResourceAttrPair(resourceName, "resource_id", autoscalingTargetResourceName, "resource_id"),
resource.TestCheckResourceAttrPair(resourceName, "scalable_dimension", autoscalingTargetResourceName, "scalable_dimension"),
resource.TestCheckResourceAttr(resourceName, "schedule", fmt.Sprintf("at(%s)", ts)),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.0.min_capacity", "1"),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.0.max_capacity", "5"),
resource.TestCheckResourceAttr(resourceName, "timezone", "UTC"),
resource.TestCheckResourceAttr(resourceName, "start_time", startTime),
resource.TestCheckResourceAttr(resourceName, "end_time", endTime),
acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "autoscaling", regexache.MustCompile(fmt.Sprintf("scheduledAction:.+:scheduledActionName/%s$", rName))),
),
},
{
Config: testAccScheduledActionConfigWithStartAndEndTime_ecs(rName, tsUpdate, startTime, endTime),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckScheduledActionExistsAndContainStartTimeEndTime(ctx, resourceName, &sa, startTime, endTime),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrPair(resourceName, "service_namespace", autoscalingTargetResourceName, "service_namespace"),
resource.TestCheckResourceAttrPair(resourceName, "resource_id", autoscalingTargetResourceName, "resource_id"),
resource.TestCheckResourceAttrPair(resourceName, "scalable_dimension", autoscalingTargetResourceName, "scalable_dimension"),
resource.TestCheckResourceAttr(resourceName, "schedule", fmt.Sprintf("at(%s)", tsUpdate)),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.0.min_capacity", "1"),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.0.max_capacity", "5"),
resource.TestCheckResourceAttr(resourceName, "timezone", "UTC"),
acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "autoscaling", regexache.MustCompile(fmt.Sprintf("scheduledAction:.+:scheduledActionName/%s$", rName))),
),
},
},
})
}

func TestAccAppAutoScalingScheduledAction_UpdateStartAndEndTime_ecs(t *testing.T) {
ctx := acctest.Context(t)
var sa applicationautoscaling.ScheduledAction
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
ts := time.Now().AddDate(0, 0, 1).Format("2006-01-02T15:04:05")
tsUpdate := time.Now().AddDate(0, 0, 2).Format("2006-01-02T15:04:05")
resourceName := "aws_appautoscaling_scheduled_action.test"
autoscalingTargetResourceName := "aws_appautoscaling_target.test"
startTime := time.Now().AddDate(0, 0, 2).Format("2006-01-02T15:04:05Z")
endTime := time.Now().AddDate(0, 0, 8).Format("2006-01-02T15:04:05Z")
startTimeUpdate := time.Now().AddDate(0, 0, 4).Format("2006-01-02T15:04:05Z")
endTimeUpdate := time.Now().AddDate(0, 0, 10).Format("2006-01-02T15:04:05Z")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, applicationautoscaling.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckScheduledActionDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccScheduledActionConfigWithStartAndEndTime_ecs(rName, ts, startTime, endTime),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckScheduledActionExistsAndContainStartTimeEndTime(ctx, resourceName, &sa, startTime, endTime),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrPair(resourceName, "service_namespace", autoscalingTargetResourceName, "service_namespace"),
resource.TestCheckResourceAttrPair(resourceName, "resource_id", autoscalingTargetResourceName, "resource_id"),
resource.TestCheckResourceAttrPair(resourceName, "scalable_dimension", autoscalingTargetResourceName, "scalable_dimension"),
resource.TestCheckResourceAttr(resourceName, "schedule", fmt.Sprintf("at(%s)", ts)),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.0.min_capacity", "1"),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.0.max_capacity", "5"),
resource.TestCheckResourceAttr(resourceName, "timezone", "UTC"),
resource.TestCheckResourceAttr(resourceName, "start_time", startTime),
resource.TestCheckResourceAttr(resourceName, "end_time", endTime),
acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "autoscaling", regexache.MustCompile(fmt.Sprintf("scheduledAction:.+:scheduledActionName/%s$", rName))),
),
},
{
Config: testAccScheduledActionConfigWithStartAndEndTime_ecs(rName, tsUpdate, startTimeUpdate, endTimeUpdate),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckScheduledActionExistsAndContainStartTimeEndTime(ctx, resourceName, &sa, startTimeUpdate, endTimeUpdate),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrPair(resourceName, "service_namespace", autoscalingTargetResourceName, "service_namespace"),
resource.TestCheckResourceAttrPair(resourceName, "resource_id", autoscalingTargetResourceName, "resource_id"),
resource.TestCheckResourceAttrPair(resourceName, "scalable_dimension", autoscalingTargetResourceName, "scalable_dimension"),
resource.TestCheckResourceAttr(resourceName, "schedule", fmt.Sprintf("at(%s)", tsUpdate)),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.0.min_capacity", "1"),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.0.max_capacity", "5"),
resource.TestCheckResourceAttr(resourceName, "timezone", "UTC"),
resource.TestCheckResourceAttr(resourceName, "start_time", startTimeUpdate),
resource.TestCheckResourceAttr(resourceName, "end_time", endTimeUpdate),
acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "autoscaling", regexache.MustCompile(fmt.Sprintf("scheduledAction:.+:scheduledActionName/%s$", rName))),
),
},
},
})
}

func TestAccAppAutoScalingScheduledAction_AddStartTimeAndEndTimeAfterResourceCreated_ecs(t *testing.T) {
ctx := acctest.Context(t)
var sa applicationautoscaling.ScheduledAction
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
ts := time.Now().AddDate(0, 0, 1).Format("2006-01-02T15:04:05")
tsUpdate := time.Now().AddDate(0, 0, 2).Format("2006-01-02T15:04:05")
resourceName := "aws_appautoscaling_scheduled_action.test"
autoscalingTargetResourceName := "aws_appautoscaling_target.test"
startTime := time.Now().AddDate(0, 0, 2).Format("2006-01-02T15:04:05Z")
endTime := time.Now().AddDate(0, 0, 8).Format("2006-01-02T15:04:05Z")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, applicationautoscaling.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckScheduledActionDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccScheduledActionConfig_ecs(rName, ts),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckScheduledActionExists(ctx, resourceName, &sa),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrPair(resourceName, "service_namespace", autoscalingTargetResourceName, "service_namespace"),
resource.TestCheckResourceAttrPair(resourceName, "resource_id", autoscalingTargetResourceName, "resource_id"),
resource.TestCheckResourceAttrPair(resourceName, "scalable_dimension", autoscalingTargetResourceName, "scalable_dimension"),
resource.TestCheckResourceAttr(resourceName, "schedule", fmt.Sprintf("at(%s)", ts)),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.0.min_capacity", "1"),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.0.max_capacity", "5"),
resource.TestCheckResourceAttr(resourceName, "timezone", "UTC"),
resource.TestCheckNoResourceAttr(resourceName, "start_time"),
resource.TestCheckNoResourceAttr(resourceName, "end_time"),
acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "autoscaling", regexache.MustCompile(fmt.Sprintf("scheduledAction:.+:scheduledActionName/%s$", rName))),
),
},
{
Config: testAccScheduledActionConfigWithStartAndEndTime_ecs(rName, tsUpdate, startTime, endTime),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckScheduledActionExistsAndContainStartTimeEndTime(ctx, resourceName, &sa, startTime, endTime),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrPair(resourceName, "service_namespace", autoscalingTargetResourceName, "service_namespace"),
resource.TestCheckResourceAttrPair(resourceName, "resource_id", autoscalingTargetResourceName, "resource_id"),
resource.TestCheckResourceAttrPair(resourceName, "scalable_dimension", autoscalingTargetResourceName, "scalable_dimension"),
resource.TestCheckResourceAttr(resourceName, "schedule", fmt.Sprintf("at(%s)", tsUpdate)),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.0.min_capacity", "1"),
resource.TestCheckResourceAttr(resourceName, "scalable_target_action.0.max_capacity", "5"),
resource.TestCheckResourceAttr(resourceName, "timezone", "UTC"),
resource.TestCheckResourceAttr(resourceName, "start_time", startTime),
resource.TestCheckResourceAttr(resourceName, "end_time", endTime),
acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "autoscaling", regexache.MustCompile(fmt.Sprintf("scheduledAction:.+:scheduledActionName/%s$", rName))),
),
},
},
})
}

func TestAccAppAutoScalingScheduledAction_dynamoDB(t *testing.T) {
ctx := acctest.Context(t)
var sa1, sa2 applicationautoscaling.ScheduledAction
Expand Down Expand Up @@ -593,6 +764,49 @@ func testAccCheckScheduledActionDestroy(ctx context.Context) resource.TestCheckF
}
}

func testAccCheckScheduledActionExistsAndContainStartTimeEndTime(ctx context.Context, name string, obj *applicationautoscaling.ScheduledAction, expectedStartTime, expectedEndTime string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

if rs.Primary.ID == "" {
return fmt.Errorf("Application Autoscaling scheduled action (%s) ID not set", name)
}

conn := acctest.Provider.Meta().(*conns.AWSClient).AppAutoScalingConn(ctx)

sa, err := tfappautoscaling.FindScheduledAction(ctx, conn, rs.Primary.Attributes["name"], rs.Primary.Attributes["service_namespace"], rs.Primary.Attributes["resource_id"])
if err != nil {
return err
}

if sa.StartTime == nil && len(expectedStartTime) != 0 {
return fmt.Errorf("Unexpected empty Start Time value for scheduled action (%s)", name)
}

st := sa.StartTime.Format("2006-01-02T15:04:05Z")

if st != expectedStartTime {
return fmt.Errorf("Unexpected stored start time, expected %s, got %s", expectedStartTime, st)
}

if sa.EndTime == nil && len(expectedEndTime) != 0 {
return fmt.Errorf("Unexpected empty End Time value for scheduled action (%s)", name)
}

et := sa.EndTime.Format("2006-01-02T15:04:05Z")
if et != expectedEndTime {
return fmt.Errorf("Unexpected stored end time, expected %s, got %s", expectedEndTime, et)
}

*obj = *sa

return nil
}
}

func testAccCheckScheduledActionExists(ctx context.Context, name string, obj *applicationautoscaling.ScheduledAction) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down Expand Up @@ -759,6 +973,64 @@ resource "aws_ecs_service" "test" {
`, rName, ts)
}

func testAccScheduledActionConfigWithStartAndEndTime_ecs(rName, ts, startTime, endTime string) string {
return fmt.Sprintf(`
resource "aws_appautoscaling_scheduled_action" "test" {
name = %[1]q
service_namespace = aws_appautoscaling_target.test.service_namespace
resource_id = aws_appautoscaling_target.test.resource_id
scalable_dimension = aws_appautoscaling_target.test.scalable_dimension
schedule = "at(%[2]s)"

start_time = %[3]q
end_time = %[4]q

scalable_target_action {
min_capacity = 1
max_capacity = 5
}
}

resource "aws_appautoscaling_target" "test" {
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.test.name}/${aws_ecs_service.test.name}"
scalable_dimension = "ecs:service:DesiredCount"
min_capacity = 1
max_capacity = 3
}

resource "aws_ecs_cluster" "test" {
name = %[1]q
}

resource "aws_ecs_task_definition" "test" {
family = %[1]q

container_definitions = <<EOF
[
{
"name": "busybox",
"image": "busybox:latest",
"cpu": 10,
"memory": 128,
"essential": true
}
]
EOF
}

resource "aws_ecs_service" "test" {
name = %[1]q
cluster = aws_ecs_cluster.test.id
task_definition = aws_ecs_task_definition.test.arn
desired_count = 1

deployment_maximum_percent = 200
deployment_minimum_healthy_percent = 50
}
`, rName, ts, startTime, endTime)
}

func testAccScheduledActionConfig_emr(rName, ts string) string {
return fmt.Sprintf(`
resource "aws_appautoscaling_scheduled_action" "test" {
Expand Down
Loading