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 1 commit
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
8 changes: 8 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,14 @@ func resourceAwsLambdaEventSourceMappingRead(d *schema.ResourceData, meta interf
d.Set("uuid", eventSourceMappingConfiguration.UUID)
d.Set("function_name", eventSourceMappingConfiguration.FunctionArn)

if *eventSourceMappingConfiguration.State == "Enabled" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I would recommend something like:

state := aws.StringValue(eventSourceMappingConfiguration.State)
d.Set("enabled", (state == "Enabling" || state == "Enabled"))

The key here being that d.Set() is always called to ensure we are reading the value from the API into the Terraform state to detect configuration drift. The aws.StringValue() SDK function helps prevent potential panics should the State attribute be missing from the API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointer for StringValue! I had to be a bit more advanced for the enabled state though, otherwise it also maps Creating, Updating, Deleting to false which breaks the tests (and is also not what we want).

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
}

Expand Down
80 changes: 80 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,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: We generally prefer to use our helper function to simplify this logic:

if isAWSErr(err, lambda.ErrCodeResourceInUseException, "") {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks for the pointer!

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