Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Commit

Permalink
Fixed function ARN extraction (master ARN is not what we are looking …
Browse files Browse the repository at this point in the history
…for).
  • Loading branch information
rafalwrzeszcz committed May 31, 2019
1 parent fbc16eb commit a7bb61e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import com.amazonaws.services.lambda.AWSLambda;
import com.amazonaws.services.lambda.model.CreateFunctionRequest;
import com.amazonaws.services.lambda.model.CreateFunctionResult;
import com.amazonaws.services.lambda.model.DeleteFunctionRequest;
import com.amazonaws.services.lambda.model.FunctionCode;
import com.amazonaws.services.lambda.model.PublishVersionRequest;
Expand Down Expand Up @@ -75,7 +75,7 @@ public LambdaEdgeManager(AWSLambda lambda, AmazonS3 s3, ObjectMapper objectMappe
* @return Data about published version.
*/
public CustomResourceResponse<PublishVersionResult> create(EdgeDeployRequest input, String physicalResourceId) {
this.lambda.createFunction(
CreateFunctionResult result = this.lambda.createFunction(
new CreateFunctionRequest()
.withFunctionName(input.getFunctionName())
.withDescription(input.getFunctionDescription())
Expand All @@ -94,7 +94,10 @@ public CustomResourceResponse<PublishVersionResult> create(EdgeDeployRequest inp
)
);

return this.publishLambdaVersion(input.getFunctionName());
return new CustomResourceResponse<>(
this.publishLambdaVersion(input.getFunctionName()),
result.getFunctionArn()
);
}

/**
Expand Down Expand Up @@ -126,7 +129,10 @@ public CustomResourceResponse<PublishVersionResult> update(EdgeDeployRequest inp
)
);

return this.publishLambdaVersion(input.getFunctionName());
return new CustomResourceResponse<>(
this.publishLambdaVersion(input.getFunctionName()),
physicalResourceId
);
}

/**
Expand Down Expand Up @@ -159,13 +165,11 @@ public CustomResourceResponse<PublishVersionResult> delete(EdgeDeployRequest inp
* @param functionName Function stackSetName.
* @return Published version data.
*/
private CustomResourceResponse<PublishVersionResult> publishLambdaVersion(String functionName) {
PublishVersionResult result = this.lambda.publishVersion(
private PublishVersionResult publishLambdaVersion(String functionName) {
return this.lambda.publishVersion(
new PublishVersionRequest()
.withFunctionName(functionName)
);

return new CustomResourceResponse<>(result, result.getMasterArn());
}

/**
Expand All @@ -181,11 +185,7 @@ private ByteBuffer buildZipFile(EdgeDeployRequest input) {
this.s3.getObject(input.getPackageBucket(), input.getPackageKey()).getObjectContent()
)
) {
// copy entire package content
ZipEntry entry;
while ((entry = archive.getNextEntry()) != null) {
zip.writeEntry(entry.getName(), archive);
}
zip.copyFrom(archive);

// dump custom configuration from request
zip.writeEntry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
Expand Down Expand Up @@ -92,6 +93,20 @@ private void writeEntry(String name, ContentWriter handler) throws IOException {
this.zip.closeEntry();
}

/**
* Copies another archive into current one.
*
* @param archive Source.
* @throws IOException When reading source archive fails.
*/
public void copyFrom(ZipInputStream archive) throws IOException {
// copy entire package content
ZipEntry entry;
while ((entry = archive.getNextEntry()) != null) {
this.writeEntry(entry.getName(), archive);
}
}

/**
* Closes active stream.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.amazonaws.services.lambda.AWSLambda;
import com.amazonaws.services.lambda.model.CreateFunctionRequest;
import com.amazonaws.services.lambda.model.CreateFunctionResult;
import com.amazonaws.services.lambda.model.DeleteFunctionRequest;
import com.amazonaws.services.lambda.model.PublishVersionRequest;
import com.amazonaws.services.lambda.model.PublishVersionResult;
Expand Down Expand Up @@ -70,7 +71,7 @@ public class LambdaEdgeManagerTest {

private static final String PACKAGE_KEY = "maven/release/pl/wrzasq/lambda.zip";

private static final String MASTER_ARN = "arn:aws:lambda:test";
private static final String FUNCTION_ARN = "arn:aws:lambda:test";

private static final String VARIABLE_1_KEY = "id";

Expand Down Expand Up @@ -135,7 +136,13 @@ public void create() throws IOException {
.when(this.lambda.publishVersion(this.publishRequest.capture()))
.thenReturn(
new PublishVersionResult()
.withMasterArn(LambdaEdgeManagerTest.MASTER_ARN)
.withMasterArn(LambdaEdgeManagerTest.FUNCTION_ARN)
);
Mockito
.when(this.lambda.createFunction(this.createRequest.capture()))
.thenReturn(
new CreateFunctionResult()
.withFunctionArn(LambdaEdgeManagerTest.FUNCTION_ARN)
);

CustomResourceResponse<PublishVersionResult> response = manager.create(input, null);
Expand Down Expand Up @@ -265,9 +272,9 @@ public void create() throws IOException {
);

Assertions.assertEquals(
LambdaEdgeManagerTest.MASTER_ARN,
LambdaEdgeManagerTest.FUNCTION_ARN,
response.getPhysicalResourceId(),
"LambdaEdgeManager.create() should set master function ARN as it's physical ID."
"LambdaEdgeManager.create() should set function ARN as it's physical ID."
);
}

Expand Down Expand Up @@ -314,10 +321,13 @@ public void update() throws IOException {
.when(this.lambda.publishVersion(this.publishRequest.capture()))
.thenReturn(
new PublishVersionResult()
.withMasterArn(LambdaEdgeManagerTest.MASTER_ARN)
.withMasterArn(LambdaEdgeManagerTest.FUNCTION_ARN)
);

CustomResourceResponse<PublishVersionResult> response = manager.update(input, null);
CustomResourceResponse<PublishVersionResult> response = manager.update(
input,
LambdaEdgeManagerTest.FUNCTION_ARN
);

Mockito.verify(this.lambda).updateFunctionCode(this.updateCodeRequest.capture());
Mockito.verify(this.lambda).updateFunctionConfiguration(this.updateConfigurationRequest.capture());
Expand Down Expand Up @@ -409,9 +419,9 @@ public void update() throws IOException {
);

Assertions.assertEquals(
LambdaEdgeManagerTest.MASTER_ARN,
LambdaEdgeManagerTest.FUNCTION_ARN,
response.getPhysicalResourceId(),
"LambdaEdgeManager.update() should set master function ARN as it's physical ID."
"LambdaEdgeManager.update() should set function ARN as it's physical ID."
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,30 @@ public void writeEntryFromStream() throws IOException {
"ZipBuilder.writeEntry() should write file content."
);
}

@Test
public void copyFrom() throws IOException {
ZipBuilder zip = new ZipBuilder();
zip.writeEntry("test.txt", new ByteArrayInputStream(new byte[]{'t', 'e', 's', 't'}));
ByteBuffer buffer = zip.dump();

ZipBuilder destination = new ZipBuilder();
destination.copyFrom(new ZipInputStream(new ByteArrayInputStream(buffer.array())));

ZipInputStream stream = new ZipInputStream(new ByteArrayInputStream(buffer.array()));
ZipEntry entry = stream.getNextEntry();

Scanner scanner = new Scanner(stream);

Assertions.assertEquals(
"test.txt",
entry.getName(),
"ZipBuilder.copyFrom() should copy entry filename."
);
Assertions.assertEquals(
"test",
scanner.next(),
"ZipBuilder.copyFrom() should copy file content."
);
}
}

0 comments on commit a7bb61e

Please sign in to comment.