Skip to content

Commit

Permalink
Merge pull request #8917 from jzbruno/add_aws_lex_intent_resource
Browse files Browse the repository at this point in the history
Add Lex intent data source and resource
  • Loading branch information
gdavison authored Sep 29, 2020
2 parents d7cf40f + 1501488 commit a12d7d1
Show file tree
Hide file tree
Showing 12 changed files with 2,432 additions and 15 deletions.
97 changes: 97 additions & 0 deletions aws/data_source_aws_lex_intent.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 dataSourceAwsLexIntent() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsLexIntentRead,

Schema: map[string]*schema.Schema{
"arn": {
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]_?)+$`), ""),
),
},
"parent_intent_signature": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Optional: true,
Default: LexIntentVersionLatest,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 64),
validation.StringMatch(regexp.MustCompile(`\$LATEST|[0-9]+`), ""),
),
},
},
}
}

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

intentName := d.Get("name").(string)
resp, err := conn.GetIntent(&lexmodelbuildingservice.GetIntentInput{
Name: aws.String(intentName),
Version: aws.String(d.Get("version").(string)),
})
if err != nil {
return fmt.Errorf("error getting intent %s: %w", intentName, err)
}

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

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)
d.Set("parent_intent_signature", resp.ParentIntentSignature)
d.Set("version", resp.Version)

d.SetId(intentName)

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

import (
"testing"

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

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: composeConfig(
testAccAwsLexIntentConfig_basic(rName),
testAccDataSourceAwsLexIntentConfig_basic(),
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
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"),
resource.TestCheckResourceAttrPair(dataSourceName, "version", resourceName, "version"),
),
},
},
})
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: composeConfig(
testAccAwsLexIntentConfig_createVersion(rName),
testAccDataSourceAwsLexIntentConfig_withVersion(),
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
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"),
resource.TestCheckResourceAttrPair(dataSourceName, "version", resourceName, "version"),
),
},
},
})
}

func testAccDataSourceAwsLexIntentConfig_basic() string {
return `
data "aws_lex_intent" "test" {
name = aws_lex_intent.test.name
}
`
}

func testAccDataSourceAwsLexIntentConfig_withVersion() string {
return `
data "aws_lex_intent" "test" {
name = aws_lex_intent.test.name
version = "1"
}
`
}
10 changes: 4 additions & 6 deletions aws/data_source_aws_lex_slot_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ func TestAccDataSourceAwsLexSlotType_basic(t *testing.T) {
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "checksum", resourceName, "checksum"),
resource.TestCheckResourceAttrPair(dataSourceName, "created_date", resourceName, "created_date"),
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrPair(dataSourceName, "enumeration_value.#", resourceName, "enumeration_value.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "last_updated_date", resourceName, "last_updated_date"),
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(dataSourceName, "value_selection_strategy", resourceName, "value_selection_strategy"),
resource.TestCheckResourceAttrPair(dataSourceName, "version", resourceName, "version"),
resource.TestCheckResourceAttrPair(dataSourceName, "created_date", resourceName, "created_date"),
testAccCheckResourceAttrRfc3339(dataSourceName, "created_date"),
resource.TestCheckResourceAttrPair(dataSourceName, "last_updated_date", resourceName, "last_updated_date"),
testAccCheckResourceAttrRfc3339(dataSourceName, "last_updated_date"),
),
},
},
Expand All @@ -54,12 +52,12 @@ func TestAccDataSourceAwsLexSlotType_withVersion(t *testing.T) {
),
Check: resource.ComposeAggregateTestCheckFunc(
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"),
resource.TestCheckResourceAttrPair(dataSourceName, "value_selection_strategy", resourceName, "value_selection_strategy"),
resource.TestCheckResourceAttrPair(dataSourceName, "version", resourceName, "version"),
testAccCheckResourceAttrRfc3339(dataSourceName, "created_date"),
testAccCheckResourceAttrRfc3339(dataSourceName, "last_updated_date"),
),
},
},
Expand Down
20 changes: 20 additions & 0 deletions aws/internal/service/lex/waiter/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,23 @@ func LexSlotTypeStatus(conn *lexmodelbuildingservice.LexModelBuildingService, id
return output, LexModelBuildingServiceStatusCreated, nil
}
}

func LexIntentStatus(conn *lexmodelbuildingservice.LexModelBuildingService, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := conn.GetIntentVersions(&lexmodelbuildingservice.GetIntentVersionsInput{
Name: aws.String(id),
})
if tfawserr.ErrCodeEquals(err, lexmodelbuildingservice.ErrCodeNotFoundException) {
return nil, LexModelBuildingServiceStatusNotFound, nil
}
if err != nil {
return nil, LexModelBuildingServiceStatusUnknown, err
}

if output == nil || len(output.Intents) == 0 {
return nil, LexModelBuildingServiceStatusNotFound, nil
}

return output, LexModelBuildingServiceStatusCreated, nil
}
}
17 changes: 17 additions & 0 deletions aws/internal/service/lex/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,25 @@ import (

const (
LexSlotTypeDeleteTimeout = 5 * time.Minute
LexIntentDeleteTimeout = 5 * time.Minute
)

func LexIntentDeleted(conn *lexmodelbuildingservice.LexModelBuildingService, intentId string) (*lexmodelbuildingservice.GetIntentVersionsOutput, error) {
stateChangeConf := &resource.StateChangeConf{
Pending: []string{LexModelBuildingServiceStatusCreated},
Target: []string{}, // An empty slice indicates that the resource is gone
Refresh: LexIntentStatus(conn, intentId),
Timeout: LexIntentDeleteTimeout,
}
outputRaw, err := stateChangeConf.WaitForState()

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

return nil, err
}

func LexSlotTypeDeleted(conn *lexmodelbuildingservice.LexModelBuildingService, slotTypeId string) (*lexmodelbuildingservice.GetSlotTypeVersionsOutput, 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 @@ -282,6 +282,7 @@ func Provider() *schema.Provider {
"aws_lambda_layer_version": dataSourceAwsLambdaLayerVersion(),
"aws_launch_configuration": dataSourceAwsLaunchConfiguration(),
"aws_launch_template": dataSourceAwsLaunchTemplate(),
"aws_lex_intent": dataSourceAwsLexIntent(),
"aws_lex_slot_type": dataSourceAwsLexSlotType(),
"aws_mq_broker": dataSourceAwsMqBroker(),
"aws_msk_cluster": dataSourceAwsMskCluster(),
Expand Down Expand Up @@ -706,6 +707,7 @@ func Provider() *schema.Provider {
"aws_lambda_provisioned_concurrency_config": resourceAwsLambdaProvisionedConcurrencyConfig(),
"aws_launch_configuration": resourceAwsLaunchConfiguration(),
"aws_launch_template": resourceAwsLaunchTemplate(),
"aws_lex_intent": resourceAwsLexIntent(),
"aws_lex_slot_type": resourceAwsLexSlotType(),
"aws_licensemanager_association": resourceAwsLicenseManagerAssociation(),
"aws_licensemanager_license_configuration": resourceAwsLicenseManagerLicenseConfiguration(),
Expand Down
Loading

0 comments on commit a12d7d1

Please sign in to comment.