Skip to content

Commit

Permalink
refactor id to take the 3 required fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharon Nam authored and Sharon Nam committed Nov 2, 2023
1 parent 2134dfe commit e1f2b24
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
35 changes: 32 additions & 3 deletions internal/service/lexv2models/bot_locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
fwflex "github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -79,6 +80,12 @@ func (r *resourceBotLocale) Schema(ctx context.Context, req resource.SchemaReque
stringplanmodifier.RequiresReplace(),
},
},
"id": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"n_lu_intent_confidence_threshold": schema.Float64Attribute{
Required: true,
},
Expand Down Expand Up @@ -121,6 +128,10 @@ func (r *resourceBotLocale) Schema(ctx context.Context, req resource.SchemaReque
}
}

const (
botLocaleIDPartCount = 3
)

func (r *resourceBotLocale) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
conn := r.Meta().LexV2ModelsClient(ctx)

Expand Down Expand Up @@ -167,10 +178,19 @@ func (r *resourceBotLocale) Create(ctx context.Context, req resource.CreateReque
return
}

idParts := []string{
aws.ToString(out.LocaleId),
aws.ToString(out.BotId),
aws.ToString(out.BotVersion),
}
id, err := fwflex.FlattenResourceId(idParts, botLocaleIDPartCount, false)

Check failure on line 186 in internal/service/lexv2models/bot_locale.go

View workflow job for this annotation

GitHub Actions / 2 of 2

SA4006: this value of `err` is never used (staticcheck)

plan.LocaleID = flex.StringToFramework(ctx, out.LocaleId)
plan.Id = types.StringValue(id)
state := plan
state.LocaleID = flex.StringToFramework(ctx, out.LocaleId)
state.BotID = flex.StringToFramework(ctx, out.BotId)
state.Id = types.StringValue(id)
state.Description = flex.StringToFramework(ctx, out.Description)

vs, _ := flattenVoiceSettings(ctx, out.VoiceSettings)
Expand Down Expand Up @@ -203,7 +223,7 @@ func (r *resourceBotLocale) Read(ctx context.Context, req resource.ReadRequest,
return
}

out, err := FindBotLocaleByID(ctx, conn, state.LocaleID.ValueString())
out, err := FindBotLocaleByID(ctx, conn, state.Id.ValueString())
if tfresource.NotFound(err) {
resp.State.RemoveResource(ctx)
return
Expand Down Expand Up @@ -309,7 +329,9 @@ func (r *resourceBotLocale) Delete(ctx context.Context, req resource.DeleteReque
}

in := &lexmodelsv2.DeleteBotLocaleInput{
LocaleId: aws.String(state.LocaleID.ValueString()),
LocaleId: aws.String(state.LocaleID.ValueString()),
BotId: aws.String(state.BotID.ValueString()),
BotVersion: aws.String(state.BotVersion.ValueString()),
}

_, err := conn.DeleteBotLocale(ctx, in)
Expand Down Expand Up @@ -408,8 +430,14 @@ func statusBotLocale(ctx context.Context, conn *lexmodelsv2.Client, id string) r
}

func FindBotLocaleByID(ctx context.Context, conn *lexmodelsv2.Client, id string) (*lexmodelsv2.DescribeBotLocaleOutput, error) {
parts, err := fwflex.ExpandResourceId(id, botLocaleIDPartCount, false)
if err != nil {
return nil, err
}
in := &lexmodelsv2.DescribeBotLocaleInput{
LocaleId: aws.String(id),
LocaleId: aws.String(parts[0]),
BotId: aws.String(parts[1]),
BotVersion: aws.String(parts[2]),
}

out, err := conn.DescribeBotLocale(ctx, in)
Expand Down Expand Up @@ -473,6 +501,7 @@ type resourceBotLocaleData struct {
VoiceSettings types.List `tfsdk:"voice_settings"`
Description types.String `tfsdk:"description"`
NluIntentCOnfidenceThreshold types.Float64 `tfsdk:"n_lu_intent_confidence_threshold"`
Id types.String `tfsdk:"id"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
}

Expand Down
19 changes: 15 additions & 4 deletions internal/service/lexv2models/bot_locale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lexmodelsv2"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -43,7 +44,7 @@ func TestAccLexV2ModelsBotLocale_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckBotLocaleExists(ctx, resourceName, &botlocale),
resource.TestCheckResourceAttrSet(resourceName, "bot_id"),
resource.TestCheckResourceAttr(resourceName, "locale_id", "en_US"),
resource.TestCheckResourceAttrSet(resourceName, "locale_id"),
resource.TestCheckResourceAttrSet(resourceName, "bot_version"),
resource.TestCheckResourceAttr(resourceName, "n_lu_intent_confidence_threshold", "0.70"),
),
Expand Down Expand Up @@ -98,7 +99,12 @@ func testAccCheckBotLocaleDestroy(ctx context.Context) resource.TestCheckFunc {
continue
}

_, err := tflexv2models.FindBotLocaleByID(ctx, conn, rs.Primary.ID)
input := &lexmodelsv2.DescribeBotLocaleInput{
LocaleId: aws.String(rs.Primary.Attributes["locale_id"]),
BotId: aws.String(rs.Primary.Attributes["bot_id"]),
BotVersion: aws.String(rs.Primary.Attributes["bot_version"]),
}
_, err := conn.DescribeBotLocale(ctx, input)
if tfresource.NotFound(err) {
continue
}
Expand Down Expand Up @@ -126,7 +132,12 @@ func testAccCheckBotLocaleExists(ctx context.Context, name string, botlocale *le
}

conn := acctest.Provider.Meta().(*conns.AWSClient).LexV2ModelsClient(ctx)
resp, err := tflexv2models.FindBotLocaleByID(ctx, conn, rs.Primary.ID)
resp, err := conn.DescribeBotLocale(ctx, &lexmodelsv2.DescribeBotLocaleInput{
LocaleId: aws.String(rs.Primary.Attributes["locale_id"]),
BotId: aws.String(rs.Primary.Attributes["bot_id"]),
BotVersion: aws.String(rs.Primary.Attributes["bot_version"]),
})
// resp, err := tflexv2models.FindBotLocaleByID(ctx, conn, rs.Primary.ID)
if err != nil {
return create.Error(names.AuditManager, create.ErrActionCheckingExistence, tflexv2models.ResNameBotLocale, rs.Primary.ID, err)
}
Expand All @@ -152,9 +163,9 @@ resource "aws_lexv2models_bot" "test" {
}
resource "aws_lexv2models_bot_locale" "testlocale" {
locale_id = %[2]q
bot_id = aws_lexv2models_bot.test.id
bot_version = "DRAFT"
locale_id = %[2]q
n_lu_intent_confidence_threshold = %[3]g
}
`, rName, localeid, thres))
Expand Down

0 comments on commit e1f2b24

Please sign in to comment.