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

r/aws_lambda_event_source_mapping: Infer enabled status from State atttribute #5292

Merged
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
11 changes: 11 additions & 0 deletions aws/resource_aws_lambda_event_source_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ func resourceAwsLambdaEventSourceMappingRead(d *schema.ResourceData, meta interf
d.Set("uuid", eventSourceMappingConfiguration.UUID)
d.Set("function_name", eventSourceMappingConfiguration.FunctionArn)

state := aws.StringValue(eventSourceMappingConfiguration.State)

switch state {
case "Enabled", "Enabling":
d.Set("enabled", true)
case "Disabled", "Disabling":
d.Set("enabled", false)
default:
log.Printf("[DEBUG] Lambda event source mapping is neither enabled nor disabled but %s", *eventSourceMappingConfiguration.State)
}

return nil
}

Expand Down
79 changes: 79 additions & 0 deletions aws/resource_aws_lambda_event_source_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,85 @@ 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 {
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))
}

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
Expand Down