Example usage for com.amazonaws.services.apigateway.model CreateDeploymentResult getId

List of usage examples for com.amazonaws.services.apigateway.model CreateDeploymentResult getId

Introduction

In this page you can find the example usage for com.amazonaws.services.apigateway.model CreateDeploymentResult getId.

Prototype


public String getId() 

Source Link

Document

The identifier for the deployment resource.

Usage

From source file:squash.deployment.lambdas.ApiGatewayCustomResourceLambda.java

License:Apache License

void constructApiAndUploadSdk(String restApiId, AmazonApiGateway apiGatewayClient, String region,
        String validDatesGETLambdaURI, String bookingsGETLambdaURI, String bookingsPUTDELETELambdaURI,
        String bookingRulesGETLambdaURI, String bookingRuleOrExclusionPUTDELETELambdaURI,
        String bookingsApiGatewayInvocationRole, String stageName, LambdaLogger logger) throws Exception {
    // Create the API's resources
    logger.log("Creating API resources");
    String validDates = createTopLevelResourceOnApi("validdates", restApiId, apiGatewayClient, logger).getId();
    String bookings = createTopLevelResourceOnApi("bookings", restApiId, apiGatewayClient, logger).getId();
    String bookingRules = createTopLevelResourceOnApi("bookingrules", restApiId, apiGatewayClient, logger)
            .getId();/*from www.  j av a2 s. c  o  m*/
    String reservationForm = createTopLevelResourceOnApi("reservationform", restApiId, apiGatewayClient, logger)
            .getId();
    String cancellationForm = createTopLevelResourceOnApi("cancellationform", restApiId, apiGatewayClient,
            logger).getId();

    // Create the API's methods
    logger.log("Creating API methods");
    Map<String, String> extraParameters = new HashMap<>();

    String revvingSuffix = System.getenv("RevvingSuffix");

    // Methods on the validdates resource
    logger.log("Creating methods on validdates resource");
    extraParameters.put("ValidDatesGETLambdaURI", validDatesGETLambdaURI);
    extraParameters.put("BookingsGETLambdaURI", bookingsGETLambdaURI);
    extraParameters.put("BookingsPUTDELETELambdaURI", bookingsPUTDELETELambdaURI);
    extraParameters.put("BookingRulesGETLambdaURI", bookingRulesGETLambdaURI);
    extraParameters.put("BookingRulesPUTDELETELambdaURI", bookingRuleOrExclusionPUTDELETELambdaURI);
    extraParameters.put("BookingsApiGatewayInvocationRole", bookingsApiGatewayInvocationRole);
    createMethodOnResource("ValidDatesGET", validDates, restApiId, extraParameters, apiGatewayClient,
            revvingSuffix, region, logger);
    createMethodOnResource("ValidDatesOPTIONS", validDates, restApiId, extraParameters, apiGatewayClient,
            revvingSuffix, region, logger);

    // Methods on the bookings resource
    logger.log("Creating methods on bookings resource");
    createMethodOnResource("BookingsGET", bookings, restApiId, extraParameters, apiGatewayClient, revvingSuffix,
            region, logger);
    createMethodOnResource("BookingsDELETE", bookings, restApiId, extraParameters, apiGatewayClient,
            revvingSuffix, region, logger);
    createMethodOnResource("BookingsPUT", bookings, restApiId, extraParameters, apiGatewayClient, revvingSuffix,
            region, logger);
    createMethodOnResource("BookingsPOST", bookings, restApiId, extraParameters, apiGatewayClient,
            revvingSuffix, region, logger);
    createMethodOnResource("BookingsOPTIONS", bookings, restApiId, extraParameters, apiGatewayClient,
            revvingSuffix, region, logger);

    // Methods on the bookingrules resource
    logger.log("Creating methods on bookingrules resource");
    createMethodOnResource("BookingrulesGET", bookingRules, restApiId, extraParameters, apiGatewayClient,
            revvingSuffix, region, logger);
    createMethodOnResource("BookingrulesDELETE", bookingRules, restApiId, extraParameters, apiGatewayClient,
            revvingSuffix, region, logger);
    createMethodOnResource("BookingrulesPUT", bookingRules, restApiId, extraParameters, apiGatewayClient,
            revvingSuffix, region, logger);
    createMethodOnResource("BookingrulesOPTIONS", bookingRules, restApiId, extraParameters, apiGatewayClient,
            revvingSuffix, region, logger);

    // Methods on the reservationform resource
    logger.log("Creating methods on reservationform resource");
    createMethodOnResource("ReservationformGET", reservationForm, restApiId, extraParameters, apiGatewayClient,
            revvingSuffix, region, logger);
    createMethodOnResource("ReservationformOPTIONS", reservationForm, restApiId, extraParameters,
            apiGatewayClient, revvingSuffix, region, logger);

    // Methods on the cancellationform resource
    logger.log("Creating methods on cancellationform resource");
    createMethodOnResource("CancellationformGET", cancellationForm, restApiId, extraParameters,
            apiGatewayClient, revvingSuffix, region, logger);
    createMethodOnResource("CancellationformOPTIONS", cancellationForm, restApiId, extraParameters,
            apiGatewayClient, revvingSuffix, region, logger);

    // Deploy the api to a stage (with default throttling settings)
    logger.log("Deploying API to stage: " + stageName);
    CreateDeploymentRequest createDeploymentRequest = new CreateDeploymentRequest();
    createDeploymentRequest.setCacheClusterEnabled(false);
    createDeploymentRequest.setDescription("A deployment of the Squash api");
    createDeploymentRequest.setStageDescription("A stage for the Squash api");
    createDeploymentRequest.setStageName(stageName);
    createDeploymentRequest.setRestApiId(restApiId);
    CreateDeploymentResult createDeploymentResult = apiGatewayClient.createDeployment(createDeploymentRequest);
    logger.log("Deployed to stage with ID: " + createDeploymentResult.getId());

    // FIXME
    // Throttle all methods on this stage - does not seem to work yet?
    // logger.log("Throttling all of stage's methods");
    // GetStagesRequest getStagesRequest = new GetStagesRequest();
    // getStagesRequest.setRestApiId(restApiId);
    // GetStagesResult getStagesResult =
    // apiGatewayClient.getStages(getStagesRequest);
    // List<Stage> stages = getStagesResult.getItem();
    // Stage stage = stages.stream().filter(s ->
    // s.getStageName().equals(stageName)).findFirst().get();
    // MethodSetting methodSetting = new MethodSetting();
    // methodSetting.setThrottlingBurstLimit(10);
    // methodSetting.setThrottlingRateLimit(1.0);
    // stage.addMethodSettingsEntry("*/*", methodSetting); // Adds to all
    // methods
    // logger.log("Throttling completed");

    // Download javascript sdk and upload it to the S3 bucket serving the
    // squash site
    logger.log("Downloading Javascript SDK");
    GetSdkRequest getSdkRequest = new GetSdkRequest();
    getSdkRequest.setRestApiId(restApiId);
    getSdkRequest.setStageName(stageName);
    getSdkRequest.setSdkType("JavaScript");
    // This is for Android sdks but it crashes if the map is empty - so set
    // to something
    Map<String, String> paramsMap = new HashMap<>();
    paramsMap.put("GroupID", "Dummy");
    getSdkRequest.setParameters(paramsMap);
    GetSdkResult getSdkResult = apiGatewayClient.getSdk(getSdkRequest);

    // Copy the sdk to S3 via AWS lambda's temporary file system
    logger.log("Copying Javascript SDK to S3");
    try {
        logger.log("Saving SDK to lambda's temporary file system");
        ByteBuffer sdkBuffer = getSdkResult.getBody().asReadOnlyBuffer();
        try (FileOutputStream fileOutputStream = new FileOutputStream("/tmp/sdk.zip")) {
            try (WritableByteChannel channel = Channels.newChannel(fileOutputStream)) {
                channel.write(sdkBuffer);
            }
        }
        // Unzip the sdk
        logger.log("SDK saved. Now unzipping");
        String outputFolder = "/tmp/extractedSdk";
        ZipFile zipFile = new ZipFile("/tmp/sdk.zip");
        try {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                logger.log("Unzipping next entry: " + entry.getName());
                File entryDestination = new File(outputFolder, entry.getName());
                if (entry.isDirectory()) {
                    entryDestination.mkdirs();
                } else {
                    entryDestination.getParentFile().mkdirs();
                    InputStream in = zipFile.getInputStream(entry);
                    OutputStream out = new FileOutputStream(entryDestination);
                    IOUtils.copy(in, out);
                    IOUtils.closeQuietly(in);
                    out.close();
                }
            }
        } finally {
            zipFile.close();
        }
        logger.log("SDK unzipped.");

        // GZIP all the sdk files individually
        logger.log("Gzip-ing sdk files to enable serving gzip-ed from S3");
        FileUtils.gzip(Arrays.asList(new File(outputFolder)), Collections.emptyList(), logger);
        logger.log("Gzip-ed sdk files to enable serving gzip-ed from S3");

        // Rev the files by appending revving-suffix to names - for cache-ing
        File sdkFolder = new File("/tmp/extractedSdk/apiGateway-js-sdk");
        FileUtils.appendRevvingSuffix(revvingSuffix, sdkFolder.toPath(), logger);

        // Upload the sdk from the temporary filesystem to S3.
        logger.log("Uploading unzipped Javascript SDK to S3 bucket: " + squashWebsiteBucket);
        TransferUtils.waitForS3Transfer(TransferManagerBuilder.defaultTransferManager()
                .uploadDirectory(squashWebsiteBucket, "", sdkFolder, true), logger);
        logger.log("Uploaded sdk successfully to S3");

        // Add gzip content-encoding metadata to zip-ed files
        logger.log("Updating gzip metadata on Javascript SDK in S3 bucket");
        TransferUtils.addGzipContentEncodingMetadata(squashWebsiteBucket, Optional.empty(), logger);
        logger.log("Updated gzip metadata on Javascript SDK in S3 bucket");

        // Add cache-control metadata to zip-ed files. js files will have
        // 1-year cache validity, since they are rev-ved.
        logger.log("Updating cache-control metadata on Javascript SDK in S3 bucket");
        TransferUtils.addCacheControlHeader("max-age=31536000", squashWebsiteBucket, Optional.empty(), ".js",
                logger);
        logger.log("Updated cache-control metadata on Javascript SDK in S3 bucket");

        logger.log("Setting public read permission on uploaded sdk");
        TransferUtils.setPublicReadPermissionsOnBucket(squashWebsiteBucket, Optional.empty(), logger);
        logger.log("Finished setting public read permissions on uploaded sdk");
    } catch (Exception e) {
        logger.log("Exception caught whilst copying Javascript SDK to S3: " + e.getMessage());
        throw e;
    }
}