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

Add Lex bot alias data source and resoruce #8919

Merged
merged 4 commits into from
Oct 5, 2020
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
97 changes: 97 additions & 0 deletions aws/data_source_aws_lex_bot_alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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 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: validation.All(
validation.StringLenBetween(2, 50),
validation.StringMatch(regexp.MustCompile(`^([A-Za-z]_?)+$`), ""),
),
},
"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: validation.All(
validation.StringLenBetween(1, 100),
validation.StringMatch(regexp.MustCompile(`^([A-Za-z]_?)+$`), ""),
),
},
},
}
}

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 getting 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
}
48 changes: 48 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,48 @@
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"

resource.ParallelTest(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
Loading