Skip to content

Commit

Permalink
Merge pull request #8916 from jzbruno/add_aws_lex_slot_type_resource
Browse files Browse the repository at this point in the history
Add Lex slot type data source and resource
  • Loading branch information
gdavison committed Aug 28, 2020
2 parents 5f7bdf7 + dde1133 commit 6302bf6
Show file tree
Hide file tree
Showing 10 changed files with 1,155 additions and 0 deletions.
104 changes: 104 additions & 0 deletions aws/data_source_aws_lex_slot_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package aws

import (
"fmt"
"regexp"
"time"

"github.com/aws/aws-sdk-go/aws"
"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 dataSourceAwsLexSlotType() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsLexSlotTypeRead,

Schema: map[string]*schema.Schema{
"checksum": {
Type: schema.TypeString,
Computed: true,
},
"created_date": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"enumeration_value": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"synonyms": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"value": {
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]_?)+$`), ""),
),
},
"value_selection_strategy": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Optional: true,
Default: LexSlotTypeVersionLatest,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 64),
validation.StringMatch(regexp.MustCompile(`\$LATEST|[0-9]+`), ""),
),
},
},
}
}

func dataSourceAwsLexSlotTypeRead(d *schema.ResourceData, meta interface{}) error {
slotTypeName := d.Get("name").(string)

conn := meta.(*AWSClient).lexmodelconn

resp, err := conn.GetSlotType(&lexmodelbuildingservice.GetSlotTypeInput{
Name: aws.String(slotTypeName),
Version: aws.String(d.Get("version").(string)),
})
if err != nil {
return fmt.Errorf("error getting slot type %s: %w", slotTypeName, err)
}

d.Set("checksum", resp.Checksum)
d.Set("created_date", resp.CreatedDate.Format(time.RFC3339))
d.Set("description", resp.Description)
d.Set("enumeration_value", flattenLexEnumerationValues(resp.EnumerationValues))
d.Set("last_updated_date", resp.LastUpdatedDate.Format(time.RFC3339))
d.Set("name", resp.Name)
d.Set("value_selection_strategy", resp.ValueSelectionStrategy)
d.Set("version", resp.Version)

d.SetId(slotTypeName)

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

import (
"testing"

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

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: composeConfig(
testAccAwsLexSlotTypeConfig_basic(rName),
testAccDataSourceAwsLexSlotTypeConfig_basic(),
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "checksum", resourceName, "checksum"),
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrPair(dataSourceName, "enumeration_value.#", resourceName, "enumeration_value.#"),
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"),
),
},
},
})
}

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

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

func testAccDataSourceAwsLexSlotTypeConfig_basic() string {
return `
data "aws_lex_slot_type" "test" {
name = "${aws_lex_slot_type.test.name}"
}
`
}

func testAccDataSourceAwsLexSlotTypeConfig_withVersion() string {
return `
data "aws_lex_slot_type" "test" {
name = "${aws_lex_slot_type.test.name}"
version = "1"
}
`
}
34 changes: 34 additions & 0 deletions aws/internal/service/lex/waiter/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package waiter

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lexmodelbuildingservice"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const (
LexModelBuildingServiceStatusCreated = "Created"
LexModelBuildingServiceStatusNotFound = "NotFound"
LexModelBuildingServiceStatusUnknown = "Unknown"
)

func LexSlotTypeStatus(conn *lexmodelbuildingservice.LexModelBuildingService, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := conn.GetSlotTypeVersions(&lexmodelbuildingservice.GetSlotTypeVersionsInput{
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.SlotTypes) == 0 {
return nil, LexModelBuildingServiceStatusNotFound, nil
}

return output, LexModelBuildingServiceStatusCreated, nil
}
}
28 changes: 28 additions & 0 deletions aws/internal/service/lex/waiter/waiter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package waiter

import (
"time"

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

const (
LexSlotTypeDeleteTimeout = 5 * time.Minute
)

func LexSlotTypeDeleted(conn *lexmodelbuildingservice.LexModelBuildingService, slotTypeId string) (*lexmodelbuildingservice.GetSlotTypeVersionsOutput, error) {
stateChangeConf := &resource.StateChangeConf{
Pending: []string{LexModelBuildingServiceStatusCreated},
Target: []string{}, // An empty slice indicates that the resource is gone
Refresh: LexSlotTypeStatus(conn, slotTypeId),
Timeout: LexSlotTypeDeleteTimeout,
}
outputRaw, err := stateChangeConf.WaitForState()

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

return nil, err
}
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func Provider() *schema.Provider {
"aws_lambda_layer_version": dataSourceAwsLambdaLayerVersion(),
"aws_launch_configuration": dataSourceAwsLaunchConfiguration(),
"aws_launch_template": dataSourceAwsLaunchTemplate(),
"aws_lex_slot_type": dataSourceAwsLexSlotType(),
"aws_mq_broker": dataSourceAwsMqBroker(),
"aws_msk_cluster": dataSourceAwsMskCluster(),
"aws_msk_configuration": dataSourceAwsMskConfiguration(),
Expand Down Expand Up @@ -690,6 +691,7 @@ func Provider() *schema.Provider {
"aws_lambda_provisioned_concurrency_config": resourceAwsLambdaProvisionedConcurrencyConfig(),
"aws_launch_configuration": resourceAwsLaunchConfiguration(),
"aws_launch_template": resourceAwsLaunchTemplate(),
"aws_lex_slot_type": resourceAwsLexSlotType(),
"aws_licensemanager_association": resourceAwsLicenseManagerAssociation(),
"aws_licensemanager_license_configuration": resourceAwsLicenseManagerLicenseConfiguration(),
"aws_lightsail_domain": resourceAwsLightsailDomain(),
Expand Down
Loading

0 comments on commit 6302bf6

Please sign in to comment.