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

r/aws_connect_bot_association - add new resource #21097

Merged
merged 42 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
cb3834f
feat: migrate old resources
thornleyk Sep 24, 2021
22b0d93
feat: add data source
thornleyk Sep 25, 2021
7d67769
Moving CONST to waiter.
AdamTylerLynch Sep 25, 2021
c20a22e
Moving utility function below CRUD
AdamTylerLynch Sep 25, 2021
badded9
Migrated LexAssociationByName to finder.
AdamTylerLynch Sep 25, 2021
2e52045
cleaning up formatting
AdamTylerLynch Sep 25, 2021
d653270
migrating AccTests to use finder
AdamTylerLynch Sep 25, 2021
e750f10
adjusting AccTest format to pass linters.
AdamTylerLynch Sep 25, 2021
3ed8658
updating Region as required, updated AccTest
AdamTylerLynch Sep 25, 2021
411e81f
updated AccTest with region
AdamTylerLynch Sep 25, 2021
e3fc2e8
Implement finder and add region to the DataSource test.
AdamTylerLynch Sep 26, 2021
be4ef06
updated Lex Bot docs and req field formats.
AdamTylerLynch Sep 26, 2021
c929a97
added exteded sample into the docs.
AdamTylerLynch Sep 26, 2021
cdc55bf
refactor attribute names to match API input.
AdamTylerLynch Sep 26, 2021
2bdee0d
fixing AccTests
AdamTylerLynch Sep 26, 2021
0ff052d
fixing AccTests
AdamTylerLynch Sep 26, 2021
a397a79
fix: add retry on lex building state response
thornleyk Sep 27, 2021
7b4d803
chore: refactor connect bot association
thornleyk Sep 29, 2021
b7e3dac
chore: merge upstream
thornleyk Sep 30, 2021
f9f69b1
chore: merge provider
thornleyk Sep 30, 2021
fa1ac5e
Merge branch 'main' of https://github.com/hashicorp/terraform-provide…
thornleyk Sep 30, 2021
480cd26
Merge branch 'hashicorp-main' into f-aws_connect_bot_association
thornleyk Sep 30, 2021
6367e45
chore: revert change
thornleyk Sep 30, 2021
dfdea2a
chore: revert change
thornleyk Sep 30, 2021
8d90a00
chore: revert
thornleyk Sep 30, 2021
b3e64a4
chore: fix markdown wiki tf format
thornleyk Sep 30, 2021
e165fd1
chore: additional docs fix
thornleyk Sep 30, 2021
ccb2116
chore: remove interpolation resources in wiki
thornleyk Sep 30, 2021
d81586d
chore: additional documentation
thornleyk Sep 30, 2021
ab860f7
Fixing logic to evaluate ResourceNotFound
AdamTylerLynch Sep 30, 2021
8f830d2
Merge branch 'main' of https://github.com/abebars/terraform-provider-…
abebars Oct 29, 2021
695b403
Add BotAssociation timeouts
abebars Oct 29, 2021
da43562
refactor Resource ID
abebars Oct 29, 2021
de51f60
move functions to find
abebars Oct 29, 2021
a89515d
apply new code style after provider changes
abebars Oct 29, 2021
db71b62
add r/d to provider
abebars Oct 29, 2021
dd3b878
matching function names in the refactor
AdamTylerLynch Nov 6, 2021
a6c0124
Update CHANGELOG for #21097
anGie44 Dec 9, 2021
b807196
CR updates
anGie44 Dec 9, 2021
f3d2b8e
tfproviderdocks lint fix
anGie44 Dec 9, 2021
c3f85f2
remove constants provider aws go SDK
anGie44 Dec 9, 2021
3c27bab
update list indexing in test
anGie44 Dec 9, 2021
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
7 changes: 7 additions & 0 deletions .changelog/21097.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-data-source
aws_connect_bot_association
```

```release-note:new-resource
aws_connect_bot_association
```
10 changes: 6 additions & 4 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,9 @@ func Provider() *schema.Provider {

"aws_cognito_user_pools": cognitoidp.DataSourceUserPools(),

"aws_connect_contact_flow": connect.DataSourceContactFlow(),
"aws_connect_instance": connect.DataSourceInstance(),
"aws_connect_bot_association": connect.DataSourceBotAssociation(),
"aws_connect_contact_flow": connect.DataSourceContactFlow(),
"aws_connect_instance": connect.DataSourceInstance(),

"aws_cur_report_definition": cur.DataSourceReportDefinition(),

Expand Down Expand Up @@ -926,8 +927,9 @@ func Provider() *schema.Provider {
"aws_config_organization_managed_rule": config.ResourceOrganizationManagedRule(),
"aws_config_remediation_configuration": config.ResourceRemediationConfiguration(),

"aws_connect_contact_flow": connect.ResourceContactFlow(),
"aws_connect_instance": connect.ResourceInstance(),
"aws_connect_bot_association": connect.ResourceBotAssociation(),
"aws_connect_contact_flow": connect.ResourceContactFlow(),
"aws_connect_instance": connect.ResourceInstance(),

"aws_cur_report_definition": cur.ResourceReportDefinition(),

Expand Down
222 changes: 222 additions & 0 deletions internal/service/connect/bot_association.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
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-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func ResourceBotAssociation() *schema.Resource {
return &schema.Resource{
CreateContext: resourceBotAssociationCreate,
ReadContext: resourceBotAssociationRead,
DeleteContext: resourceBotAssociationDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"lex_bot": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"lex_region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(2, 50),
},
},
},
},
/* We would need a schema like this to support a v1/v2 hybrid
"lex_v2_bot": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"alias_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidARN,
ForceNew: true,
},
},
},
},
*/
},
}
}

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

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

input := &connect.AssociateBotInput{
InstanceId: aws.String(instanceId),
}

if v, ok := d.GetOk("lex_bot"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
lexBot := expandLexBot(v.([]interface{}))
if lexBot.LexRegion == nil {
lexBot.LexRegion = aws.String(meta.(*conns.AWSClient).Region)
}
input.LexBot = lexBot
}

/* We would need something like this and additionally the opposite on the above
if v, ok := d.GetOk("lex_v2_bot"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.LexV2Bot = expandLexV2Bot(v.([]interface{}))
}
*/

_, err := tfresource.RetryWhen(
connectBotAssociationCreateTimeout,
func() (interface{}, error) {
return conn.AssociateBotWithContext(ctx, input)
},
func(err error) (bool, error) {
if tfawserr.ErrCodeEquals(err, connect.ErrCodeInvalidRequestException) {
return true, err
}

return false, err
},
)

lbaId := BotV1AssociationCreateResourceID(instanceId, aws.StringValue(input.LexBot.Name), aws.StringValue(input.LexBot.LexRegion))

if err != nil {
return diag.FromErr(fmt.Errorf("error creating Connect Bot Association (%s): %w", lbaId, err))
}

d.SetId(lbaId)

return resourceBotAssociationRead(ctx, d, meta)
}

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

instanceId, name, region, err := BotV1AssociationParseResourceID(d.Id())

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

lexBot, err := FindBotAssociationV1ByNameAndRegionWithContext(ctx, conn, instanceId, name, region)

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

if err != nil {
return diag.FromErr(fmt.Errorf("error reading Connect Bot Association (%s): %w", d.Id(), err))
}

if lexBot == nil {
return diag.FromErr(fmt.Errorf("error reading Connect Bot Association (%s): empty output", d.Id()))
}

d.Set("instance_id", instanceId)
if err := d.Set("lex_bot", flattenLexBot(lexBot)); err != nil {
return diag.FromErr(fmt.Errorf("error setting lex_bot: %w", err))
}

return nil
}

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

instanceID, name, region, err := BotV1AssociationParseResourceID(d.Id())

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

lexBot := &connect.LexBot{
Name: aws.String(name),
LexRegion: aws.String(region),
}

input := &connect.DisassociateBotInput{
InstanceId: aws.String(instanceID),
LexBot: lexBot,
}

_, err = conn.DisassociateBotWithContext(ctx, input)

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

if err != nil {
return diag.FromErr(fmt.Errorf("error deleting Connect Bot Association (%s): %w", d.Id(), err))
}

return nil
}

func expandLexBot(l []interface{}) *connect.LexBot {
if len(l) == 0 || l[0] == nil {
return nil
}

tfMap, ok := l[0].(map[string]interface{})
if !ok {
return nil
}

result := &connect.LexBot{
Name: aws.String(tfMap["name"].(string)),
}

if v, ok := tfMap["lex_region"].(string); ok && v != "" {
result.LexRegion = aws.String(v)
}

return result
}

func flattenLexBot(bot *connect.LexBot) []interface{} {
if bot == nil {
return []interface{}{}
}

m := map[string]interface{}{
"lex_region": bot.LexRegion,
"name": bot.Name,
}

return []interface{}{m}
}
74 changes: 74 additions & 0 deletions internal/service/connect/bot_association_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package connect

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"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"
)

func DataSourceBotAssociation() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBotAssociationRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
},
"lex_bot": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"lex_region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(2, 50),
},
},
},
},
},
}
}

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

instanceID := d.Get("instance_id").(string)

var name, region string
if v, ok := d.GetOk("lex_bot"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
lexBot := expandLexBot(v.([]interface{}))
name = aws.StringValue(lexBot.Name)
region = aws.StringValue(lexBot.LexRegion)
}

lexBot, err := FindBotAssociationV1ByNameAndRegionWithContext(ctx, conn, instanceID, name, region)
if err != nil {
return diag.FromErr(fmt.Errorf("error finding Connect Bot Association (%s,%s): %w", instanceID, name, err))
}

if lexBot == nil {
return diag.FromErr(fmt.Errorf("error finding Connect Bot Association (%s,%s) : not found", instanceID, name))
}

d.SetId(meta.(*conns.AWSClient).Region)

d.Set("instance_id", instanceID)
if err := d.Set("lex_bot", flattenLexBot(lexBot)); err != nil {
return diag.FromErr(fmt.Errorf("error setting lex_bot: %w", err))
}

return nil
}
Loading