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

Feature/add importer for asg scheduled action #8300

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
25 changes: 25 additions & 0 deletions aws/resource_aws_autoscaling_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -19,6 +20,9 @@ func resourceAwsAutoscalingSchedule() *schema.Resource {
Read: resourceAwsAutoscalingScheduleRead,
Update: resourceAwsAutoscalingScheduleCreate,
Delete: resourceAwsAutoscalingScheduleDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsAutoscalingScheduleImport,
},

Schema: map[string]*schema.Schema{
"arn": {
Expand Down Expand Up @@ -71,6 +75,27 @@ func resourceAwsAutoscalingSchedule() *schema.Resource {
}
}

func resourceAwsAutoscalingScheduleImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
splitId := strings.Split(d.Id(), "/")
if len(splitId) != 2 {
return []*schema.ResourceData{}, fmt.Errorf("wrong format of resource: %s. Please follow 'asg-name/action-name'", d.Id())
}

asgName := splitId[0]
actionName := splitId[1]

err := d.Set("autoscaling_group_name", asgName)
if err != nil {
return []*schema.ResourceData{}, fmt.Errorf("failed to set autoscaling_group_name value")
}
err = d.Set("scheduled_action_name", actionName)
if err != nil {
return []*schema.ResourceData{}, fmt.Errorf("failed to set scheduled_action_name value")
}
d.SetId(actionName)
return []*schema.ResourceData{d}, nil
}

func resourceAwsAutoscalingScheduleCreate(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
params := &autoscaling.PutScheduledUpdateGroupActionInput{
Expand Down
61 changes: 55 additions & 6 deletions aws/resource_aws_autoscaling_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"regexp"
"testing"
"time"

Expand All @@ -18,6 +19,10 @@ func TestAccAWSAutoscalingSchedule_basic(t *testing.T) {
start := testAccAWSAutoscalingScheduleValidStart(t)
end := testAccAWSAutoscalingScheduleValidEnd(t)

scheduledActionName := "foobar"
resourceName := fmt.Sprintf("aws_autoscaling_schedule.%s", scheduledActionName)
importInput := fmt.Sprintf("%s/%s", rName, scheduledActionName)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -26,9 +31,22 @@ func TestAccAWSAutoscalingSchedule_basic(t *testing.T) {
{
Config: testAccAWSAutoscalingScheduleConfig(rName, start, end),
Check: resource.ComposeTestCheckFunc(
testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule),
testAccCheckScalingScheduleExists(resourceName, &schedule),
),
},
{
ResourceName: resourceName,
ImportStateId: importInput,
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: resourceName,
ImportStateId: fmt.Sprintf("%s/nonexistent", rName),
ImportState: true,
ImportStateVerify: false,
ExpectError: regexp.MustCompile(`(Cannot import non-existent remote object)`),
},
},
})
}
Expand Down Expand Up @@ -72,6 +90,11 @@ func TestAccAWSAutoscalingSchedule_recurrence(t *testing.T) {
var schedule autoscaling.ScheduledUpdateGroupAction

rName := fmt.Sprintf("tf-test-%d", acctest.RandInt())

scheduledActionName := "foobar"
resourceName := fmt.Sprintf("aws_autoscaling_schedule.%s", scheduledActionName)
importInput := fmt.Sprintf("%s/%s", rName, scheduledActionName)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -80,10 +103,16 @@ func TestAccAWSAutoscalingSchedule_recurrence(t *testing.T) {
{
Config: testAccAWSAutoscalingScheduleConfig_recurrence(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule),
resource.TestCheckResourceAttr("aws_autoscaling_schedule.foobar", "recurrence", "0 8 * * *"),
testAccCheckScalingScheduleExists(resourceName, &schedule),
resource.TestCheckResourceAttr(resourceName, "recurrence", "0 8 * * *"),
),
},
{
ResourceName: resourceName,
ImportStateId: importInput,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -95,6 +124,10 @@ func TestAccAWSAutoscalingSchedule_zeroValues(t *testing.T) {
start := testAccAWSAutoscalingScheduleValidStart(t)
end := testAccAWSAutoscalingScheduleValidEnd(t)

scheduledActionName := "foobar"
resourceName := fmt.Sprintf("aws_autoscaling_schedule.%s", scheduledActionName)
importInput := fmt.Sprintf("%s/%s", rName, scheduledActionName)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -103,9 +136,15 @@ func TestAccAWSAutoscalingSchedule_zeroValues(t *testing.T) {
{
Config: testAccAWSAutoscalingScheduleConfig_zeroValues(rName, start, end),
Check: resource.ComposeTestCheckFunc(
testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule),
testAccCheckScalingScheduleExists(resourceName, &schedule),
),
},
{
ResourceName: resourceName,
ImportStateId: importInput,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -117,6 +156,10 @@ func TestAccAWSAutoscalingSchedule_negativeOne(t *testing.T) {
start := testAccAWSAutoscalingScheduleValidStart(t)
end := testAccAWSAutoscalingScheduleValidEnd(t)

scheduledActionName := "foobar"
resourceName := fmt.Sprintf("aws_autoscaling_schedule.%s", scheduledActionName)
importInput := fmt.Sprintf("%s/%s", rName, scheduledActionName)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -125,11 +168,17 @@ func TestAccAWSAutoscalingSchedule_negativeOne(t *testing.T) {
{
Config: testAccAWSAutoscalingScheduleConfig_negativeOne(rName, start, end),
Check: resource.ComposeTestCheckFunc(
testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule),
testAccCheckScalingScheduleExists(resourceName, &schedule),
testAccCheckScalingScheduleHasNoDesiredCapacity(&schedule),
resource.TestCheckResourceAttr("aws_autoscaling_schedule.foobar", "desired_capacity", "-1"),
resource.TestCheckResourceAttr(resourceName, "desired_capacity", "-1"),
),
},
{
ResourceName: resourceName,
ImportStateId: importInput,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/autoscaling_schedule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,11 @@ Set to -1 if you don't want to change the maximum size at the scheduled time.

## Attribute Reference
* `arn` - The ARN assigned by AWS to the autoscaling schedule.

## Import

AutoScaling ScheduledAction can be imported using the `auto-scaling-group-name` and `scheduled-action-name`, e.g.

```
$ terraform import aws_autoscaling_schedule.resource-name auto-scaling-group-name/scheduled-action-name
```