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 5 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: Always send `start_time` and `end_time` values on update when configured
```
16 changes: 6 additions & 10 deletions internal/service/appautoscaling/scheduled_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,19 @@ func resourceScheduledActionPut(ctx context.Context, d *schema.ResourceData, met
}
input.Timezone = aws.String(d.Get("timezone").(string))
} else {
if d.HasChange("end_time") {
if v, ok := d.GetOk("end_time"); ok {
t, _ := time.Parse(time.RFC3339, v.(string))
input.EndTime = aws.Time(t)
}
if v, ok := d.GetOk("end_time"); ok {
t, _ := time.Parse(time.RFC3339, v.(string))
input.EndTime = aws.Time(t)
}
if d.HasChange("scalable_target_action") {
input.ScalableTargetAction = expandScalableTargetAction(d.Get("scalable_target_action").([]interface{}))
}
if d.HasChange("schedule") {
input.Schedule = aws.String(d.Get("schedule").(string))
}
if d.HasChange("start_time") {
if v, ok := d.GetOk("start_time"); ok {
t, _ := time.Parse(time.RFC3339, v.(string))
input.StartTime = aws.Time(t)
}
if v, ok := d.GetOk("start_time"); ok {
t, _ := time.Parse(time.RFC3339, v.(string))
input.StartTime = aws.Time(t)
}
if d.HasChange("timezone") {
input.Timezone = aws.String(d.Get("timezone").(string))
Expand Down
228 changes: 228 additions & 0 deletions internal/service/appautoscaling/scheduled_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,177 @@ func TestAccAppAutoScalingScheduledAction_ecs(t *testing.T) {
})
}

func TestAccAppAutoScalingScheduledAction_ecsUpdateScheduleRetainStartAndEndTime(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_ecsWithStartAndEndTime(rName, ts, startTime, endTime),
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.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: testAccScheduledActionConfig_ecsWithStartAndEndTime(rName, tsUpdate, startTime, endTime),
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)", 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_ecsUpdateStartAndEndTime(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: testAccScheduledActionConfig_ecsWithStartAndEndTime(rName, ts, startTime, endTime),
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.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: testAccScheduledActionConfig_ecsWithStartAndEndTime(rName, tsUpdate, startTimeUpdate, endTimeUpdate),
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)", 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_ecsAddStartTimeAndEndTimeAfterResourceCreated(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: testAccScheduledActionConfig_ecsWithStartAndEndTime(rName, tsUpdate, startTime, endTime),
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)", 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_emr(t *testing.T) {
ctx := acctest.Context(t)
var sa applicationautoscaling.ScheduledAction
Expand Down Expand Up @@ -784,6 +955,63 @@ resource "aws_ecs_service" "test" {
`, rName, ts)
}

func testAccScheduledActionConfig_ecsWithStartAndEndTime(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 acctest.ConfigCompose(acctest.ConfigAvailableAZsNoOptInDefaultExclude(), fmt.Sprintf(`
resource "aws_appautoscaling_scheduled_action" "test" {
Expand Down
Loading