Skip to content

Commit

Permalink
CR updates
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 committed Dec 10, 2021
1 parent 271563a commit d20f759
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 105 deletions.
2 changes: 1 addition & 1 deletion internal/service/connect/contact_flow_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ resource "aws_connect_contact_flow" "test" {
name = %[2]q
description = "Test Contact Flow Description"
type = "CONTACT_FLOW"
content = file("./testdata/connect_contact_flow.json")
content = file("./test-fixtures/connect_contact_flow.json")
tags = {
"Name" = "Test Contact Flow",
"Application" = "Terraform",
Expand Down
4 changes: 2 additions & 2 deletions internal/service/connect/contact_flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func testAccContactFlow_filename(t *testing.T) {
CheckDestroy: testAccCheckContactFlowDestroy,
Steps: []resource.TestStep{
{
Config: testAccContactFlowConfig_filename(rName, rName2, "Created", "testdata/connect_contact_flow.json"),
Config: testAccContactFlowConfig_filename(rName, rName2, "Created", "test-fixtures/connect_contact_flow.json"),
Check: resource.ComposeTestCheckFunc(
testAccCheckContactFlowExists(resourceName, &v),
resource.TestCheckResourceAttrSet(resourceName, "arn"),
Expand All @@ -115,7 +115,7 @@ func testAccContactFlow_filename(t *testing.T) {
},
},
{
Config: testAccContactFlowConfig_filename(rName, rName2, "Updated", "testdata/connect_contact_flow_updated.json"),
Config: testAccContactFlowConfig_filename(rName, rName2, "Updated", "test-fixtures/connect_contact_flow_updated.json"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckContactFlowExists(resourceName, &v),
resource.TestCheckResourceAttrSet(resourceName, "arn"),
Expand Down
13 changes: 12 additions & 1 deletion internal/service/connect/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,20 @@ func FindLambdaFunctionAssociationByArnWithContext(ctx context.Context, conn *co
return !lastPage
})

if tfawserr.ErrCodeEquals(err, connect.ErrCodeResourceNotFoundException) {
return "", &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return "", err
}

if result == "" {
return "", tfresource.NewEmptyResultError(input)
}

return result, nil
}
}
12 changes: 6 additions & 6 deletions internal/service/connect/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

const botV1AssociationIDSeparator = ":"
const lambdaFunctionAssociationIDSeparator = ":"
const lambdaFunctionAssociationIDSeparator = ","

func BotV1AssociationParseResourceID(id string) (string, string, string, error) {
parts := strings.SplitN(id, botV1AssociationIDSeparator, 3)
Expand All @@ -27,13 +27,13 @@ func BotV1AssociationCreateResourceID(instanceID string, botName string, region

func LambdaFunctionAssociationParseResourceID(id string) (string, string, error) {
parts := strings.SplitN(id, lambdaFunctionAssociationIDSeparator, 2)
if len(parts) == 2 && parts[0] != "" && parts[1] != "" {
return parts[0], parts[1], nil
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return "", "",
fmt.Errorf("unexpected format for ID (%q), expected instanceID"+lambdaFunctionAssociationIDSeparator+
"functionARN", id)
}

return "", "",
fmt.Errorf("unexpected format for ID (%q), expected instanceID"+lambdaFunctionAssociationIDSeparator+
"function-arn", id)
return parts[0], parts[1], nil
}

func LambdaFunctionAssociationCreateResourceID(instanceID string, functionArn string) string {
Expand Down
61 changes: 25 additions & 36 deletions internal/service/connect/lambda_function_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,29 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func ResourceLambdaFunctionAssociation() *schema.Resource {
return &schema.Resource{
CreateContext: resourceLambdaFunctionAssociationCreate,
ReadContext: resourceLambdaFunctionAssociationRead,
UpdateContext: resourceLambdaFunctionAssociationRead,
DeleteContext: resourceLambdaFunctionAssociationDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
instanceID, functionArn, err := LambdaFunctionAssociationParseResourceID(d.Id())
if err != nil {
return nil, err
}
d.Set("function_arn", functionArn)
d.Set("instance_id", instanceID)
d.SetId(LambdaFunctionAssociationCreateResourceID(instanceID, functionArn))

return []*schema.ResourceData{d}, nil
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(connectLambdaFunctionAssociationCreatedTimeout),
Delete: schema.DefaultTimeout(connectLambdaFunctionAssociationDeletedTimeout),
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"function_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: verify.ValidARN,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
Expand All @@ -54,48 +42,46 @@ func ResourceLambdaFunctionAssociation() *schema.Resource {
func resourceLambdaFunctionAssociationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).ConnectConn

instanceId := d.Get("instance_id").(string)
functionArn := d.Get("function_arn").(string)

input := &connect.AssociateLambdaFunctionInput{
InstanceId: aws.String(d.Get("instance_id").(string)),
FunctionArn: aws.String(d.Get("function_arn").(string)),
InstanceId: aws.String(instanceId),
FunctionArn: aws.String(functionArn),
}

lfaId := LambdaFunctionAssociationCreateResourceID(d.Get("instance_id").(string), d.Get("function_arn").(string))

lfaArn, err := FindLambdaFunctionAssociationByArnWithContext(ctx, conn, d.Get("instance_id").(string), d.Get("function_arn").(string))
_, err := conn.AssociateLambdaFunctionWithContext(ctx, input)
if err != nil {
return diag.FromErr(fmt.Errorf("error finding Connect Lambda Function Association by Function ARN (%s): %w", lfaArn, err))
return diag.FromErr(fmt.Errorf("error creating Connect Lambda Function Association (%s,%s): %s", instanceId, functionArn, err))
}
log.Printf("[DEBUG] Creating Connect Lambda Association %s", input)

_, err = conn.AssociateLambdaFunctionWithContext(ctx, input)
if err != nil {
return diag.FromErr(fmt.Errorf("error creating Connect Lambda Function Association (%s): %s", lfaArn, err))
}
d.SetId(LambdaFunctionAssociationCreateResourceID(instanceId, functionArn))

d.SetId(lfaId)
return resourceLambdaFunctionAssociationRead(ctx, d, meta)
}

func resourceLambdaFunctionAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).ConnectConn

instanceID, functionArn, err := LambdaFunctionAssociationParseResourceID(d.Id())

if err != nil {
return diag.FromErr(err)
}

lfaArn, err := FindLambdaFunctionAssociationByArnWithContext(ctx, conn, instanceID, functionArn)
if err != nil {
return diag.FromErr(fmt.Errorf("error finding Connect Lambda Function Association by Function ARN (%s): %w", functionArn, err))
}

if !d.IsNewResource() && lfaArn == "" {
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Connect Lambda Function Association (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

d.Set("function_arn", functionArn)
if err != nil {
return diag.FromErr(fmt.Errorf("error finding Connect Lambda Function Association by Function ARN (%s): %w", functionArn, err))
}

d.Set("function_arn", lfaArn)
d.Set("instance_id", instanceID)

return nil
Expand All @@ -114,12 +100,15 @@ func resourceLambdaFunctionAssociationDelete(ctx context.Context, d *schema.Reso
FunctionArn: aws.String(functionArn),
}

log.Printf("[DEBUG] Deleting Connect Lambda Function Association %s", d.Id())
_, err = conn.DisassociateLambdaFunctionWithContext(ctx, input)

_, err = conn.DisassociateLambdaFunction(input)
if tfawserr.ErrCodeEquals(err, connect.ErrCodeResourceNotFoundException) {
return nil
}

if err != nil && !tfawserr.ErrCodeEquals(err, "ResourceNotFoundException", "") {
if err != nil {
return diag.FromErr(fmt.Errorf("error deleting Connect Lambda Function Association (%s): %w", d.Id(), err))
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func dataSourceLambdaFunctionAssociationRead(ctx context.Context, d *schema.Reso
return diag.FromErr(fmt.Errorf("error finding Connect Lambda Function Association by ARN (%s): not found", functionArn))
}

d.SetId(meta.(*conns.AWSClient).Region)
d.Set("function_arn", functionArn)
d.Set("instance_id", instanceID)
d.SetId(fmt.Sprintf("%s:%s", d.Get("instance_id").(string), d.Get("function_arn").(string)))

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func TestAccLambdaFunctionAssociationDataSource_basic(t *testing.T) {
func TestAccConnectLambdaFunctionAssociationDataSource_basic(t *testing.T) {
rName := sdkacctest.RandStringFromCharSet(8, sdkacctest.CharSetAlpha)
rName2 := sdkacctest.RandomWithPrefix("resource-test-terraform")
resourceName := "aws_connect_lambda_function_association.test"
Expand All @@ -34,12 +34,14 @@ func TestAccLambdaFunctionAssociationDataSource_basic(t *testing.T) {

func testAccLambdaFunctionAssociationDataSource_BaseConfig(rName string, rName2 string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}
resource "aws_lambda_function" "test" {
filename = "testdata/lambdatest.zip"
function_name = %[1]q
role = aws_iam_role.test.arn
handler = "exports.handler"
runtime = "nodejs12.x"
runtime = "nodejs14.x"
}
resource "aws_iam_role" "test" {
Expand All @@ -52,7 +54,7 @@ resource "aws_iam_role" "test" {
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
"Service": "lambda.${data.aws_partition.current.dns_suffix}"
},
"Effect": "Allow",
"Sid": ""
Expand Down
62 changes: 21 additions & 41 deletions internal/service/connect/lambda_function_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

//Serialized acceptance tests due to Connect account limits (max 2 parallel tests)
func TestAccLambdaFunctionAssociation_serial(t *testing.T) {
func TestAccConnectLambdaFunctionAssociation_serial(t *testing.T) {
testCases := map[string]func(t *testing.T){
"basic": testAccLambdaFunctionAssociation_basic,
"disappears": testAccLambdaFunctionAssociation_disappears,
Expand All @@ -32,10 +32,10 @@ func TestAccLambdaFunctionAssociation_serial(t *testing.T) {

func testAccLambdaFunctionAssociation_basic(t *testing.T) {
rName := sdkacctest.RandStringFromCharSet(8, sdkacctest.CharSetAlpha)
rName2 := sdkacctest.RandomWithPrefix("resource-test-terraform")
rName2 := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_connect_lambda_function_association.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, connect.EndpointsID),
Providers: acctest.Providers,
Expand All @@ -60,14 +60,14 @@ func testAccLambdaFunctionAssociation_basic(t *testing.T) {

func testAccLambdaFunctionAssociation_disappears(t *testing.T) {
rName := sdkacctest.RandStringFromCharSet(8, sdkacctest.CharSetAlpha)
rName2 := sdkacctest.RandomWithPrefix("resource-test-terraform")
rName2 := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_connect_lambda_function_association.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, connect.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccLambdaFunctionAssociationDestroy,
CheckDestroy: testAccCheckLambdaFunctionAssociationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsConnectLambdaFunctionAssociationConfigBasic(rName, rName2),
Expand All @@ -81,7 +81,7 @@ func testAccLambdaFunctionAssociation_disappears(t *testing.T) {
})
}

func testAccLambdaFunctionAssociationDestroy(s *terraform.State) error {
func testAccCheckLambdaFunctionAssociationDestroy(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).ConnectConn

for _, rs := range s.RootModule().Resources {
Expand All @@ -95,12 +95,17 @@ func testAccLambdaFunctionAssociationDestroy(s *terraform.State) error {
}

lfaArn, err := tfconnect.FindLambdaFunctionAssociationByArnWithContext(context.Background(), conn, instanceID, functionArn)
if err != nil && !tfawserr.ErrCodeEquals(err, connect.ErrCodeResourceNotFoundException) {
return fmt.Errorf("error Connect Lambda Function Association (%s): still exists: %w", functionArn, err)

if tfawserr.ErrCodeEquals(err, connect.ErrCodeResourceNotFoundException) {
continue
}

if err != nil {
return err
}

if lfaArn != "" {
return fmt.Errorf("error Connect Lambda Function Association (%s): still exists", functionArn)
return fmt.Errorf("Connect Lambda Function Association (%s): still exists", functionArn)
}
}
return nil
Expand All @@ -116,6 +121,7 @@ func testAccCheckLambdaFunctionAssociationExists(resourceName string) resource.T
if rs.Primary.ID == "" {
return fmt.Errorf("error Connect Lambda Function Association ID not set")
}

instanceID, functionArn, err := tfconnect.LambdaFunctionAssociationParseResourceID(rs.Primary.ID)

if err != nil {
Expand All @@ -125,6 +131,7 @@ func testAccCheckLambdaFunctionAssociationExists(resourceName string) resource.T
conn := acctest.Provider.Meta().(*conns.AWSClient).ConnectConn

lfaArn, err := tfconnect.FindLambdaFunctionAssociationByArnWithContext(context.Background(), conn, instanceID, functionArn)

if err != nil {
return fmt.Errorf("error finding Connect Lambda Function Association by Function Arn (%s): %w", functionArn, err)
}
Expand All @@ -137,43 +144,16 @@ func testAccCheckLambdaFunctionAssociationExists(resourceName string) resource.T
}
}

func testAccCheckLambdaFunctionAssociationDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_connect_lambda_function_association" {
continue
}

if rs.Primary.ID == "" {
return fmt.Errorf("Connect Lambda Function Association ID not set")
}
instanceID, functionArn, err := tfconnect.LambdaFunctionAssociationParseResourceID(rs.Primary.ID)

if err != nil {
return err
}

conn := acctest.Provider.Meta().(*conns.AWSClient).ConnectConn

lfaArn, err := tfconnect.FindLambdaFunctionAssociationByArnWithContext(context.Background(), conn, instanceID, functionArn)
if err != nil && !tfawserr.ErrCodeEquals(err, connect.ErrCodeResourceNotFoundException) {
return fmt.Errorf("error LambdaFunction Association by Function Arn (%s): still exists: %w ", functionArn, err)
}

if lfaArn != "" {
return fmt.Errorf("error LambdaFunction Association by Function Arn (%s): still exists", functionArn)
}
}
return nil
}

func testAccLambdaFunctionAssociationConfigBase(rName string, rName2 string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}
resource "aws_lambda_function" "test" {
filename = "testdata/lambdatest.zip"
function_name = %[1]q
role = aws_iam_role.test.arn
handler = "exports.handler"
runtime = "nodejs12.x"
runtime = "nodejs14.x"
}
resource "aws_iam_role" "test" {
Expand All @@ -186,7 +166,7 @@ resource "aws_iam_role" "test" {
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
"Service": "lambda.${data.aws_partition.current.dns_suffix}"
},
"Effect": "Allow",
"Sid": ""
Expand Down
Loading

0 comments on commit d20f759

Please sign in to comment.