Skip to content

Commit

Permalink
Merge branch 'add_aws_lex_bot_alias', pull request #8919
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison committed Oct 5, 2020
2 parents 54d2a97 + 2dba226 commit 815f395
Show file tree
Hide file tree
Showing 15 changed files with 1,339 additions and 73 deletions.
22 changes: 7 additions & 15 deletions aws/data_source_aws_lex_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package aws

import (
"fmt"
"regexp"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/lexmodelbuildingservice"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceAwsLexBot() *schema.Resource {
Expand Down Expand Up @@ -62,12 +60,9 @@ func dataSourceAwsLexBot() *schema.Resource {
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 100),
validation.StringMatch(regexp.MustCompile(`^([A-Za-z]_?)+$`), ""),
),
Type: schema.TypeString,
Required: true,
ValidateFunc: validateLexBotName,
},
"nlu_intent_confidence_threshold": {
Type: schema.TypeFloat,
Expand All @@ -78,13 +73,10 @@ func dataSourceAwsLexBot() *schema.Resource {
Computed: true,
},
"version": {
Type: schema.TypeString,
Optional: true,
Default: LexBotVersionLatest,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 64),
validation.StringMatch(regexp.MustCompile(`\$LATEST|[0-9]+`), ""),
),
Type: schema.TypeString,
Optional: true,
Default: LexBotVersionLatest,
ValidateFunc: validateLexBotVersion,
},
"voice_id": {
Type: schema.TypeString,
Expand Down
89 changes: 89 additions & 0 deletions aws/data_source_aws_lex_bot_alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package aws

import (
"fmt"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/lexmodelbuildingservice"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceAwsLexBotAlias() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsLexBotAliasRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"bot_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateLexBotName,
},
"bot_version": {
Type: schema.TypeString,
Computed: true,
},
"checksum": {
Type: schema.TypeString,
Computed: true,
},
"created_date": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"last_updated_date": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateLexBotAliasName,
},
},
}
}

func dataSourceAwsLexBotAliasRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lexmodelconn

botName := d.Get("bot_name").(string)
botAliasName := d.Get("name").(string)
d.SetId(fmt.Sprintf("%s:%s", botName, botAliasName))

resp, err := conn.GetBotAlias(&lexmodelbuildingservice.GetBotAliasInput{
BotName: aws.String(botName),
Name: aws.String(botAliasName),
})
if err != nil {
return fmt.Errorf("error reading Lex bot alias (%s): %w", d.Id(), err)
}

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "lex",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("bot:%s", d.Id()),
}
d.Set("arn", arn.String())

d.Set("bot_name", resp.BotName)
d.Set("bot_version", resp.BotVersion)
d.Set("checksum", resp.Checksum)
d.Set("created_date", resp.CreatedDate.Format(time.RFC3339))
d.Set("description", resp.Description)
d.Set("last_updated_date", resp.LastUpdatedDate.Format(time.RFC3339))
d.Set("name", resp.Name)

return nil
}
49 changes: 49 additions & 0 deletions aws/data_source_aws_lex_bot_alias_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package aws

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func testAccDataSourceAwsLexBotAlias_basic(t *testing.T) {
rName := acctest.RandStringFromCharSet(8, acctest.CharSetAlpha)
dataSourceName := "data.aws_lex_bot_alias.test"
resourceName := "aws_lex_bot_alias.test"

// If this test runs in parallel with other Lex Bot tests, it loses its description
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: composeConfig(
testAccAwsLexBotConfig_intent(rName),
testAccAwsLexBotConfig_createVersion(rName),
testAccAwsLexBotAliasConfig_basic(rName),
testAccDataSourceAwsLexBotAliasConfig_basic(),
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "bot_name", resourceName, "bot_name"),
resource.TestCheckResourceAttrPair(dataSourceName, "bot_version", resourceName, "bot_version"),
resource.TestCheckResourceAttrPair(dataSourceName, "checksum", resourceName, "checksum"),
resource.TestCheckResourceAttrPair(dataSourceName, "created_date", resourceName, "created_date"),
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrPair(dataSourceName, "last_updated_date", resourceName, "last_updated_date"),
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
),
},
},
})
}

func testAccDataSourceAwsLexBotAliasConfig_basic() string {
return `
data "aws_lex_bot_alias" "test" {
name = aws_lex_bot_alias.test.name
bot_name = aws_lex_bot.test.name
}
`
}
21 changes: 21 additions & 0 deletions aws/internal/service/lex/waiter/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,24 @@ func LexBotStatus(conn *lexmodelbuildingservice.LexModelBuildingService, id stri
return output, LexModelBuildingServiceStatusCreated, nil
}
}

func LexBotAliasStatus(conn *lexmodelbuildingservice.LexModelBuildingService, botAliasName, botName string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := conn.GetBotAlias(&lexmodelbuildingservice.GetBotAliasInput{
BotName: aws.String(botName),
Name: aws.String(botAliasName),
})
if tfawserr.ErrCodeEquals(err, lexmodelbuildingservice.ErrCodeNotFoundException) {
return nil, LexModelBuildingServiceStatusNotFound, nil
}
if err != nil {
return nil, LexModelBuildingServiceStatusUnknown, err
}

if output == nil {
return nil, LexModelBuildingServiceStatusNotFound, nil
}

return output, LexModelBuildingServiceStatusCreated, nil
}
}
16 changes: 16 additions & 0 deletions aws/internal/service/lex/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ func LexBotDeleted(conn *lexmodelbuildingservice.LexModelBuildingService, botId
return nil, err
}

func LexBotAliasDeleted(conn *lexmodelbuildingservice.LexModelBuildingService, botAliasName, botName string) (*lexmodelbuildingservice.GetBotAliasOutput, error) {
stateChangeConf := &resource.StateChangeConf{
Pending: []string{LexModelBuildingServiceStatusCreated},
Target: []string{}, // An empty slice indicates that the resource is gone
Refresh: LexBotAliasStatus(conn, botAliasName, botName),
Timeout: LexBotDeleteTimeout,
}
outputRaw, err := stateChangeConf.WaitForState()

if v, ok := outputRaw.(*lexmodelbuildingservice.GetBotAliasOutput); ok {
return v, err
}

return nil, err
}

func LexIntentDeleted(conn *lexmodelbuildingservice.LexModelBuildingService, intentId string) (*lexmodelbuildingservice.GetIntentVersionsOutput, error) {
stateChangeConf := &resource.StateChangeConf{
Pending: []string{LexModelBuildingServiceStatusCreated},
Expand Down
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ func Provider() *schema.Provider {
"aws_launch_configuration": dataSourceAwsLaunchConfiguration(),
"aws_launch_template": dataSourceAwsLaunchTemplate(),
"aws_lex_bot": dataSourceAwsLexBot(),
"aws_lex_bot_alias": dataSourceAwsLexBotAlias(),
"aws_lex_intent": dataSourceAwsLexIntent(),
"aws_lex_slot_type": dataSourceAwsLexSlotType(),
"aws_mq_broker": dataSourceAwsMqBroker(),
Expand Down Expand Up @@ -717,6 +718,7 @@ func Provider() *schema.Provider {
"aws_launch_configuration": resourceAwsLaunchConfiguration(),
"aws_launch_template": resourceAwsLaunchTemplate(),
"aws_lex_bot": resourceAwsLexBot(),
"aws_lex_bot_alias": resourceAwsLexBotAlias(),
"aws_lex_intent": resourceAwsLexIntent(),
"aws_lex_slot_type": resourceAwsLexSlotType(),
"aws_licensemanager_association": resourceAwsLicenseManagerAssociation(),
Expand Down
21 changes: 14 additions & 7 deletions aws/resource_aws_lex_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,10 @@ func resourceAwsLexBot() *schema.Resource {
ValidateFunc: validation.StringInSlice(lexmodelbuildingservice.Locale_Values(), false),
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.All(
validation.StringLenBetween(2, 50),
validation.StringMatch(regexp.MustCompile(`^([A-Za-z]_?)+$`), ""),
),
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateLexBotName,
},
"nlu_intent_confidence_threshold": {
Type: schema.TypeFloat,
Expand Down Expand Up @@ -182,6 +179,16 @@ func resourceAwsLexBot() *schema.Resource {
}
}

var validateLexBotName = validation.All(
validation.StringLenBetween(2, 50),
validation.StringMatch(regexp.MustCompile(`^([A-Za-z]_?)+$`), ""),
)

var validateLexBotVersion = validation.All(
validation.StringLenBetween(1, 64),
validation.StringMatch(regexp.MustCompile(`\$LATEST|[0-9]+`), ""),
)

func resourceAwsLexBotCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lexmodelconn
name := d.Get("name").(string)
Expand Down
Loading

0 comments on commit 815f395

Please sign in to comment.