Skip to content

Make API Gateway integ test's deleteRestApi more reliable & best-effort #3083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@
import software.amazon.awssdk.services.apigateway.model.PutMethodResponse;
import software.amazon.awssdk.services.apigateway.model.Resource;
import software.amazon.awssdk.services.apigateway.model.RestApi;
import software.amazon.awssdk.services.apigateway.model.TooManyRequestsException;
import software.amazon.awssdk.services.apigateway.model.UpdateApiKeyRequest;
import software.amazon.awssdk.services.apigateway.model.UpdateResourceRequest;
import software.amazon.awssdk.services.apigateway.model.UpdateRestApiRequest;
import software.amazon.awssdk.utils.Lazy;
import software.amazon.awssdk.utils.Logger;

public class ServiceIntegrationTest extends IntegrationTestBase {
Expand All @@ -61,6 +63,10 @@ public class ServiceIntegrationTest extends IntegrationTestBase {

private static final String DESCRIPTION = "fooDesc";

// Limit deletes to once every 31 seconds
// https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#api-gateway-control-service-limits-table
private static final Lazy<RateLimiter> DELETE_RATE_LIMITER = new Lazy<>(() -> RateLimiter.create(1.0 / 31));

private static String restApiId = null;

@BeforeClass
Expand All @@ -84,7 +90,14 @@ public static void createRestApi() {
@AfterClass
public static void deleteRestApiKey() {
if (restApiId != null) {
apiGateway.deleteRestApi(DeleteRestApiRequest.builder().restApiId(restApiId).build());
DELETE_RATE_LIMITER.getValue().acquire();
try {
apiGateway.deleteRestApi(DeleteRestApiRequest.builder().restApiId(restApiId).build());
} catch (TooManyRequestsException e) {
log.warn(() -> String.format("Failed to delete REST API %s (%s). This API should be deleted automatically in a "
+ "future 'deleteStaleRestApis' execution.",
NAME, restApiId), e);
}
}
}

Expand All @@ -95,10 +108,6 @@ private static void deleteStaleRestApis() {
AtomicInteger success = new AtomicInteger();
AtomicInteger failure = new AtomicInteger();

// Limit deletes to once every 31 seconds
// https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#api-gateway-control-service-limits-table
RateLimiter rateLimiter = RateLimiter.create(1.0 / 31);

log.info(() -> String.format("Searching for stale REST APIs older than %s days...", maxApiAge.toDays()));
for (RestApi api : apiGateway.getRestApisPaginator().items()) {
if (Instant.now().isAfter(startTime.plus(maxRunTime))) {
Expand All @@ -109,7 +118,7 @@ private static void deleteStaleRestApis() {
}
Duration apiAge = Duration.between(api.createdDate(), Instant.now());
if (api.name().startsWith(NAME_PREFIX) && apiAge.compareTo(maxApiAge) > 0) {
rateLimiter.acquire();
DELETE_RATE_LIMITER.getValue().acquire();
try {
apiGateway.deleteRestApi(r -> r.restApiId(api.id()));
log.info(() -> String.format("Successfully deleted REST API %s (%s) which was %s days old.",
Expand Down