Skip to content

Commit

Permalink
Merge pull request #36445 from mattburgess/apigateway-awssdkv2-migration
Browse files Browse the repository at this point in the history
apigateway: Migrate to AWS SDK v2
  • Loading branch information
ewbankkit committed Apr 5, 2024
2 parents 565eb3a + 0af4208 commit de20e13
Show file tree
Hide file tree
Showing 118 changed files with 3,629 additions and 3,953 deletions.
11 changes: 11 additions & 0 deletions .changelog/36445.txt
@@ -0,0 +1,11 @@
```release-note:enhancement
resource/aws_api_gateway_documentation_part: Add `documentation_part_id` attribute
```

```release-note:enhancement
resource/aws_wafregional_web_acl_association: Add plan-time validation of `resource_arn`
```

```release-note:enhancement
resource/aws_wafregional_web_acl_association: Add configurable timeouts
```
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -18,6 +18,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/acmpca v1.29.4
github.com/aws/aws-sdk-go-v2/service/amp v1.25.4
github.com/aws/aws-sdk-go-v2/service/amplify v1.21.5
github.com/aws/aws-sdk-go-v2/service/apigateway v1.23.6
github.com/aws/aws-sdk-go-v2/service/appconfig v1.29.2
github.com/aws/aws-sdk-go-v2/service/appfabric v1.7.4
github.com/aws/aws-sdk-go-v2/service/appflow v1.41.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -56,6 +56,8 @@ github.com/aws/aws-sdk-go-v2/service/amp v1.25.4 h1:TgkApdPnCVX7RtHMcsswmUYsDnnj
github.com/aws/aws-sdk-go-v2/service/amp v1.25.4/go.mod h1:i5BA2ACkXa8Pzqinz/xEukdVJnMdfQLRcx7ftb5g0pk=
github.com/aws/aws-sdk-go-v2/service/amplify v1.21.5 h1:2r72Kx6V0U6js4KZryjZO+tzxri7dQ5nT0BHPXoWxwg=
github.com/aws/aws-sdk-go-v2/service/amplify v1.21.5/go.mod h1:DBFsAufxSC9f12Z3JCcqlErAAE6KHDGrHBe+KgNV1ZA=
github.com/aws/aws-sdk-go-v2/service/apigateway v1.23.6 h1:YZ4tYuH59Xd5q3bYmDqKXt8fQVJ19WPoq4lKzW1iLMg=
github.com/aws/aws-sdk-go-v2/service/apigateway v1.23.6/go.mod h1:3h9BDpayKgNNrpHZBvL7gCIeikqiE7oBxGGcrzmtLAM=
github.com/aws/aws-sdk-go-v2/service/appconfig v1.29.2 h1:Nm1Pqug23c/Ib+/FgwYpFZiLJyuohWxy0bdCj28SFNE=
github.com/aws/aws-sdk-go-v2/service/appconfig v1.29.2/go.mod h1:Z4uxjsQCQYIZQYOf5js8AN9B5ZCFfwRkEHuiihgjHWs=
github.com/aws/aws-sdk-go-v2/service/appfabric v1.7.4 h1:xQyow1kdXLj4kcU9ja1MyZSFSlb1Ab4ZDYeTO/mSkRE=
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.

57 changes: 40 additions & 17 deletions internal/service/apigateway/account.go
Expand Up @@ -5,21 +5,22 @@ package apigateway

import (
"context"
"log"

"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"
"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"
)

// @SDKResource("aws_api_gateway_account")
func ResourceAccount() *schema.Resource {
// @SDKResource("aws_api_gateway_account", name="Account")
func resourceAccount() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAccountUpdate,
ReadWithoutTimeout: resourceAccountRead,
Expand Down Expand Up @@ -67,34 +68,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 = []types.PatchOperation{{
Op: types.OpReplace,
Path: aws.String("/cloudwatchRoleArn"),
Value: aws.String(v.(string)),
}}
} else {
input.PatchOperations = []*apigateway.PatchOperation{{
Op: aws.String(apigateway.OpReplace),
input.PatchOperations = []types.PatchOperation{{
Op: types.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[*types.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[*types.BadRequestException](err, "API Gateway could not successfully write to CloudWatch Logs using the ARN specified") {
return true, err
}

Expand All @@ -115,20 +116,42 @@ 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 := findAccount(ctx, conn)

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] API Gateway Account (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}

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)
}

return diags
}

func findAccount(ctx context.Context, conn *apigateway.Client) (*apigateway.GetAccountOutput, error) {
input := &apigateway.GetAccountInput{}

output, err := conn.GetAccount(ctx, input)

if err != nil {
return nil, err
}

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

return output, nil
}
4 changes: 2 additions & 2 deletions internal/service/apigateway/account_test.go
Expand Up @@ -13,12 +13,12 @@ import (
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccAPIGatewayAccount_basic(t *testing.T) {
func testAccAccount_basic(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_api_gateway_account.test"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.APIGatewayServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Expand Down
60 changes: 31 additions & 29 deletions internal/service/apigateway/api_key.go
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"
"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 All @@ -27,12 +28,13 @@ import (

// @SDKResource("aws_api_gateway_api_key", name="API Key")
// @Tags(identifierAttribute="arn")
func ResourceAPIKey() *schema.Resource {
func resourceAPIKey() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAPIKeyCreate,
ReadWithoutTimeout: resourceAPIKeyRead,
UpdateWithoutTimeout: resourceAPIKeyUpdate,
DeleteWithoutTimeout: resourceAPIKeyDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand Down Expand Up @@ -86,12 +88,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,22 +103,22 @@ 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())
apiKey, err := findAPIKeyByID(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] API Gateway API Key (%s) not found, removing from state", d.Id())
Expand Down Expand Up @@ -148,40 +150,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) []types.PatchOperation {
operations := make([]types.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, types.PatchOperation{
Op: types.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, types.PatchOperation{
Op: types.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, types.PatchOperation{
Op: types.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, types.PatchOperation{
Op: types.OpReplace,
Path: aws.String("/customerId"),
Value: aws.String(d.Get("customer_id").(string)),
})
Expand All @@ -192,10 +194,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 +212,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[*types.NotFoundException](err) {
return diags
}

Expand All @@ -228,15 +230,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[*types.NotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
Expand Down

0 comments on commit de20e13

Please sign in to comment.