Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/kendra_thesaurus: new resource #25199

Merged
merged 2 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/25199.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_kendra_thesaurus
```
3 changes: 2 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,8 @@ func Provider() *schema.Provider {
"aws_mskconnect_custom_plugin": kafkaconnect.ResourceCustomPlugin(),
"aws_mskconnect_worker_configuration": kafkaconnect.ResourceWorkerConfiguration(),

"aws_kendra_index": kendra.ResourceIndex(),
"aws_kendra_index": kendra.ResourceIndex(),
"aws_kendra_thesaurus": kendra.ResourceThesaurus(),

"aws_keyspaces_keyspace": keyspaces.ResourceKeyspace(),
"aws_keyspaces_table": keyspaces.ResourceTable(),
Expand Down
39 changes: 39 additions & 0 deletions internal/service/kendra/find.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package kendra

import (
"context"
"errors"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/kendra"
"github.com/aws/aws-sdk-go-v2/service/kendra/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func FindThesaurusByID(ctx context.Context, conn *kendra.Client, id, indexId string) (*kendra.DescribeThesaurusOutput, error) {
in := &kendra.DescribeThesaurusInput{
Id: aws.String(id),
IndexId: aws.String(indexId),
}

out, err := conn.DescribeThesaurus(ctx, in)

var resourceNotFoundException *types.ResourceNotFoundException
if errors.As(err, &resourceNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: in,
}
}

if err != nil {
return nil, err
}

if out == nil {
return nil, tfresource.NewEmptyResultError(in)
}

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

import (
"fmt"
"strings"
)

func ThesaurusParseResourceID(id string) (string, string, error) {
parts := strings.Split(id, "/")

if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return "", "", fmt.Errorf("please make sure ID is in format THESAURUS_ID/INDEX_ID")
}

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

import (
"testing"

tfkendra "github.com/hashicorp/terraform-provider-aws/internal/service/kendra"
)

func TestThesaurusParseResourceID(t *testing.T) {
testCases := []struct {
TestName string
Input string
ExpectedId string
ExpectedIndexId string
Error bool
}{
{
TestName: "empty",
Input: "",
ExpectedId: "",
ExpectedIndexId: "",
Error: true,
},
{
TestName: "Invalid ID",
Input: "abcdefg12345678/",
ExpectedId: "",
ExpectedIndexId: "",
Error: true,
},
{
TestName: "Invalid ID separator",
Input: "abcdefg12345678:qwerty09876",
ExpectedId: "",
ExpectedIndexId: "",
Error: true,
},
{
TestName: "Invalid ID with more than 1 separator",
Input: "abcdefg12345678/qwerty09876/zxcvbnm123456",
ExpectedId: "",
ExpectedIndexId: "",
Error: true,
},
{
TestName: "Valid ID",
Input: "abcdefg12345678/qwerty09876",
ExpectedId: "abcdefg12345678",
ExpectedIndexId: "qwerty09876",
Error: false,
},
}

for _, testCase := range testCases {
t.Run(testCase.TestName, func(t *testing.T) {
gotId, gotIndexId, err := tfkendra.ThesaurusParseResourceID(testCase.Input)

if err != nil && !testCase.Error {
t.Errorf("got error (%s), expected no error", err)
}

if err == nil && testCase.Error {
t.Errorf("got (Id: %s, IndexId: %s) and no error, expected error", gotId, gotIndexId)
}

if gotId != testCase.ExpectedId {
t.Errorf("got %s, expected %s", gotId, testCase.ExpectedIndexId)
}

if gotIndexId != testCase.ExpectedIndexId {
t.Errorf("got %s, expected %s", gotIndexId, testCase.ExpectedIndexId)
}
})
}
}
27 changes: 20 additions & 7 deletions internal/service/kendra/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import (
const (
// Allow IAM role to become visible to the index
propagationTimeout = 2 * time.Minute

// validationExceptionMessage describes the error returned when the IAM role has not yet propagated
validationExceptionMessage = "Please make sure your role exists and has `kendra.amazonaws.com` as trusted entity"
)

func ResourceIndex() *schema.Resource {
Expand Down Expand Up @@ -390,7 +393,7 @@ func resourceIndexCreate(ctx context.Context, d *schema.ResourceData, meta inter
func(err error) (bool, error) {
var validationException *types.ValidationException

if errors.As(err, &validationException) && strings.Contains(validationException.ErrorMessage(), "Please make sure your role exists and has `kendra.amazonaws.com` as trusted entity") {
if errors.As(err, &validationException) && strings.Contains(validationException.ErrorMessage(), validationExceptionMessage) {
return true, err
}

Expand Down Expand Up @@ -440,10 +443,6 @@ func resourceIndexRead(ctx context.Context, d *schema.ResourceData, meta interfa
return diag.Errorf("error getting Kendra Index (%s): %s", d.Id(), err)
}

if resp == nil {
return diag.Errorf("error getting Kendra Index (%s): empty response", d.Id())
}

arn := arn.ARN{
Partition: meta.(*conns.AWSClient).Partition,
Service: "kendra",
Expand Down Expand Up @@ -536,10 +535,24 @@ func resourceIndexUpdate(ctx context.Context, d *schema.ResourceData, meta inter
input.UserTokenConfigurations = expandUserTokenConfigurations(d.Get("user_token_configurations").([]interface{}))
}

_, err := conn.UpdateIndex(ctx, input)
_, err := tfresource.RetryWhen(
propagationTimeout,
func() (interface{}, error) {
return conn.UpdateIndex(ctx, input)
},
func(err error) (bool, error) {
var validationException *types.ValidationException

if errors.As(err, &validationException) && strings.Contains(validationException.ErrorMessage(), validationExceptionMessage) {
return true, err
}

return false, err
},
)

if err != nil {
return diag.Errorf("[ERROR] Error updating Index (%s): %s", d.Id(), err)
return diag.Errorf("error updating Index (%s): %s", d.Id(), err)
}

// waiter since the status changes from UPDATING to either ACTIVE or FAILED
Expand Down
36 changes: 18 additions & 18 deletions internal/service/kendra/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func testAccPreCheck(t *testing.T) {
}
}

func TestAccKendraIndex_basic(t *testing.T) {
func testAccIndex_basic(t *testing.T) {
var index kendra.DescribeIndexOutput

rName := sdkacctest.RandomWithPrefix("resource-test-terraform")
Expand All @@ -45,7 +45,7 @@ func TestAccKendraIndex_basic(t *testing.T) {
description := "basic"
resourceName := "aws_kendra_index.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, names.KendraEndpointID),
ProviderFactories: acctest.ProviderFactories,
Expand Down Expand Up @@ -88,15 +88,15 @@ func TestAccKendraIndex_basic(t *testing.T) {
})
}

func TestAccKendraIndex_serverSideEncryption(t *testing.T) {
func testAccIndex_serverSideEncryption(t *testing.T) {
var index kendra.DescribeIndexOutput

rName := sdkacctest.RandomWithPrefix("resource-test-terraform")
rName2 := sdkacctest.RandomWithPrefix("resource-test-terraform")
rName3 := sdkacctest.RandomWithPrefix("resource-test-terraform")
resourceName := "aws_kendra_index.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, names.KendraEndpointID),
ProviderFactories: acctest.ProviderFactories,
Expand All @@ -119,7 +119,7 @@ func TestAccKendraIndex_serverSideEncryption(t *testing.T) {
})
}

func TestAccKendraIndex_updateCapacityUnits(t *testing.T) {
func testAccIndex_updateCapacityUnits(t *testing.T) {
var index kendra.DescribeIndexOutput

rName := sdkacctest.RandomWithPrefix("resource-test-terraform")
Expand All @@ -131,7 +131,7 @@ func TestAccKendraIndex_updateCapacityUnits(t *testing.T) {
updatedStorageCapacityUnits := 2
resourceName := "aws_kendra_index.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, names.KendraEndpointID),
ProviderFactories: acctest.ProviderFactories,
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestAccKendraIndex_updateCapacityUnits(t *testing.T) {
},
})
}
func TestAccKendraIndex_updateDescription(t *testing.T) {
func testAccIndex_updateDescription(t *testing.T) {
var index kendra.DescribeIndexOutput

rName := sdkacctest.RandomWithPrefix("resource-test-terraform")
Expand All @@ -173,7 +173,7 @@ func TestAccKendraIndex_updateDescription(t *testing.T) {
updatedDescription := "updated description"
resourceName := "aws_kendra_index.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, names.KendraEndpointID),
ProviderFactories: acctest.ProviderFactories,
Expand Down Expand Up @@ -202,7 +202,7 @@ func TestAccKendraIndex_updateDescription(t *testing.T) {
})
}

func TestAccKendraIndex_updateName(t *testing.T) {
func testAccIndex_updateName(t *testing.T) {
var index kendra.DescribeIndexOutput

rName := sdkacctest.RandomWithPrefix("resource-test-terraform")
Expand All @@ -212,7 +212,7 @@ func TestAccKendraIndex_updateName(t *testing.T) {
description := "description"
resourceName := "aws_kendra_index.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, names.KendraEndpointID),
ProviderFactories: acctest.ProviderFactories,
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestAccKendraIndex_updateName(t *testing.T) {
})
}

func TestAccKendraIndex_updateUserTokenJSON(t *testing.T) {
func testAccIndex_updateUserTokenJSON(t *testing.T) {
var index kendra.DescribeIndexOutput

rName := sdkacctest.RandomWithPrefix("resource-test-terraform")
Expand All @@ -253,7 +253,7 @@ func TestAccKendraIndex_updateUserTokenJSON(t *testing.T) {
updatedUserNameAttributeField := "usernames"
resourceName := "aws_kendra_index.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, names.KendraEndpointID),
ProviderFactories: acctest.ProviderFactories,
Expand Down Expand Up @@ -298,7 +298,7 @@ func TestAccKendraIndex_updateUserTokenJSON(t *testing.T) {
})
}

func TestAccKendraIndex_updateTags(t *testing.T) {
func testAccIndex_updateTags(t *testing.T) {
var index kendra.DescribeIndexOutput

rName := sdkacctest.RandomWithPrefix("resource-test-terraform")
Expand All @@ -307,7 +307,7 @@ func TestAccKendraIndex_updateTags(t *testing.T) {
description := "description"
resourceName := "aws_kendra_index.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, names.KendraEndpointID),
ProviderFactories: acctest.ProviderFactories,
Expand Down Expand Up @@ -349,7 +349,7 @@ func TestAccKendraIndex_updateTags(t *testing.T) {
})
}

func TestAccKendraIndex_updateRoleARN(t *testing.T) {
func testAccIndex_updateRoleARN(t *testing.T) {
var index kendra.DescribeIndexOutput

rName := sdkacctest.RandomWithPrefix("resource-test-terraform")
Expand All @@ -358,7 +358,7 @@ func TestAccKendraIndex_updateRoleARN(t *testing.T) {
description := "description"
resourceName := "aws_kendra_index.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, names.KendraEndpointID),
ProviderFactories: acctest.ProviderFactories,
Expand Down Expand Up @@ -387,7 +387,7 @@ func TestAccKendraIndex_updateRoleARN(t *testing.T) {
})
}

func TestAccKendraIndex_disappears(t *testing.T) {
func testAccIndex_disappears(t *testing.T) {
var index kendra.DescribeIndexOutput

rName := sdkacctest.RandomWithPrefix("resource-test-terraform")
Expand All @@ -396,7 +396,7 @@ func TestAccKendraIndex_disappears(t *testing.T) {
description := "disappears"
resourceName := "aws_kendra_index.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, names.KendraEndpointID),
ProviderFactories: acctest.ProviderFactories,
Expand Down
Loading