Skip to content

Commit

Permalink
appsync: Migrate to AWS SDK v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mattburgess committed Mar 25, 2024
1 parent 0f14d58 commit 9c276d1
Show file tree
Hide file tree
Showing 32 changed files with 703 additions and 710 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/appfabric v1.7.3
github.com/aws/aws-sdk-go-v2/service/appflow v1.41.3
github.com/aws/aws-sdk-go-v2/service/apprunner v1.28.3
github.com/aws/aws-sdk-go-v2/service/appsync v1.31.3
github.com/aws/aws-sdk-go-v2/service/athena v1.40.3
github.com/aws/aws-sdk-go-v2/service/auditmanager v1.32.3
github.com/aws/aws-sdk-go-v2/service/batch v1.35.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ github.com/aws/aws-sdk-go-v2/service/appflow v1.41.3 h1:tK9duUSGFRz/8nshXYyddbw2
github.com/aws/aws-sdk-go-v2/service/appflow v1.41.3/go.mod h1:D08dEVi5M9I0qAIl1IyMydjFQPcjQqCp2Mw3WqhCDbQ=
github.com/aws/aws-sdk-go-v2/service/apprunner v1.28.3 h1:nby4wV20qfVF2yswXCFKL7lTkddNvYUj2P9+k/UbYQc=
github.com/aws/aws-sdk-go-v2/service/apprunner v1.28.3/go.mod h1:ECUaiPa9O5LwqgVNeBduNwayctvWkc5nb8NdBLmLJ00=
github.com/aws/aws-sdk-go-v2/service/appsync v1.31.3 h1:P6IEs0+zLDoYP2vzLoPccaQRn1MnjkXwE6t0NlPeUQc=
github.com/aws/aws-sdk-go-v2/service/appsync v1.31.3/go.mod h1:vBD0NGVgRoouqxDtybMYcBPeFE9yCbfleWZyw95qkWk=
github.com/aws/aws-sdk-go-v2/service/athena v1.40.3 h1:Q54tyTwpoEyJNmP4WqwT9hdPHpbpNahvcW9so6lItQw=
github.com/aws/aws-sdk-go-v2/service/athena v1.40.3/go.mod h1:HP/WmaAcHBNMHa6EwxTMPdqCIbV0uCnWR8WNTp2AG5c=
github.com/aws/aws-sdk-go-v2/service/auditmanager v1.32.3 h1:Y9Tqv+Pb93GGLpxYoKYdz9ZdwpnT5HP6AFazR3moNlM=
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.

53 changes: 27 additions & 26 deletions internal/service/appsync/api_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"context"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appsync"
"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/appsync"
awstypes "github.com/aws/aws-sdk-go-v2/service/appsync/types"
"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"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)
Expand All @@ -35,14 +36,14 @@ func ResourceAPICache() *schema.Resource {
Required: true,
},
"api_caching_behavior": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(appsync.ApiCachingBehavior_Values(), false),
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: enum.Validate[awstypes.ApiCachingBehavior](),
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(appsync.ApiCacheType_Values(), false),
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: enum.Validate[awstypes.ApiCacheType](),
},
"ttl": {
Type: schema.TypeInt,
Expand All @@ -64,26 +65,26 @@ func ResourceAPICache() *schema.Resource {

func resourceAPICacheCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AppSyncConn(ctx)
conn := meta.(*conns.AWSClient).AppSyncClient(ctx)

apiID := d.Get("api_id").(string)

params := &appsync.CreateApiCacheInput{
ApiId: aws.String(apiID),
Type: aws.String(d.Get("type").(string)),
ApiCachingBehavior: aws.String(d.Get("api_caching_behavior").(string)),
Ttl: aws.Int64(int64(d.Get("ttl").(int))),
Type: awstypes.ApiCacheType(d.Get("type").(string)),
ApiCachingBehavior: awstypes.ApiCachingBehavior(d.Get("api_caching_behavior").(string)),
Ttl: int64(d.Get("ttl").(int)),
}

if v, ok := d.GetOk("at_rest_encryption_enabled"); ok {
params.AtRestEncryptionEnabled = aws.Bool(v.(bool))
params.AtRestEncryptionEnabled = v.(bool)
}

if v, ok := d.GetOk("transit_encryption_enabled"); ok {
params.TransitEncryptionEnabled = aws.Bool(v.(bool))
params.TransitEncryptionEnabled = v.(bool)
}

_, err := conn.CreateApiCacheWithContext(ctx, params)
_, err := conn.CreateApiCache(ctx, params)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Appsync API Cache: %s", err)
}
Expand All @@ -99,7 +100,7 @@ func resourceAPICacheCreate(ctx context.Context, d *schema.ResourceData, meta in

func resourceAPICacheRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AppSyncConn(ctx)
conn := meta.(*conns.AWSClient).AppSyncClient(ctx)

cache, err := FindAPICacheByID(ctx, conn, d.Id())
if !d.IsNewResource() && tfresource.NotFound(err) {
Expand All @@ -124,25 +125,25 @@ func resourceAPICacheRead(ctx context.Context, d *schema.ResourceData, meta inte

func resourceAPICacheUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AppSyncConn(ctx)
conn := meta.(*conns.AWSClient).AppSyncClient(ctx)

params := &appsync.UpdateApiCacheInput{
ApiId: aws.String(d.Id()),
}

if d.HasChange("type") {
params.Type = aws.String(d.Get("type").(string))
params.Type = awstypes.ApiCacheType(d.Get("type").(string))
}

if d.HasChange("api_caching_behavior") {
params.ApiCachingBehavior = aws.String(d.Get("api_caching_behavior").(string))
params.ApiCachingBehavior = awstypes.ApiCachingBehavior(d.Get("api_caching_behavior").(string))
}

if d.HasChange("ttl") {
params.Ttl = aws.Int64(int64(d.Get("ttl").(int)))
params.Ttl = int64(d.Get("ttl").(int))
}

_, err := conn.UpdateApiCacheWithContext(ctx, params)
_, err := conn.UpdateApiCache(ctx, params)
if err != nil {
return sdkdiag.AppendErrorf(diags, "updating Appsync API Cache %q: %s", d.Id(), err)
}
Expand All @@ -156,14 +157,14 @@ func resourceAPICacheUpdate(ctx context.Context, d *schema.ResourceData, meta in

func resourceAPICacheDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AppSyncConn(ctx)
conn := meta.(*conns.AWSClient).AppSyncClient(ctx)

input := &appsync.DeleteApiCacheInput{
ApiId: aws.String(d.Id()),
}
_, err := conn.DeleteApiCacheWithContext(ctx, input)
_, err := conn.DeleteApiCache(ctx, input)
if err != nil {
if tfawserr.ErrCodeEquals(err, appsync.ErrCodeNotFoundException) {
if errs.IsA[*awstypes.NotFoundException](err) {
return diags
}
return sdkdiag.AppendErrorf(diags, "deleting Appsync API Cache: %s", err)
Expand Down
16 changes: 8 additions & 8 deletions internal/service/appsync/api_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/appsync"
awstypes "github.com/aws/aws-sdk-go-v2/service/appsync/types"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
Expand All @@ -21,12 +21,12 @@ import (

func testAccAPICache_basic(t *testing.T) {
ctx := acctest.Context(t)
var apiCache appsync.ApiCache
var apiCache awstypes.ApiCache
resourceName := "aws_appsync_api_cache.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, appsync.EndpointsID) },
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, names.AppSyncEndpointID) },
ErrorCheck: acctest.ErrorCheck(t, names.AppSyncServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAPICacheDestroy(ctx),
Expand All @@ -51,12 +51,12 @@ func testAccAPICache_basic(t *testing.T) {

func testAccAPICache_disappears(t *testing.T) {
ctx := acctest.Context(t)
var apiCache appsync.ApiCache
var apiCache awstypes.ApiCache
resourceName := "aws_appsync_api_cache.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, appsync.EndpointsID) },
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, names.AppSyncEndpointID) },
ErrorCheck: acctest.ErrorCheck(t, names.AppSyncServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAPICacheDestroy(ctx),
Expand All @@ -75,7 +75,7 @@ func testAccAPICache_disappears(t *testing.T) {

func testAccCheckAPICacheDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).AppSyncConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).AppSyncClient(ctx)
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_appsync_api_cache" {
continue
Expand All @@ -95,14 +95,14 @@ func testAccCheckAPICacheDestroy(ctx context.Context) resource.TestCheckFunc {
}
}

func testAccCheckAPICacheExists(ctx context.Context, resourceName string, apiCache *appsync.ApiCache) resource.TestCheckFunc {
func testAccCheckAPICacheExists(ctx context.Context, resourceName string, apiCache *awstypes.ApiCache) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Appsync Api Cache Not found in state: %s", resourceName)
}

conn := acctest.Provider.Meta().(*conns.AWSClient).AppSyncConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).AppSyncClient(ctx)
cache, err := tfappsync.FindAPICacheByID(ctx, conn, rs.Primary.ID)
if err != nil {
return err
Expand Down
40 changes: 21 additions & 19 deletions internal/service/appsync/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appsync"
"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/appsync"
awstypes "github.com/aws/aws-sdk-go-v2/service/appsync/types"
"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"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
)

// @SDKResource("aws_appsync_api_key")
Expand Down Expand Up @@ -64,7 +66,7 @@ func ResourceAPIKey() *schema.Resource {

func resourceAPIKeyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AppSyncConn(ctx)
conn := meta.(*conns.AWSClient).AppSyncClient(ctx)

apiID := d.Get("api_id").(string)

Expand All @@ -74,20 +76,20 @@ func resourceAPIKeyCreate(ctx context.Context, d *schema.ResourceData, meta inte
}
if v, ok := d.GetOk("expires"); ok {
t, _ := time.Parse(time.RFC3339, v.(string))
params.Expires = aws.Int64(t.Unix())
params.Expires = t.Unix()
}
resp, err := conn.CreateApiKeyWithContext(ctx, params)
resp, err := conn.CreateApiKey(ctx, params)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Appsync API Key: %s", err)
}

d.SetId(fmt.Sprintf("%s:%s", apiID, aws.StringValue(resp.ApiKey.Id)))
d.SetId(fmt.Sprintf("%s:%s", apiID, aws.ToString(resp.ApiKey.Id)))
return append(diags, resourceAPIKeyRead(ctx, d, meta)...)
}

func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AppSyncConn(ctx)
conn := meta.(*conns.AWSClient).AppSyncClient(ctx)

apiID, keyID, err := DecodeAPIKeyID(d.Id())
if err != nil {
Expand All @@ -107,13 +109,13 @@ func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, meta interf
d.Set("api_id", apiID)
d.Set("key", key.Id)
d.Set("description", key.Description)
d.Set("expires", time.Unix(aws.Int64Value(key.Expires), 0).UTC().Format(time.RFC3339))
d.Set("expires", time.Unix(key.Expires, 0).UTC().Format(time.RFC3339))
return diags
}

func resourceAPIKeyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AppSyncConn(ctx)
conn := meta.(*conns.AWSClient).AppSyncClient(ctx)

apiID, keyID, err := DecodeAPIKeyID(d.Id())
if err != nil {
Expand All @@ -129,10 +131,10 @@ func resourceAPIKeyUpdate(ctx context.Context, d *schema.ResourceData, meta inte
}
if d.HasChange("expires") {
t, _ := time.Parse(time.RFC3339, d.Get("expires").(string))
params.Expires = aws.Int64(t.Unix())
params.Expires = t.Unix()
}

_, err = conn.UpdateApiKeyWithContext(ctx, params)
_, err = conn.UpdateApiKey(ctx, params)
if err != nil {
return sdkdiag.AppendErrorf(diags, "updating Appsync API Key (%s): %s", d.Id(), err)
}
Expand All @@ -142,7 +144,7 @@ func resourceAPIKeyUpdate(ctx context.Context, d *schema.ResourceData, meta inte

func resourceAPIKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AppSyncConn(ctx)
conn := meta.(*conns.AWSClient).AppSyncClient(ctx)

apiID, keyID, err := DecodeAPIKeyID(d.Id())
if err != nil {
Expand All @@ -153,9 +155,9 @@ func resourceAPIKeyDelete(ctx context.Context, d *schema.ResourceData, meta inte
ApiId: aws.String(apiID),
Id: aws.String(keyID),
}
_, err = conn.DeleteApiKeyWithContext(ctx, input)
_, err = conn.DeleteApiKey(ctx, input)
if err != nil {
if tfawserr.ErrCodeEquals(err, appsync.ErrCodeNotFoundException) {
if errs.IsA[*awstypes.NotFoundException](err) {
return diags
}
return sdkdiag.AppendErrorf(diags, "deleting Appsync API Key (%s): %s", d.Id(), err)
Expand All @@ -172,17 +174,17 @@ func DecodeAPIKeyID(id string) (string, string, error) {
return parts[0], parts[1], nil
}

func GetAPIKey(ctx context.Context, apiID, keyID string, conn *appsync.AppSync) (*appsync.ApiKey, error) {
func GetAPIKey(ctx context.Context, apiID, keyID string, conn *appsync.Client) (*awstypes.ApiKey, error) {
input := &appsync.ListApiKeysInput{
ApiId: aws.String(apiID),
}
for {
resp, err := conn.ListApiKeysWithContext(ctx, input)
resp, err := conn.ListApiKeys(ctx, input)
if err != nil {
return nil, err
}
for _, apiKey := range resp.ApiKeys {
if aws.StringValue(apiKey.Id) == keyID {
for _, apiKey := range tfslices.ToPointers(resp.ApiKeys) {
if aws.ToString(apiKey.Id) == keyID {
return apiKey, nil
}
}
Expand Down
Loading

0 comments on commit 9c276d1

Please sign in to comment.