Skip to content

Commit eb02d81

Browse files
authored
Merge pull request #29991 from deepan83/main
b-cachekeyparameters-29910
2 parents ff1ecde + eda2bdf commit eb02d81

File tree

7 files changed

+287
-307
lines changed

7 files changed

+287
-307
lines changed

.changelog/29991.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/aws_api_gateway_integration: Fix bug that cleared unchanged `cache_key_parameters` values on Update
3+
```

internal/service/apigateway/account.go

+51-68
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package apigateway
22

33
import (
44
"context"
5-
"log"
65

76
"github.com/aws/aws-sdk-go/aws"
87
"github.com/aws/aws-sdk-go/service/apigateway"
98
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
109
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1210
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1311
"github.com/hashicorp/terraform-provider-aws/internal/conns"
1412
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
@@ -21,7 +19,8 @@ func ResourceAccount() *schema.Resource {
2119
CreateWithoutTimeout: resourceAccountUpdate,
2220
ReadWithoutTimeout: resourceAccountRead,
2321
UpdateWithoutTimeout: resourceAccountUpdate,
24-
DeleteWithoutTimeout: resourceAccountDelete,
22+
DeleteWithoutTimeout: schema.NoopContext,
23+
2524
Importer: &schema.ResourceImporter{
2625
StateContext: schema.ImportStatePassthroughContext,
2726
},
@@ -51,89 +50,73 @@ func ResourceAccount() *schema.Resource {
5150
}
5251
}
5352

54-
func resourceAccountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
53+
func resourceAccountUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
5554
var diags diag.Diagnostics
5655
conn := meta.(*conns.AWSClient).APIGatewayConn()
5756

58-
account, err := conn.GetAccountWithContext(ctx, &apigateway.GetAccountInput{})
59-
if err != nil {
60-
return sdkdiag.AppendErrorf(diags, "reading API Gateway Account: %s", err)
57+
input := &apigateway.UpdateAccountInput{}
58+
59+
// Unfortunately AWS API doesn't allow empty ARNs,
60+
// even though that's default settings for new AWS accounts
61+
// BadRequestException: The role ARN is not well formed
62+
if v, ok := d.GetOk("cloudwatch_role_arn"); ok {
63+
input.PatchOperations = []*apigateway.PatchOperation{{
64+
Op: aws.String(apigateway.OpReplace),
65+
Path: aws.String("/cloudwatchRoleArn"),
66+
Value: aws.String(v.(string)),
67+
}}
68+
} else {
69+
input.PatchOperations = []*apigateway.PatchOperation{}
6170
}
6271

63-
log.Printf("[DEBUG] Received API Gateway Account: %s", account)
72+
_, err := tfresource.RetryWhen(ctx, propagationTimeout,
73+
func() (interface{}, error) {
74+
return conn.UpdateAccountWithContext(ctx, input)
75+
},
76+
func(err error) (bool, error) {
77+
if tfawserr.ErrMessageContains(err, apigateway.ErrCodeBadRequestException, "The role ARN does not have required permissions") {
78+
return true, err
79+
}
6480

65-
if _, ok := d.GetOk("cloudwatch_role_arn"); ok {
66-
// CloudwatchRoleArn cannot be empty nor made empty via API
67-
// This resource can however be useful w/out defining cloudwatch_role_arn
68-
// (e.g. for referencing throttle_settings)
69-
d.Set("cloudwatch_role_arn", account.CloudwatchRoleArn)
81+
if tfawserr.ErrMessageContains(err, apigateway.ErrCodeBadRequestException, "API Gateway could not successfully write to CloudWatch Logs using the ARN specified") {
82+
return true, err
83+
}
84+
85+
return false, err
86+
},
87+
)
88+
89+
if err != nil {
90+
return sdkdiag.AppendErrorf(diags, "updating API Gateway Account: %s", err)
7091
}
71-
if err := d.Set("throttle_settings", FlattenThrottleSettings(account.ThrottleSettings)); err != nil {
72-
return sdkdiag.AppendErrorf(diags, "reading API Gateway Account: %s", err)
92+
93+
if d.IsNewResource() {
94+
d.SetId("api-gateway-account")
7395
}
7496

75-
return diags
97+
return append(diags, resourceAccountRead(ctx, d, meta)...)
7698
}
7799

78-
func resourceAccountUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
100+
func resourceAccountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
79101
var diags diag.Diagnostics
80102
conn := meta.(*conns.AWSClient).APIGatewayConn()
81103

82-
input := apigateway.UpdateAccountInput{}
83-
operations := make([]*apigateway.PatchOperation, 0)
84-
85-
if d.HasChange("cloudwatch_role_arn") {
86-
arn := d.Get("cloudwatch_role_arn").(string)
87-
if len(arn) > 0 {
88-
// Unfortunately AWS API doesn't allow empty ARNs,
89-
// even though that's default settings for new AWS accounts
90-
// BadRequestException: The role ARN is not well formed
91-
operations = append(operations, &apigateway.PatchOperation{
92-
Op: aws.String("replace"),
93-
Path: aws.String("/cloudwatchRoleArn"),
94-
Value: aws.String(arn),
95-
})
96-
}
97-
}
98-
input.PatchOperations = operations
99-
100-
log.Printf("[INFO] Updating API Gateway Account: %s", input)
101-
102-
// Retry due to eventual consistency of IAM
103-
expectedErrMsg := "The role ARN does not have required permissions"
104-
otherErrMsg := "API Gateway could not successfully write to CloudWatch Logs using the ARN specified"
105-
var out *apigateway.Account
106-
var err error
107-
err = resource.RetryContext(ctx, propagationTimeout, func() *resource.RetryError {
108-
out, err = conn.UpdateAccountWithContext(ctx, &input)
109-
110-
if err != nil {
111-
if tfawserr.ErrMessageContains(err, "BadRequestException", expectedErrMsg) ||
112-
tfawserr.ErrMessageContains(err, "BadRequestException", otherErrMsg) {
113-
log.Printf("[DEBUG] Retrying API Gateway Account update: %s", err)
114-
return resource.RetryableError(err)
115-
}
116-
return resource.NonRetryableError(err)
117-
}
104+
account, err := conn.GetAccountWithContext(ctx, &apigateway.GetAccountInput{})
118105

119-
return nil
120-
})
121-
if tfresource.TimedOut(err) {
122-
out, err = conn.UpdateAccountWithContext(ctx, &input)
123-
}
124106
if err != nil {
125-
return sdkdiag.AppendErrorf(diags, "Updating API Gateway Account failed: %s", err)
107+
return sdkdiag.AppendErrorf(diags, "reading API Gateway Account: %s", err)
126108
}
127-
log.Printf("[DEBUG] API Gateway Account updated: %s", out)
128109

129-
d.SetId("api-gateway-account")
130-
return append(diags, resourceAccountRead(ctx, d, meta)...)
131-
}
132-
133-
func resourceAccountDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
134-
var
135-
// There is no API for "deleting" account or resetting it to "default" settings
136-
diags diag.Diagnostics
110+
if _, ok := d.GetOk("cloudwatch_role_arn"); ok {
111+
// Backwards compatibility:
112+
// CloudwatchRoleArn cannot be empty nor made empty via API
113+
// This resource can however be useful w/out defining cloudwatch_role_arn
114+
// (e.g. for referencing throttle_settings)
115+
d.Set("cloudwatch_role_arn", account.CloudwatchRoleArn)
116+
}
117+
if err := d.Set("throttle_settings", flattenThrottleSettings(account.ThrottleSettings)); err != nil {
118+
return sdkdiag.AppendErrorf(diags, "setting throttle_settings: %s", err)
119+
}
137120

138121
return diags
139122
}

0 commit comments

Comments
 (0)