-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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 #25555 from GlennChia/f-aws_kendra_thesaurus
d/aws_kendra_thesaurus
- Loading branch information
Showing
9 changed files
with
330 additions
and
22 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,3 @@ | ||
```release-note:new-data-source | ||
aws_kendra_thesaurus | ||
``` |
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
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
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
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
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,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 | ||
} |
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,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)) | ||
} |
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.