Skip to content

Commit

Permalink
data-source/aws_lambda_function: Implement standalone Read function a…
Browse files Browse the repository at this point in the history
…nd refactor testing to check data source state values against the resource state values

References:
* #5812
* hashicorp/terraform#10810 (comment)
* #6966

Previously, the `aws_lambda_function` data source was utilizing the Read function from the `aws_lambda_function` resource. This legacy practice has longterm maintenance issues with missing schema and documentation updates.

Here we implement a fresh new Read function for the data source that includes the following changes:

* Properly error when Lambda Function is not found
* Always return the `arn` attribute as unqualified (e.g. without a qualifier or version suffix)
* Always return the `qualified_arn` attribute as qualified (e.g. with the qualifier or version suffix)
* Always return the `tags` attribute

The acceptance testing changes modernize and simplify the testing:

* Utilize `resource.TestCheckResourceAttrPair()` where possible to ensure data source state values match appropriate resource state values
* Consolidate random naming to single variable
* Only provision VPC resources in VPC specific test

Output from acceptance testing:

```
--- PASS: TestAccDataSourceAWSLambdaFunction_version (20.89s)
--- PASS: TestAccDataSourceAWSLambdaFunction_environment (22.75s)
--- PASS: TestAccDataSourceAWSLambdaFunction_alias (23.68s)
--- PASS: TestAccDataSourceAWSLambdaFunction_basic (23.76s)
--- PASS: TestAccDataSourceAWSLambdaFunction_layers (28.82s)
--- PASS: TestAccDataSourceAWSLambdaFunction_vpc (36.48s)
```
  • Loading branch information
bflad committed Feb 23, 2019
1 parent 731bbd3 commit ba9e4de
Show file tree
Hide file tree
Showing 3 changed files with 288 additions and 196 deletions.
113 changes: 111 additions & 2 deletions aws/data_source_aws_lambda_function.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package aws

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down Expand Up @@ -151,11 +157,114 @@ func dataSourceAwsLambdaFunction() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
},
}
}

func dataSourceAwsLambdaFunctionRead(d *schema.ResourceData, meta interface{}) error {
d.SetId(d.Get("function_name").(string))
return resourceAwsLambdaFunctionRead(d, meta)
conn := meta.(*AWSClient).lambdaconn
functionName := d.Get("function_name").(string)

input := &lambda.GetFunctionInput{
FunctionName: aws.String(functionName),
}

if v, ok := d.GetOk("qualifier"); ok {
input.Qualifier = aws.String(v.(string))
}

log.Printf("[DEBUG] Getting Lambda Function: %s", input)
output, err := conn.GetFunction(input)

if err != nil {
return fmt.Errorf("error getting Lambda Function (%s): %s", functionName, err)
}

if output == nil {
return fmt.Errorf("error getting Lambda Function (%s): empty response", functionName)
}

function := output.Configuration

functionARN := aws.StringValue(function.FunctionArn)
qualifierSuffix := fmt.Sprintf(":%s", d.Get("qualifier").(string))
versionSuffix := fmt.Sprintf(":%s", aws.StringValue(function.Version))

qualifiedARN := functionARN
if !strings.HasSuffix(functionARN, qualifierSuffix) && !strings.HasSuffix(functionARN, versionSuffix) {
qualifiedARN = functionARN + versionSuffix
}

unqualifiedARN := strings.TrimSuffix(functionARN, qualifierSuffix)

d.Set("arn", unqualifiedARN)

deadLetterConfig := []interface{}{}
if function.DeadLetterConfig != nil {
deadLetterConfig = []interface{}{
map[string]interface{}{
"target_arn": aws.StringValue(function.DeadLetterConfig.TargetArn),
},
}
}
if err := d.Set("dead_letter_config", deadLetterConfig); err != nil {
return fmt.Errorf("error setting dead_letter_config: %s", err)
}

d.Set("description", function.Description)

if err := d.Set("environment", flattenLambdaEnvironment(function.Environment)); err != nil {
return fmt.Errorf("error setting environment: %s", err)
}

d.Set("handler", function.Handler)
d.Set("invoke_arn", lambdaFunctionInvokeArn(aws.StringValue(function.FunctionArn), meta))
d.Set("kms_key_arn", function.KMSKeyArn)
d.Set("last_modified", function.LastModified)

if err := d.Set("layers", flattenLambdaLayers(function.Layers)); err != nil {
return fmt.Errorf("Error setting layers for Lambda Function (%s): %s", d.Id(), err)
}

d.Set("memory_size", function.MemorySize)
d.Set("qualified_arn", qualifiedARN)

reservedConcurrentExecutions := int64(-1)
if output.Concurrency != nil {
reservedConcurrentExecutions = aws.Int64Value(output.Concurrency.ReservedConcurrentExecutions)
}
d.Set("reserved_concurrent_executions", reservedConcurrentExecutions)

d.Set("role", function.Role)
d.Set("runtime", function.Runtime)
d.Set("source_code_hash", function.CodeSha256)
d.Set("source_code_size", function.CodeSize)

if err := d.Set("tags", tagsToMapGeneric(output.Tags)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

tracingConfig := []map[string]interface{}{
{
"mode": lambda.TracingModePassThrough,
},
}
if function.TracingConfig != nil {
tracingConfig[0]["mode"] = aws.StringValue(function.TracingConfig.Mode)
}
if err := d.Set("tracing_config", tracingConfig); err != nil {
return fmt.Errorf("error setting tracing_config: %s", tracingConfig)
}

d.Set("timeout", function.Timeout)
d.Set("version", function.Version)

if err := d.Set("vpc_config", flattenLambdaVpcConfigResponse(function.VpcConfig)); err != nil {
return fmt.Errorf("error setting vpc_config: %s", err)
}

d.SetId(aws.StringValue(function.FunctionName))

return nil
}
Loading

0 comments on commit ba9e4de

Please sign in to comment.