From 55225da37e92598575ab9f4906d0a2708983bd0f Mon Sep 17 00:00:00 2001 From: Florian Sellmayr Date: Sun, 22 Jul 2018 11:54:09 +0700 Subject: [PATCH 1/2] r/aws_lambda_event_source_mapping: Infer enabled status from State attribute --- ...esource_aws_lambda_event_source_mapping.go | 8 ++ ...ce_aws_lambda_event_source_mapping_test.go | 80 +++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/aws/resource_aws_lambda_event_source_mapping.go b/aws/resource_aws_lambda_event_source_mapping.go index 8b20bb94997..feb37a5f812 100644 --- a/aws/resource_aws_lambda_event_source_mapping.go +++ b/aws/resource_aws_lambda_event_source_mapping.go @@ -191,6 +191,14 @@ func resourceAwsLambdaEventSourceMappingRead(d *schema.ResourceData, meta interf d.Set("uuid", eventSourceMappingConfiguration.UUID) d.Set("function_name", eventSourceMappingConfiguration.FunctionArn) + if *eventSourceMappingConfiguration.State == "Enabled" { + d.Set("enabled", true) + } else if *eventSourceMappingConfiguration.State == "Disabled" { + d.Set("enabled", false) + } else { + log.Printf("[DEBUG] Lambda event source mapping is neither enabled nor disabled but %s", *eventSourceMappingConfiguration.State) + } + return nil } diff --git a/aws/resource_aws_lambda_event_source_mapping_test.go b/aws/resource_aws_lambda_event_source_mapping_test.go index 06661465338..6bc17e3bbf2 100644 --- a/aws/resource_aws_lambda_event_source_mapping_test.go +++ b/aws/resource_aws_lambda_event_source_mapping_test.go @@ -230,6 +230,86 @@ func TestAccAWSLambdaEventSourceMapping_sqsDisappears(t *testing.T) { }) } +func TestAccAWSLambdaEventSourceMapping_changesInEnabledAreDetected(t *testing.T) { + var conf lambda.EventSourceMappingConfiguration + + rString := acctest.RandString(8) + roleName := fmt.Sprintf("tf_acc_role_lambda_sqs_import_%s", rString) + policyName := fmt.Sprintf("tf_acc_policy_lambda_sqs_import_%s", rString) + attName := fmt.Sprintf("tf_acc_att_lambda_sqs_import_%s", rString) + streamName := fmt.Sprintf("tf_acc_stream_lambda_sqs_import_%s", rString) + funcName := fmt.Sprintf("tf_acc_lambda_sqs_import_%s", rString) + uFuncName := fmt.Sprintf("tf_acc_lambda_sqs_import_updated_%s", rString) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccAWSLambdaEventSourceMappingConfig_sqs(roleName, policyName, attName, streamName, funcName, uFuncName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf), + testAccCheckAWSLambdaEventSourceMappingIsBeingDisabled(&conf), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccCheckAWSLambdaEventSourceMappingIsBeingDisabled(conf *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).lambdaconn + // Disable enabled state + err := resource.Retry(10*time.Minute, func() *resource.RetryError { + params := &lambda.UpdateEventSourceMappingInput{ + UUID: conf.UUID, + Enabled: aws.Bool(false), + } + + _, err := conn.UpdateEventSourceMapping(params) + + if err != nil { + cgw, ok := err.(awserr.Error) + if ok && cgw.Code() == lambda.ErrCodeResourceInUseException { + return resource.RetryableError(fmt.Errorf( + "Waiting for Lambda Event Source Mapping to be ready to be updated: %v", conf.UUID)) + } + + return resource.NonRetryableError( + fmt.Errorf("Error updating Lambda Event Source Mapping: %s", err)) + } + + return nil + }) + + if err != nil { + return err + } + + // wait for state to be propagated + return resource.Retry(10*time.Minute, func() *resource.RetryError { + params := &lambda.GetEventSourceMappingInput{ + UUID: conf.UUID, + } + newConf, err := conn.GetEventSourceMapping(params) + if err != nil { + return resource.NonRetryableError( + fmt.Errorf("Error getting Lambda Event Source Mapping: %s", err)) + } + + if *newConf.State != "Disabled" { + return resource.RetryableError(fmt.Errorf( + "Waiting to get Lambda Event Source Mapping to be fully enabled, it's currently %s: %v", *newConf.State, conf.UUID)) + + } + + return nil + }) + + } +} + func testAccCheckAWSLambdaEventSourceMappingDisappears(conf *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc { return func(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).lambdaconn From 6a70b2c7b6daeded485bbc97d51ffc912d4287e3 Mon Sep 17 00:00:00 2001 From: Florian Sellmayr Date: Fri, 27 Jul 2018 13:12:22 +0800 Subject: [PATCH 2/2] r/aws_lambda_event_source_mapping: Map enabling and disabling status and use isAWSErr helper --- aws/resource_aws_lambda_event_source_mapping.go | 9 ++++++--- aws/resource_aws_lambda_event_source_mapping_test.go | 3 +-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/aws/resource_aws_lambda_event_source_mapping.go b/aws/resource_aws_lambda_event_source_mapping.go index feb37a5f812..431caaca110 100644 --- a/aws/resource_aws_lambda_event_source_mapping.go +++ b/aws/resource_aws_lambda_event_source_mapping.go @@ -191,11 +191,14 @@ func resourceAwsLambdaEventSourceMappingRead(d *schema.ResourceData, meta interf d.Set("uuid", eventSourceMappingConfiguration.UUID) d.Set("function_name", eventSourceMappingConfiguration.FunctionArn) - if *eventSourceMappingConfiguration.State == "Enabled" { + state := aws.StringValue(eventSourceMappingConfiguration.State) + + switch state { + case "Enabled", "Enabling": d.Set("enabled", true) - } else if *eventSourceMappingConfiguration.State == "Disabled" { + case "Disabled", "Disabling": d.Set("enabled", false) - } else { + default: log.Printf("[DEBUG] Lambda event source mapping is neither enabled nor disabled but %s", *eventSourceMappingConfiguration.State) } diff --git a/aws/resource_aws_lambda_event_source_mapping_test.go b/aws/resource_aws_lambda_event_source_mapping_test.go index 6bc17e3bbf2..535988f50e2 100644 --- a/aws/resource_aws_lambda_event_source_mapping_test.go +++ b/aws/resource_aws_lambda_event_source_mapping_test.go @@ -270,8 +270,7 @@ func testAccCheckAWSLambdaEventSourceMappingIsBeingDisabled(conf *lambda.EventSo _, err := conn.UpdateEventSourceMapping(params) if err != nil { - cgw, ok := err.(awserr.Error) - if ok && cgw.Code() == lambda.ErrCodeResourceInUseException { + if isAWSErr(err, lambda.ErrCodeResourceInUseException, "") { return resource.RetryableError(fmt.Errorf( "Waiting for Lambda Event Source Mapping to be ready to be updated: %v", conf.UUID)) }