Skip to content

Commit

Permalink
use partition datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharon Nam authored and Sharon Nam committed Sep 27, 2023
1 parent decbb65 commit 02472af
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 50 deletions.
62 changes: 29 additions & 33 deletions internal/service/lexv2models/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ func (r *resourceBot) Create(ctx context.Context, req resource.CreateRequest, re
return
}

dpInput, d := expandDataPrivacy(ctx, dp)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
dpInput := expandDataPrivacy(ctx, dp)

in := lexmodelsv2.CreateBotInput{
BotName: aws.String(plan.Name.ValueString()),
Expand All @@ -178,6 +174,12 @@ func (r *resourceBot) Create(ctx context.Context, req resource.CreateRequest, re
in.Description = aws.String(plan.Description.ValueString())
}

var bm []membersData
if !plan.Members.IsNull() {
bmInput := expandMembers(ctx, bm)
in.BotMembers = bmInput
}

out, err := conn.CreateBot(ctx, &in)
if err != nil {
resp.Diagnostics.AddError(
Expand Down Expand Up @@ -255,7 +257,7 @@ func (r *resourceBot) Read(ctx context.Context, req resource.ReadRequest, resp *
state.Description = flex.StringToFramework(ctx, out.Description)
state.IdleSessionTTLInSeconds = flex.Int32ToFramework(ctx, out.IdleSessionTTLInSeconds)

datap, _ := flattenDataPrivacy(ctx, out.DataPrivacy)
datap, _ := flattenDataPrivacy(out.DataPrivacy)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -280,18 +282,13 @@ func (r *resourceBot) Update(ctx context.Context, req resource.UpdateRequest, re
!plan.TestBotAliasTags.Equal(state.TestBotAliasTags) ||
!plan.DataPrivacy.Equal(state.DataPrivacy) ||
!plan.Type.Equal(state.Type) {

var dp []dataPrivacyData
resp.Diagnostics.Append(plan.DataPrivacy.ElementsAs(ctx, &dp, false)...)
if resp.Diagnostics.HasError() {
return
}

dpInput, d := expandDataPrivacy(ctx, dp)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
dpInput := expandDataPrivacy(ctx, dp)

in := lexmodelsv2.UpdateBotInput{
BotId: flex.StringFromFramework(ctx, plan.ID),
Expand Down Expand Up @@ -481,9 +478,7 @@ func FindBotByID(ctx context.Context, conn *lexmodelsv2.Client, id string) (*lex
return out, nil
}

func flattenDataPrivacy(ctx context.Context, apiObject *awstypes.DataPrivacy) (types.List, diag.Diagnostics) {
// attributeTypes := flex.AttributeTypesMust[dataPrivacyData](ctx)

func flattenDataPrivacy(apiObject *awstypes.DataPrivacy) (types.List, diag.Diagnostics) {
var diags diag.Diagnostics
elemType := types.ObjectType{AttrTypes: dataPrivacyAttrTypes}

Expand All @@ -503,35 +498,37 @@ func flattenDataPrivacy(ctx context.Context, apiObject *awstypes.DataPrivacy) (t
return listVal, diags
}

func expandDataPrivacy(ctx context.Context, tfList []dataPrivacyData) (*awstypes.DataPrivacy, diag.Diagnostics) {
var diags diag.Diagnostics
func expandDataPrivacy(ctx context.Context, tfList []dataPrivacyData) *awstypes.DataPrivacy {
if len(tfList) == 0 {
return nil, diags
return nil
}

dp := tfList[0]
cdBool := flex.BoolFromFramework(ctx, dp.ChildDirected)

return &awstypes.DataPrivacy{
ChildDirected: aws.ToBool(cdBool),
}, diags
}
}

func expandMembers(tfList []membersData) (*awstypes.BotMember, diag.Diagnostics) {
var diags diag.Diagnostics

func expandMembers(ctx context.Context, tfList []membersData) []awstypes.BotMember {
if len(tfList) == 0 {
return nil, diags
return nil
}
var mb []awstypes.BotMember

for _, item := range tfList {
new := awstypes.BotMember{
BotMemberAliasId: flex.StringFromFramework(ctx, item.AliasID),
BotMemberAliasName: flex.StringFromFramework(ctx, item.AliasName),
BotMemberId: flex.StringFromFramework(ctx, item.ID),
BotMemberName: flex.StringFromFramework(ctx, item.Name),
BotMemberVersion: flex.StringFromFramework(ctx, item.Version),
}
mb = append(mb, new)
}

mb := tfList[0]
return &awstypes.BotMember{
BotMemberAliasId: aws.String(mb.AliasID.ValueString()),
BotMemberAliasName: aws.String(mb.AliasName.ValueString()),
BotMemberId: aws.String(mb.ID.ValueString()),
BotMemberName: aws.String(mb.Name.ValueString()),
BotMemberVersion: aws.String(mb.Version.ValueString()),
}, diags
return mb
}

func (rd *resourceBotData) refreshFromOutput(ctx context.Context, out *lexmodelsv2.DescribeBotOutput) diag.Diagnostics {
Expand All @@ -547,8 +544,7 @@ func (rd *resourceBotData) refreshFromOutput(ctx context.Context, out *lexmodels
rd.Description = flex.StringToFramework(ctx, out.Description)
rd.IdleSessionTTLInSeconds = flex.Int32ToFramework(ctx, out.IdleSessionTTLInSeconds)

// TIP: Setting a complex type.
datap, d := flattenDataPrivacy(ctx, out.DataPrivacy)
datap, d := flattenDataPrivacy(out.DataPrivacy)
diags.Append(d...)
rd.DataPrivacy = datap

Expand Down
9 changes: 3 additions & 6 deletions internal/service/lexv2models/bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ import (

func TestAccLexV2ModelsBot_basic(t *testing.T) {
ctx := acctest.Context(t)
// TIP: This is a long-running test guard for tests that run longer than
// 300s (5 min) generally.
// if testing.Short() {
// t.Skip("skipping long-running test in short mode")
// }

var bot lexmodelsv2.DescribeBotOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -209,6 +204,8 @@ func testAccPreCheck(ctx context.Context, t *testing.T) {

func testAccBotBaseConfig(rName string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}
resource "aws_iam_role" "test_role" {
name = %[1]q
assume_role_policy = jsonencode({
Expand All @@ -228,7 +225,7 @@ resource "aws_iam_role" "test_role" {
resource "aws_iam_role_policy_attachment" "test-attach" {
role = aws_iam_role.test_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonLexFullAccess"
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AmazonLexFullAccess"
}
`, rName)
}
Expand Down
22 changes: 11 additions & 11 deletions website/docs/r/lexv2models_bot.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ resource "aws_lexv2models_bot" "example" {

The following arguments are required:

* `name` - The name of the bot. The bot name must be unique in the account that creates the bot. Type String. Length Constraints: Minimum length of 1. Maximum length of 100.
* `name` - Name of the bot. The bot name must be unique in the account that creates the bot. Type String. Length Constraints: Minimum length of 1. Maximum length of 100.
* `data_privacy` - Provides information on additional privacy protections Amazon Lex should use with the bot's data. See [`data_privacy`](#data-privacy)
* `idle_session_ttl_in_seconds` - The time, in seconds, that Amazon Lex should keep information about a user's conversation with the bot. You can specify between 60 (1 minute) and 86,400 (24 hours) seconds.
* `role_arn` - The Amazon Resource Name (ARN) of an IAM role that has permission to access the bot.
* `idle_session_ttl_in_seconds` - Time, in seconds, that Amazon Lex should keep information about a user's conversation with the bot. You can specify between 60 (1 minute) and 86,400 (24 hours) seconds.
* `role_arn` - ARN of an IAM role that has permission to access the bot.

The following arguments are optional:

* `members` - List of bot members in a network to be created. See [`bot_members`](#bot-members).
* `bot_tags` - A list of tags to add to the bot. You can only add tags when you create a bot.
* `bot_type` - The type of a bot to create.
* `description` - A description of the bot. It appears in lists to help you identify a particular bot.
* `test_bot_alias_tags` - A list of tags to add to the test alias for a bot. You can only add tags when you create a bot.
* `bot_tags` - List of tags to add to the bot. You can only add tags when you create a bot.
* `bot_type` - Type of a bot to create.
* `description` - Description of the bot. It appears in lists to help you identify a particular bot.
* `test_bot_alias_tags` - List of tags to add to the test alias for a bot. You can only add tags when you create a bot.

## Attribute Reference

This resource exports the following attributes in addition to the arguments above:

* `id` - A unique identifier for a particular bot.
* `id` - Unique identifier for a particular bot.

### Data Privacy

Expand All @@ -64,9 +64,9 @@ This resource exports the following attributes in addition to the arguments abov

[Configuration options](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts):

* `create` - (Default `60m`)
* `update` - (Default `180m`)
* `delete` - (Default `90m`)
* `create` - (Default `30m`)
* `update` - (Default `30m`)
* `delete` - (Default `30m`)

## Import

Expand Down

0 comments on commit 02472af

Please sign in to comment.