From 431d08aa0e08760843580a4237296d7d547a9263 Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Sun, 14 Jul 2019 15:44:21 +0900 Subject: [PATCH 1/3] Add support for AutoScaling Lifecycle Hook Import --- ...resource_aws_autoscaling_lifecycle_hook.go | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/aws/resource_aws_autoscaling_lifecycle_hook.go b/aws/resource_aws_autoscaling_lifecycle_hook.go index 500924eac48..7508a9d0f25 100644 --- a/aws/resource_aws_autoscaling_lifecycle_hook.go +++ b/aws/resource_aws_autoscaling_lifecycle_hook.go @@ -20,6 +20,10 @@ func resourceAwsAutoscalingLifecycleHook() *schema.Resource { Update: resourceAwsAutoscalingLifecycleHookPut, Delete: resourceAwsAutoscalingLifecycleHookDelete, + Importer: &schema.ResourceImporter{ + State: resourceAwsAutoscalingLifecycleHookImport, + }, + Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -192,3 +196,19 @@ func getAwsAutoscalingLifecycleHook(d *schema.ResourceData, meta interface{}) (* // lifecycle hook not found return nil, nil } + +func resourceAwsAutoscalingLifecycleHookImport(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 /", d.Id()) + } + + asgName := idParts[0] + lifecycleHookName := idParts[1] + + d.Set("name", lifecycleHookName) + d.Set("autoscaling_group_name", asgName) + d.SetId(lifecycleHookName) + + return []*schema.ResourceData{d}, nil +} From 064b7be84644a1111bd7f5348ae9ee6db4b0f2ee Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Mon, 15 Jul 2019 20:07:35 +0900 Subject: [PATCH 2/3] Update document for importing Autoscaling Lifecycle Hook --- website/docs/r/autoscaling_lifecycle_hooks.html.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/website/docs/r/autoscaling_lifecycle_hooks.html.markdown b/website/docs/r/autoscaling_lifecycle_hooks.html.markdown index 7a3e7b0e188..9880734906c 100644 --- a/website/docs/r/autoscaling_lifecycle_hooks.html.markdown +++ b/website/docs/r/autoscaling_lifecycle_hooks.html.markdown @@ -68,3 +68,11 @@ The following arguments are supported: * `notification_metadata` - (Optional) Contains additional information that you want to include any time Auto Scaling sends a message to the notification target. * `notification_target_arn` - (Optional) The ARN of the notification target that Auto Scaling will use to notify you when an instance is in the transition state for the lifecycle hook. This ARN target can be either an SQS queue or an SNS topic. * `role_arn` - (Optional) The ARN of the IAM role that allows the Auto Scaling group to publish to the specified notification target. + +## Import + +AutoScaling Lifecycle Hook can be imported using the role autoscaling_group_name and name separated by `/`. + +``` +$ terraform import aws_aws_autoscaling_lifecycle_hook.test-lifecycle-hook asg-name/lifecycle-hook-name +``` From b656507a7bb50b2ada7a4ed433df9a3b901162c0 Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Mon, 15 Jul 2019 20:15:29 +0900 Subject: [PATCH 3/3] Add test for support Autoscaling Lifecycle Hook Import --- ...ource_aws_autoscaling_lifecycle_hook_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/aws/resource_aws_autoscaling_lifecycle_hook_test.go b/aws/resource_aws_autoscaling_lifecycle_hook_test.go index 4a28cd6a26c..a6eee055f7d 100644 --- a/aws/resource_aws_autoscaling_lifecycle_hook_test.go +++ b/aws/resource_aws_autoscaling_lifecycle_hook_test.go @@ -29,6 +29,12 @@ func TestAccAWSAutoscalingLifecycleHook_basic(t *testing.T) { resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "lifecycle_transition", "autoscaling:EC2_INSTANCE_LAUNCHING"), ), }, + { + ResourceName: "aws_autoscaling_lifecycle_hook.foobar", + ImportState: true, + ImportStateIdFunc: testAccAWSAutoscalingLifecycleHookImportStateIdFunc("aws_autoscaling_lifecycle_hook.foobar"), + ImportStateVerify: true, + }, }, }) } @@ -107,6 +113,17 @@ func testAccCheckAWSAutoscalingLifecycleHookDestroy(s *terraform.State) error { return nil } +func testAccAWSAutoscalingLifecycleHookImportStateIdFunc(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 testAccAWSAutoscalingLifecycleHookConfig(name string) string { return fmt.Sprintf(` resource "aws_launch_configuration" "foobar" {