Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws2-lambda: Add lambda function tag operations test #2749 #3139

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.URI;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -54,6 +55,7 @@
import software.amazon.awssdk.services.lambda.model.ListAliasesRequest;
import software.amazon.awssdk.services.lambda.model.ListAliasesResponse;
import software.amazon.awssdk.services.lambda.model.ListFunctionsResponse;
import software.amazon.awssdk.services.lambda.model.ListTagsResponse;
import software.amazon.awssdk.services.lambda.model.Runtime;
import software.amazon.awssdk.services.lambda.model.UpdateFunctionCodeRequest;
import software.amazon.awssdk.services.lambda.model.UpdateFunctionCodeResponse;
Expand Down Expand Up @@ -101,6 +103,15 @@ public String getFunction(@PathParam("functionName") String functionName) {
.configuration().functionName();
}

@Path("/function/getArn/{functionName}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getFunctionArn(@PathParam("functionName") String functionName) {
return producerTemplate
.requestBody(componentUri(functionName, Lambda2Operations.getFunction), null, GetFunctionResponse.class)
.configuration().functionArn();
}

@Path("/function/update/{functionName}")
@PUT
@Consumes("application/zip")
Expand Down Expand Up @@ -233,6 +244,60 @@ public List<String> listAliases(@QueryParam("functionName") String functionName)
}
}

@Path("/tag/create")
@POST
@Produces(MediaType.TEXT_PLAIN)
public Response tagLambdaFunction(@QueryParam("functionArn") String functionArn,
@QueryParam("tagResourceKey") String tagResourceKey, @QueryParam("tagResourceValue") String tagResourceValue)
throws Exception {
Map<String, String> resourceTags = Map.of(tagResourceKey, tagResourceValue);
final String response = producerTemplate.requestBodyAndHeaders(
componentUri(null, Lambda2Operations.tagResource),
null,
new LinkedHashMap<String, Object>() {
{
put(Lambda2Constants.RESOURCE_ARN, functionArn);
put(Lambda2Constants.RESOURCE_TAGS, resourceTags);
}
},
String.class);
return Response
.created(new URI("https://camel.apache.org/"))
.entity(response)
.build();
}

@Path("/tag/list")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Map<String, String> listLambdaFunctionTags(@QueryParam("functionArn") String functionArn) {
return producerTemplate.requestBodyAndHeaders(
componentUri(null, Lambda2Operations.listTags),
null,
new LinkedHashMap<String, Object>() {
{
put(Lambda2Constants.RESOURCE_ARN, functionArn);
}
},
ListTagsResponse.class)
.tags();
}

@Path("/tag/delete")
@DELETE
public void untagLambdaFunction(@QueryParam("functionArn") String functionArn,
@QueryParam("tagResourceKey") String tagResourceKey) {
producerTemplate.requestBodyAndHeaders(
componentUri(null, Lambda2Operations.untagResource),
null,
new LinkedHashMap<String, Object>() {
{
put(Lambda2Constants.RESOURCE_ARN, functionArn);
put(Lambda2Constants.RESOURCE_TAG_KEYS, List.of(tagResourceKey));
}
});
}

private static String componentUri(String functionName, Lambda2Operations operation) {
return "aws2-lambda:" + functionName + "?operation=" + operation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
import org.junit.jupiter.api.Test;

import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@QuarkusTest
@QuarkusTestResource(Aws2TestResource.class)
Expand Down Expand Up @@ -71,8 +73,17 @@ public void performingOperationsOnLambdaFunctionShouldSucceed() {
}
});

String functionArn = RestAssured.given()
.accept(ContentType.JSON)
.get("/aws2-lambda/function/getArn/" + functionName)
.then()
.statusCode(200)
.extract().asString();
assertNotNull(functionArn);

getUpdateListAndInvokeFunctionShouldSucceed(functionName);
createGetDeleteAndListAliasShouldSucceed(functionName);
createListDeleteFunctionTagsShouldSucceed(functionName, functionArn);

RestAssured.given()
.delete("/aws2-lambda/function/delete/" + functionName)
Expand Down Expand Up @@ -164,6 +175,42 @@ public void createGetDeleteAndListAliasShouldSucceed(String functionName) {
.body("$", not(hasItem(aliasName)));
}

public void createListDeleteFunctionTagsShouldSucceed(String functionName, String functionArn) {

String uuid = java.util.UUID.randomUUID().toString().replace("-", "");
String tagResourceKey = functionName + "-tagKey-" + uuid;
String tagResourceValue = functionName + "-tagValue-" + uuid;

RestAssured.given()
.queryParam("functionArn", functionArn)
.queryParam("tagResourceKey", tagResourceKey)
.queryParam("tagResourceValue", tagResourceValue)
.post("/aws2-lambda/tag/create")
.then()
.statusCode(201);

RestAssured.given()
.queryParam("functionArn", functionArn)
.get("/aws2-lambda/tag/list")
.then()
.statusCode(200)
.body(tagResourceKey, is(tagResourceValue));

RestAssured.given()
.queryParam("functionArn", functionArn)
.queryParam("tagResourceKey", tagResourceKey)
.delete("/aws2-lambda/tag/delete")
.then()
.statusCode(204);

RestAssured.given()
.queryParam("functionArn", functionArn)
.get("/aws2-lambda/tag/list")
.then()
.statusCode(200)
.body(tagResourceKey, is(emptyOrNullString()));
}

static byte[] createInitialLambdaFunctionZip() {
return createLambdaFunctionZip(INITIAL_FUNCTION_SOURCE);
}
Expand Down