Skip to content

Commit

Permalink
Use enum for InvocationType
Browse files Browse the repository at this point in the history
  • Loading branch information
kbalk committed Mar 29, 2021
1 parent 0afc01b commit 6d632a9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 36 deletions.
26 changes: 4 additions & 22 deletions modules/aws/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aws

import (
"encoding/json"
"errors"
"fmt"

"github.com/aws/aws-sdk-go/service/lambda"
Expand All @@ -13,14 +12,15 @@ import (
// 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 "RequestResponse" or "DryRun".
// InvocationType can be one of lambda.InvocationTypeRequestResponse
// or lambda.InvocationTypeDryRun.
// * RequestResponse (default) - Invoke the function synchronously.
// Keep the connection open until the function returns a response
// or times out.
//
// * DryRun - Validate parameter values and verify that the user or
// role has permission to invoke the function.
InvocationType *string
InvocationType *string `enum:"lambda.InvocationType"`

// Lambda function input; will be converted to JSON.
Payload interface{}
Expand Down Expand Up @@ -74,27 +74,9 @@ func InvokeFunctionWithParams(t testing.TestingT, region, functionName string, i
return nil, err
}

// Verify the InvocationType is one of the allowed values and report
// an error if its 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)
}
}

invokeInput := &lambda.InvokeInput{
FunctionName: &functionName,
InvocationType: &invocationType,
InvocationType: input.InvocationType,
}

if input.Payload != nil {
Expand Down
16 changes: 2 additions & 14 deletions test/terraform_aws_lambda_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 @@ -101,7 +102,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 := "DryRun"
invocationType := lambda.InvocationTypeDryRun
input := &aws.LambdaOptions{InvocationType: &invocationType}
out, err := aws.InvokeFunctionWithParams(t, awsRegion, functionName, input)

Expand All @@ -110,19 +111,6 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
// invocation.
require.Nil(t, err)
assert.Equal(t, int(*out.StatusCode), 204)

// Call InvokeFunctionWithParams with a LambdaOptions struct with an
// unsupported InvocationType. The function should fail.
invocationType = "Event"
input = &aws.LambdaOptions{
InvocationType: &invocationType,
Payload: ExampleFunctionPayload{ShouldFail: false, Echo: "hi!"},
}
out, err = aws.InvokeFunctionWithParams(t, awsRegion, functionName, input)
require.NotNil(t, err)
msg := "LambdaOptions.InvocationType, if specified, must either be \"RequestResponse\" or \"DryRun\""
assert.Contains(t, err.Error(), msg)
assert.Contains(t, *out.FunctionError, msg)
}

type ExampleFunctionPayload struct {
Expand Down

0 comments on commit 6d632a9

Please sign in to comment.