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); }
private void createPostMethod(final CreateRestApiResult createApiResult, final CreateResourceResult crrs) { final PutMethodRequest pmrq = new PutMethodRequest().withHttpMethod("POST") .withRestApiId(createApiResult.getId()) .withResourceId(crrs.getId()) .withAuthorizationType("NONE"); this.awsApiClient.putMethod(pmrq); }
private void integratePostMethod(final CreateRestApiResult createApiResult, final CreateResourceResult crrs, final String iarn) { final PutIntegrationRequest pirq = new PutIntegrationRequest().withType(IntegrationType.AWS) .withRestApiId(createApiResult.getId()) .withResourceId(crrs.getId()) .withHttpMethod("POST") .withIntegrationHttpMethod("POST") .withUri(iarn) .withPassthroughBehavior("WHEN_NO_TEMPLATES") .withRequestTemplates(Collections.emptyMap()); this.awsApiClient.putIntegration(pirq); final PutIntegrationResponseRequest pirsrq = new PutIntegrationResponseRequest() .withRestApiId(createApiResult.getId()) .withResourceId(crrs.getId()) .withHttpMethod("POST") .withStatusCode("200") .withResponseTemplates(Collections.singletonMap("application/json", "")); this.awsApiClient.putIntegrationResponse(pirsrq); final PutMethodResponseRequest pmrsrq = new PutMethodResponseRequest().withRestApiId(createApiResult.getId()) .withResourceId(crrs.getId()) .withHttpMethod("POST") .withStatusCode("200") .withResponseModels(Collections.singletonMap("application/json", "Empty")); this.awsApiClient.putMethodResponse(pmrsrq); }
private void integratePostMethod(final CreateRestApiResult createApiResult, final CreateResourceResult crrs, final String iarn) { final String template = loadTemplate(); final PutIntegrationRequest pirq = new PutIntegrationRequest().withType(IntegrationType.AWS) .withRestApiId(createApiResult.getId()) .withResourceId(crrs.getId()) .withHttpMethod("POST") .withIntegrationHttpMethod("POST") .withUri(iarn) .withPassthroughBehavior("WHEN_NO_TEMPLATES") .withRequestTemplates(Collections.singletonMap("application/x-www-form-urlencoded", template)); this.awsApiClient.putIntegration(pirq); final PutIntegrationResponseRequest pirsrq = new PutIntegrationResponseRequest() .withRestApiId(createApiResult.getId()) .withResourceId(crrs.getId()) .withHttpMethod("POST") .withStatusCode("200") .withResponseTemplates(Collections.singletonMap("application/json", "")); this.awsApiClient.putIntegrationResponse(pirsrq); final PutMethodResponseRequest pmrsrq = new PutMethodResponseRequest().withRestApiId(createApiResult.getId()) .withResourceId(crrs.getId()) .withHttpMethod("POST") .withStatusCode("200") .withResponseModels(Collections.singletonMap("application/json", "Empty")); this.awsApiClient.putMethodResponse(pmrsrq); }
private void createApi(final CreateFunctionResult function) { final CreateRestApiResult createApiResult = createApi(); final CreateResourceResult crrs = createResource(createApiResult); final String iarn = iarn(function); createMethod(createApiResult, crrs, iarn); stageApi(createApiResult); final String accountId = accountId(iarn); permitInvokeLambda(accountId, createApiResult, function); }
private void createMethod(final CreateRestApiResult createApiResult, final CreateResourceResult crrs, final String iarn) { final HashMap<String, Boolean> params = new HashMap<>(); params.put("method.request.querystring.code", true); params.put("method.request.querystring.state", false); final PutMethodRequest pmrq = new PutMethodRequest().withRestApiId(createApiResult.getId()) .withResourceId(crrs.getId()) .withHttpMethod(HttpMethod) .withRequestParameters(params) .withAuthorizationType("NONE"); this.awsApiClient.putMethod(pmrq); final String tplt = "{\"code\":\"$input.params('code')\",\"state\":\"$input.params('state')\"}"; final PutIntegrationRequest pirq = new PutIntegrationRequest().withType(IntegrationType.AWS) .withRestApiId(createApiResult.getId()) .withResourceId(crrs.getId()) .withHttpMethod(HttpMethod) .withIntegrationHttpMethod("POST") .withPassthroughBehavior("NEVER") .withRequestTemplates(Collections.singletonMap("application/json", tplt)) .withUri(iarn); this.awsApiClient.putIntegration(pirq); final PutMethodResponseRequest pmrsrq = new PutMethodResponseRequest().withRestApiId(createApiResult.getId()) .withResourceId(crrs.getId()) .withHttpMethod(HttpMethod) .withStatusCode("302") .withResponseParameters(Collections.singletonMap("method.response.header.Location", true)) .withResponseModels(Collections.emptyMap()); this.awsApiClient.putMethodResponse(pmrsrq); final PutIntegrationResponseRequest pirsrq = new PutIntegrationResponseRequest() .withRestApiId(createApiResult.getId()) .withResourceId(crrs.getId()) .withHttpMethod(HttpMethod) .withStatusCode("302") .withResponseParameters(Collections.singletonMap("method.response.header.Location", "'http://translate.banjocreek.io/thankyou.html'")) .withResponseTemplates(Collections.emptyMap()); this.awsApiClient.putIntegrationResponse(pirsrq); }
private void createApi(final CreateFunctionResult function) { final CreateRestApiResult createApiResult = createApi(); final CreateResourceResult crrs = createResource(createApiResult); createPostMethod(createApiResult, crrs); final String iarn = iarn(function); integratePostMethod(createApiResult, crrs, iarn); stageApi(createApiResult); final String accountId = accountId(iarn); permitInvokeLambda(accountId, createApiResult, function); }