From 72b325dbb77d280cc882c872658271f24768ebe1 Mon Sep 17 00:00:00 2001 From: GlennChia Date: Tue, 11 Jan 2022 12:13:47 +0800 Subject: [PATCH 1/9] feat(connect): contact flow module datasrc schema --- .../contact_flow_module_data_source.go | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 internal/service/connect/contact_flow_module_data_source.go diff --git a/internal/service/connect/contact_flow_module_data_source.go b/internal/service/connect/contact_flow_module_data_source.go new file mode 100644 index 000000000000..092d27641045 --- /dev/null +++ b/internal/service/connect/contact_flow_module_data_source.go @@ -0,0 +1,58 @@ +package connect + +import ( + "context" + "fmt" + + "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" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" +) + +func DataSourceContactFlowModule() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceContactFlowModuleRead, + Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Computed: true, + }, + "contact_flow_module_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ExactlyOneOf: []string{"contact_flow_module_id", "name"}, + }, + "content": { + Type: schema.TypeString, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Computed: true, + }, + "instance_id": { + Type: schema.TypeString, + Required: true, + }, + "name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ExactlyOneOf: []string{"name", "contact_flow_module_id"}, + }, + "state": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tftags.TagsSchemaComputed(), + }, + } +} From a3a7d20865b5b9b6e9584011e2add7d5b270b48f Mon Sep 17 00:00:00 2001 From: GlennChia Date: Tue, 11 Jan 2022 12:14:04 +0800 Subject: [PATCH 2/9] feat(connect): contact flow module datasrc reads uses either contact_flow_module_id or name DescribeContactFlowModuleWithContext api ListContactFlowModulesPagesWithContext api --- .../contact_flow_module_data_source.go | 90 +++++++++++++++++++ internal/service/connect/enum.go | 4 + 2 files changed, 94 insertions(+) diff --git a/internal/service/connect/contact_flow_module_data_source.go b/internal/service/connect/contact_flow_module_data_source.go index 092d27641045..b2eac7b29fd6 100644 --- a/internal/service/connect/contact_flow_module_data_source.go +++ b/internal/service/connect/contact_flow_module_data_source.go @@ -56,3 +56,93 @@ func DataSourceContactFlowModule() *schema.Resource { }, } } + +func dataSourceContactFlowModuleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).ConnectConn + ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig + + instanceID := d.Get("instance_id").(string) + + input := &connect.DescribeContactFlowModuleInput{ + InstanceId: aws.String(instanceID), + } + + if v, ok := d.GetOk("contact_flow_module_id"); ok { + input.ContactFlowModuleId = aws.String(v.(string)) + } else if v, ok := d.GetOk("name"); ok { + name := v.(string) + contactFlowModuleSummary, err := dataSourceGetConnectContactFlowModuleSummaryByName(ctx, conn, instanceID, name) + + if err != nil { + return diag.FromErr(fmt.Errorf("error finding Connect Contact Flow Module Summary by name (%s): %w", name, err)) + } + + if contactFlowModuleSummary == nil { + return diag.FromErr(fmt.Errorf("error finding Connect Contact Flow Module Summary by name (%s): not found", name)) + } + + input.ContactFlowModuleId = contactFlowModuleSummary.Id + } + + resp, err := conn.DescribeContactFlowModuleWithContext(ctx, input) + + if err != nil { + return diag.FromErr(fmt.Errorf("error getting Connect Contact Flow Module: %w", err)) + } + + if resp == nil || resp.ContactFlowModule == nil { + return diag.FromErr(fmt.Errorf("error getting Connect Contact Flow Module: empty response")) + } + + contactFlowModule := resp.ContactFlowModule + + d.Set("arn", contactFlowModule.Arn) + d.Set("contact_flow_module_id", contactFlowModule.Id) + d.Set("content", contactFlowModule.Content) + d.Set("description", contactFlowModule.Description) + d.Set("name", contactFlowModule.Name) + d.Set("state", contactFlowModule.State) + d.Set("status", contactFlowModule.Status) + + if err := d.Set("tags", KeyValueTags(contactFlowModule.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { + return diag.FromErr(fmt.Errorf("error setting tags: %s", err)) + } + + d.SetId(fmt.Sprintf("%s:%s", instanceID, aws.StringValue(contactFlowModule.Id))) + + return nil +} + +func dataSourceGetConnectContactFlowModuleSummaryByName(ctx context.Context, conn *connect.Connect, instanceID, name string) (*connect.ContactFlowModuleSummary, error) { + var result *connect.ContactFlowModuleSummary + + input := &connect.ListContactFlowModulesInput{ + InstanceId: aws.String(instanceID), + MaxResults: aws.Int64(ListContactFlowModulesMaxResults), + } + + err := conn.ListContactFlowModulesPagesWithContext(ctx, input, func(page *connect.ListContactFlowModulesOutput, lastPage bool) bool { + if page == nil { + return !lastPage + } + + for _, cf := range page.ContactFlowModulesSummaryList { + if cf == nil { + continue + } + + if aws.StringValue(cf.Name) == name { + result = cf + return false + } + } + + return !lastPage + }) + + if err != nil { + return nil, err + } + + return result, nil +} diff --git a/internal/service/connect/enum.go b/internal/service/connect/enum.go index 2e0f48b91cc3..75c93922748d 100644 --- a/internal/service/connect/enum.go +++ b/internal/service/connect/enum.go @@ -8,7 +8,11 @@ const BotAssociationStatusNotFound = "ResourceNotFoundException" const ( ListInstancesMaxResults = 10 // MaxResults Valid Range: Minimum value of 1. Maximum value of 1000 + // https://docs.aws.amazon.com/connect/latest/APIReference/API_ListContactFlows.html ListContactFlowsMaxResults = 60 + // MaxResults Valid Range: Minimum value of 1. Maximum value of 1000 + // https://docs.aws.amazon.com/connect/latest/APIReference/API_ListContactFlowModules.html + ListContactFlowModulesMaxResults = 60 // MaxResults Valid Range: Minimum value of 1. Maximum value of 25 ListBotsMaxResults = 25 // MaxResults Valid Range: Minimum value of 1. Maximum value of 1000 From 434f8bbf5b57d924f86f37d77b0aa34c942129fa Mon Sep 17 00:00:00 2001 From: GlennChia Date: Tue, 11 Jan 2022 12:14:12 +0800 Subject: [PATCH 3/9] feat(connect): contact flow mod datasrc provider --- internal/provider/provider.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 7d8ea622abc4..8bb73cfc8815 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -409,6 +409,7 @@ func Provider() *schema.Provider { "aws_connect_bot_association": connect.DataSourceBotAssociation(), "aws_connect_contact_flow": connect.DataSourceContactFlow(), + "aws_connect_contact_flow_module": connect.DataSourceContactFlowModule(), "aws_connect_hours_of_operation": connect.DataSourceHoursOfOperation(), "aws_connect_instance": connect.DataSourceInstance(), "aws_connect_lambda_function_association": connect.DataSourceLambdaFunctionAssociation(), From 36a583d632ba4f7a3261f1766e0e3727c64c6fb6 Mon Sep 17 00:00:00 2001 From: GlennChia Date: Tue, 11 Jan 2022 12:15:09 +0800 Subject: [PATCH 4/9] test(connect): id for contact flow mod datasrc --- .../contact_flow_module_data_source_test.go | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 internal/service/connect/contact_flow_module_data_source_test.go diff --git a/internal/service/connect/contact_flow_module_data_source_test.go b/internal/service/connect/contact_flow_module_data_source_test.go new file mode 100644 index 000000000000..b1b3a13cf428 --- /dev/null +++ b/internal/service/connect/contact_flow_module_data_source_test.go @@ -0,0 +1,78 @@ +package connect_test + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/service/connect" + 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 TestAccConnectContactFlowModuleDataSource_contactFlowModuleID(t *testing.T) { + rName := sdkacctest.RandomWithPrefix("resource-test-terraform") + resourceName := "aws_connect_contact_flow_module.test" + datasourceName := "data.aws_connect_contact_flow_module.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, connect.EndpointsID), + Providers: acctest.Providers, + Steps: []resource.TestStep{ + { + Config: testAccContactFlowModuleDataSourceConfig_ContactFlowModuleID(rName, resourceName), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrPair(datasourceName, "id", resourceName, "id"), + resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"), + resource.TestCheckResourceAttrPair(datasourceName, "contact_flow_module_id", resourceName, "contact_flow_module_id"), + resource.TestCheckResourceAttrPair(datasourceName, "instance_id", resourceName, "instance_id"), + resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"), + resource.TestCheckResourceAttrPair(datasourceName, "description", resourceName, "description"), + resource.TestCheckResourceAttrPair(datasourceName, "content", resourceName, "content"), + resource.TestCheckResourceAttrSet(datasourceName, "state"), + // Using contact_flow_module_id returns the "status" attribute + // https://docs.aws.amazon.com/connect/latest/APIReference/API_DescribeContactFlowModule.html#API_DescribeContactFlowModule_ResponseSyntax + resource.TestCheckResourceAttrSet(datasourceName, "status"), + resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"), + ), + }, + }, + }) +} + +func testAccContactFlowModuleBaseDataSourceConfig(rName, rName2 string) string { + return fmt.Sprintf(` +resource "aws_connect_instance" "test" { + identity_management_type = "CONNECT_MANAGED" + inbound_calls_enabled = true + instance_alias = %[1]q + outbound_calls_enabled = true +} + +resource "aws_connect_contact_flow_module" "test" { + instance_id = aws_connect_instance.test.id + name = %[2]q + description = "Test Contact Flow Module Description" + content = file("./test-fixtures/connect_contact_flow_module.json") + + tags = { + "Name" = "Test Contact Flow Module", + "Application" = "Terraform", + "Method" = "Create" + } +} + `, rName, rName2) +} + +func testAccContactFlowModuleDataSourceConfig_ContactFlowModuleID(rName, rName2 string) string { + return acctest.ConfigCompose( + testAccContactFlowModuleBaseDataSourceConfig(rName, rName2), + ` +data "aws_connect_contact_flow_module" "test" { + instance_id = aws_connect_instance.test.id + contact_flow_module_id = aws_connect_contact_flow_module.test.contact_flow_module_id +} +`) +} + From 6d85e66c77afd8b1d822cf2bcb79ccb036b77091 Mon Sep 17 00:00:00 2001 From: GlennChia Date: Tue, 11 Jan 2022 12:15:30 +0800 Subject: [PATCH 5/9] test(connect): name for contact flow mod datasrc --- .../contact_flow_module_data_source_test.go | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/internal/service/connect/contact_flow_module_data_source_test.go b/internal/service/connect/contact_flow_module_data_source_test.go index b1b3a13cf428..72415e67b8cb 100644 --- a/internal/service/connect/contact_flow_module_data_source_test.go +++ b/internal/service/connect/contact_flow_module_data_source_test.go @@ -41,6 +41,37 @@ func TestAccConnectContactFlowModuleDataSource_contactFlowModuleID(t *testing.T) }) } +func TestAccConnectContactFlowModuleDataSource_name(t *testing.T) { + rName := sdkacctest.RandomWithPrefix("resource-test-terraform") + rName2 := sdkacctest.RandomWithPrefix("resource-test-terraform") + resourceName := "aws_connect_contact_flow_module.test" + datasourceName := "data.aws_connect_contact_flow_module.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, connect.EndpointsID), + Providers: acctest.Providers, + Steps: []resource.TestStep{ + { + Config: testAccContactFlowModuleDataSourceConfig_Name(rName, rName2), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrPair(datasourceName, "id", resourceName, "id"), + resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"), + resource.TestCheckResourceAttrPair(datasourceName, "contact_flow_module_id", resourceName, "contact_flow_module_id"), + resource.TestCheckResourceAttrPair(datasourceName, "instance_id", resourceName, "instance_id"), + resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"), + resource.TestCheckResourceAttrPair(datasourceName, "description", resourceName, "description"), + resource.TestCheckResourceAttrPair(datasourceName, "content", resourceName, "content"), + resource.TestCheckResourceAttrSet(datasourceName, "state"), + // Using name does not return the "status" attribute + // https://docs.aws.amazon.com/connect/latest/APIReference/API_ListContactFlowModules.html#API_ListContactFlowModules_ResponseSyntax + resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"), + ), + }, + }, + }) +} + func testAccContactFlowModuleBaseDataSourceConfig(rName, rName2 string) string { return fmt.Sprintf(` resource "aws_connect_instance" "test" { @@ -76,3 +107,13 @@ data "aws_connect_contact_flow_module" "test" { `) } +func testAccContactFlowModuleDataSourceConfig_Name(rName, rName2 string) string { + return acctest.ConfigCompose( + testAccContactFlowModuleBaseDataSourceConfig(rName, rName2), + ` +data "aws_connect_contact_flow_module" "test" { + instance_id = aws_connect_instance.test.id + name = aws_connect_contact_flow_module.test.name +} +`) +} From 33ab4eea4a268c33334137e6ea66b78690539072 Mon Sep 17 00:00:00 2001 From: GlennChia Date: Tue, 11 Jan 2022 12:16:07 +0800 Subject: [PATCH 6/9] docs(connect): data source for contact flow module --- .../connect_contact_flow_module.html.markdown | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 website/docs/d/connect_contact_flow_module.html.markdown diff --git a/website/docs/d/connect_contact_flow_module.html.markdown b/website/docs/d/connect_contact_flow_module.html.markdown new file mode 100644 index 000000000000..7cd00ed2a28d --- /dev/null +++ b/website/docs/d/connect_contact_flow_module.html.markdown @@ -0,0 +1,52 @@ +--- +subcategory: "Connect" +layout: "aws" +page_title: "AWS: aws_connect_contact_flow_module" +description: |- + Provides details about a specific Amazon Connect Contact Flow Module. +--- + +# Data Source: aws_connect_contact_flow_module + +Provides details about a specific Amazon Connect Contact Flow Module. + +## Example Usage + +By `name` + +```hcl +data "aws_connect_contact_flow_module" "example" { + instance_id = "aaaaaaaa-bbbb-cccc-dddd-111111111111" + name = "example" +} +``` + +By `contact_flow_module_id` + +```hcl +data "aws_connect_contact_flow_module" "example" { + instance_id = "aaaaaaaa-bbbb-cccc-dddd-111111111111" + contact_flow_module_id = "cccccccc-bbbb-cccc-dddd-111111111111" +} +``` + +## Argument Reference + +~> **NOTE:** `instance_id` and one of either `name` or `contact_flow_module_id` is required. + +The following arguments are supported: + +* `contact_flow_module_id` - (Optional) Returns information on a specific Contact Flow Module by contact flow module id +* `instance_id` - (Required) Reference to the hosting Amazon Connect Instance +* `name` - (Optional) Returns information on a specific Contact Flow Module by name + +## Attributes Reference + +In addition to all of the arguments above, the following attributes are exported: + +* `arn` - The Amazon Resource Name (ARN) of the Contact Flow Module. +* `content` - Specifies the logic of the Contact Flow Module. +* `description` - Specifies the description of the Contact Flow Module. +* `tags` - A map of tags to assign to the Contact Flow Module. +* `state` - Specifies the type of Contact Flow Module Module. Values are either `ACTIVE` or `ARCHIVED`. +* `status` - The status of the Contact Flow Module Module. This attribute is returned only if `contact_flow_module_id` is used in the data source. \ No newline at end of file From 6687b07d1968ef1876c29906c3fb412ab8d3fd3e Mon Sep 17 00:00:00 2001 From: GlennChia Date: Tue, 11 Jan 2022 12:25:41 +0800 Subject: [PATCH 7/9] ci(connect): changelog contact flow module datasrc --- .changelog/22518.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/22518.txt diff --git a/.changelog/22518.txt b/.changelog/22518.txt new file mode 100644 index 000000000000..c0d0639e7e83 --- /dev/null +++ b/.changelog/22518.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_connect_contact_flow_module +``` \ No newline at end of file From 138cc63ebde1cffb408be3016f5a7fc9658cc532 Mon Sep 17 00:00:00 2001 From: GlennChia Date: Tue, 11 Jan 2022 23:54:51 +0800 Subject: [PATCH 8/9] test(connect): check status even when name is used --- .../service/connect/contact_flow_module_data_source_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/internal/service/connect/contact_flow_module_data_source_test.go b/internal/service/connect/contact_flow_module_data_source_test.go index 72415e67b8cb..88359ea24283 100644 --- a/internal/service/connect/contact_flow_module_data_source_test.go +++ b/internal/service/connect/contact_flow_module_data_source_test.go @@ -31,8 +31,6 @@ func TestAccConnectContactFlowModuleDataSource_contactFlowModuleID(t *testing.T) resource.TestCheckResourceAttrPair(datasourceName, "description", resourceName, "description"), resource.TestCheckResourceAttrPair(datasourceName, "content", resourceName, "content"), resource.TestCheckResourceAttrSet(datasourceName, "state"), - // Using contact_flow_module_id returns the "status" attribute - // https://docs.aws.amazon.com/connect/latest/APIReference/API_DescribeContactFlowModule.html#API_DescribeContactFlowModule_ResponseSyntax resource.TestCheckResourceAttrSet(datasourceName, "status"), resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"), ), @@ -63,8 +61,7 @@ func TestAccConnectContactFlowModuleDataSource_name(t *testing.T) { resource.TestCheckResourceAttrPair(datasourceName, "description", resourceName, "description"), resource.TestCheckResourceAttrPair(datasourceName, "content", resourceName, "content"), resource.TestCheckResourceAttrSet(datasourceName, "state"), - // Using name does not return the "status" attribute - // https://docs.aws.amazon.com/connect/latest/APIReference/API_ListContactFlowModules.html#API_ListContactFlowModules_ResponseSyntax + resource.TestCheckResourceAttrSet(datasourceName, "status"), resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"), ), }, From d91f5286de8f7bbe869e58f57f206c23370fff6b Mon Sep 17 00:00:00 2001 From: GlennChia Date: Tue, 11 Jan 2022 23:55:33 +0800 Subject: [PATCH 9/9] docs(connect): status returned for both queries --- website/docs/d/connect_contact_flow_module.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/d/connect_contact_flow_module.html.markdown b/website/docs/d/connect_contact_flow_module.html.markdown index 7cd00ed2a28d..a35814d0d308 100644 --- a/website/docs/d/connect_contact_flow_module.html.markdown +++ b/website/docs/d/connect_contact_flow_module.html.markdown @@ -49,4 +49,4 @@ In addition to all of the arguments above, the following attributes are exported * `description` - Specifies the description of the Contact Flow Module. * `tags` - A map of tags to assign to the Contact Flow Module. * `state` - Specifies the type of Contact Flow Module Module. Values are either `ACTIVE` or `ARCHIVED`. -* `status` - The status of the Contact Flow Module Module. This attribute is returned only if `contact_flow_module_id` is used in the data source. \ No newline at end of file +* `status` - The status of the Contact Flow Module Module. Values are either `PUBLISHED` or `SAVED`. \ No newline at end of file