From 3c53e68b84007db2ab66d04f5d7a562c48c8c3e7 Mon Sep 17 00:00:00 2001 From: Ahmed Bebars Date: Fri, 29 Oct 2021 00:57:53 -0400 Subject: [PATCH] Refactor resources to new formating after #21306 --- aws/internal/service/connect/waiter/waiter.go | 62 ---------- internal/provider/provider.go | 4 + .../service/connect/bot_association.go | 46 ++++---- .../connect/bot_association_data_source.go | 10 +- .../bot_association_data_source_test.go | 43 +++---- .../service/connect/bot_association_test.go | 81 ++++++------- .../finder.go => service/connect/find.go} | 19 ++- internal/{generate => }/service/connect/id.go | 0 .../connect/lambda_function_association.go | 111 ++++++++++++++++++ ...lambda_function_association_data_source.go | 17 +-- ...a_function_association_data_source_test.go | 25 ++-- .../lambda_function_association_test.go | 83 ++++++------- internal/service/connect/wait.go | 6 + ...aws_connect_lambda_function_association.go | 106 ----------------- 14 files changed, 284 insertions(+), 329 deletions(-) delete mode 100644 aws/internal/service/connect/waiter/waiter.go rename resource_aws_connect_bot_association.go => internal/service/connect/bot_association.go (74%) rename data_source_aws_connect_bot_association.go => internal/service/connect/bot_association_data_source.go (81%) rename data_source_aws_connect_bot_association_test.go => internal/service/connect/bot_association_data_source_test.go (64%) rename resource_aws_connect_bot_association_test.go => internal/service/connect/bot_association_test.go (57%) rename internal/{generate/service/connect/finder/finder.go => service/connect/find.go} (71%) rename internal/{generate => }/service/connect/id.go (100%) create mode 100644 internal/service/connect/lambda_function_association.go rename data_source_aws_connect_lambda_function_association.go => internal/service/connect/lambda_function_association_data_source.go (61%) rename data_source_aws_connect_lambda_function_association_test.go => internal/service/connect/lambda_function_association_data_source_test.go (66%) rename resource_aws_connect_lambda_function_association_test.go => internal/service/connect/lambda_function_association_test.go (56%) delete mode 100644 resource_aws_connect_lambda_function_association.go diff --git a/aws/internal/service/connect/waiter/waiter.go b/aws/internal/service/connect/waiter/waiter.go deleted file mode 100644 index 4188ef27d1a..00000000000 --- a/aws/internal/service/connect/waiter/waiter.go +++ /dev/null @@ -1,62 +0,0 @@ -package waiter - -import ( - "context" - "time" - - "github.com/aws/aws-sdk-go/service/connect" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - tfconnect "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect" -) - -const ( - // ConnectInstanceCreateTimeout Timeout for connect instance creation - ConnectInstanceCreatedTimeout = 5 * time.Minute - ConnectInstanceDeletedTimeout = 5 * time.Minute - - ConnectContactFlowCreateTimeout = 5 * time.Minute - ConnectContactFlowUpdateTimeout = 5 * time.Minute - - ConnectLambdaFunctionAssociationCreatedTimeout = 1 * time.Minute - ConnectLambdaFunctionAssociationDeletedTimeout = 1 * time.Minute - - ConnectBotAssociationCreateTimeout = 5 * time.Minute - ConnectBotAssociationDeleteTimeout = 5 * time.Minute -) - -func InstanceCreated(ctx context.Context, conn *connect.Connect, instanceId string) (*connect.DescribeInstanceOutput, error) { - stateConf := &resource.StateChangeConf{ - Pending: []string{connect.InstanceStatusCreationInProgress}, - Target: []string{connect.InstanceStatusActive}, - Refresh: InstanceStatus(ctx, conn, instanceId), - Timeout: ConnectInstanceCreatedTimeout, - } - - outputRaw, err := stateConf.WaitForState() - - if v, ok := outputRaw.(*connect.DescribeInstanceOutput); ok { - return v, err - } - - return nil, err -} - -// We don't have a PENDING_DELETION or DELETED for the Connect instance. -// If the Connect Instance has an associated EXISTING DIRECTORY, removing the connect instance -// will cause an error because it is still has authorized applications. -func InstanceDeleted(ctx context.Context, conn *connect.Connect, instanceId string) (*connect.DescribeInstanceOutput, error) { - stateConf := &resource.StateChangeConf{ - Pending: []string{connect.InstanceStatusActive}, - Target: []string{tfconnect.InstanceStatusStatusNotFound}, - Refresh: InstanceStatus(ctx, conn, instanceId), - Timeout: ConnectInstanceDeletedTimeout, - } - - outputRaw, err := stateConf.WaitForState() - - if v, ok := outputRaw.(*connect.DescribeInstanceOutput); ok { - return v, err - } - - return nil, err -} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 3891ed4d0de..5f24a4e3d41 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -394,8 +394,10 @@ func Provider() *schema.Provider { "aws_cognito_user_pools": cognitoidp.DataSourceUserPools(), + "aws_connect_bot_association": connect.DataSourceBotAssociation(), "aws_connect_contact_flow": connect.DataSourceContactFlow(), "aws_connect_instance": connect.DataSourceInstance(), + "aws_connect_lambda_function_association": connect.DataSourceLambdaFunctionAssociation(), "aws_cur_report_definition": cur.DataSourceReportDefinition(), @@ -926,8 +928,10 @@ func Provider() *schema.Provider { "aws_config_organization_managed_rule": config.ResourceOrganizationManagedRule(), "aws_config_remediation_configuration": config.ResourceRemediationConfiguration(), + "aws_connect_bot_association": connect.ResourceBotAssociation(), "aws_connect_contact_flow": connect.ResourceContactFlow(), "aws_connect_instance": connect.ResourceInstance(), + "aws_connect_lambda_function_association": connect.ResourceLambdaFunctionAssociation(), "aws_cur_report_definition": cur.ResourceReportDefinition(), diff --git a/resource_aws_connect_bot_association.go b/internal/service/connect/bot_association.go similarity index 74% rename from resource_aws_connect_bot_association.go rename to internal/service/connect/bot_association.go index b597b49b590..c441d176442 100644 --- a/resource_aws_connect_bot_association.go +++ b/internal/service/connect/bot_association.go @@ -1,4 +1,4 @@ -package aws +package connect import ( "context" @@ -14,21 +14,19 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" - tfconnect "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect/finder" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect/waiter" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func resourceAwsConnectBotAssociation() *schema.Resource { +func ResourceBotAssociation() *schema.Resource { return &schema.Resource{ - CreateContext: resourceAwsConnectBotAssociationCreate, - ReadContext: resourceAwsConnectBotAssociationRead, - UpdateContext: resourceAwsConnectBotAssociationRead, - DeleteContext: resourceAwsConnectBotAssociationDelete, + CreateContext: resourceBotAssociationCreate, + ReadContext: resourceBotAssociationRead, + UpdateContext: resourceBotAssociationRead, + DeleteContext: resourceBotAssociationDelete, Importer: &schema.ResourceImporter{ State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - instanceID, name, region, err := resourceAwsConnectBotV1AssociationParseID(d.Id()) + instanceID, name, region, err := resourceBotV1AssociationParseID(d.Id()) if err != nil { return nil, err } @@ -41,8 +39,8 @@ func resourceAwsConnectBotAssociation() *schema.Resource { }, }, Timeouts: &schema.ResourceTimeout{ - Create: schema.DefaultTimeout(waiter.ConnectBotAssociationCreateTimeout), - Delete: schema.DefaultTimeout(waiter.ConnectBotAssociationDeleteTimeout), + Create: schema.DefaultTimeout(connectBotAssociationCreateTimeout), + Delete: schema.DefaultTimeout(connectBotAssociationDeleteTimeout), }, Schema: map[string]*schema.Schema{ /* @@ -85,8 +83,8 @@ func resourceAwsConnectBotAssociation() *schema.Resource { } } -func resourceAwsConnectBotAssociationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - conn := meta.(*AWSClient).connectconn +func resourceBotAssociationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).ConnectConn input := &connect.AssociateBotInput{ InstanceId: aws.String(d.Get("instance_id").(string)), @@ -131,17 +129,17 @@ func resourceAwsConnectBotAssociationCreate(ctx context.Context, d *schema.Resou } d.SetId(lbaId) - return resourceAwsConnectBotAssociationRead(ctx, d, meta) + return resourceBotAssociationRead(ctx, d, meta) } -func resourceAwsConnectBotAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - conn := meta.(*AWSClient).connectconn +func resourceBotAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).ConnectConn instanceID := d.Get("instance_id") name := d.Get("bot_name") - lexBot, err := finder.BotAssociationV1ByNameWithContext(ctx, conn, instanceID.(string), name.(string)) + lexBot, err := FindBotAssociationV1ByNameWithContext(ctx, conn, instanceID.(string), name.(string)) - if isAWSErr(err, tfconnect.BotAssociationStatusNotFound, "") || errors.Is(err, tfresource.ErrEmptyResult) { + if tfawserr.ErrMessageContains(err, BotAssociationStatusNotFound, "") || errors.Is(err, tfresource.ErrEmptyResult) { log.Printf("[WARN] Connect Bot V1 Association (%s) not found, removing from state", d.Id()) d.SetId("") return nil @@ -177,10 +175,10 @@ func resourceAwsConnectBotAssociationRead(ctx context.Context, d *schema.Resourc return nil } -func resourceAwsConnectBotAssociationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - conn := meta.(*AWSClient).connectconn +func resourceBotAssociationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).ConnectConn - instanceID, name, region, err := resourceAwsConnectBotV1AssociationParseID(d.Id()) + instanceID, name, region, err := resourceBotV1AssociationParseID(d.Id()) if err != nil { return diag.FromErr(err) @@ -205,7 +203,7 @@ func resourceAwsConnectBotAssociationDelete(ctx context.Context, d *schema.Resou return nil } -func resourceAwsConnectBotV1AssociationParseID(id string) (string, string, string, error) { +func resourceBotV1AssociationParseID(id string) (string, string, string, error) { parts := strings.SplitN(id, ":", 3) if len(parts) != 3 || parts[0] == "" || parts[1] == "" || parts[2] == "" { diff --git a/data_source_aws_connect_bot_association.go b/internal/service/connect/bot_association_data_source.go similarity index 81% rename from data_source_aws_connect_bot_association.go rename to internal/service/connect/bot_association_data_source.go index 400780e37d7..cc91a3288c3 100644 --- a/data_source_aws_connect_bot_association.go +++ b/internal/service/connect/bot_association_data_source.go @@ -1,4 +1,4 @@ -package aws +package connect import ( "context" @@ -7,10 +7,10 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect/finder" + "github.com/hashicorp/terraform-provider-aws/internal/conns" ) -func dataSourceAwsConnectBotAssociation() *schema.Resource { +func DataSourceBotAssociation() *schema.Resource { return &schema.Resource{ ReadContext: dataSourceAwsConnectLexBotAssociationRead, Schema: map[string]*schema.Schema{ @@ -33,11 +33,11 @@ func dataSourceAwsConnectBotAssociation() *schema.Resource { } func dataSourceAwsConnectLexBotAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - conn := meta.(*AWSClient).connectconn + conn := meta.(*conns.AWSClient).ConnectConn instanceID := d.Get("instance_id") name := d.Get("bot_name") - lexBot, err := finder.BotAssociationV1ByNameWithContext(ctx, conn, instanceID.(string), name.(string)) + lexBot, err := FindBotAssociationV1ByNameWithContext(ctx, conn, instanceID.(string), name.(string)) if err != nil { return diag.FromErr(fmt.Errorf("error finding Connect Bot V1 Association by name (%s): %w", name, err)) } diff --git a/data_source_aws_connect_bot_association_test.go b/internal/service/connect/bot_association_data_source_test.go similarity index 64% rename from data_source_aws_connect_bot_association_test.go rename to internal/service/connect/bot_association_data_source_test.go index 790d08ab0d4..0a1f72f3348 100644 --- a/data_source_aws_connect_bot_association_test.go +++ b/internal/service/connect/bot_association_data_source_test.go @@ -1,4 +1,4 @@ -package aws +package connect import ( "context" @@ -8,28 +8,29 @@ import ( "testing" "github.com/aws/aws-sdk-go/service/connect" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/aws-sdk-go-base/tfawserr" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" - tfconnect "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect/finder" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func TestAccAwsConnectBotAssociationDataSource_basic(t *testing.T) { - rName := acctest.RandStringFromCharSet(8, acctest.CharSetAlpha) - rName2 := acctest.RandomWithPrefix("resource-test-terraform") +func TestAccBotAssociationDataSource_basic(t *testing.T) { + rName := sdkacctest.RandStringFromCharSet(8, sdkacctest.CharSetAlpha) + rName2 := sdkacctest.RandomWithPrefix("resource-test-terraform") resourceName := "aws_connect_bot_association.test" datasourceName := "data.aws_connect_bot_association.test" resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ErrorCheck: testAccErrorCheck(t, connect.EndpointsID), - Providers: testAccProviders, - CheckDestroy: testAccAwsConnectBotAssociationDataSourceDestroy, + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, connect.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccBotAssociationDataSourceDestroy, Steps: []resource.TestStep{ { - Config: testAccAwsConnectBotAssociationDataSourceConfigBasic(rName, rName2), + Config: testAccBotAssociationDataSourceConfigBasic(rName, rName2), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "instance_id", resourceName, "instance_id"), resource.TestCheckResourceAttrPair(datasourceName, "bot_name", resourceName, "bot_name"), @@ -40,7 +41,7 @@ func TestAccAwsConnectBotAssociationDataSource_basic(t *testing.T) { }) } -func testAccAwsConnectBotAssociationDataSourceDestroy(s *terraform.State) error { +func testAccBotAssociationDataSourceDestroy(s *terraform.State) error { for _, rs := range s.RootModule().Resources { if rs.Type != "aws_connect_bot_association" { continue @@ -49,16 +50,16 @@ func testAccAwsConnectBotAssociationDataSourceDestroy(s *terraform.State) error if rs.Primary.ID == "" { return fmt.Errorf("Connect Connect Bot V1 Association ID not set") } - instanceID, name, _, err := resourceAwsConnectBotV1AssociationParseID(rs.Primary.ID) + instanceID, name, _, err := resourceBotV1AssociationParseID(rs.Primary.ID) if err != nil { return err } - conn := testAccProvider.Meta().(*AWSClient).connectconn + conn := acctest.Provider.Meta().(*conns.AWSClient).ConnectConn - lexBot, err := finder.BotAssociationV1ByNameWithContext(context.Background(), conn, instanceID, name) + lexBot, err := FindBotAssociationV1ByNameWithContext(context.Background(), conn, instanceID, name) - if isAWSErr(err, tfconnect.BotAssociationStatusNotFound, "") || errors.Is(err, tfresource.ErrEmptyResult) { + if tfawserr.ErrCodeEquals(err, BotAssociationStatusNotFound, "") || errors.Is(err, tfresource.ErrEmptyResult) { log.Printf("[DEBUG] Connect Bot V1 Association (%s) not found, has been removed from state", name) return nil } @@ -74,7 +75,7 @@ func testAccAwsConnectBotAssociationDataSourceDestroy(s *terraform.State) error return nil } -func testAccAwsConnectBotAssociationDataSourceBaseConfig(rName string, rName2 string) string { +func testAccBotAssociationDataSourceBaseConfig(rName string, rName2 string) string { return fmt.Sprintf(` data "aws_region" "current" {} @@ -127,8 +128,8 @@ resource "aws_connect_bot_association" "test" { `, rName, rName2) } -func testAccAwsConnectBotAssociationDataSourceConfigBasic(rName string, rName2 string) string { - return fmt.Sprintf(testAccAwsConnectBotAssociationDataSourceBaseConfig(rName, rName2) + ` +func testAccBotAssociationDataSourceConfigBasic(rName string, rName2 string) string { + return fmt.Sprintf(testAccBotAssociationDataSourceBaseConfig(rName, rName2) + ` data "aws_connect_bot_association" "test" { instance_id = aws_connect_instance.test.id bot_name = aws_connect_bot_association.test.bot_name diff --git a/resource_aws_connect_bot_association_test.go b/internal/service/connect/bot_association_test.go similarity index 57% rename from resource_aws_connect_bot_association_test.go rename to internal/service/connect/bot_association_test.go index 0d7acf8b1c5..7d74e928a19 100644 --- a/resource_aws_connect_bot_association_test.go +++ b/internal/service/connect/bot_association_test.go @@ -1,4 +1,4 @@ -package aws +package connect import ( "context" @@ -8,19 +8,20 @@ import ( "testing" "github.com/aws/aws-sdk-go/service/connect" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/aws-sdk-go-base/tfawserr" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" - tfconnect "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect/finder" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) //Serialized acceptance tests due to Connect account limits (max 2 parallel tests) -func TestAccAwsConnectBotAssociation_serial(t *testing.T) { +func TestAccBotAssociation_serial(t *testing.T) { testCases := map[string]func(t *testing.T){ - "basic": testAccAwsConnectBotAssociation_basic, - "disappears": testAccAwsConnectBotAssociation_disappears, + "basic": testAccBotAssociation_basic, + "disappears": testAccBotAssociation_disappears, } for name, tc := range testCases { @@ -31,22 +32,22 @@ func TestAccAwsConnectBotAssociation_serial(t *testing.T) { } } -func testAccAwsConnectBotAssociation_basic(t *testing.T) { +func testAccBotAssociation_basic(t *testing.T) { var v connect.LexBot - rName := acctest.RandStringFromCharSet(8, acctest.CharSetAlpha) - rName2 := acctest.RandomWithPrefix("resource-test-terraform") + rName := sdkacctest.RandStringFromCharSet(8, sdkacctest.CharSetAlpha) + rName2 := sdkacctest.RandomWithPrefix("resource-test-terraform") resourceName := "aws_connect_bot_association.test" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ErrorCheck: testAccErrorCheck(t, connect.EndpointsID), - Providers: testAccProviders, - CheckDestroy: testAccCheckAwsConnectBotAssociationDestroy, + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, connect.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckBotAssociationDestroy, Steps: []resource.TestStep{ { - Config: testAccAwsConnectBotV1AssociationConfigBasic(rName, rName2), + Config: testAccBotV1AssociationConfigBasic(rName, rName2), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsConnectBotAssociationExists(resourceName, &v), + testAccCheckBotAssociationExists(resourceName, &v), resource.TestCheckResourceAttrSet(resourceName, "instance_id"), resource.TestCheckResourceAttrSet(resourceName, "bot_name"), resource.TestCheckResourceAttrSet(resourceName, "lex_region"), @@ -61,24 +62,24 @@ func testAccAwsConnectBotAssociation_basic(t *testing.T) { }) } -func testAccAwsConnectBotAssociation_disappears(t *testing.T) { +func testAccBotAssociation_disappears(t *testing.T) { var v connect.LexBot - rName := acctest.RandStringFromCharSet(8, acctest.CharSetAlpha) - rName2 := acctest.RandomWithPrefix("resource-test-terraform") + rName := sdkacctest.RandStringFromCharSet(8, sdkacctest.CharSetAlpha) + rName2 := sdkacctest.RandomWithPrefix("resource-test-terraform") resourceName := "aws_connect_bot_association.test" instanceResourceName := "aws_connect_bot_association.test" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ErrorCheck: testAccErrorCheck(t, connect.EndpointsID), - Providers: testAccProviders, - CheckDestroy: testAccCheckAwsConnectBotAssociationDestroy, + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, connect.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckBotAssociationDestroy, Steps: []resource.TestStep{ { - Config: testAccAwsConnectBotV1AssociationConfigBasic(rName, rName2), + Config: testAccBotV1AssociationConfigBasic(rName, rName2), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsConnectBotAssociationExists(resourceName, &v), - testAccCheckResourceDisappears(testAccProvider, resourceAwsConnectBotAssociation(), instanceResourceName), + testAccCheckBotAssociationExists(resourceName, &v), + acctest.CheckResourceDisappears(acctest.Provider, ResourceBotAssociation(), instanceResourceName), ), ExpectNonEmptyPlan: true, }, @@ -86,7 +87,7 @@ func testAccAwsConnectBotAssociation_disappears(t *testing.T) { }) } -func testAccCheckAwsConnectBotAssociationExists(resourceName string, function *connect.LexBot) resource.TestCheckFunc { +func testAccCheckBotAssociationExists(resourceName string, function *connect.LexBot) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] if !ok { @@ -96,15 +97,15 @@ func testAccCheckAwsConnectBotAssociationExists(resourceName string, function *c if rs.Primary.ID == "" { return fmt.Errorf("Connect Bot V1 Association ID not set") } - instanceID, name, _, err := resourceAwsConnectBotV1AssociationParseID(rs.Primary.ID) + instanceID, name, _, err := resourceBotV1AssociationParseID(rs.Primary.ID) if err != nil { return err } - conn := testAccProvider.Meta().(*AWSClient).connectconn + conn := acctest.Provider.Meta().(*conns.AWSClient).ConnectConn - lexBot, err := finder.BotAssociationV1ByNameWithContext(context.Background(), conn, instanceID, name) + lexBot, err := FindBotAssociationV1ByNameWithContext(context.Background(), conn, instanceID, name) if err != nil { return fmt.Errorf("error finding Connect Bot V1 Association by name (%s): %w", name, err) @@ -120,7 +121,7 @@ func testAccCheckAwsConnectBotAssociationExists(resourceName string, function *c } } -func testAccCheckAwsConnectBotAssociationDestroy(s *terraform.State) error { +func testAccCheckBotAssociationDestroy(s *terraform.State) error { for _, rs := range s.RootModule().Resources { if rs.Type != "aws_connect_bot_association" { continue @@ -129,16 +130,16 @@ func testAccCheckAwsConnectBotAssociationDestroy(s *terraform.State) error { if rs.Primary.ID == "" { return fmt.Errorf("Connect Connect Bot V1 Association ID not set") } - instanceID, name, _, err := resourceAwsConnectBotV1AssociationParseID(rs.Primary.ID) + instanceID, name, _, err := resourceBotV1AssociationParseID(rs.Primary.ID) if err != nil { return err } - conn := testAccProvider.Meta().(*AWSClient).connectconn + conn := acctest.Provider.Meta().(*conns.AWSClient).ConnectConn - lexBot, err := finder.BotAssociationV1ByNameWithContext(context.Background(), conn, instanceID, name) + lexBot, err := FindBotAssociationV1ByNameWithContext(context.Background(), conn, instanceID, name) - if isAWSErr(err, tfconnect.BotAssociationStatusNotFound, "") || errors.Is(err, tfresource.ErrEmptyResult) { + if tfawserr.ErrCodeEquals(err, BotAssociationStatusNotFound, "") || errors.Is(err, tfresource.ErrEmptyResult) { log.Printf("[DEBUG] Connect Bot V1 Association (%s) not found, has been removed from state", name) return nil } @@ -154,7 +155,7 @@ func testAccCheckAwsConnectBotAssociationDestroy(s *terraform.State) error { return nil } -func testAccAwsConnectBotV1AssociationConfigBase(rName string, rName2 string) string { +func testAccBotV1AssociationConfigBase(rName string, rName2 string) string { return fmt.Sprintf(` resource "aws_lex_intent" "test" { create_version = true @@ -201,9 +202,9 @@ resource "aws_connect_instance" "test" { `, rName, rName2) } -func testAccAwsConnectBotV1AssociationConfigBasic(rName string, rName2 string) string { - return composeConfig( - testAccAwsConnectBotV1AssociationConfigBase(rName, rName2), +func testAccBotV1AssociationConfigBasic(rName string, rName2 string) string { + return acctest.ConfigCompose( + testAccBotV1AssociationConfigBase(rName, rName2), ` data "aws_region" "current" {} diff --git a/internal/generate/service/connect/finder/finder.go b/internal/service/connect/find.go similarity index 71% rename from internal/generate/service/connect/finder/finder.go rename to internal/service/connect/find.go index d9addead3cc..d4bb7367d06 100644 --- a/internal/generate/service/connect/finder/finder.go +++ b/internal/service/connect/find.go @@ -1,20 +1,19 @@ -package finder +package connect import ( "context" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/connect" - tfconnect "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func LambdaFunctionAssociationByArnWithContext(ctx context.Context, conn *connect.Connect, instanceID string, functionArn string) (string, error) { +func FindLambdaFunctionAssociationByArnWithContext(ctx context.Context, conn *connect.Connect, instanceID string, functionArn string) (string, error) { var result string input := &connect.ListLambdaFunctionsInput{ InstanceId: aws.String(instanceID), - MaxResults: aws.Int64(tfconnect.ListLambdaFunctionsMaxResults), + MaxResults: aws.Int64(ListLambdaFunctionsMaxResults), } err := conn.ListLambdaFunctionsPagesWithContext(ctx, input, func(page *connect.ListLambdaFunctionsOutput, lastPage bool) bool { @@ -43,13 +42,13 @@ func LambdaFunctionAssociationByArnWithContext(ctx context.Context, conn *connec return result, nil } -func BotAssociationV1ByNameWithContext(ctx context.Context, conn *connect.Connect, instanceID string, name string) (*connect.LexBot, error) { +func FindBotAssociationV1ByNameWithContext(ctx context.Context, conn *connect.Connect, instanceID string, name string) (*connect.LexBot, error) { var result *connect.LexBot input := &connect.ListBotsInput{ InstanceId: aws.String(instanceID), - LexVersion: aws.String(tfconnect.LexBotV1Version), - MaxResults: aws.Int64(tfconnect.ListBotsMaxResults), + LexVersion: aws.String(LexBotV1Version), + MaxResults: aws.Int64(ListBotsMaxResults), } err := conn.ListBotsPagesWithContext(ctx, input, func(page *connect.ListBotsOutput, lastPage bool) bool { @@ -81,8 +80,8 @@ func BotAssociationV2ByAliasArnWithContext(ctx context.Context, conn *connect.Co input := &connect.ListBotsInput{ InstanceId: aws.String(instanceID), - LexVersion: aws.String(tfconnect.LexBotV2Version), - MaxResults: aws.Int64(tfconnect.ListBotsMaxResults), + LexVersion: aws.String(LexBotV2Version), + MaxResults: aws.Int64(ListBotsMaxResults), } err := conn.ListBotsPagesWithContext(ctx, input, func(page *connect.ListBotsOutput, lastPage bool) bool { diff --git a/internal/generate/service/connect/id.go b/internal/service/connect/id.go similarity index 100% rename from internal/generate/service/connect/id.go rename to internal/service/connect/id.go diff --git a/internal/service/connect/lambda_function_association.go b/internal/service/connect/lambda_function_association.go new file mode 100644 index 00000000000..9b2d2eaef58 --- /dev/null +++ b/internal/service/connect/lambda_function_association.go @@ -0,0 +1,111 @@ +package connect + +import ( + "context" + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/connect" + "github.com/hashicorp/aws-sdk-go-base/tfawserr" + "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/verify" +) + +func ResourceLambdaFunctionAssociation() *schema.Resource { + return &schema.Resource{ + CreateContext: resourceLambdaFunctionAssociationCreate, + ReadContext: resourceLambdaFunctionAssociationRead, + UpdateContext: resourceLambdaFunctionAssociationRead, + DeleteContext: resourceLambdaFunctionAssociationDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + Schema: map[string]*schema.Schema{ + "function_arn": { + Type: schema.TypeString, + Required: true, + ValidateFunc: verify.ValidARN, + }, + "instance_id": { + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func resourceLambdaFunctionAssociationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).ConnectConn + + input := &connect.AssociateLambdaFunctionInput{ + InstanceId: aws.String(d.Get("instance_id").(string)), + FunctionArn: aws.String(d.Get("function_arn").(string)), + } + + lfaId := fmt.Sprintf("%s:%s", 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)) + if err != nil { + return diag.FromErr(fmt.Errorf("error finding Connect Lambda Function Association by Function ARN (%s): %w", lfaArn, 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(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 := LambdaFunctionAssociationParseID(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 == "" { + log.Printf("[WARN] Connect Lambda Function Association (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + + d.Set("function_arn", functionArn) + d.Set("instance_id", instanceID) + + return nil +} + +func resourceLambdaFunctionAssociationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).ConnectConn + + instanceID, functionArn, err := LambdaFunctionAssociationParseID(d.Id()) + if err != nil { + return diag.FromErr(err) + } + + input := &connect.DisassociateLambdaFunctionInput{ + InstanceId: aws.String(instanceID), + FunctionArn: aws.String(functionArn), + } + + log.Printf("[DEBUG] Deleting Connect Lambda Function Association %s", d.Id()) + + _, err = conn.DisassociateLambdaFunction(input) + + if err != nil && !tfawserr.ErrCodeEquals(err, "ResourceNotFoundException", "") { + return diag.FromErr(fmt.Errorf("error deleting Connect Lambda Function Association (%s): %w", d.Id(), err)) + } + return nil +} diff --git a/data_source_aws_connect_lambda_function_association.go b/internal/service/connect/lambda_function_association_data_source.go similarity index 61% rename from data_source_aws_connect_lambda_function_association.go rename to internal/service/connect/lambda_function_association_data_source.go index 457090aec48..fdfe76a9f44 100644 --- a/data_source_aws_connect_lambda_function_association.go +++ b/internal/service/connect/lambda_function_association_data_source.go @@ -1,4 +1,4 @@ -package aws +package connect import ( "context" @@ -6,17 +6,18 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect/finder" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/verify" ) -func dataSourceAwsConnectLambdaFunctionAssociation() *schema.Resource { +func DataSourceLambdaFunctionAssociation() *schema.Resource { return &schema.Resource{ - ReadContext: dataSourceAwsConnectLambdaFunctionAssociationRead, + ReadContext: dataSourceLambdaFunctionAssociationRead, Schema: map[string]*schema.Schema{ "function_arn": { Type: schema.TypeString, Required: true, - ValidateFunc: validateArn, + ValidateFunc: verify.ValidARN, }, "instance_id": { Type: schema.TypeString, @@ -26,12 +27,12 @@ func dataSourceAwsConnectLambdaFunctionAssociation() *schema.Resource { } } -func dataSourceAwsConnectLambdaFunctionAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - conn := meta.(*AWSClient).connectconn +func dataSourceLambdaFunctionAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).ConnectConn functionArn := d.Get("function_arn") instanceID := d.Get("instance_id") - lfaArn, err := finder.LambdaFunctionAssociationByArnWithContext(ctx, conn, instanceID.(string), functionArn.(string)) + lfaArn, err := FindLambdaFunctionAssociationByArnWithContext(ctx, conn, instanceID.(string), functionArn.(string)) if err != nil { return diag.FromErr(fmt.Errorf("error finding Connect Lambda Function Association by ARN (%s): %w", functionArn, err)) } diff --git a/data_source_aws_connect_lambda_function_association_test.go b/internal/service/connect/lambda_function_association_data_source_test.go similarity index 66% rename from data_source_aws_connect_lambda_function_association_test.go rename to internal/service/connect/lambda_function_association_data_source_test.go index b8b97dab0e4..2ae921c7a41 100644 --- a/data_source_aws_connect_lambda_function_association_test.go +++ b/internal/service/connect/lambda_function_association_data_source_test.go @@ -1,27 +1,28 @@ -package aws +package connect import ( "fmt" "testing" "github.com/aws/aws-sdk-go/service/connect" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" ) -func TestAccAwsConnectLambdaFunctionAssociationDataSource_basic(t *testing.T) { - rName := acctest.RandStringFromCharSet(8, acctest.CharSetAlpha) - rName2 := acctest.RandomWithPrefix("resource-test-terraform") +func TestAccLambdaFunctionAssociationDataSource_basic(t *testing.T) { + rName := sdkacctest.RandStringFromCharSet(8, sdkacctest.CharSetAlpha) + rName2 := sdkacctest.RandomWithPrefix("resource-test-terraform") resourceName := "aws_connect_lambda_function_association.test" datasourceName := "data.aws_connect_lambda_function_association.test" resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ErrorCheck: testAccErrorCheck(t, connect.EndpointsID), - Providers: testAccProviders, + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, connect.EndpointsID), + Providers: acctest.Providers, Steps: []resource.TestStep{ { - Config: testAccAwsConnectLambdaFunctionAssociationDataSourceConfigBasic(rName, rName2), + Config: testAccLambdaFunctionAssociationDataSourceConfigBasic(rName, rName2), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "instance_id", resourceName, "instance_id"), resource.TestCheckResourceAttrPair(datasourceName, "function_arn", resourceName, "function_arn"), @@ -31,7 +32,7 @@ func TestAccAwsConnectLambdaFunctionAssociationDataSource_basic(t *testing.T) { }) } -func testAccAwsConnectLambdaFunctionAssociationDataSourceBaseConfig(rName string, rName2 string) string { +func testAccLambdaFunctionAssociationDataSourceBaseConfig(rName string, rName2 string) string { return fmt.Sprintf(` resource "aws_lambda_function" "test" { filename = "test-fixtures/lambdatest.zip" @@ -75,8 +76,8 @@ resource "aws_connect_lambda_function_association" "test" { `, rName, rName2) } -func testAccAwsConnectLambdaFunctionAssociationDataSourceConfigBasic(rName string, rName2 string) string { - return fmt.Sprintf(testAccAwsConnectLambdaFunctionAssociationDataSourceBaseConfig(rName, rName2) + ` +func testAccLambdaFunctionAssociationDataSourceConfigBasic(rName string, rName2 string) string { + return fmt.Sprintf(testAccLambdaFunctionAssociationDataSourceBaseConfig(rName, rName2) + ` data "aws_connect_lambda_function_association" "test" { function_arn = aws_connect_lambda_function_association.test.function_arn instance_id = aws_connect_instance.test.id diff --git a/resource_aws_connect_lambda_function_association_test.go b/internal/service/connect/lambda_function_association_test.go similarity index 56% rename from resource_aws_connect_lambda_function_association_test.go rename to internal/service/connect/lambda_function_association_test.go index 5e6533fe172..d81ba5cf49b 100644 --- a/resource_aws_connect_lambda_function_association_test.go +++ b/internal/service/connect/lambda_function_association_test.go @@ -1,4 +1,4 @@ -package aws +package connect import ( "context" @@ -6,18 +6,19 @@ import ( "testing" "github.com/aws/aws-sdk-go/service/connect" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/aws-sdk-go-base/tfawserr" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" - tfconnect "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect/finder" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/internal/conns" ) //Serialized acceptance tests due to Connect account limits (max 2 parallel tests) -func TestAccAwsConnectLambdaFunctionAssociation_serial(t *testing.T) { +func TestAccLambdaFunctionAssociation_serial(t *testing.T) { testCases := map[string]func(t *testing.T){ - "basic": testAccAwsConnectLambdaFunctionAssociation_basic, - "disappears": testAccAwsConnectLambdaFunctionAssociation_disappears, + "basic": testAccLambdaFunctionAssociation_basic, + "disappears": testAccLambdaFunctionAssociation_disappears, } for name, tc := range testCases { @@ -28,21 +29,21 @@ func TestAccAwsConnectLambdaFunctionAssociation_serial(t *testing.T) { } } -func testAccAwsConnectLambdaFunctionAssociation_basic(t *testing.T) { - rName := acctest.RandStringFromCharSet(8, acctest.CharSetAlpha) - rName2 := acctest.RandomWithPrefix("resource-test-terraform") +func testAccLambdaFunctionAssociation_basic(t *testing.T) { + rName := sdkacctest.RandStringFromCharSet(8, sdkacctest.CharSetAlpha) + rName2 := sdkacctest.RandomWithPrefix("resource-test-terraform") resourceName := "aws_connect_lambda_function_association.test" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ErrorCheck: testAccErrorCheck(t, connect.EndpointsID), - Providers: testAccProviders, - CheckDestroy: testAccCheckAwsConnectLambdaFunctionAssociationDestroy, + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, connect.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckLambdaFunctionAssociationDestroy, Steps: []resource.TestStep{ { Config: testAccAwsConnectLambdaFunctionAssociationConfigBasic(rName, rName2), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsConnectLambdaFunctionAssociationExists(resourceName), + testAccCheckLambdaFunctionAssociationExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "instance_id"), resource.TestCheckResourceAttrSet(resourceName, "function_arn"), ), @@ -56,22 +57,22 @@ func testAccAwsConnectLambdaFunctionAssociation_basic(t *testing.T) { }) } -func testAccAwsConnectLambdaFunctionAssociation_disappears(t *testing.T) { - rName := acctest.RandStringFromCharSet(8, acctest.CharSetAlpha) - rName2 := acctest.RandomWithPrefix("resource-test-terraform") +func testAccLambdaFunctionAssociation_disappears(t *testing.T) { + rName := sdkacctest.RandStringFromCharSet(8, sdkacctest.CharSetAlpha) + rName2 := sdkacctest.RandomWithPrefix("resource-test-terraform") resourceName := "aws_connect_lambda_function_association.test" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ErrorCheck: testAccErrorCheck(t, connect.EndpointsID), - Providers: testAccProviders, - CheckDestroy: testAccAwsConnectLambdaFunctionAssociationDestroy, + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, connect.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccLambdaFunctionAssociationDestroy, Steps: []resource.TestStep{ { Config: testAccAwsConnectLambdaFunctionAssociationConfigBasic(rName, rName2), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsConnectLambdaFunctionAssociationExists(resourceName), - testAccCheckResourceDisappears(testAccProvider, resourceAwsConnectLambdaFunctionAssociation(), resourceName), + testAccCheckLambdaFunctionAssociationExists(resourceName), + acctest.CheckResourceDisappears(acctest.Provider, ResourceLambdaFunctionAssociation(), resourceName), ), ExpectNonEmptyPlan: true, }, @@ -79,21 +80,21 @@ func testAccAwsConnectLambdaFunctionAssociation_disappears(t *testing.T) { }) } -func testAccAwsConnectLambdaFunctionAssociationDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*AWSClient).connectconn +func testAccLambdaFunctionAssociationDestroy(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).ConnectConn for _, rs := range s.RootModule().Resources { if rs.Type != "aws_connect_lambda_function_association" { continue } - instanceID, functionArn, err := tfconnect.LambdaFunctionAssociationParseID(rs.Primary.ID) + instanceID, functionArn, err := LambdaFunctionAssociationParseID(rs.Primary.ID) if err != nil { return err } - lfaArn, err := finder.LambdaFunctionAssociationByArnWithContext(context.Background(), conn, instanceID, functionArn) - if err != nil && !isAWSErr(err, "ResourceNotFoundException", "") { + lfaArn, err := 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) } @@ -104,7 +105,7 @@ func testAccAwsConnectLambdaFunctionAssociationDestroy(s *terraform.State) error return nil } -func testAccCheckAwsConnectLambdaFunctionAssociationExists(resourceName string) resource.TestCheckFunc { +func testAccCheckLambdaFunctionAssociationExists(resourceName string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] if !ok { @@ -114,15 +115,15 @@ func testAccCheckAwsConnectLambdaFunctionAssociationExists(resourceName string) if rs.Primary.ID == "" { return fmt.Errorf("error Connect Lambda Function Association ID not set") } - instanceID, functionArn, err := tfconnect.LambdaFunctionAssociationParseID(rs.Primary.ID) + instanceID, functionArn, err := LambdaFunctionAssociationParseID(rs.Primary.ID) if err != nil { return err } - conn := testAccProvider.Meta().(*AWSClient).connectconn + conn := acctest.Provider.Meta().(*conns.AWSClient).ConnectConn - lfaArn, err := finder.LambdaFunctionAssociationByArnWithContext(context.Background(), conn, instanceID, functionArn) + lfaArn, err := 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) } @@ -135,7 +136,7 @@ func testAccCheckAwsConnectLambdaFunctionAssociationExists(resourceName string) } } -func testAccCheckAwsConnectLambdaFunctionAssociationDestroy(s *terraform.State) error { +func testAccCheckLambdaFunctionAssociationDestroy(s *terraform.State) error { for _, rs := range s.RootModule().Resources { if rs.Type != "aws_connect_lambda_function_association" { continue @@ -144,16 +145,16 @@ func testAccCheckAwsConnectLambdaFunctionAssociationDestroy(s *terraform.State) if rs.Primary.ID == "" { return fmt.Errorf("Connect Lambda Function Association ID not set") } - instanceID, functionArn, err := tfconnect.LambdaFunctionAssociationParseID(rs.Primary.ID) + instanceID, functionArn, err := LambdaFunctionAssociationParseID(rs.Primary.ID) if err != nil { return err } - conn := testAccProvider.Meta().(*AWSClient).connectconn + conn := acctest.Provider.Meta().(*conns.AWSClient).ConnectConn - lfaArn, err := finder.LambdaFunctionAssociationByArnWithContext(context.Background(), conn, instanceID, functionArn) - if err != nil && !isAWSErr(err, "ResourceNotFoundException", "") { + lfaArn, err := 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) } @@ -164,7 +165,7 @@ func testAccCheckAwsConnectLambdaFunctionAssociationDestroy(s *terraform.State) return nil } -func testAccAwsConnectLambdaFunctionAssociationConfigBase(rName string, rName2 string) string { +func testAccLambdaFunctionAssociationConfigBase(rName string, rName2 string) string { return fmt.Sprintf(` resource "aws_lambda_function" "test" { filename = "test-fixtures/lambdatest.zip" @@ -204,8 +205,8 @@ resource "aws_connect_instance" "test" { } func testAccAwsConnectLambdaFunctionAssociationConfigBasic(rName string, rName2 string) string { - return composeConfig( - testAccAwsConnectLambdaFunctionAssociationConfigBase(rName, rName2), ` + return acctest.ConfigCompose( + testAccLambdaFunctionAssociationConfigBase(rName, rName2), ` resource "aws_connect_lambda_function_association" "test" { instance_id = aws_connect_instance.test.id function_arn = aws_lambda_function.test.arn diff --git a/internal/service/connect/wait.go b/internal/service/connect/wait.go index eb5eb53f602..25f51c3674f 100644 --- a/internal/service/connect/wait.go +++ b/internal/service/connect/wait.go @@ -15,6 +15,12 @@ const ( connectContactFlowCreateTimeout = 5 * time.Minute connectContactFlowUpdateTimeout = 5 * time.Minute + + connectLambdaFunctionAssociationCreatedTimeout = 1 * time.Minute + connectLambdaFunctionAssociationDeletedTimeout = 1 * time.Minute + + connectBotAssociationCreateTimeout = 5 * time.Minute + connectBotAssociationDeleteTimeout = 5 * time.Minute ) func waitInstanceCreated(ctx context.Context, conn *connect.Connect, instanceId string) (*connect.DescribeInstanceOutput, error) { diff --git a/resource_aws_connect_lambda_function_association.go b/resource_aws_connect_lambda_function_association.go deleted file mode 100644 index 364d642d3f7..00000000000 --- a/resource_aws_connect_lambda_function_association.go +++ /dev/null @@ -1,106 +0,0 @@ -package aws - -import ( - "context" - "fmt" - "log" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/connect" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - tfconnect "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/connect/finder" -) - -func resourceAwsConnectLambdaFunctionAssociation() *schema.Resource { - return &schema.Resource{ - CreateContext: resourceAwsConnectLambdaFunctionAssociationCreate, - ReadContext: resourceAwsConnectLambdaFunctionAssociationRead, - UpdateContext: resourceAwsConnectLambdaFunctionAssociationRead, - DeleteContext: resourceAwsConnectLambdaFunctionAssociationDelete, - Importer: &schema.ResourceImporter{ - StateContext: schema.ImportStatePassthroughContext, - }, - Schema: map[string]*schema.Schema{ - "function_arn": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validateArn, - }, - "instance_id": { - Type: schema.TypeString, - Required: true, - }, - }, - } -} - -func resourceAwsConnectLambdaFunctionAssociationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - conn := meta.(*AWSClient).connectconn - - input := &connect.AssociateLambdaFunctionInput{ - InstanceId: aws.String(d.Get("instance_id").(string)), - FunctionArn: aws.String(d.Get("function_arn").(string)), - } - - lfaid := tfconnect.LambdaFunctionAssociationID(d.Get("instance_id").(string), d.Get("function_arn").(string)) - - 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", lfaid, err)) - } - - d.SetId(lfaid) - return resourceAwsConnectLambdaFunctionAssociationRead(ctx, d, meta) -} - -func resourceAwsConnectLambdaFunctionAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - conn := meta.(*AWSClient).connectconn - - instanceID, functionArn, err := tfconnect.LambdaFunctionAssociationParseID(d.Id()) - if err != nil { - return diag.FromErr(err) - } - - lfaArn, err := finder.LambdaFunctionAssociationByArnWithContext(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 == "" { - log.Printf("[WARN] Connect Lambda Function Association (%s) not found, removing from state", d.Id()) - d.SetId("") - return nil - } - - d.Set("function_arn", functionArn) - d.Set("instance_id", instanceID) - - return nil -} - -func resourceAwsConnectLambdaFunctionAssociationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - conn := meta.(*AWSClient).connectconn - - instanceID, functionArn, err := tfconnect.LambdaFunctionAssociationParseID(d.Id()) - if err != nil { - return diag.FromErr(err) - } - - input := &connect.DisassociateLambdaFunctionInput{ - InstanceId: aws.String(instanceID), - FunctionArn: aws.String(functionArn), - } - - log.Printf("[DEBUG] Deleting Connect Lambda Function Association %s", d.Id()) - - _, err = conn.DisassociateLambdaFunction(input) - - if err != nil && !isAWSErr(err, "ResourceNotFoundException", "") { - return diag.FromErr(fmt.Errorf("error deleting Connect Lambda Function Association (%s): %w", d.Id(), err)) - } - return nil -}