Skip to content

Commit

Permalink
Use an enum type for InvocationType
Browse files Browse the repository at this point in the history
  • Loading branch information
kbalk committed Mar 30, 2021
1 parent dee2893 commit c10bd38
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
48 changes: 31 additions & 17 deletions modules/aws/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,40 @@ import (
"github.com/stretchr/testify/require"
)

type InvocationTypeOption string

const (
InvocationTypeRequestResponse InvocationTypeOption = "RequestResponse"
InvocationTypeDryRun = "DryRun"
)

func (itype *InvocationTypeOption) Value() (string, error) {
if itype != nil {
switch *itype {
case
InvocationTypeRequestResponse,
InvocationTypeDryRun:
return string(*itype), nil
default:
msg := fmt.Sprintf("LambdaOptions.InvocationType, if specified, must either be \"%s\" or \"%s\"",
InvocationTypeRequestResponse,
InvocationTypeDryRun)
return "", errors.New(msg)
}
}
return string(InvocationTypeRequestResponse), nil
}

// LambdaOptions contains additional parameters for InvokeFunctionWithParams().
// It contains a subset of the fields found in the lambda.InvokeInput struct.
type LambdaOptions struct {
// InvocationType can be one of lambda.InvocationTypeRequestResponse
// or lambda.InvocationTypeDryRun.
// InvocationType can be one of InvocationTypeOption values:
// * InvocationTypeRequestResponse (default) - Invoke the function
// synchronously. Keep the connection open until the function
// returns a response or times out.
// * InvocationTypeDryRun - Validate parameter values and verify
// that the user or role has permission to invoke the function.
InvocationType *string
InvocationType *InvocationTypeOption

// Lambda function input; will be converted to JSON.
Payload interface{}
Expand Down Expand Up @@ -84,21 +107,12 @@ func InvokeFunctionWithParamsE(t testing.TestingT, region, functionName string,
}

// Verify the InvocationType is one of the allowed values and report
// an error if its not. By default the InvocationType will be
// an error if it's not. By default the InvocationType will be
// "RequestResponse".
invocationType := lambda.InvocationTypeRequestResponse
if input.InvocationType != nil {
switch *input.InvocationType {
case
lambda.InvocationTypeRequestResponse,
lambda.InvocationTypeDryRun:
invocationType = *input.InvocationType
default:
msg := fmt.Sprintf("LambdaOptions.InvocationType, if specified, must either be \"%s\" or \"%s\"",
lambda.InvocationTypeRequestResponse,
lambda.InvocationTypeDryRun)
return &LambdaOutput{FunctionError: &msg}, errors.New(msg)
}
invocationType, err := input.InvocationType.Value()
if err != nil {
msg := err.Error()
return &LambdaOutput{FunctionError: &msg}, err
}

invokeInput := &lambda.InvokeInput{
Expand Down
5 changes: 2 additions & 3 deletions test/terraform_aws_lambda_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/lambda"
"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
Expand Down Expand Up @@ -102,7 +101,7 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
// Call InvokeFunctionWithParms with an InvocationType of "DryRun".
// A "DryRun" invocation does not execute the function, so the example
// test function will not be checking the payload.
invocationType := lambda.InvocationTypeDryRun
var invocationType aws.InvocationTypeOption = aws.InvocationTypeDryRun
input := &aws.LambdaOptions{InvocationType: &invocationType}
out := aws.InvokeFunctionWithParams(t, awsRegion, functionName, input)

Expand All @@ -113,7 +112,7 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {

// Invoke the function, this time causing the Lambda to error and
// capturing the error.
invocationType = lambda.InvocationTypeRequestResponse
invocationType = aws.InvocationTypeRequestResponse
input = &aws.LambdaOptions{
InvocationType: &invocationType,
Payload: ExampleFunctionPayload{ShouldFail: true, Echo: "hi!"},
Expand Down

0 comments on commit c10bd38

Please sign in to comment.