Skip to content

Commit

Permalink
apigateway: Migrate to AWS SDK v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mattburgess committed Mar 19, 2024
1 parent 8812420 commit b9dfeaf
Show file tree
Hide file tree
Showing 72 changed files with 1,498 additions and 1,536 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/account v1.16.2
github.com/aws/aws-sdk-go-v2/service/acm v1.25.2
github.com/aws/aws-sdk-go-v2/service/amp v1.25.2
github.com/aws/aws-sdk-go-v2/service/apigateway v1.23.4
github.com/aws/aws-sdk-go-v2/service/appconfig v1.29.0
github.com/aws/aws-sdk-go-v2/service/appfabric v1.7.2
github.com/aws/aws-sdk-go-v2/service/appflow v1.41.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ github.com/aws/aws-sdk-go-v2/service/acm v1.25.2 h1:5oS1s5fZ4VyWj0tVSF7ihpE1lkaj
github.com/aws/aws-sdk-go-v2/service/acm v1.25.2/go.mod h1:hGHCrWRY/be0yX4017aNZc0fpjMyBM2NNT5BgDrk4+o=
github.com/aws/aws-sdk-go-v2/service/amp v1.25.2 h1:XXrduSuT8w94c3dG2dvNQioa7ZunXHXC7xwKeRzy87A=
github.com/aws/aws-sdk-go-v2/service/amp v1.25.2/go.mod h1:scSghJ8LpkHW0Uge1p2Qpu7zmZ10yIKs0ylLk+XkPas=
github.com/aws/aws-sdk-go-v2/service/apigateway v1.23.4 h1:ftJ/AYiHiPMjKF3mt9TRfCHsrZsVuhxKnF2YJw/DVfw=
github.com/aws/aws-sdk-go-v2/service/apigateway v1.23.4/go.mod h1:gMxPkuoIOoHhgsbQHmZ6CCgvKLbG7a9M71U8t7oOJc4=
github.com/aws/aws-sdk-go-v2/service/appconfig v1.29.0 h1:kFSKQPVszQU+r2++ugfrwCchdRKaFmLhc2yMTdE5lls=
github.com/aws/aws-sdk-go-v2/service/appconfig v1.29.0/go.mod h1:rzi8EH52dX8O8xG+CnwS4nb47IGVRGkg7LJn7DzjUAs=
github.com/aws/aws-sdk-go-v2/service/appfabric v1.7.2 h1:FUjonDjo+7YH51llB+UupHszcr/1HCCrMUX6FbdPpLc=
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.

30 changes: 15 additions & 15 deletions internal/service/apigateway/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ package apigateway
import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway"
"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/apigateway"
awstypes "github.com/aws/aws-sdk-go-v2/service/apigateway/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"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"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)
Expand Down Expand Up @@ -67,34 +67,34 @@ func ResourceAccount() *schema.Resource {

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

input := &apigateway.UpdateAccountInput{}

if v, ok := d.GetOk("cloudwatch_role_arn"); ok {
input.PatchOperations = []*apigateway.PatchOperation{{
Op: aws.String(apigateway.OpReplace),
input.PatchOperations = []awstypes.PatchOperation{{
Op: awstypes.OpReplace,
Path: aws.String("/cloudwatchRoleArn"),
Value: aws.String(v.(string)),
}}
} else {
input.PatchOperations = []*apigateway.PatchOperation{{
Op: aws.String(apigateway.OpReplace),
input.PatchOperations = []awstypes.PatchOperation{{
Op: awstypes.OpReplace,
Path: aws.String("/cloudwatchRoleArn"),
Value: aws.String(""),
}}
}

_, err := tfresource.RetryWhen(ctx, propagationTimeout,
func() (interface{}, error) {
return conn.UpdateAccountWithContext(ctx, input)
return conn.UpdateAccount(ctx, input)
},
func(err error) (bool, error) {
if tfawserr.ErrMessageContains(err, apigateway.ErrCodeBadRequestException, "The role ARN does not have required permissions") {
if errs.IsAErrorMessageContains[*awstypes.BadRequestException](err, "The role ARN does not have required permissions") {
return true, err
}

if tfawserr.ErrMessageContains(err, apigateway.ErrCodeBadRequestException, "API Gateway could not successfully write to CloudWatch Logs using the ARN specified") {
if errs.IsAErrorMessageContains[*awstypes.BadRequestException](err, "API Gateway could not successfully write to CloudWatch Logs using the ARN specified") {
return true, err
}

Expand All @@ -115,17 +115,17 @@ func resourceAccountUpdate(ctx context.Context, d *schema.ResourceData, meta int

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

account, err := conn.GetAccountWithContext(ctx, &apigateway.GetAccountInput{})
account, err := conn.GetAccount(ctx, &apigateway.GetAccountInput{})

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading API Gateway Account: %s", err)
}

d.Set("api_key_version", account.ApiKeyVersion)
d.Set("cloudwatch_role_arn", account.CloudwatchRoleArn)
d.Set("features", flex.FlattenStringSet(account.Features))
d.Set("features", account.Features)
if err := d.Set("throttle_settings", flattenThrottleSettings(account.ThrottleSettings)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting throttle_settings: %s", err)
}
Expand Down
55 changes: 28 additions & 27 deletions internal/service/apigateway/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/apigateway"
"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/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/apigateway"
awstypes "github.com/aws/aws-sdk-go-v2/service/apigateway/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"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -86,12 +87,12 @@ func ResourceAPIKey() *schema.Resource {

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

name := d.Get("name").(string)
input := &apigateway.CreateApiKeyInput{
Description: aws.String(d.Get("description").(string)),
Enabled: aws.Bool(d.Get("enabled").(bool)),
Enabled: d.Get("enabled").(bool),
Name: aws.String(name),
Tags: getTagsIn(ctx),
Value: aws.String(d.Get("value").(string)),
Expand All @@ -101,20 +102,20 @@ func resourceAPIKeyCreate(ctx context.Context, d *schema.ResourceData, meta inte
input.CustomerId = aws.String(v.(string))
}

apiKey, err := conn.CreateApiKeyWithContext(ctx, input)
apiKey, err := conn.CreateApiKey(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating API Gateway API Key (%s): %s", name, err)
}

d.SetId(aws.StringValue(apiKey.Id))
d.SetId(aws.ToString(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).APIGatewayConn(ctx)
conn := meta.(*conns.AWSClient).APIGatewayClient(ctx)

apiKey, err := FindAPIKeyByID(ctx, conn, d.Id())

Expand Down Expand Up @@ -148,40 +149,40 @@ func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, meta interf
return diags
}

func resourceAPIKeyUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
operations := make([]*apigateway.PatchOperation, 0)
func resourceAPIKeyUpdateOperations(d *schema.ResourceData) []awstypes.PatchOperation {
operations := make([]awstypes.PatchOperation, 0)

if d.HasChange("enabled") {
isEnabled := "false"
if d.Get("enabled").(bool) {
isEnabled = "true"
}
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String(apigateway.OpReplace),
operations = append(operations, awstypes.PatchOperation{
Op: awstypes.OpReplace,
Path: aws.String("/enabled"),
Value: aws.String(isEnabled),
})
}

if d.HasChange("description") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String(apigateway.OpReplace),
operations = append(operations, awstypes.PatchOperation{
Op: awstypes.OpReplace,
Path: aws.String("/description"),
Value: aws.String(d.Get("description").(string)),
})
}

if d.HasChange("name") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String(apigateway.OpReplace),
operations = append(operations, awstypes.PatchOperation{
Op: awstypes.OpReplace,
Path: aws.String("/name"),
Value: aws.String(d.Get("name").(string)),
})
}

if d.HasChange("customer_id") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String(apigateway.OpReplace),
operations = append(operations, awstypes.PatchOperation{
Op: awstypes.OpReplace,
Path: aws.String("/customerId"),
Value: aws.String(d.Get("customer_id").(string)),
})
Expand All @@ -192,10 +193,10 @@ func resourceAPIKeyUpdateOperations(d *schema.ResourceData) []*apigateway.PatchO

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

if d.HasChangesExcept("tags", "tags_all") {
_, err := conn.UpdateApiKeyWithContext(ctx, &apigateway.UpdateApiKeyInput{
_, err := conn.UpdateApiKey(ctx, &apigateway.UpdateApiKeyInput{
ApiKey: aws.String(d.Id()),
PatchOperations: resourceAPIKeyUpdateOperations(d),
})
Expand All @@ -210,14 +211,14 @@ 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).APIGatewayConn(ctx)
conn := meta.(*conns.AWSClient).APIGatewayClient(ctx)

log.Printf("[DEBUG] Deleting API Gateway API Key: %s", d.Id())
_, err := conn.DeleteApiKeyWithContext(ctx, &apigateway.DeleteApiKeyInput{
_, err := conn.DeleteApiKey(ctx, &apigateway.DeleteApiKeyInput{
ApiKey: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, apigateway.ErrCodeNotFoundException) {
if errs.IsA[*awstypes.NotFoundException](err) {
return diags
}

Expand All @@ -228,15 +229,15 @@ func resourceAPIKeyDelete(ctx context.Context, d *schema.ResourceData, meta inte
return diags
}

func FindAPIKeyByID(ctx context.Context, conn *apigateway.APIGateway, id string) (*apigateway.ApiKey, error) {
func FindAPIKeyByID(ctx context.Context, conn *apigateway.Client, id string) (*apigateway.GetApiKeyOutput, error) {
input := &apigateway.GetApiKeyInput{
ApiKey: aws.String(id),
IncludeValue: aws.Bool(true),
}

output, err := conn.GetApiKeyWithContext(ctx, input)
output, err := conn.GetApiKey(ctx, input)

if tfawserr.ErrCodeEquals(err, apigateway.ErrCodeNotFoundException) {
if errs.IsA[*awstypes.NotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
Expand Down
10 changes: 5 additions & 5 deletions internal/service/apigateway/api_key_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -61,7 +61,7 @@ func DataSourceAPIKey() *schema.Resource {

func dataSourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).APIGatewayConn(ctx)
conn := meta.(*conns.AWSClient).APIGatewayClient(ctx)
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

id := d.Get("id").(string)
Expand All @@ -71,12 +71,12 @@ func dataSourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, meta inte
return sdkdiag.AppendErrorf(diags, "reading API Gateway API Key (%s): %s", id, err)
}

d.SetId(aws.StringValue(apiKey.Id))
d.Set("created_date", aws.TimeValue(apiKey.CreatedDate).Format(time.RFC3339))
d.SetId(aws.ToString(apiKey.Id))
d.Set("created_date", aws.ToTime(apiKey.CreatedDate).Format(time.RFC3339))
d.Set("customer_id", apiKey.CustomerId)
d.Set("description", apiKey.Description)
d.Set("enabled", apiKey.Enabled)
d.Set("last_updated_date", aws.TimeValue(apiKey.LastUpdatedDate).Format(time.RFC3339))
d.Set("last_updated_date", aws.ToTime(apiKey.LastUpdatedDate).Format(time.RFC3339))
d.Set("name", apiKey.Name)
d.Set("value", apiKey.Value)

Expand Down
Loading

0 comments on commit b9dfeaf

Please sign in to comment.