48
48
import software .amazon .awssdk .services .apigateway .model .PutMethodResponse ;
49
49
import software .amazon .awssdk .services .apigateway .model .Resource ;
50
50
import software .amazon .awssdk .services .apigateway .model .RestApi ;
51
+ import software .amazon .awssdk .services .apigateway .model .TooManyRequestsException ;
51
52
import software .amazon .awssdk .services .apigateway .model .UpdateApiKeyRequest ;
52
53
import software .amazon .awssdk .services .apigateway .model .UpdateResourceRequest ;
53
54
import software .amazon .awssdk .services .apigateway .model .UpdateRestApiRequest ;
55
+ import software .amazon .awssdk .utils .Lazy ;
54
56
import software .amazon .awssdk .utils .Logger ;
55
57
56
58
public class ServiceIntegrationTest extends IntegrationTestBase {
@@ -61,6 +63,10 @@ public class ServiceIntegrationTest extends IntegrationTestBase {
61
63
62
64
private static final String DESCRIPTION = "fooDesc" ;
63
65
66
+ // Limit deletes to once every 31 seconds
67
+ // https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#api-gateway-control-service-limits-table
68
+ private static final Lazy <RateLimiter > DELETE_RATE_LIMITER = new Lazy <>(() -> RateLimiter .create (1.0 / 31 ));
69
+
64
70
private static String restApiId = null ;
65
71
66
72
@ BeforeClass
@@ -84,7 +90,14 @@ public static void createRestApi() {
84
90
@ AfterClass
85
91
public static void deleteRestApiKey () {
86
92
if (restApiId != null ) {
87
- apiGateway .deleteRestApi (DeleteRestApiRequest .builder ().restApiId (restApiId ).build ());
93
+ DELETE_RATE_LIMITER .getValue ().acquire ();
94
+ try {
95
+ apiGateway .deleteRestApi (DeleteRestApiRequest .builder ().restApiId (restApiId ).build ());
96
+ } catch (TooManyRequestsException e ) {
97
+ log .warn (() -> String .format ("Failed to delete REST API %s (%s). This API should be deleted automatically in a "
98
+ + "future 'deleteStaleRestApis' execution." ,
99
+ NAME , restApiId ), e );
100
+ }
88
101
}
89
102
}
90
103
@@ -95,10 +108,6 @@ private static void deleteStaleRestApis() {
95
108
AtomicInteger success = new AtomicInteger ();
96
109
AtomicInteger failure = new AtomicInteger ();
97
110
98
- // Limit deletes to once every 31 seconds
99
- // https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#api-gateway-control-service-limits-table
100
- RateLimiter rateLimiter = RateLimiter .create (1.0 / 31 );
101
-
102
111
log .info (() -> String .format ("Searching for stale REST APIs older than %s days..." , maxApiAge .toDays ()));
103
112
for (RestApi api : apiGateway .getRestApisPaginator ().items ()) {
104
113
if (Instant .now ().isAfter (startTime .plus (maxRunTime ))) {
@@ -109,7 +118,7 @@ private static void deleteStaleRestApis() {
109
118
}
110
119
Duration apiAge = Duration .between (api .createdDate (), Instant .now ());
111
120
if (api .name ().startsWith (NAME_PREFIX ) && apiAge .compareTo (maxApiAge ) > 0 ) {
112
- rateLimiter .acquire ();
121
+ DELETE_RATE_LIMITER . getValue () .acquire ();
113
122
try {
114
123
apiGateway .deleteRestApi (r -> r .restApiId (api .id ()));
115
124
log .info (() -> String .format ("Successfully deleted REST API %s (%s) which was %s days old." ,
0 commit comments