Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d/aws_connect_quick_connect #22527

Merged
merged 7 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/22527.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_connect_quick_connect
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ func Provider() *schema.Provider {
"aws_connect_hours_of_operation": connect.DataSourceHoursOfOperation(),
"aws_connect_instance": connect.DataSourceInstance(),
"aws_connect_lambda_function_association": connect.DataSourceLambdaFunctionAssociation(),
"aws_connect_quick_connect": connect.DataSourceQuickConnect(),

"aws_cur_report_definition": cur.DataSourceReportDefinition(),

Expand Down
3 changes: 3 additions & 0 deletions internal/service/connect/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const (
// ListLambdaFunctionsMaxResults Valid Range: Minimum value of 1. Maximum value of 25.
//https://docs.aws.amazon.com/connect/latest/APIReference/API_ListLambdaFunctions.html
ListLambdaFunctionsMaxResults = 25
// ListLambdaFunctionsMaxResults Valid Range: Minimum value of 1. Maximum value of 1000.
// https://docs.aws.amazon.com/connect/latest/APIReference/API_ListQuickConnects.html
ListQuickConnectsMaxResults = 60
)

func InstanceAttributeMapping() map[string]string {
Expand Down
195 changes: 195 additions & 0 deletions internal/service/connect/quick_connect_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
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-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

func DataSourceQuickConnect() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceQuickConnectRead,
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 100),
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"name", "quick_connect_id"},
},
"quick_connect_config": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"phone_config": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"phone_number": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"queue_config": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"contact_flow_id": {
Type: schema.TypeString,
Computed: true,
},
"queue_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"quick_connect_type": {
Type: schema.TypeString,
Computed: true,
},
"user_config": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"contact_flow_id": {
Type: schema.TypeString,
Computed: true,
},
"user_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
"quick_connect_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"quick_connect_id", "name"},
},
"tags": tftags.TagsSchemaComputed(),
},
}
}

func dataSourceQuickConnectRead(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.DescribeQuickConnectInput{
InstanceId: aws.String(instanceID),
}

if v, ok := d.GetOk("quick_connect_id"); ok {
input.QuickConnectId = aws.String(v.(string))
} else if v, ok := d.GetOk("name"); ok {
name := v.(string)
quickConnectSummary, err := dataSourceGetConnectQuickConnectSummaryByName(ctx, conn, instanceID, name)

if err != nil {
return diag.FromErr(fmt.Errorf("error finding Connect Quick Connect Summary by name (%s): %w", name, err))
}

if quickConnectSummary == nil {
return diag.FromErr(fmt.Errorf("error finding Connect Quick Connect Summary by name (%s): not found", name))
}

input.QuickConnectId = quickConnectSummary.Id
}

resp, err := conn.DescribeQuickConnectWithContext(ctx, input)

if err != nil {
return diag.FromErr(fmt.Errorf("error getting Connect Quick Connect: %w", err))
}

if resp == nil || resp.QuickConnect == nil {
return diag.FromErr(fmt.Errorf("error getting Connect Quick Connect: empty response"))
}

quickConnect := resp.QuickConnect

d.Set("arn", quickConnect.QuickConnectARN)
d.Set("description", quickConnect.Description)
d.Set("name", quickConnect.Name)
d.Set("quick_connect_id", quickConnect.QuickConnectId)

if err := d.Set("quick_connect_config", flattenQuickConnectConfig(quickConnect.QuickConnectConfig)); err != nil {
return diag.FromErr(fmt.Errorf("error setting quick_connect_config: %s", err))
}

if err := d.Set("tags", KeyValueTags(quickConnect.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(quickConnect.QuickConnectId)))

return nil
}

func dataSourceGetConnectQuickConnectSummaryByName(ctx context.Context, conn *connect.Connect, instanceID, name string) (*connect.QuickConnectSummary, error) {
var result *connect.QuickConnectSummary

input := &connect.ListQuickConnectsInput{
InstanceId: aws.String(instanceID),
MaxResults: aws.Int64(ListQuickConnectsMaxResults),
}

err := conn.ListQuickConnectsPagesWithContext(ctx, input, func(page *connect.ListQuickConnectsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, cf := range page.QuickConnectSummaryList {
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
}
123 changes: 123 additions & 0 deletions internal/service/connect/quick_connect_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
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 TestAccConnectQuickConnectDataSource_QuickConnectID(t *testing.T) {
rName := sdkacctest.RandomWithPrefix("resource-test-terraform")
resourceName := "aws_connect_quick_connect.test"
datasourceName := "data.aws_connect_quick_connect.test"
phoneNumber := "+12345678912"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, connect.EndpointsID),
Providers: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccQuickConnectDataSourceConfig_QuickConnectID(rName, resourceName, phoneNumber),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrPair(datasourceName, "instance_id", resourceName, "instance_id"),
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "quick_connect_config.#", resourceName, "quick_connect_config.#"),
resource.TestCheckResourceAttrPair(datasourceName, "quick_connect_config.0.quick_connect_type", resourceName, "quick_connect_config.0.quick_connect_type"),
resource.TestCheckResourceAttrPair(datasourceName, "quick_connect_config.0.phone_config.#", resourceName, "quick_connect_config.0.phone_config.#"),
resource.TestCheckResourceAttrPair(datasourceName, "quick_connect_config.0.phone_config.0.phone_number", resourceName, "quick_connect_config.0.phone_config.0.phone_number"),
resource.TestCheckResourceAttrPair(datasourceName, "quick_connect_id", resourceName, "quick_connect_id"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
),
},
},
})
}

func TestAccConnectQuickConnectDataSource_name(t *testing.T) {
rName := sdkacctest.RandomWithPrefix("resource-test-terraform")
rName2 := sdkacctest.RandomWithPrefix("resource-test-terraform")
resourceName := "aws_connect_quick_connect.test"
datasourceName := "data.aws_connect_quick_connect.test"
phoneNumber := "+12345678912"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, connect.EndpointsID),
Providers: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccQuickConnectDataSourceConfig_Name(rName, rName2, phoneNumber),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrPair(datasourceName, "instance_id", resourceName, "instance_id"),
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "quick_connect_config.#", resourceName, "quick_connect_config.#"),
resource.TestCheckResourceAttrPair(datasourceName, "quick_connect_config.0.quick_connect_type", resourceName, "quick_connect_config.0.quick_connect_type"),
resource.TestCheckResourceAttrPair(datasourceName, "quick_connect_config.0.phone_config.#", resourceName, "quick_connect_config.0.phone_config.#"),
resource.TestCheckResourceAttrPair(datasourceName, "quick_connect_config.0.phone_config.0.phone_number", resourceName, "quick_connect_config.0.phone_config.0.phone_number"),
resource.TestCheckResourceAttrPair(datasourceName, "quick_connect_id", resourceName, "quick_connect_id"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
),
},
},
})
}

func testAccQuickConnectBaseDataSourceConfig(rName, rName2, phoneNumber 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_quick_connect" "test" {
instance_id = aws_connect_instance.test.id
name = %[2]q
description = "Test Quick Connect Description"

quick_connect_config {
quick_connect_type = "PHONE_NUMBER"

phone_config {
phone_number = %[3]q
}
}

tags = {
"Name" = "Test Quick Connect"
}
}
`, rName, rName2, phoneNumber)
}

func testAccQuickConnectDataSourceConfig_QuickConnectID(rName, rName2, phoneNumber string) string {
return acctest.ConfigCompose(
testAccQuickConnectBaseDataSourceConfig(rName, rName2, phoneNumber),
`
data "aws_connect_quick_connect" "test" {
instance_id = aws_connect_instance.test.id
quick_connect_id = aws_connect_quick_connect.test.quick_connect_id
}
`)
}

func testAccQuickConnectDataSourceConfig_Name(rName, rName2, phoneNumber string) string {
return acctest.ConfigCompose(
testAccQuickConnectBaseDataSourceConfig(rName, rName2, phoneNumber),
`
data "aws_connect_quick_connect" "test" {
instance_id = aws_connect_instance.test.id
name = aws_connect_quick_connect.test.name
}
`)
}
Loading