Skip to content

Commit

Permalink
Merge pull request #32339 from hashicorp/td-migrate-keyspaces-to-aws-…
Browse files Browse the repository at this point in the history
…sdk-v2

Tech debt: Migrate `keyspaces` resources to AWS SDK for Go v2
  • Loading branch information
ewbankkit authored Jul 3, 2023
2 parents 47d1797 + 26239f7 commit eca426c
Show file tree
Hide file tree
Showing 17 changed files with 432 additions and 314 deletions.
3 changes: 3 additions & 0 deletions .changelog/32339.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_aws_keyspaces_table: Add `client_side_timestamps` configuration block
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/inspector2 v1.15.0
github.com/aws/aws-sdk-go-v2/service/ivschat v1.4.7
github.com/aws/aws-sdk-go-v2/service/kendra v1.41.0
github.com/aws/aws-sdk-go-v2/service/keyspaces v1.3.2
github.com/aws/aws-sdk-go-v2/service/lambda v1.37.0
github.com/aws/aws-sdk-go-v2/service/lightsail v1.27.1
github.com/aws/aws-sdk-go-v2/service/medialive v1.31.6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ github.com/aws/aws-sdk-go-v2/service/ivschat v1.4.7 h1:pI950CQHVEFW2/+UklRO4TWzH
github.com/aws/aws-sdk-go-v2/service/ivschat v1.4.7/go.mod h1:oOLFrfP14cyQdTsc3I8VohdYb8g86I7xduoMALyKLj0=
github.com/aws/aws-sdk-go-v2/service/kendra v1.41.0 h1:QZIaiIYfU8KCUuT4nCif9fifXryv+/pI/ounZMB4/0s=
github.com/aws/aws-sdk-go-v2/service/kendra v1.41.0/go.mod h1:huU0BQC+O9qcc3vrANhwkC9KP0hGg1quffiyfbJ1Ktg=
github.com/aws/aws-sdk-go-v2/service/keyspaces v1.3.2 h1:nL5UBozBBCgCV5nv03YvDnuuCH11iI4pNjH/EchcBEU=
github.com/aws/aws-sdk-go-v2/service/keyspaces v1.3.2/go.mod h1:kce6B9bwqIlk810LDwZC4bT928MQc9c88OtXkqGIymc=
github.com/aws/aws-sdk-go-v2/service/lambda v1.37.0 h1:xzyM5ZR9kZW0/Bkw5EiihOy6B+BYclp5K+yb6OHjc7s=
github.com/aws/aws-sdk-go-v2/service/lambda v1.37.0/go.mod h1:Q8zQi5nZpjUF/H55dKEpKfEvFWJkgZzjjqvDb2AR5b4=
github.com/aws/aws-sdk-go-v2/service/lightsail v1.27.1 h1:q9YZIsBPkeafzYPAJUquHZW5WkdmsLLu4jWlWzNEyjI=
Expand Down
6 changes: 3 additions & 3 deletions internal/conns/awsclient_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions internal/service/keyspaces/exports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package keyspaces

// Exports for use in tests only.
var (
ResourceKeyspace = resourceKeyspace
ResourceTable = resourceTable

FindKeyspaceByName = findKeyspaceByName
FindTableByTwoPartKey = findTableByTwoPartKey
)
69 changes: 0 additions & 69 deletions internal/service/keyspaces/find.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/service/keyspaces/generate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go run ../../generate/tags/main.go -ListTags -ServiceTagsSlice -UpdateTags -UntagInTagsElem=Tags -UntagInNeedTagType
//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ServiceTagsSlice -UpdateTags -UntagInTagsElem=Tags -UntagInNeedTagType
//go:generate go run ../../generate/servicepackage/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.

Expand Down
57 changes: 42 additions & 15 deletions internal/service/keyspaces/keyspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"regexp"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/keyspaces"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/keyspaces"
"github.com/aws/aws-sdk-go-v2/service/keyspaces/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"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"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
Expand All @@ -21,7 +23,7 @@ import (

// @SDKResource("aws_keyspaces_keyspace", name="Keyspace")
// @Tags(identifierAttribute="arn")
func ResourceKeyspace() *schema.Resource {
func resourceKeyspace() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceKeyspaceCreate,
ReadWithoutTimeout: resourceKeyspaceRead,
Expand Down Expand Up @@ -60,15 +62,15 @@ func ResourceKeyspace() *schema.Resource {
}

func resourceKeyspaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KeyspacesConn(ctx)
conn := meta.(*conns.AWSClient).KeyspacesClient(ctx)

name := d.Get("name").(string)
input := &keyspaces.CreateKeyspaceInput{
KeyspaceName: aws.String(name),
Tags: getTagsIn(ctx),
}

_, err := conn.CreateKeyspaceWithContext(ctx, input)
_, err := conn.CreateKeyspace(ctx, input)

if err != nil {
return diag.Errorf("creating Keyspaces Keyspace (%s): %s", name, err)
Expand All @@ -77,7 +79,7 @@ func resourceKeyspaceCreate(ctx context.Context, d *schema.ResourceData, meta in
d.SetId(name)

_, err = tfresource.RetryWhenNotFound(ctx, d.Timeout(schema.TimeoutCreate), func() (interface{}, error) {
return FindKeyspaceByName(ctx, conn, d.Id())
return findKeyspaceByName(ctx, conn, d.Id())
})

if err != nil {
Expand All @@ -88,9 +90,9 @@ func resourceKeyspaceCreate(ctx context.Context, d *schema.ResourceData, meta in
}

func resourceKeyspaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KeyspacesConn(ctx)
conn := meta.(*conns.AWSClient).KeyspacesClient(ctx)

keyspace, err := FindKeyspaceByName(ctx, conn, d.Id())
keyspace, err := findKeyspaceByName(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Keyspaces Keyspace (%s) not found, removing from state", d.Id())
Expand All @@ -114,18 +116,18 @@ func resourceKeyspaceUpdate(ctx context.Context, d *schema.ResourceData, meta in
}

func resourceKeyspaceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KeyspacesConn(ctx)
conn := meta.(*conns.AWSClient).KeyspacesClient(ctx)

log.Printf("[DEBUG] Deleting Keyspaces Keyspace: (%s)", d.Id())
_, err := tfresource.RetryWhenAWSErrMessageContains(ctx, d.Timeout(schema.TimeoutDelete),
_, err := tfresource.RetryWhenIsAErrorMessageContains[*types.ConflictException](ctx, d.Timeout(schema.TimeoutDelete),
func() (interface{}, error) {
return conn.DeleteKeyspaceWithContext(ctx, &keyspaces.DeleteKeyspaceInput{
return conn.DeleteKeyspace(ctx, &keyspaces.DeleteKeyspaceInput{
KeyspaceName: aws.String(d.Id()),
})
},
keyspaces.ErrCodeConflictException, "a table under it is currently being created or deleted")
"a table under it is currently being created or deleted")

if tfawserr.ErrCodeEquals(err, keyspaces.ErrCodeResourceNotFoundException) {
if errs.IsA[*types.ResourceNotFoundException](err) {
return nil
}

Expand All @@ -134,7 +136,7 @@ func resourceKeyspaceDelete(ctx context.Context, d *schema.ResourceData, meta in
}

_, err = tfresource.RetryUntilNotFound(ctx, d.Timeout(schema.TimeoutDelete), func() (interface{}, error) {
return FindKeyspaceByName(ctx, conn, d.Id())
return findKeyspaceByName(ctx, conn, d.Id())
})

if err != nil {
Expand All @@ -143,3 +145,28 @@ func resourceKeyspaceDelete(ctx context.Context, d *schema.ResourceData, meta in

return nil
}

func findKeyspaceByName(ctx context.Context, conn *keyspaces.Client, name string) (*keyspaces.GetKeyspaceOutput, error) {
input := keyspaces.GetKeyspaceInput{
KeyspaceName: aws.String(name),
}

output, err := conn.GetKeyspace(ctx, &input)

if errs.IsA[*types.ResourceNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output, nil
}
12 changes: 6 additions & 6 deletions internal/service/keyspaces/keyspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/keyspaces"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfkeyspaces "github.com/hashicorp/terraform-provider-aws/internal/service/keyspaces"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

func testAccPreCheck(t *testing.T) {
Expand All @@ -27,7 +27,7 @@ func TestAccKeyspacesKeyspace_basic(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, keyspaces.EndpointsID),
ErrorCheck: acctest.ErrorCheck(t, names.KeyspacesEndpointID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckKeyspaceDestroy(ctx),
Steps: []resource.TestStep{
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestAccKeyspacesKeyspace_disappears(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, keyspaces.EndpointsID),
ErrorCheck: acctest.ErrorCheck(t, names.KeyspacesEndpointID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckKeyspaceDestroy(ctx),
Steps: []resource.TestStep{
Expand All @@ -79,7 +79,7 @@ func TestAccKeyspacesKeyspace_tags(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, keyspaces.EndpointsID),
ErrorCheck: acctest.ErrorCheck(t, names.KeyspacesEndpointID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckKeyspaceDestroy(ctx),
Steps: []resource.TestStep{
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestAccKeyspacesKeyspace_tags(t *testing.T) {

func testAccCheckKeyspaceDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).KeyspacesConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).KeyspacesClient(ctx)

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_keyspaces_keyspace" {
Expand Down Expand Up @@ -154,7 +154,7 @@ func testAccCheckKeyspaceExists(ctx context.Context, n string) resource.TestChec
return fmt.Errorf("No Keyspaces Keyspace ID is set")
}

conn := acctest.Provider.Meta().(*conns.AWSClient).KeyspacesConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).KeyspacesClient(ctx)

_, err := tfkeyspaces.FindKeyspaceByName(ctx, conn, rs.Primary.Attributes["name"])

Expand Down
21 changes: 12 additions & 9 deletions internal/service/keyspaces/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eca426c

Please sign in to comment.