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_lex_bot, r/aws_lex_intent and r/aws_lex_slot_type: Fix computed behaviour for versions #20336

Merged
merged 5 commits into from
Jul 29, 2021
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
11 changes: 11 additions & 0 deletions .changelog/20336.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:bug
aws/resource_aws_lex_bot: Fix computed `version` for dependent resources
```

```release-note:bug
aws/resource_aws_lex_intent: Fix computed `version` for dependent resources
```

```release-note:bug
aws/resource_aws_lex_slot_type: Fix computed `version` for dependent resources
```
33 changes: 33 additions & 0 deletions aws/resource_aws_lex_bot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"context"
"fmt"
"log"
"regexp"
Expand Down Expand Up @@ -177,9 +178,41 @@ func resourceAwsLexBot() *schema.Resource {
Computed: true,
},
},
CustomizeDiff: updateComputedAttributesOnBotCreateVersion,
}
}

func updateComputedAttributesOnBotCreateVersion(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
createVersion := d.Get("create_version").(bool)
if createVersion && hasBotConfigChanges(d) {
d.SetNewComputed("version")
}
return nil
}

func hasBotConfigChanges(d resourceDiffer) bool {
for _, key := range []string{
"description",
"child_directed",
"detect_sentiment",
"enable_model_improvements",
"idle_session_ttl_in_seconds",
"intent",
"locale",
"nlu_intent_confidence_threshold",
"abort_statement.0.response_card",
"abort_statement.0.message",
"clarification_prompt",
"process_behavior",
"voice_id",
} {
if d.HasChange(key) {
return true
}
}
return false
}

var validateLexBotName = validation.All(
validation.StringLenBetween(2, 50),
validation.StringMatch(regexp.MustCompile(`^([A-Za-z]_?)+$`), ""),
Expand Down
169 changes: 139 additions & 30 deletions aws/resource_aws_lex_bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,63 @@ func TestAccAwsLexBot_intents(t *testing.T) {
})
}

func TestAccAwsLexBot_computeVersion(t *testing.T) {
var v1 lexmodelbuildingservice.GetBotOutput
var v2 lexmodelbuildingservice.GetBotAliasOutput

botResourceName := "aws_lex_bot.test"
botAliasResourceName := "aws_lex_bot_alias.test"
intentResourceName := "aws_lex_intent.test"
intentResourceName2 := "aws_lex_intent.test_2"

testBotID := "test_bot_" + acctest.RandStringFromCharSet(8, acctest.CharSetAlpha)

version := "1"
updatedVersion := "2"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(lexmodelbuildingservice.EndpointsID, t) },
ErrorCheck: testAccErrorCheck(t, lexmodelbuildingservice.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsLexBotDestroy,
Steps: []resource.TestStep{
{
Config: composeConfig(
testAccAwsLexBotConfig_createVersion(testBotID),
testAccAwsLexBotConfig_intentMultiple(testBotID),
testAccAwsLexBotAliasConfig_basic(testBotID),
),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAwsLexBotExistsWithVersion(botResourceName, version, &v1),
resource.TestCheckResourceAttr(botResourceName, "version", version),
resource.TestCheckResourceAttr(botResourceName, "intent.#", "1"),
resource.TestCheckResourceAttr(botResourceName, "intent.0.intent_version", version),
testAccCheckAwsLexBotAliasExists(botAliasResourceName, &v2),
resource.TestCheckResourceAttr(botAliasResourceName, "bot_version", version),
resource.TestCheckResourceAttr(intentResourceName, "version", version),
),
},
{
Config: composeConfig(
testAccAwsLexBotConfig_intentMultipleSecondUpdated(testBotID),
testAccAwsLexBotConfig_multipleIntentsWithVersion(testBotID),
testAccAwsLexBotAliasConfig_basic(testBotID),
),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAwsLexBotExistsWithVersion(botResourceName, updatedVersion, &v1),
resource.TestCheckResourceAttr(botResourceName, "version", updatedVersion),
resource.TestCheckResourceAttr(botResourceName, "intent.#", "2"),
resource.TestCheckResourceAttr(botResourceName, "intent.0.intent_version", version),
resource.TestCheckResourceAttr(botResourceName, "intent.1.intent_version", updatedVersion),
resource.TestCheckResourceAttr(botAliasResourceName, "bot_version", updatedVersion),
resource.TestCheckResourceAttr(intentResourceName, "version", version),
resource.TestCheckResourceAttr(intentResourceName2, "version", updatedVersion),
),
},
},
})
}

func TestAccAwsLexBot_locale(t *testing.T) {
var v lexmodelbuildingservice.GetBotOutput
rName := "aws_lex_bot.test"
Expand Down Expand Up @@ -753,8 +810,8 @@ func testAccCheckAwsLexBotDestroy(s *terraform.State) error {
func testAccAwsLexBotConfig_intent(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_intent" "test" {
name = "%s"
create_version = true
name = "%s"
fulfillment_activity {
type = "ReturnIntent"
}
Expand All @@ -768,8 +825,8 @@ resource "aws_lex_intent" "test" {
func testAccAwsLexBotConfig_intentMultiple(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_intent" "test" {
name = "%[1]s"
create_version = true
name = "%[1]s"
fulfillment_activity {
type = "ReturnIntent"
}
Expand All @@ -779,24 +836,50 @@ resource "aws_lex_intent" "test" {
}

resource "aws_lex_intent" "test_2" {
create_version = true
name = "%[1]stwo"
fulfillment_activity {
type = "ReturnIntent"
}
sample_utterances = [
"I would like to pick up flowers",
]
}
`, rName)
}

func testAccAwsLexBotConfig_intentMultipleSecondUpdated(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_intent" "test" {
create_version = true
name = "%[1]s"
fulfillment_activity {
type = "ReturnIntent"
}
sample_utterances = [
"I would like to pick up flowers",
]
}

resource "aws_lex_intent" "test_2" {
create_version = true
name = "%[1]stwo"
fulfillment_activity {
type = "ReturnIntent"
}
sample_utterances = [
"I would like to return these flowers",
]
}
`, rName)
}

func testAccAwsLexBotConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers on the behalf of a user"
child_directed = false
description = "Bot to order flowers on the behalf of a user"
name = "%s"
abort_statement {
message {
content = "Sorry, I'm not able to assist at this time"
Expand All @@ -814,10 +897,10 @@ resource "aws_lex_bot" "test" {
func testAccAwsLexBotConfig_createVersion(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers on the behalf of a user"
child_directed = false
create_version = true
description = "Bot to order flowers on the behalf of a user"
name = "%s"
process_behavior = "BUILD"
abort_statement {
message {
Expand All @@ -836,9 +919,9 @@ resource "aws_lex_bot" "test" {
func testAccAwsLexBotConfig_abortStatement(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers on the behalf of a user"
child_directed = false
description = "Bot to order flowers on the behalf of a user"
name = "%s"
abort_statement {
message {
content = "Sorry, I'm not able to assist at this time"
Expand All @@ -856,9 +939,9 @@ resource "aws_lex_bot" "test" {
func testAccAwsLexBotConfig_abortStatementUpdate(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers on the behalf of a user"
child_directed = false
description = "Bot to order flowers on the behalf of a user"
name = "%s"
abort_statement {
message {
content = "Sorry, I'm not able to assist at this time"
Expand All @@ -883,9 +966,9 @@ resource "aws_lex_bot" "test" {
func testAccAwsLexBotConfig_clarificationPrompt(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers on the behalf of a user"
child_directed = false
description = "Bot to order flowers on the behalf of a user"
name = "%s"
abort_statement {
message {
content = "Sorry, I'm not able to assist at this time"
Expand All @@ -910,9 +993,9 @@ resource "aws_lex_bot" "test" {
func testAccAwsLexBotConfig_clarificationPromptUpdate(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers on the behalf of a user"
child_directed = false
description = "Bot to order flowers on the behalf of a user"
name = "%s"
abort_statement {
message {
content = "Sorry, I'm not able to assist at this time"
Expand Down Expand Up @@ -944,9 +1027,9 @@ resource "aws_lex_bot" "test" {
func testAccAwsLexBotConfig_childDirectedUpdate(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers on the behalf of a user"
child_directed = true
description = "Bot to order flowers on the behalf of a user"
name = "%s"
abort_statement {
message {
content = "Sorry, I'm not able to assist at this time"
Expand All @@ -964,9 +1047,9 @@ resource "aws_lex_bot" "test" {
func testAccAwsLexBotConfig_descriptionUpdate(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers"
child_directed = false
description = "Bot to order flowers"
name = "%s"
abort_statement {
message {
content = "Sorry, I'm not able to assist at this time"
Expand All @@ -984,10 +1067,10 @@ resource "aws_lex_bot" "test" {
func testAccAwsLexBotConfig_detectSentimentUpdate(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers on the behalf of a user"
child_directed = false
description = "Bot to order flowers on the behalf of a user"
detect_sentiment = true
name = "%s"
abort_statement {
message {
content = "Sorry, I'm not able to assist at this time"
Expand All @@ -1005,10 +1088,10 @@ resource "aws_lex_bot" "test" {
func testAccAwsLexBotConfig_enableModelImprovementsUpdate(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers on the behalf of a user"
child_directed = false
description = "Bot to order flowers on the behalf of a user"
enable_model_improvements = true
name = "%s"
nlu_intent_confidence_threshold = 0.5
abort_statement {
message {
Expand All @@ -1027,10 +1110,10 @@ resource "aws_lex_bot" "test" {
func testAccAwsLexBotConfig_idleSessionTtlInSecondsUpdate(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers on the behalf of a user"
child_directed = false
description = "Bot to order flowers on the behalf of a user"
idle_session_ttl_in_seconds = 600
name = "%s"
abort_statement {
message {
content = "Sorry, I'm not able to assist at this time"
Expand All @@ -1048,9 +1131,9 @@ resource "aws_lex_bot" "test" {
func testAccAwsLexBotConfig_intentsUpdate(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers on the behalf of a user"
child_directed = false
description = "Bot to order flowers on the behalf of a user"
name = "%s"
abort_statement {
message {
content = "Sorry, I'm not able to assist at this time"
Expand All @@ -1072,11 +1155,11 @@ resource "aws_lex_bot" "test" {
func testAccAwsLexBotConfig_localeUpdate(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers on the behalf of a user"
child_directed = false
description = "Bot to order flowers on the behalf of a user"
enable_model_improvements = true
locale = "en-GB"
name = "%s"
abort_statement {
message {
content = "Sorry, I'm not able to assist at this time"
Expand All @@ -1094,9 +1177,9 @@ resource "aws_lex_bot" "test" {
func testAccAwsLexBotConfig_voiceIdUpdate(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
name = "%s"
description = "Bot to order flowers on the behalf of a user"
child_directed = false
description = "Bot to order flowers on the behalf of a user"
name = "%s"
voice_id = "Justin"
abort_statement {
message {
Expand All @@ -1111,3 +1194,29 @@ resource "aws_lex_bot" "test" {
}
`, rName)
}

func testAccAwsLexBotConfig_multipleIntentsWithVersion(rName string) string {
return fmt.Sprintf(`
resource "aws_lex_bot" "test" {
child_directed = false
create_version = true
description = "Bot to order flowers on the behalf of a user"
name = "%s"
process_behavior = "BUILD"
abort_statement {
message {
content = "Sorry, I'm not able to assist at this time"
content_type = "PlainText"
}
}
intent {
intent_name = aws_lex_intent.test.name
intent_version = aws_lex_intent.test.version
}
intent {
intent_name = aws_lex_intent.test_2.name
intent_version = aws_lex_intent.test_2.version
}
}
`, rName)
}
Loading