@@ -2,13 +2,11 @@ package apigateway
2
2
3
3
import (
4
4
"context"
5
- "log"
6
5
7
6
"github.com/aws/aws-sdk-go/aws"
8
7
"github.com/aws/aws-sdk-go/service/apigateway"
9
8
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
10
9
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13
11
"github.com/hashicorp/terraform-provider-aws/internal/conns"
14
12
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
@@ -21,7 +19,8 @@ func ResourceAccount() *schema.Resource {
21
19
CreateWithoutTimeout : resourceAccountUpdate ,
22
20
ReadWithoutTimeout : resourceAccountRead ,
23
21
UpdateWithoutTimeout : resourceAccountUpdate ,
24
- DeleteWithoutTimeout : resourceAccountDelete ,
22
+ DeleteWithoutTimeout : schema .NoopContext ,
23
+
25
24
Importer : & schema.ResourceImporter {
26
25
StateContext : schema .ImportStatePassthroughContext ,
27
26
},
@@ -51,89 +50,73 @@ func ResourceAccount() *schema.Resource {
51
50
}
52
51
}
53
52
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 {
55
54
var diags diag.Diagnostics
56
55
conn := meta .(* conns.AWSClient ).APIGatewayConn ()
57
56
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 {}
61
70
}
62
71
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
+ }
64
80
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 )
70
91
}
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" )
73
95
}
74
96
75
- return diags
97
+ return append ( diags , resourceAccountRead ( ctx , d , meta ) ... )
76
98
}
77
99
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 {
79
101
var diags diag.Diagnostics
80
102
conn := meta .(* conns.AWSClient ).APIGatewayConn ()
81
103
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 {})
118
105
119
- return nil
120
- })
121
- if tfresource .TimedOut (err ) {
122
- out , err = conn .UpdateAccountWithContext (ctx , & input )
123
- }
124
106
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 )
126
108
}
127
- log .Printf ("[DEBUG] API Gateway Account updated: %s" , out )
128
109
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
+ }
137
120
138
121
return diags
139
122
}
0 commit comments