diff --git a/builtin/providers/aws/resource_aws_autoscaling_schedule.go b/builtin/providers/aws/resource_aws_autoscaling_schedule.go index 200b0ee37..5b43db46d 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_schedule.go +++ b/builtin/providers/aws/resource_aws_autoscaling_schedule.go @@ -113,11 +113,17 @@ func resourceAwsAutoscalingScheduleCreate(d *schema.ResourceData, meta interface } func resourceAwsAutoscalingScheduleRead(d *schema.ResourceData, meta interface{}) error { - sa, err := resourceAwsASGScheduledActionRetrieve(d, meta) + sa, err, exists := resourceAwsASGScheduledActionRetrieve(d, meta) if err != nil { return err } + if !exists { + log.Printf("Error retrieving Autoscaling Scheduled Actions. Removing from state") + d.SetId("") + return nil + } + d.Set("autoscaling_group_name", sa.AutoScalingGroupName) d.Set("arn", sa.ScheduledActionARN) d.Set("desired_capacity", sa.DesiredCapacity) @@ -153,7 +159,7 @@ func resourceAwsAutoscalingScheduleDelete(d *schema.ResourceData, meta interface return nil } -func resourceAwsASGScheduledActionRetrieve(d *schema.ResourceData, meta interface{}) (*autoscaling.ScheduledUpdateGroupAction, error) { +func resourceAwsASGScheduledActionRetrieve(d *schema.ResourceData, meta interface{}) (*autoscaling.ScheduledUpdateGroupAction, error, bool) { autoscalingconn := meta.(*AWSClient).autoscalingconn params := &autoscaling.DescribeScheduledActionsInput{ @@ -164,13 +170,13 @@ func resourceAwsASGScheduledActionRetrieve(d *schema.ResourceData, meta interfac log.Printf("[INFO] Describing Autoscaling Scheduled Action: %+v", params) actions, err := autoscalingconn.DescribeScheduledActions(params) if err != nil { - return nil, fmt.Errorf("Error retrieving Autoscaling Scheduled Actions: %s", err) + return nil, fmt.Errorf("Error retrieving Autoscaling Scheduled Actions: %s", err), false } if len(actions.ScheduledUpdateGroupActions) != 1 || *actions.ScheduledUpdateGroupActions[0].ScheduledActionName != d.Id() { - return nil, fmt.Errorf("Unable to find Autoscaling Scheduled Action: %#v", actions.ScheduledUpdateGroupActions) + return nil, nil, false } - return actions.ScheduledUpdateGroupActions[0], nil + return actions.ScheduledUpdateGroupActions[0], nil, true } diff --git a/builtin/providers/aws/resource_aws_autoscaling_schedule_test.go b/builtin/providers/aws/resource_aws_autoscaling_schedule_test.go index c332e1400..3acbbcf90 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_schedule_test.go +++ b/builtin/providers/aws/resource_aws_autoscaling_schedule_test.go @@ -28,6 +28,40 @@ func TestAccAWSAutoscalingSchedule_basic(t *testing.T) { }) } +func TestAccAWSAutoscalingSchedule_disappears(t *testing.T) { + var schedule autoscaling.ScheduledUpdateGroupAction + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAutoscalingScheduleDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAutoscalingScheduleConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule), + testAccCheckScalingScheduleDisappears(&schedule), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccCheckScalingScheduleDisappears(schedule *autoscaling.ScheduledUpdateGroupAction) resource.TestCheckFunc { + return func(s *terraform.State) error { + autoscalingconn := testAccProvider.Meta().(*AWSClient).autoscalingconn + params := &autoscaling.DeleteScheduledActionInput{ + AutoScalingGroupName: schedule.AutoScalingGroupName, + ScheduledActionName: schedule.ScheduledActionName, + } + if _, err := autoscalingconn.DeleteScheduledAction(params); err != nil { + return err + } + return nil + } +} + func TestAccAWSAutoscalingSchedule_recurrence(t *testing.T) { var schedule autoscaling.ScheduledUpdateGroupAction @@ -87,6 +121,8 @@ func testAccCheckScalingScheduleExists(n string, policy *autoscaling.ScheduledUp return fmt.Errorf("Scaling Schedule not found") } + *policy = *resp.ScheduledUpdateGroupActions[0] + return nil } }