Skip to content

Commit

Permalink
Merge pull request #12 from BIBSYSDEV/develop
Browse files Browse the repository at this point in the history
preparing first release
  • Loading branch information
svenngjeto authored Apr 7, 2020
2 parents 422f12a + 3a3019a commit e59a9d1
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 199 deletions.
10 changes: 4 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ dependencies {
testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.2.4'
testImplementation group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.19.0'
testImplementation group: 'commons-io', name: 'commons-io', version: '2.6'
}

test {
failFast = true
finalizedBy jacocoTestReport
}

task buildZip(type: Zip) {
Expand All @@ -42,12 +46,6 @@ task buildZip(type: Zip) {
project.tasks.build.dependsOn project.tasks.buildZip



test {
failFast = true
finalizedBy jacocoTestReport
}

// We don't want a jar, just a fatJar
//jar.enabled = false

Expand Down
12 changes: 8 additions & 4 deletions buildspec_publish_to_sar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ phases:
java: openjdk11
commands:
- pip3 install aws-sam-cli
- apt install -q -y jq

build:
commands:
# Use AWS SAM to build and package the application by using AWS CloudFormation
- sam build --debug
- sam package --debug --template-file .aws-sam/build/template.yaml --s3-bucket $S3_BUCKET --output-template-file sampackaged.yaml --force-upload
- sam build
- sam package --template-file .aws-sam/build/template.yaml --s3-bucket $S3_BUCKET --output-template-file sampackaged_raw.yaml
post_build:
commands:
# Getting latest version tag from git
- APPLICATION_VERSION=$(curl -s https://api.github.com/repos/BIBSYSDEV/$GIT_REPO/releases/latest | jq -r '.tag_name')
# Updating metadata.labels in template
- envsubst '${CODEBUILD_RESOLVED_SOURCE_VERSION},${GIT_REPO}' < sampackaged_raw.yaml > sampackaged.yaml
# publishing to SAR
- sam publish --template sampackaged.yaml

- sam publish --semantic-version $APPLICATION_VERSION --template sampackaged.yaml
artifacts:
files:
- sampackaged.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.AbortMultipartUploadRequest;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.google.gson.Gson;
Expand Down Expand Up @@ -36,25 +35,26 @@ public class AbortMultipartUploadHandler implements RequestHandler<Map<String, O

public final transient String bucketName;
private final transient String allowedOrigin;
private static final AmazonS3 s3Client = createAmazonS3Client();
private final transient AmazonS3 s3Client;

public AbortMultipartUploadHandler() {
this(new Environment());
}

public AbortMultipartUploadHandler(Environment environment) {
this(environment, environment.createAmazonS3Client());
}


/**
* Construct for lambda eventhandler to create an upload request for S3.
*/
public AbortMultipartUploadHandler(Environment environment) {
public AbortMultipartUploadHandler(Environment environment, AmazonS3 s3Client) {
this.allowedOrigin = environment.get(ALLOWED_ORIGIN_KEY)
.orElseThrow(() -> new IllegalStateException(String.format(MISSING_ENV_TEXT,ALLOWED_ORIGIN_KEY)));
this.bucketName = environment.get(S3_UPLOAD_BUCKET_KEY)
.orElseThrow(() -> new IllegalStateException(String.format(MISSING_ENV_TEXT,S3_UPLOAD_BUCKET_KEY)));
}

private static AmazonS3 createAmazonS3Client() {
return AmazonS3ClientBuilder.standard()
.build();
this.s3Client = s3Client;
}

public AmazonS3 getS3Client() {
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/no/unit/nva/amazon/s3/CompleteUploadHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest;
import com.amazonaws.services.s3.model.CompleteMultipartUploadResult;
Expand Down Expand Up @@ -42,7 +41,11 @@ public class CompleteUploadHandler implements RequestHandler<Map<String, Object>
private final transient AmazonS3 s3Client;

public CompleteUploadHandler() {
this(new Environment(), createAmazonS3Client());
this(new Environment());
}

public CompleteUploadHandler(Environment environment) {
this(environment, environment.createAmazonS3Client());
}


Expand All @@ -57,19 +60,12 @@ public CompleteUploadHandler(Environment environment, AmazonS3 s3Client) {
this.s3Client = s3Client;
}

public static AmazonS3 createAmazonS3Client() {
return AmazonS3ClientBuilder.standard()
.build();
}


@Override
public GatewayResponse handleRequest(Map<String, Object> input, Context context) {

final GatewayResponse response = new GatewayResponse();
response.setHeaders(headers());


CompleteUploadRequestBody requestBody;
try {
requestBody = checkParameters(input);
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/no/unit/nva/amazon/s3/CreateUploadHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest;
import com.amazonaws.services.s3.model.InitiateMultipartUploadResult;
import com.amazonaws.services.s3.model.ObjectMetadata;
Expand Down Expand Up @@ -33,7 +32,7 @@ public class CreateUploadHandler implements RequestHandler<Map<String, Object>,
public static final String PARAMETER_FILENAME_KEY = "filename";
public static final String PARAMETER_INPUT_KEY = "input";

private static final AmazonS3 s3Client = createAmazonS3Client();
private final transient AmazonS3 s3Client;
public final transient String bucketName;
private final transient String allowedOrigin;

Expand All @@ -42,22 +41,21 @@ public CreateUploadHandler() {
this(new Environment());
}

public CreateUploadHandler(Environment environment) {
this(environment, environment.createAmazonS3Client());
}


/**
* Construct for lambda eventhandler to create an upload request for S3.
*/
public CreateUploadHandler(Environment environment) {
public CreateUploadHandler(Environment environment, AmazonS3 s3Client) {
this.allowedOrigin = environment.get(ALLOWED_ORIGIN_KEY)
.orElseThrow(() -> new IllegalStateException(String.format(MISSING_ENV_TEXT,ALLOWED_ORIGIN_KEY)));
this.bucketName = environment.get(S3_UPLOAD_BUCKET_KEY)
.orElseThrow(() -> new IllegalStateException(String.format(MISSING_ENV_TEXT,S3_UPLOAD_BUCKET_KEY)));
this.s3Client = s3Client;


}

public static AmazonS3 createAmazonS3Client() {
return AmazonS3ClientBuilder.standard()
.build();
}

public AmazonS3 getS3Client() {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/no/unit/nva/amazon/s3/Environment.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package no.unit.nva.amazon.s3;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

import java.util.Optional;

public class Environment {

public static final String DEFAULT_AWS_REGION = Regions.EU_WEST_1.getName();
public static final String AWS_REGION_KEY = "AWS_REGION";
public static final String ALLOWED_ORIGIN_KEY = "ALLOWED_ORIGIN";
public static final String S3_UPLOAD_BUCKET_KEY = "S3_UPLOAD_BUCKET";
public static final String MISSING_ENV_TEXT = "Missing environment variable %s";

/**
* Create a client to access Amazon S3 storage.
* @return client to access S3 storage
*/
public AmazonS3 createAmazonS3Client() {
return AmazonS3ClientBuilder.standard()
.withRegion(get(AWS_REGION_KEY).get())
.build();
}

/**
* Get environment variable.
*
Expand All @@ -19,4 +35,5 @@ public Optional<String> get(String name) {

return Optional.ofNullable(environmentVariable);
}

}
11 changes: 4 additions & 7 deletions src/main/java/no/unit/nva/amazon/s3/ListPartsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.ListPartsRequest;
import com.amazonaws.services.s3.model.PartListing;
Expand Down Expand Up @@ -42,9 +41,12 @@ public class ListPartsHandler implements RequestHandler<Map<String, Object>, Gat
private final transient AmazonS3 s3Client;

public ListPartsHandler() {
this(new Environment(), createAmazonS3Client());
this(new Environment());
}

public ListPartsHandler(Environment environment) {
this(environment, environment.createAmazonS3Client());
}

/**
* Construct for lambda eventhandler to create an upload request for S3.
Expand All @@ -58,11 +60,6 @@ public ListPartsHandler(Environment environment, AmazonS3 s3Client) {
this.s3Client = s3Client;
}

public static AmazonS3 createAmazonS3Client() {
return AmazonS3ClientBuilder.standard()
.build();
}

@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
@Override
public GatewayResponse handleRequest(Map<String, Object> input, Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.google.gson.Gson;
Expand Down Expand Up @@ -43,9 +42,12 @@ public class PrepareUploadPartHandler implements RequestHandler<Map<String, Obje
private final transient AmazonS3 s3Client;

public PrepareUploadPartHandler() {
this(new Environment(), createAmazonS3Client());
this(new Environment());
}

public PrepareUploadPartHandler(Environment environment) {
this(environment, environment.createAmazonS3Client());
}

/**
* Construct for lambda eventhandler to create an upload request for S3.
Expand All @@ -58,11 +60,6 @@ public PrepareUploadPartHandler(Environment environment, AmazonS3 s3Client) {
this.s3Client = s3Client;
}

public static AmazonS3 createAmazonS3Client() {
return AmazonS3ClientBuilder.standard()
.build();
}

@Override
public GatewayResponse handleRequest(Map<String, Object> input, Context context) {

Expand Down
Loading

0 comments on commit e59a9d1

Please sign in to comment.