-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
bflad
merged 2 commits into
hashicorp:master
from
flosell:fix-498-lambda-event-source-mapping-enabled
Jul 27, 2018
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, "") { There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
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. Theaws.StringValue()
SDK function helps prevent potential panics should theState
attribute be missing from the API.There was a problem hiding this comment.
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 theenabled
state though, otherwise it also mapsCreating
,Updating
,Deleting
to false which breaks the tests (and is also not what we want).