Skip to content

Commit 242a550

Browse files
aws - rest-account - Retry and Raise Client Errors (cloud-custodian#9862)
Co-authored-by: AJ Kerrigan <aj@stacklet.io>
1 parent dd3edf9 commit 242a550

File tree

13 files changed

+108
-21
lines changed

13 files changed

+108
-21
lines changed

c7n/resources/apigw.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class RestAccount(ResourceManager):
3131

3232
filter_registry = FilterRegistry('rest-account.filters')
3333
action_registry = ActionRegistry('rest-account.actions')
34+
retry = staticmethod(get_retry(('TooManyRequestsException',)))
3435

3536
class resource_type(query.TypeInfo):
3637
service = 'apigateway'
@@ -55,10 +56,11 @@ def get_model(self):
5556
def _get_account(self):
5657
client = utils.local_session(self.session_factory).client('apigateway')
5758
try:
58-
account = client.get_account()
59+
account = self.retry(client.get_account)
5960
except ClientError as e:
6061
if e.response['Error']['Code'] == 'NotFoundException':
6162
return []
63+
raise
6264
account.pop('ResponseMetadata', None)
6365
account['account_id'] = 'apigw-settings'
6466
return [account]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"status_code": 403,
3+
"data": {
4+
"Error": {
5+
"Message": "User: arn:aws:sts::644160558196:assumed-role/custodian-no-access/botocore-session-1733979413 is not authorized to perform: apigateway:GET on resource: arn:aws:apigateway:us-east-1::/account because no identity-based policy allows the apigateway:GET action",
6+
"Code": "AccessDeniedException"
7+
},
8+
"ResponseMetadata": {}
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"status_code": 429,
3+
"data": {
4+
"Error": {
5+
"Message": "Too Many Requests",
6+
"Code": "TooManyRequestsException"
7+
},
8+
"ResponseMetadata": {},
9+
"message": "Too Many Requests"
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"status_code": 200,
3+
"data": {
4+
"ResponseMetadata": {},
5+
"throttleSettings": {
6+
"burstLimit": 5000,
7+
"rateLimit": 10000.0
8+
},
9+
"features": [
10+
"UsagePlans"
11+
],
12+
"apiKeyVersion": "4"
13+
}
14+
}

tests/test_apigw.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright The Cloud Custodian Authors.
22
# SPDX-License-Identifier: Apache-2.0
3+
import time
34
from botocore.exceptions import ClientError
5+
from mock import patch
46

57
from .common import BaseTest, event_data
68
from c7n.exceptions import PolicyValidationError
@@ -75,6 +77,29 @@ def test_rest_api_update(self):
7577
after_account, = p.resource_manager._get_account()
7678
self.assertEqual(after_account["cloudwatchRoleArn"], log_role)
7779

80+
def test_rest_account_exception(self):
81+
session_factory = self.replay_flight_data('test_rest_account_exception')
82+
p = self.load_policy(
83+
{'name': 'rest-account-exception',
84+
'resource': 'aws.rest-account'},
85+
session_factory=session_factory
86+
)
87+
with self.assertRaises(ClientError) as e:
88+
p.run()
89+
self.assertEqual(e.exception.response['Error']['Code'], 'AccessDeniedException')
90+
91+
def test_rest_account_rate_limit(self):
92+
session_factory = self.replay_flight_data('test_rest_account_rate_limit')
93+
p = self.load_policy(
94+
{'name': 'rest-account-rate-limit',
95+
'resource': 'aws.rest-account'},
96+
session_factory=session_factory
97+
)
98+
with patch('c7n.utils.time.sleep', new_callable=time.sleep(0)) as func:
99+
resources = p.run()
100+
self.assertTrue(func.called)
101+
self.assertEqual(len(resources), 1)
102+
78103

79104
class TestRestApi(BaseTest):
80105

tools/c7n_awscc/tests/data/placebo/awscc_log_delete/cloudcontrolapi.DeleteResource_1.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
"ProgressEvent": {
55
"TypeName": "AWS::Logs::LogGroup",
66
"Identifier": "/aws/apigateway/welcome",
7-
"RequestToken": "be14b0b2-2aa8-4b3b-9bd5-9be1df7bcaaa",
7+
"RequestToken": "547cc454-b65a-4f8c-8036-28acf607ced8",
88
"Operation": "DELETE",
99
"OperationStatus": "IN_PROGRESS",
1010
"EventTime": {
1111
"__class__": "datetime",
12-
"year": 2022,
13-
"month": 1,
14-
"day": 17,
15-
"hour": 12,
16-
"minute": 8,
17-
"second": 32,
18-
"microsecond": 514000
12+
"year": 2024,
13+
"month": 12,
14+
"day": 11,
15+
"hour": 23,
16+
"minute": 36,
17+
"second": 57,
18+
"microsecond": 155000
1919
}
2020
},
2121
"ResponseMetadata": {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"status_code": 200,
3+
"data": {
4+
"TypeName": "AWS::Logs::LogGroup",
5+
"ResourceDescription": {
6+
"Identifier": "/aws/apigateway/welcome",
7+
"Properties": "{\"FieldIndexPolicies\":[],\"LogGroupClass\":\"STANDARD\",\"LogGroupName\":\"/aws/apigateway/welcome\",\"Arn\":\"arn:aws:logs:us-east-1:644160558196:log-group:/aws/apigateway/welcome:*\",\"DataProtectionPolicy\":{}}"
8+
},
9+
"ResponseMetadata": {}
10+
}
11+
}

tools/c7n_awscc/tests/data/placebo/awscc_log_delete/cloudcontrolapi.ListResources_1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"ResourceDescriptions": [
66
{
77
"Identifier": "/aws/apigateway/welcome",
8-
"Properties": "{\"RetentionInDays\":30,\"LogGroupName\":\"/aws/apigateway/welcome\",\"Arn\":\"arn:aws:logs:us-east-1:644160558196:log-group:/aws/apigateway/welcome:*\",\"Tags\":[{\"Value\":\"Kapil\",\"Key\":\"Owner\"}]}"
8+
"Properties": "{\"LogGroupClass\":\"STANDARD\",\"LogGroupName\":\"/aws/apigateway/welcome\",\"Arn\":\"arn:aws:logs:us-east-1:644160558196:log-group:/aws/apigateway/welcome:*\"}"
99
}
1010
],
1111
"ResponseMetadata": {}

tools/c7n_awscc/tests/data/placebo/awscc_log_update/cloudcontrolapi.GetResource_1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"TypeName": "AWS::Logs::LogGroup",
55
"ResourceDescription": {
66
"Identifier": "/aws/codebuild/custodian-build-python",
7-
"Properties": "{\"RetentionInDays\":7,\"LogGroupName\":\"/aws/codebuild/custodian-build-python\",\"Arn\":\"arn:aws:logs:us-east-1:644160558196:log-group:/aws/codebuild/custodian-build-python:*\",\"Tags\":[{\"Value\":\"Kapil\",\"Key\":\"Owner\"}]}"
7+
"Properties": "{\"FieldIndexPolicies\":[],\"LogGroupClass\":\"STANDARD\",\"LogGroupName\":\"/aws/codebuild/custodian-build-python\",\"Arn\":\"arn:aws:logs:us-east-1:644160558196:log-group:/aws/codebuild/custodian-build-python:*\",\"DataProtectionPolicy\":{}}"
88
},
99
"ResponseMetadata": {}
1010
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"status_code": 200,
3+
"data": {
4+
"TypeName": "AWS::Logs::LogGroup",
5+
"ResourceDescription": {
6+
"Identifier": "/aws/codebuild/custodian-build-python",
7+
"Properties": "{\"FieldIndexPolicies\":[],\"RetentionInDays\":7,\"LogGroupClass\":\"STANDARD\",\"LogGroupName\":\"/aws/codebuild/custodian-build-python\",\"Arn\":\"arn:aws:logs:us-east-1:644160558196:log-group:/aws/codebuild/custodian-build-python:*\",\"Tags\":[{\"Value\":\"Kapil\",\"Key\":\"Owner\"}],\"DataProtectionPolicy\":{}}"
8+
},
9+
"ResponseMetadata": {}
10+
}
11+
}

0 commit comments

Comments
 (0)