Skip to content

Commit

Permalink
Merge pull request #25555 from GlennChia/f-aws_kendra_thesaurus
Browse files Browse the repository at this point in the history
d/aws_kendra_thesaurus
  • Loading branch information
ewbankkit authored Jun 27, 2022
2 parents eb1653c + 814141f commit b72bc5d
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .changelog/25555.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_kendra_thesaurus
```
5 changes: 3 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,9 @@ func Provider() *schema.Provider {
"aws_mskconnect_custom_plugin": kafkaconnect.DataSourceCustomPlugin(),
"aws_mskconnect_worker_configuration": kafkaconnect.DataSourceWorkerConfiguration(),

"aws_kendra_faq": kendra.DataSourceFaq(),
"aws_kendra_index": kendra.DataSourceIndex(),
"aws_kendra_faq": kendra.DataSourceFaq(),
"aws_kendra_index": kendra.DataSourceIndex(),
"aws_kendra_thesaurus": kendra.DataSourceThesaurus(),

"aws_kinesis_stream": kinesis.DataSourceStream(),
"aws_kinesis_stream_consumer": kinesis.DataSourceStreamConsumer(),
Expand Down
4 changes: 2 additions & 2 deletions internal/service/kendra/faq_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func TestAccKendraFaqDataSource_basic(t *testing.T) {
func testAccFaqDataSource_basic(t *testing.T) {
datasourceName := "data.aws_kendra_faq.test"
resourceName := "aws_kendra_faq.test"
rName := sdkacctest.RandomWithPrefix("resource-test-terraform")
Expand All @@ -20,7 +20,7 @@ func TestAccKendraFaqDataSource_basic(t *testing.T) {
rName4 := sdkacctest.RandomWithPrefix("resource-test-terraform")
rName5 := sdkacctest.RandomWithPrefix("resource-test-terraform")

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, backup.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
Expand Down
4 changes: 2 additions & 2 deletions internal/service/kendra/index_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func TestAccKendraIndexDataSource_basic(t *testing.T) {
func testAccIndexDataSource_basic(t *testing.T) {
datasourceName := "data.aws_kendra_index.test"
resourceName := "aws_kendra_index.test"
rName := sdkacctest.RandomWithPrefix("resource-test-terraform")
rName2 := sdkacctest.RandomWithPrefix("resource-test-terraform")
rName3 := sdkacctest.RandomWithPrefix("resource-test-terraform")

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, backup.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
Expand Down
29 changes: 16 additions & 13 deletions internal/service/kendra/kendra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ func TestAccKendra_serial(t *testing.T) {
"Configuration_UserIdentityConfigurationWithContentSourceConfigurationRemoved": testAccExperience_Configuration_UserIdentityConfigurationWithContentSourceConfigurationRemoved,
},
"Faq": {
"basic": testAccFaq_basic,
"disappears": testAccFaq_disappears,
"tags": testAccFaq_tags,
"Description": testAccFaq_description,
"FileFormat": testAccFaq_fileFormat,
"LanguageCode": testAccFaq_languageCode,
"basic": testAccFaq_basic,
"disappears": testAccFaq_disappears,
"tags": testAccFaq_tags,
"Description": testAccFaq_description,
"FileFormat": testAccFaq_fileFormat,
"LanguageCode": testAccFaq_languageCode,
"DataSource_basic": testAccFaqDataSource_basic,
},
"Index": {
"basic": testAccIndex_basic,
Expand All @@ -37,6 +38,7 @@ func TestAccKendra_serial(t *testing.T) {
"RoleARN": testAccIndex_updateRoleARN,
"ServerSideEncryption": testAccIndex_serverSideEncryption,
"UserTokenJSON": testAccIndex_updateUserTokenJSON,
"DataSource_basic": testAccIndexDataSource_basic,
},
"QuerySuggestionsBlockList": {
"basic": testAccQuerySuggestionsBlockList_basic,
Expand All @@ -48,13 +50,14 @@ func TestAccKendra_serial(t *testing.T) {
"SourceS3Path": testAccQuerySuggestionsBlockList_SourceS3Path,
},
"Thesaurus": {
"basic": testAccThesaurus_basic,
"disappears": testAccThesaurus_disappears,
"tags": testAccThesaurus_tags,
"Description": testAccThesaurus_description,
"Name": testAccThesaurus_name,
"RoleARN": testAccThesaurus_roleARN,
"SourceS3Path": testAccThesaurus_sourceS3Path,
"basic": testAccThesaurus_basic,
"disappears": testAccThesaurus_disappears,
"tags": testAccThesaurus_tags,
"Description": testAccThesaurus_description,
"Name": testAccThesaurus_name,
"RoleARN": testAccThesaurus_roleARN,
"SourceS3Path": testAccThesaurus_sourceS3Path,
"DataSource_basic": testAccThesaurusDataSource_basic,
},
}

Expand Down
159 changes: 159 additions & 0 deletions internal/service/kendra/thesaurus_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package kendra

import (
"context"
"fmt"
"regexp"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

func DataSourceThesaurus() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceThesaurusRead,
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"error_message": {
Type: schema.TypeString,
Computed: true,
},
"file_size_bytes": {
Type: schema.TypeInt,
Computed: true,
},
"index_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`[a-zA-Z0-9][a-zA-Z0-9-]{35}`),
"Starts with an alphanumeric character. Subsequently, can contain alphanumeric characters and hyphens. Fixed length of 36.",
),
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"role_arn": {
Type: schema.TypeString,
Computed: true,
},
"source_s3_path": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Computed: true,
},
"key": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"synonym_rule_count": {
Type: schema.TypeInt,
Computed: true,
},
"tags": tftags.TagsSchemaComputed(),
"term_count": {
Type: schema.TypeInt,
Computed: true,
},
"thesaurus_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 100),
validation.StringMatch(
regexp.MustCompile(`[a-zA-Z0-9][a-zA-Z0-9_-]*`),
"Starts with an alphanumeric character. Subsequently, can contain alphanumeric characters and hyphens.",
),
),
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceThesaurusRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KendraConn
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

thesaurusID := d.Get("thesaurus_id").(string)
indexID := d.Get("index_id").(string)

resp, err := FindThesaurusByID(ctx, conn, thesaurusID, indexID)

if err != nil {
return diag.Errorf("reading Kendra Thesaurus (%s): %s", thesaurusID, err)
}

arn := arn.ARN{
Partition: meta.(*conns.AWSClient).Partition,
Service: "kendra",
Region: meta.(*conns.AWSClient).Region,
AccountID: meta.(*conns.AWSClient).AccountID,
Resource: fmt.Sprintf("index/%s/thesaurus/%s", indexID, thesaurusID),
}.String()
d.Set("arn", arn)
d.Set("created_at", aws.ToTime(resp.CreatedAt).Format(time.RFC3339))
d.Set("description", resp.Description)
d.Set("error_message", resp.ErrorMessage)
d.Set("file_size_bytes", resp.FileSizeBytes)
d.Set("index_id", resp.IndexId)
d.Set("name", resp.Name)
d.Set("role_arn", resp.RoleArn)
d.Set("status", resp.Status)
d.Set("synonym_rule_count", resp.SynonymRuleCount)
d.Set("term_count", resp.TermCount)
d.Set("thesaurus_id", resp.Id)
d.Set("updated_at", aws.ToTime(resp.UpdatedAt).Format(time.RFC3339))

if err := d.Set("source_s3_path", flattenSourceS3Path(resp.SourceS3Path)); err != nil {
return diag.Errorf("setting source_s3_path: %s", err)
}

tags, err := ListTags(ctx, conn, arn)

if err != nil {
return diag.Errorf("listing tags for Kendra Thesaurus (%s): %s", arn, err)
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

if err := d.Set("tags", tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return diag.Errorf("setting tags: %s", err)
}

d.SetId(fmt.Sprintf("%s/%s", thesaurusID, indexID))

return nil
}
91 changes: 91 additions & 0 deletions internal/service/kendra/thesaurus_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package kendra_test

import (
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/service/backup"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func testAccThesaurusDataSource_basic(t *testing.T) {
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

datasourceName := "data.aws_kendra_thesaurus.test"
resourceName := "aws_kendra_thesaurus.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
rName2 := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, backup.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccThesaurusDataSourceConfig_nonExistent,
ExpectError: regexp.MustCompile(`reading Kendra Thesaurus`),
},
{
Config: testAccThesaurusDataSourceConfig_basic(rName, rName2),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrSet(datasourceName, "created_at"),
resource.TestCheckResourceAttrPair(datasourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrSet(datasourceName, "file_size_bytes"),
resource.TestCheckResourceAttrPair(datasourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(datasourceName, "index_id", resourceName, "index_id"),
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "role_arn", resourceName, "role_arn"),
resource.TestCheckResourceAttrPair(datasourceName, "source_s3_path.#", resourceName, "source_s3_path.#"),
resource.TestCheckResourceAttrPair(datasourceName, "source_s3_path.0.bucket", resourceName, "source_s3_path.0.bucket"),
resource.TestCheckResourceAttrPair(datasourceName, "source_s3_path.0.key", resourceName, "source_s3_path.0.key"),
resource.TestCheckResourceAttrPair(datasourceName, "status", resourceName, "status"),
resource.TestCheckResourceAttrSet(datasourceName, "synonym_rule_count"),
resource.TestCheckResourceAttrSet(datasourceName, "term_count"),
resource.TestCheckResourceAttrPair(datasourceName, "thesaurus_id", resourceName, "thesaurus_id"),
resource.TestCheckResourceAttrSet(datasourceName, "updated_at"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.Key1", resourceName, "tags.Key1")),
},
},
})
}

const testAccThesaurusDataSourceConfig_nonExistent = `
data "aws_kendra_thesaurus" "test" {
index_id = "tf-acc-test-does-not-exist-kendra-id"
thesaurus_id = "tf-acc-test-does-not-exist-kendra-thesaurus-id"
}
`

func testAccThesaurusDataSourceConfig_basic(rName, rName2 string) string {
return acctest.ConfigCompose(
testAccThesaurusBaseConfig(rName),
fmt.Sprintf(`
resource "aws_kendra_thesaurus" "test" {
index_id = aws_kendra_index.test.id
name = %[1]q
description = "example description thesaurus"
role_arn = aws_iam_role.test.arn
source_s3_path {
bucket = aws_s3_bucket.test.id
key = aws_s3_object.test.key
}
tags = {
"Key1" = "Value1"
}
}
data "aws_kendra_thesaurus" "test" {
index_id = aws_kendra_index.test.id
thesaurus_id = aws_kendra_thesaurus.test.thesaurus_id
}
`, rName2))
}
6 changes: 3 additions & 3 deletions internal/service/kendra/thesaurus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func testAccThesaurus_basic(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_kendra_thesaurus.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(t)
acctest.PreCheckPartitionHasService(names.KendraEndpointID, t)
Expand Down Expand Up @@ -66,7 +66,7 @@ func testAccThesaurus_disappears(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_kendra_thesaurus.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(t)
acctest.PreCheckPartitionHasService(names.KendraEndpointID, t)
Expand Down Expand Up @@ -96,7 +96,7 @@ func testAccThesaurus_tags(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_kendra_thesaurus.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(t)
acctest.PreCheckPartitionHasService(names.KendraEndpointID, t)
Expand Down
Loading

0 comments on commit b72bc5d

Please sign in to comment.