-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8916 from jzbruno/add_aws_lex_slot_type_resource
Add Lex slot type data source and resource
- Loading branch information
Showing
10 changed files
with
1,155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.