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

Add support for AutoScaling Scaling Policy Import #7195

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

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand All @@ -19,6 +20,9 @@ func resourceAwsAutoscalingPolicy() *schema.Resource {
Read: resourceAwsAutoscalingPolicyRead,
Update: resourceAwsAutoscalingPolicyUpdate,
Delete: resourceAwsAutoscalingPolicyDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsAutoscalingPolicyImport,
},

Schema: map[string]*schema.Schema{
"arn": {
Expand Down Expand Up @@ -276,6 +280,22 @@ func resourceAwsAutoscalingPolicyDelete(d *schema.ResourceData, meta interface{}
return nil
}

func resourceAwsAutoscalingPolicyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.SplitN(d.Id(), "/", 2)
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("unexpected format (%q), expected <asg-name>/<policy-name>", d.Id())
}

asgName := idParts[0]
policyName := idParts[1]

d.Set("name", policyName)
d.Set("autoscaling_group_name", asgName)
d.SetId(policyName)

return []*schema.ResourceData{d}, nil
}

// PutScalingPolicy can safely resend all parameters without destroying the
// resource, so create and update can share this common function. It will error
// if certain mutually exclusive values are set.
Expand Down
17 changes: 17 additions & 0 deletions aws/resource_aws_autoscaling_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ func TestAccAWSAutoscalingPolicy_basic(t *testing.T) {
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_target_tracking", "target_tracking_configuration.0.target_value", "40"),
),
},
{
ResourceName: "aws_autoscaling_policy.foobar_simple",
ImportState: true,
ImportStateIdFunc: testAccAWSAutoscalingPolicyImportStateIdFunc("aws_autoscaling_policy.foobar_simple"),
ImportStateVerify: true,
},
{
Config: testAccAWSAutoscalingPolicyConfig_basicUpdate(name),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -306,6 +312,17 @@ func testAccCheckAWSAutoscalingPolicyDestroy(s *terraform.State) error {
return nil
}

func testAccAWSAutoscalingPolicyImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

return fmt.Sprintf("%s/%s", rs.Primary.Attributes["autoscaling_group_name"], rs.Primary.Attributes["name"]), nil
}
}

func testAccAWSAutoscalingPolicyConfig_base(name string) string {
return fmt.Sprintf(`
data "aws_ami" "amzn" {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/autoscaling_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,11 @@ The following arguments are supported for backwards compatibility but should not
* `autoscaling_group_name` - The scaling policy's assigned autoscaling group.
* `adjustment_type` - The scaling policy's adjustment type.
* `policy_type` - The scaling policy's type.

## Import

AutoScaling scaling policy can be imported using the role autoscaling_group_name and name separated by `/`.

```
$ terraform import aws_autoscaling_policy.test-policy asg-name/policy-name
```