private CreateResourceResult createResource(final CreateRestApiResult createApiResult) { final String rootId = this.awsApiClient .getResources(new GetResourcesRequest().withRestApiId(createApiResult.getId())) .getItems() .stream() .filter(r -> r.getPath().equals("/")) .findAny() .map(r -> r.getId()) .get(); // root better exist final CreateResourceRequest crrq = new CreateResourceRequest().withParentId("") .withParentId(rootId) .withPathPart(Path) .withRestApiId(createApiResult.getId()); return this.awsApiClient.createResource(crrq); }
CreateResourceResult createTopLevelResourceOnApi(String resourceName, String restApiId, AmazonApiGateway client, LambdaLogger logger) { logger.log("Creating top-level resource: " + resourceName); // Short sleep - this avoids the Too Many Requests error in this // custom resource when creating the cloudformation stack. pause(logger); CreateResourceRequest createResourceRequest = new CreateResourceRequest(); createResourceRequest.setRestApiId(restApiId); if (resourceName.equals("bookings")) { createResourceRequest.setPathPart("bookings"); } else if (resourceName.equals("bookingrules")) { createResourceRequest.setPathPart("bookingrules"); } else if (resourceName.equals("validdates")) { createResourceRequest.setPathPart("validdates"); } else if (resourceName.equals("reservationform")) { createResourceRequest.setPathPart("reservationform"); } else if (resourceName.equals("cancellationform")) { createResourceRequest.setPathPart("cancellationform"); } else { throw new InvalidParameterException("Invalid resource name: " + resourceName); } // Get the id of the parent resource GetResourcesRequest getResourcesRequest = new GetResourcesRequest(); // High enough limit for now getResourcesRequest.setLimit(10); getResourcesRequest.setRestApiId(restApiId); GetResourcesResult resourcesResult = client.getResources(getResourcesRequest); String rootResourceId = resourcesResult.getItems().stream() .filter(resource -> resource.getPath().equals("/")).findFirst().get().getId(); logger.log("Parent(root) resource id: " + rootResourceId); createResourceRequest.setParentId(rootResourceId); return client.createResource(createResourceRequest); }