mirror of
https://github.com/floci-io/floci.git
synced 2026-12-05 11:07:59 -05:00
feat(cloudformation): provision the AppSync resource types (#3424)
Adds AppSyncCfnProvisioner, serving AWS::AppSync::GraphQLApi, GraphQLSchema, DataSource, FunctionConfiguration, Resolver and ApiKey. Without it a template's AppSync resources fall to the dispatcher's stub arm, which reports CREATE_COMPLETE while creating nothing and resolves Fn::GetAtt to the literal "LogicalId.Attr". Two things are not simple property mapping. The AppSync control plane is REST-JSON with camelCase members, so every property is lower-camelised on the way in, with DynamoDBConfig as the one member that does not follow the rule (it is dynamodbConfig). And schema creation is asynchronous: while it is PROCESSING every other call for that API is refused with a 409, so the GraphQLSchema resource waits for a terminal status before completing, as CloudFormation does. Also carries the service-layer changes the provisioner needs: a PIPELINE resolver reaches its data through pipelineConfig and names no data source, which createResolver refused outright; CreateFunction and UpdateFunction now keep the runtime a JS resolver declares; and UpdateFunction applies a new name, which a CloudFormation rename goes through.
This commit is contained in:
1 parent
10becc593f
commit
55ada9b8a3
11 files changed
+1602
-17
No files matched your search
@@ -50,6 +50,23 @@ class AppSyncServiceTest {
|
||||
service = newService(Clock.fixed(NOW, ZoneOffset.UTC));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateFunctionAppliesANewName() {
|
||||
GraphqlApi api = service.createGraphqlApi(
|
||||
Map.of("name", "rename", "authenticationType", "API_KEY"), "us-east-1");
|
||||
service.createDataSource(api.getApiId(), Map.of("name", "ds", "type", "NONE"), "us-east-1");
|
||||
var created = service.createFunction(api.getApiId(),
|
||||
Map.of("name", "Query_getMessages_0", "dataSourceName", "ds"), "us-east-1");
|
||||
|
||||
service.updateFunction(api.getApiId(), created.getFunctionId(),
|
||||
Map.of("name", "Query_getMessages_1"));
|
||||
|
||||
// UpdateFunction takes a new name on AWS, and a CloudFormation rename goes through it:
|
||||
// ignoring the member let a stack complete while GetFunction still reported the old name.
|
||||
assertEquals("Query_getMessages_1",
|
||||
service.getFunction(api.getApiId(), created.getFunctionId()).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateApiKeyLooksUpByValue() {
|
||||
GraphqlApi api = service.createGraphqlApi(Map.of("name", "a", "authenticationType", "API_KEY"), "us-east-1");
|
||||
|
||||
+279
@@ -0,0 +1,279 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Deploys the AppSync shape a Serverless Framework {@code appSync:} block compiles to: an API with
|
||||
* inline schema, a DynamoDB and a relational data source, an {@code APPSYNC_JS} pipeline function
|
||||
* and the PIPELINE resolver that calls it, plus an API key, and asserts the control plane really
|
||||
* holds them.
|
||||
*
|
||||
* <p>The point of going through a stack rather than the provisioner alone is the ordering: the data
|
||||
* sources and resolvers are created behind the schema, whose compilation is asynchronous and locks
|
||||
* the API while it runs. It also pins the two {@code Fn::GetAtt} attributes such a template is
|
||||
* built on, {@code GraphQlApi.ApiId} and {@code FunctionConfiguration.FunctionId}: an unset
|
||||
* attribute resolves to the literal string {@code "LogicalId.Attr"} and every dependent resource
|
||||
* would then be wired to that text instead of an id.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class AppSyncCfnIntegrationTest {
|
||||
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260905/us-east-1/cloudformation/aws4_request";
|
||||
private static final String APPSYNC_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260905/us-east-1/appsync/aws4_request";
|
||||
private static final String STACK = "appsync-cfn-it";
|
||||
|
||||
private static final String TEMPLATE = """
|
||||
{
|
||||
"Parameters": {"FieldLogLevel": {"Type": "String"}},
|
||||
"Resources": {
|
||||
"GraphQlApi": {
|
||||
"Type": "AWS::AppSync::GraphQLApi",
|
||||
"Properties": {
|
||||
"Name": "appsync-cfn-it-api",
|
||||
"AuthenticationType": "API_KEY",
|
||||
"XrayEnabled": false,
|
||||
"EnvironmentVariables": {"STRIPE_PRODUCT_SMALL_ID": "prod_small"},
|
||||
"LogConfig": {"FieldLogLevel": {"Ref": "FieldLogLevel"}}
|
||||
}
|
||||
},
|
||||
"GraphQlSchema": {
|
||||
"Type": "AWS::AppSync::GraphQLSchema",
|
||||
"Properties": {
|
||||
"ApiId": {"Fn::GetAtt": ["GraphQlApi", "ApiId"]},
|
||||
"Definition": "type Message { id: ID! } type Query { getMessages: [Message] }"
|
||||
}
|
||||
},
|
||||
"GraphQlDsAuthKeys": {
|
||||
"Type": "AWS::AppSync::DataSource",
|
||||
"DependsOn": "GraphQlSchema",
|
||||
"Properties": {
|
||||
"ApiId": {"Fn::GetAtt": ["GraphQlApi", "ApiId"]},
|
||||
"Name": "publicApiAuthKeysTable",
|
||||
"Type": "AMAZON_DYNAMODB",
|
||||
"DynamoDBConfig": {
|
||||
"AwsRegion": {"Ref": "AWS::Region"},
|
||||
"TableName": "appsync-cfn-it-auth-keys",
|
||||
"UseCallerCredentials": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"GraphQlDsAccountDb": {
|
||||
"Type": "AWS::AppSync::DataSource",
|
||||
"DependsOn": "GraphQlSchema",
|
||||
"Properties": {
|
||||
"ApiId": {"Fn::GetAtt": ["GraphQlApi", "ApiId"]},
|
||||
"Name": "accountDB",
|
||||
"Type": "RELATIONAL_DATABASE",
|
||||
"RelationalDatabaseConfig": {
|
||||
"RelationalDatabaseSourceType": "RDS_HTTP_ENDPOINT",
|
||||
"RdsHttpEndpointConfig": {
|
||||
"AwsRegion": {"Ref": "AWS::Region"},
|
||||
"DbClusterIdentifier": "appsync-cfn-it-cluster",
|
||||
"DatabaseName": "accountdb",
|
||||
"AwsSecretStoreArn": "arn:aws:secretsmanager:us-east-1:000000000000:secret:db"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"GraphQlFnGetMessages": {
|
||||
"Type": "AWS::AppSync::FunctionConfiguration",
|
||||
"Properties": {
|
||||
"ApiId": {"Fn::GetAtt": ["GraphQlApi", "ApiId"]},
|
||||
"Name": "Query_getMessages_0",
|
||||
"DataSourceName": {"Fn::GetAtt": ["GraphQlDsAccountDb", "Name"]},
|
||||
"FunctionVersion": "2018-05-29",
|
||||
"Runtime": {"Name": "APPSYNC_JS", "RuntimeVersion": "1.0.0"},
|
||||
"Code": "export function request(ctx) { return {}; }"
|
||||
}
|
||||
},
|
||||
"GraphQlResolverQuerygetMessages": {
|
||||
"Type": "AWS::AppSync::Resolver",
|
||||
"Properties": {
|
||||
"ApiId": {"Fn::GetAtt": ["GraphQlApi", "ApiId"]},
|
||||
"TypeName": "Query",
|
||||
"FieldName": "getMessages",
|
||||
"Kind": "PIPELINE",
|
||||
"Runtime": {"Name": "APPSYNC_JS", "RuntimeVersion": "1.0.0"},
|
||||
"Code": "export function response(ctx) { return ctx.prev.result; }",
|
||||
"PipelineConfig": {
|
||||
"Functions": [{"Fn::GetAtt": ["GraphQlFnGetMessages", "FunctionId"]}]
|
||||
}
|
||||
}
|
||||
},
|
||||
"GraphQlApiKey": {
|
||||
"Type": "AWS::AppSync::ApiKey",
|
||||
"Properties": {
|
||||
"ApiId": {"Fn::GetAtt": ["GraphQlApi", "ApiId"]},
|
||||
"Description": "integration"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"ApiId": {"Value": {"Fn::GetAtt": ["GraphQlApi", "ApiId"]}},
|
||||
"ApiRef": {"Value": {"Ref": "GraphQlApi"}},
|
||||
"GraphQLUrl": {"Value": {"Fn::GetAtt": ["GraphQlApi", "GraphQLUrl"]}},
|
||||
"DataSourceName": {"Value": {"Fn::GetAtt": ["GraphQlDsAccountDb", "Name"]}},
|
||||
"FunctionId": {"Value": {"Fn::GetAtt": ["GraphQlFnGetMessages", "FunctionId"]}},
|
||||
"ApiKey": {"Value": {"Fn::GetAtt": ["GraphQlApiKey", "ApiKey"]}}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
@Test
|
||||
void createUpdateAndDeleteAnAppSyncApi() throws InterruptedException {
|
||||
cloudFormation("CreateStack", Map.of("FieldLogLevel", "ERROR"));
|
||||
String created = describeStacks("CREATE_COMPLETE");
|
||||
|
||||
String apiId = outputValue(created, "ApiId");
|
||||
String functionId = outputValue(created, "FunctionId");
|
||||
String apiKey = outputValue(created, "ApiKey");
|
||||
|
||||
// Every dependent resource is wired through these; the literal "LogicalId.Attr" is what an
|
||||
// unset attribute yields, so a real id is the assertion that matters.
|
||||
assertNotEquals("GraphQlApi.ApiId", apiId);
|
||||
assertNotEquals("GraphQlFnGetMessages.FunctionId", functionId);
|
||||
assertEquals(apiId, outputValue(created, "ApiRef"));
|
||||
assertEquals("accountDB", outputValue(created, "DataSourceName"));
|
||||
assertTrue(outputValue(created, "GraphQLUrl").contains(apiId),
|
||||
"GraphQLUrl should address the api: " + outputValue(created, "GraphQLUrl"));
|
||||
|
||||
appSync("/v1/apis/" + apiId)
|
||||
.statusCode(200)
|
||||
.body("graphqlApi.name", equalTo("appsync-cfn-it-api"))
|
||||
.body("graphqlApi.authenticationType", equalTo("API_KEY"))
|
||||
.body("graphqlApi.logConfig.fieldLogLevel", equalTo("ERROR"));
|
||||
|
||||
// The schema resource waits for compilation, so by the time the stack completes the SDL is
|
||||
// queryable rather than still PROCESSING.
|
||||
appSync("/v1/apis/" + apiId + "/schemacreation").statusCode(200).body("status", equalTo("SUCCESS"));
|
||||
|
||||
// Free-form map: the variable names are data and must not be camelised into oblivion.
|
||||
appSync("/v1/apis/" + apiId + "/environmentVariables")
|
||||
.statusCode(200)
|
||||
.body("environmentVariables.STRIPE_PRODUCT_SMALL_ID", equalTo("prod_small"));
|
||||
|
||||
appSync("/v1/apis/" + apiId + "/datasources/publicApiAuthKeysTable")
|
||||
.statusCode(200)
|
||||
.body("dataSource.type", equalTo("AMAZON_DYNAMODB"))
|
||||
// dynamodbConfig, not dynamoDBConfig: the one member AWS does not lower-camelise.
|
||||
.body("dataSource.dynamodbConfig.tableName", equalTo("appsync-cfn-it-auth-keys"));
|
||||
|
||||
appSync("/v1/apis/" + apiId + "/datasources/accountDB")
|
||||
.statusCode(200)
|
||||
.body("dataSource.type", equalTo("RELATIONAL_DATABASE"))
|
||||
.body("dataSource.relationalDatabaseConfig.rdsHttpEndpointConfig.dbClusterIdentifier",
|
||||
equalTo("appsync-cfn-it-cluster"))
|
||||
.body("dataSource.relationalDatabaseConfig.rdsHttpEndpointConfig.databaseName",
|
||||
equalTo("accountdb"));
|
||||
|
||||
appSync("/v1/apis/" + apiId + "/functions/" + functionId)
|
||||
.statusCode(200)
|
||||
.body("functionConfiguration.name", equalTo("Query_getMessages_0"))
|
||||
.body("functionConfiguration.dataSourceName", equalTo("accountDB"))
|
||||
.body("functionConfiguration.runtime.name", equalTo("APPSYNC_JS"));
|
||||
|
||||
// A PIPELINE resolver names no data source and reaches its data through the function above.
|
||||
appSync("/v1/apis/" + apiId + "/types/Query/resolvers/getMessages")
|
||||
.statusCode(200)
|
||||
.body("resolver.kind", equalTo("PIPELINE"))
|
||||
.body("resolver.pipelineConfig.functions[0]", equalTo(functionId))
|
||||
.body("resolver.runtime.name", equalTo("APPSYNC_JS"))
|
||||
.body("resolver.code", notNullValue());
|
||||
|
||||
assertNotEquals("GraphQlApiKey.ApiKey", apiKey);
|
||||
appSync("/v1/apis/" + apiId + "/apikeys")
|
||||
.statusCode(200)
|
||||
.body("apiKeys[0].id", equalTo(apiKey))
|
||||
.body("apiKeys[0].description", equalTo("integration"));
|
||||
|
||||
cloudFormation("UpdateStack", Map.of("FieldLogLevel", "ALL"));
|
||||
String updated = describeStacks("UPDATE_COMPLETE");
|
||||
|
||||
// Nothing is replaced by a second deploy: the same API, data source and function, updated
|
||||
// in place. Re-creating instead would answer 400 on the duplicate name.
|
||||
assertEquals(apiId, outputValue(updated, "ApiId"));
|
||||
assertEquals(functionId, outputValue(updated, "FunctionId"));
|
||||
appSync("/v1/apis/" + apiId).statusCode(200).body("graphqlApi.logConfig.fieldLogLevel", equalTo("ALL"));
|
||||
appSync("/v1/apis/" + apiId + "/types/Query/resolvers/getMessages")
|
||||
.statusCode(200)
|
||||
.body("resolver.pipelineConfig.functions[0]", equalTo(functionId));
|
||||
|
||||
cloudFormation("DeleteStack", Map.of());
|
||||
awaitStackDeleted();
|
||||
|
||||
appSync("/v1/apis/" + apiId).statusCode(404);
|
||||
}
|
||||
|
||||
private static void cloudFormation(String action, Map<String, String> parameters) {
|
||||
RequestSpecification request = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", action)
|
||||
.formParam("StackName", STACK);
|
||||
if (!"DeleteStack".equals(action)) {
|
||||
request.formParam("TemplateBody", TEMPLATE);
|
||||
}
|
||||
int index = 1;
|
||||
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
|
||||
request.formParam("Parameters.member." + index + ".ParameterKey", parameter.getKey());
|
||||
request.formParam("Parameters.member." + index + ".ParameterValue", parameter.getValue());
|
||||
index++;
|
||||
}
|
||||
request.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static String describeStacks(String expectedStatus) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", STACK)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<StackStatus>" + expectedStatus + "</StackStatus>"))
|
||||
.extract().asString();
|
||||
}
|
||||
|
||||
private static void awaitStackDeleted() throws InterruptedException {
|
||||
for (int i = 0; i < 200; i++) {
|
||||
String body = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", STACK)
|
||||
.when().post("/").then().extract().asString();
|
||||
if (body.contains("does not exist")) {
|
||||
return;
|
||||
}
|
||||
if (body.contains("<StackStatus>DELETE_FAILED</StackStatus>")) {
|
||||
fail("stack delete failed: " + body);
|
||||
}
|
||||
Thread.sleep(50);
|
||||
}
|
||||
fail("stack " + STACK + " was not deleted within the timeout");
|
||||
}
|
||||
|
||||
private static String outputValue(String xml, String key) {
|
||||
return XmlParser.extractPairs(xml, "Outputs", "OutputKey", "OutputValue").get(key);
|
||||
}
|
||||
|
||||
private static ValidatableResponse appSync(String path) {
|
||||
return given().header("Authorization", APPSYNC_AUTH).when().get(path).then();
|
||||
}
|
||||
}
|
||||
+11
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.services.acm.AcmService;
|
||||
import io.github.hectorvent.floci.services.apigateway.ApiGatewayService;
|
||||
import io.github.hectorvent.floci.services.backup.BackupService;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
|
||||
import io.github.hectorvent.floci.services.appsync.AppSyncService;
|
||||
import io.github.hectorvent.floci.services.autoscaling.AutoScalingService;
|
||||
import io.github.hectorvent.floci.services.batch.BatchService;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnDynamicReferences;
|
||||
@@ -33,6 +34,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.AutoScali
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.BackupVaultCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.BatchCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.EventsCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.AppSyncCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.CdkMetadataCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudTrailCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudWatchCfnProvisioner;
|
||||
@@ -137,6 +139,7 @@ final class CfnProvisionerFixture {
|
||||
private SnsService snsService;
|
||||
private DynamoDbService dynamoDbService;
|
||||
private LambdaService lambdaService;
|
||||
private AppSyncService appSyncService;
|
||||
private IamService iamService;
|
||||
private SsmService ssmService;
|
||||
private KmsService kmsService;
|
||||
@@ -284,6 +287,9 @@ final class CfnProvisionerFixture {
|
||||
if (acmService != null) {
|
||||
discovered.add(new AcmCfnProvisioner(acmService));
|
||||
}
|
||||
if (appSyncService != null) {
|
||||
discovered.add(new AppSyncCfnProvisioner(appSyncService));
|
||||
}
|
||||
if (lambdaService != null) {
|
||||
discovered.add(new LambdaAddressingCfnProvisioner(lambdaService));
|
||||
discovered.add(new LambdaEventInvokeConfigCfnProvisioner(lambdaService));
|
||||
@@ -372,6 +378,11 @@ final class CfnProvisionerFixture {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder appSync(AppSyncService v) {
|
||||
this.appSyncService = v;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder iam(IamService v) {
|
||||
this.iamService = v;
|
||||
return this;
|
||||
|
||||
+713
@@ -0,0 +1,713 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.appsync.AppSyncService;
|
||||
import io.github.hectorvent.floci.services.appsync.model.ApiKey;
|
||||
import io.github.hectorvent.floci.services.appsync.model.DataSource;
|
||||
import io.github.hectorvent.floci.services.appsync.model.FunctionConfiguration;
|
||||
import io.github.hectorvent.floci.services.appsync.model.GraphqlApi;
|
||||
import io.github.hectorvent.floci.services.appsync.model.Resolver;
|
||||
import io.github.hectorvent.floci.services.appsync.model.SchemaCreationStatus;
|
||||
import io.github.hectorvent.floci.services.appsync.model.SchemaCreationStatusType;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** The AppSync CFN provisioner in isolation, with only AppSyncService mocked. */
|
||||
class AppSyncCfnProvisionerTest {
|
||||
|
||||
private static final String API_ID = "abcd1234";
|
||||
|
||||
private final AppSyncService appSync = mock(AppSyncService.class);
|
||||
private final AppSyncCfnProvisioner provisioner = new AppSyncCfnProvisioner(appSync);
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
private ProvisionContext ctx() {
|
||||
// Identity resolution: these templates carry no intrinsics, and resolveNode's contract for
|
||||
// an intrinsic-free node is to hand it back unchanged.
|
||||
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
|
||||
when(engine.resolve(any())).thenAnswer(inv -> {
|
||||
JsonNode node = inv.getArgument(0);
|
||||
return node == null || node.isNull() ? null : node.asText();
|
||||
});
|
||||
when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
return new ProvisionContext(engine, "eu-west-1", "000000000000", "my-stack");
|
||||
}
|
||||
|
||||
private ProvisionContext updateCtx(String priorPhysicalId) {
|
||||
ProvisionContext create = ctx();
|
||||
return new ProvisionContext(create.engine(), create.region(), create.accountId(),
|
||||
create.stackName(), priorPhysicalId);
|
||||
}
|
||||
|
||||
private StackResource resource(String type, String logicalId) {
|
||||
StackResource r = new StackResource();
|
||||
r.setLogicalId(logicalId);
|
||||
r.setResourceType(type);
|
||||
r.setAttributes(new HashMap<>());
|
||||
return r;
|
||||
}
|
||||
|
||||
private GraphqlApi api() {
|
||||
GraphqlApi api = new GraphqlApi();
|
||||
api.setApiId(API_ID);
|
||||
api.setName("account-api");
|
||||
api.setArn("arn:aws:appsync:eu-west-1:000000000000:apis/" + API_ID);
|
||||
api.setUris(Map.of(
|
||||
"GRAPHQL", "http://localhost:4566/v1/apis/" + API_ID + "/graphql",
|
||||
"REALTIME", "ws://localhost:4566/v1/apis/" + API_ID + "/graphql/realtime"));
|
||||
return api;
|
||||
}
|
||||
|
||||
private void schemaCompletesImmediately() {
|
||||
SchemaCreationStatus success = new SchemaCreationStatus();
|
||||
success.setStatus(SchemaCreationStatusType.SUCCESS);
|
||||
when(appSync.getSchemaCreationStatus(API_ID)).thenReturn(success);
|
||||
}
|
||||
|
||||
/** The request the provisioner handed CreateGraphqlApi. */
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Object> capturedApiRequest() {
|
||||
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class);
|
||||
verify(appSync).createGraphqlApi(captor.capture(), eq("eu-west-1"));
|
||||
return captor.getValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUpdateThatDroppedTagsClearsThem() {
|
||||
when(appSync.updateGraphqlApi(eq(API_ID), any(), eq("eu-west-1"))).thenReturn(api());
|
||||
StackResource r = resource("AWS::AppSync::GraphQLApi", "GraphQlApi");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("Name", "account-api")
|
||||
.put("AuthenticationType", "API_KEY");
|
||||
|
||||
provisioner.provision(r, props, updateCtx(API_ID));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class);
|
||||
verify(appSync).updateGraphqlApi(eq(API_ID), captor.capture(), eq("eu-west-1"));
|
||||
// The service only touches tags when the member is present, so an omitted one left the
|
||||
// previous tags on an API the template no longer tags at all.
|
||||
assertEquals(Map.of(), captor.getValue().get("tags"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCreateWithNoTagsSendsNoTagsMember() {
|
||||
when(appSync.createGraphqlApi(any(), eq("eu-west-1"))).thenReturn(api());
|
||||
StackResource r = resource("AWS::AppSync::GraphQLApi", "GraphQlApi");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode()
|
||||
.put("Name", "account-api")
|
||||
.put("AuthenticationType", "API_KEY"), ctx());
|
||||
|
||||
// Nothing to drive to on a create: an untagged API keeps a null tag map, not an empty one.
|
||||
assertFalse(capturedApiRequest().containsKey("tags"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUpdateThatDroppedEnvironmentVariablesClearsThem() {
|
||||
when(appSync.updateGraphqlApi(eq(API_ID), any(), eq("eu-west-1"))).thenReturn(api());
|
||||
StackResource r = resource("AWS::AppSync::GraphQLApi", "GraphQlApi");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode()
|
||||
.put("Name", "account-api")
|
||||
.put("AuthenticationType", "API_KEY"), updateCtx(API_ID));
|
||||
|
||||
// PutGraphqlApiEnvironmentVariables replaces the whole set, so the empty map is what clears
|
||||
// it; skipping the call left variables the template dropped readable by every resolver.
|
||||
verify(appSync).putEnvironmentVariables(API_ID, Map.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCreateWithNoEnvironmentVariablesMakesNoCall() {
|
||||
when(appSync.createGraphqlApi(any(), eq("eu-west-1"))).thenReturn(api());
|
||||
StackResource r = resource("AWS::AppSync::GraphQLApi", "GraphQlApi");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode()
|
||||
.put("Name", "account-api")
|
||||
.put("AuthenticationType", "API_KEY"), ctx());
|
||||
|
||||
verify(appSync, never()).putEnvironmentVariables(anyString(), any());
|
||||
}
|
||||
|
||||
// ── GraphQLApi ───────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void graphqlApiSetsPhysicalIdAndGetAttAttributes() {
|
||||
when(appSync.createGraphqlApi(any(), eq("eu-west-1"))).thenReturn(api());
|
||||
StackResource r = resource("AWS::AppSync::GraphQLApi", "GraphQlApi");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("Name", "account-api")
|
||||
.put("AuthenticationType", "API_KEY")
|
||||
.put("XrayEnabled", false);
|
||||
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
assertEquals(API_ID, r.getPhysicalId());
|
||||
assertEquals(API_ID, r.getAttributes().get("ApiId"));
|
||||
assertEquals("arn:aws:appsync:eu-west-1:000000000000:apis/" + API_ID, r.getAttributes().get("Arn"));
|
||||
assertEquals("http://localhost:4566/v1/apis/" + API_ID + "/graphql",
|
||||
r.getAttributes().get("GraphQLUrl"));
|
||||
assertEquals("ws://localhost:4566/v1/apis/" + API_ID + "/graphql/realtime",
|
||||
r.getAttributes().get("RealtimeUrl"));
|
||||
// The DNS attributes are the endpoints' hosts, which is what a custom domain aliases.
|
||||
assertEquals("localhost", r.getAttributes().get("GraphQLDns"));
|
||||
assertEquals("localhost", r.getAttributes().get("RealtimeDns"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void graphqlApiLowerCamelisesPropertiesForTheApi() {
|
||||
when(appSync.createGraphqlApi(any(), eq("eu-west-1"))).thenReturn(api());
|
||||
StackResource r = resource("AWS::AppSync::GraphQLApi", "GraphQlApi");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("Name", "account-api")
|
||||
.put("AuthenticationType", "API_KEY")
|
||||
.put("XrayEnabled", false);
|
||||
ObjectNode logConfig = props.putObject("LogConfig");
|
||||
logConfig.put("CloudWatchLogsRoleArn", "arn:aws:iam::000000000000:role/logs");
|
||||
logConfig.put("FieldLogLevel", "ALL");
|
||||
ObjectNode oidc = props.putArray("AdditionalAuthenticationProviders").addObject();
|
||||
oidc.put("AuthenticationType", "OPENID_CONNECT");
|
||||
oidc.putObject("OpenIDConnectConfig").put("Issuer", "https://issuer").put("ClientId", "cid");
|
||||
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
Map<String, Object> request = capturedApiRequest();
|
||||
assertEquals("account-api", request.get("name"));
|
||||
assertEquals("API_KEY", request.get("authenticationType"));
|
||||
assertEquals(Boolean.FALSE, request.get("xrayEnabled"));
|
||||
Map<?, ?> resolvedLog = (Map<?, ?>) request.get("logConfig");
|
||||
assertEquals("arn:aws:iam::000000000000:role/logs", resolvedLog.get("cloudWatchLogsRoleArn"));
|
||||
assertEquals("ALL", resolvedLog.get("fieldLogLevel"));
|
||||
Map<?, ?> provider = (Map<?, ?>) ((List<?>) request.get("additionalAuthenticationProviders")).get(0);
|
||||
assertEquals("OPENID_CONNECT", provider.get("authenticationType"));
|
||||
// AWS keeps the acronym capitalised in this member name; the model's field is openIDConnectConfig.
|
||||
assertEquals("https://issuer", ((Map<?, ?>) provider.get("openIDConnectConfig")).get("issuer"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void graphqlApiEnvironmentVariablesGoThroughTheirOwnCall() {
|
||||
when(appSync.createGraphqlApi(any(), eq("eu-west-1"))).thenReturn(api());
|
||||
StackResource r = resource("AWS::AppSync::GraphQLApi", "GraphQlApi");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("Name", "account-api")
|
||||
.put("AuthenticationType", "API_KEY");
|
||||
props.putObject("EnvironmentVariables").put("STRIPE_PRODUCT_SMALL_ID", "prod_1");
|
||||
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
// CreateGraphqlApi has no environment-variable member: it is PutGraphqlApiEnvironmentVariables.
|
||||
assertFalse(capturedApiRequest().containsKey("environmentVariables"));
|
||||
verify(appSync).putEnvironmentVariables(API_ID, Map.of("STRIPE_PRODUCT_SMALL_ID", "prod_1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void graphqlApiUpdateUpdatesInPlace() {
|
||||
when(appSync.updateGraphqlApi(eq(API_ID), any(), eq("eu-west-1"))).thenReturn(api());
|
||||
StackResource r = resource("AWS::AppSync::GraphQLApi", "GraphQlApi");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("Name", "account-api")
|
||||
.put("AuthenticationType", "AWS_IAM");
|
||||
|
||||
provisioner.provision(r, props, updateCtx(API_ID));
|
||||
|
||||
verify(appSync).updateGraphqlApi(eq(API_ID), any(), eq("eu-west-1"));
|
||||
verify(appSync, never()).createGraphqlApi(any(), anyString());
|
||||
assertEquals(API_ID, r.getPhysicalId());
|
||||
}
|
||||
|
||||
// ── GraphQLSchema ────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void schemaWaitsForCompilationBeforeCompleting() {
|
||||
SchemaCreationStatus processing = new SchemaCreationStatus();
|
||||
processing.setStatus(SchemaCreationStatusType.PROCESSING);
|
||||
SchemaCreationStatus success = new SchemaCreationStatus();
|
||||
success.setStatus(SchemaCreationStatusType.SUCCESS);
|
||||
when(appSync.getSchemaCreationStatus(API_ID)).thenReturn(processing, processing, success);
|
||||
StackResource r = resource("AWS::AppSync::GraphQLSchema", "GraphQlSchema");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("ApiId", API_ID)
|
||||
.put("Definition", "type Query { ping: String }");
|
||||
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
verify(appSync).startSchemaCreation(API_ID, "type Query { ping: String }");
|
||||
// Every AppSync call for the API is refused with a 409 while the schema is PROCESSING, so a
|
||||
// provision that returned early would fail the data sources and resolvers behind it.
|
||||
verify(appSync, org.mockito.Mockito.times(3)).getSchemaCreationStatus(API_ID);
|
||||
assertEquals(API_ID + "GraphQLSchema", r.getPhysicalId());
|
||||
assertEquals(API_ID, r.getAttributes().get("ApiId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void schemaCompilationFailureFailsTheResource() {
|
||||
SchemaCreationStatus failed = new SchemaCreationStatus();
|
||||
failed.setStatus(SchemaCreationStatusType.FAILED);
|
||||
failed.setDetails("Unknown type Widget");
|
||||
when(appSync.getSchemaCreationStatus(API_ID)).thenReturn(failed);
|
||||
StackResource r = resource("AWS::AppSync::GraphQLSchema", "GraphQlSchema");
|
||||
ObjectNode props = mapper.createObjectNode().put("ApiId", API_ID).put("Definition", "type Q { a: Widget }");
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx()));
|
||||
|
||||
assertTrue(e.getMessage().contains("Unknown type Widget"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void schemaFromS3IsRefusedRatherThanSilentlyEmpty() {
|
||||
StackResource r = resource("AWS::AppSync::GraphQLSchema", "GraphQlSchema");
|
||||
ObjectNode props = mapper.createObjectNode().put("ApiId", API_ID);
|
||||
props.putObject("DefinitionS3Location").put("Bucket", "b").put("Key", "schema.graphql");
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx()));
|
||||
|
||||
assertTrue(e.getMessage().contains("DefinitionS3Location"), e.getMessage());
|
||||
verify(appSync, never()).startSchemaCreation(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void schemaWithoutApiIdIsRefused() {
|
||||
StackResource r = resource("AWS::AppSync::GraphQLSchema", "GraphQlSchema");
|
||||
ObjectNode props = mapper.createObjectNode().put("Definition", "type Query { ping: String }");
|
||||
|
||||
assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx()));
|
||||
verifyNoInteractions(appSync);
|
||||
}
|
||||
|
||||
// ── DataSource ───────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void dataSourceKeepsAwsSpellingOfDynamodbConfig() {
|
||||
DataSource ds = new DataSource();
|
||||
ds.setName("publicApiAuthKeysTable");
|
||||
ds.setDataSourceArn("arn:aws:appsync:eu-west-1:000000000000:apis/" + API_ID
|
||||
+ "/datasources/publicApiAuthKeysTable");
|
||||
when(appSync.createDataSource(eq(API_ID), any(), eq("eu-west-1"))).thenReturn(ds);
|
||||
StackResource r = resource("AWS::AppSync::DataSource", "GraphQlDsAuthKeys");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("ApiId", API_ID)
|
||||
.put("Name", "publicApiAuthKeysTable")
|
||||
.put("Type", "AMAZON_DYNAMODB")
|
||||
.put("ServiceRoleArn", "arn:aws:iam::000000000000:role/ds");
|
||||
props.putObject("DynamoDBConfig")
|
||||
.put("AwsRegion", "eu-west-1")
|
||||
.put("TableName", "qip-local-public-api-auth-keys")
|
||||
.put("UseCallerCredentials", false);
|
||||
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class);
|
||||
verify(appSync).createDataSource(eq(API_ID), captor.capture(), eq("eu-west-1"));
|
||||
Map<String, Object> request = captor.getValue();
|
||||
// dynamoDBConfig, what the plain lower-camel rule would produce, is silently ignored by
|
||||
// the service, leaving a DynamoDB data source with no table.
|
||||
assertNull(request.get("dynamoDBConfig"));
|
||||
Map<?, ?> dynamodb = (Map<?, ?>) request.get("dynamodbConfig");
|
||||
assertEquals("qip-local-public-api-auth-keys", dynamodb.get("tableName"));
|
||||
assertEquals("eu-west-1", dynamodb.get("awsRegion"));
|
||||
assertEquals(Boolean.FALSE, dynamodb.get("useCallerCredentials"));
|
||||
assertEquals("arn:aws:iam::000000000000:role/ds", request.get("serviceRoleArn"));
|
||||
// The ApiId is a path parameter, not a member of the request body.
|
||||
assertFalse(request.containsKey("apiId"));
|
||||
|
||||
assertEquals(ds.getDataSourceArn(), r.getPhysicalId());
|
||||
assertEquals(ds.getDataSourceArn(), r.getAttributes().get("DataSourceArn"));
|
||||
assertEquals("publicApiAuthKeysTable", r.getAttributes().get("Name"));
|
||||
assertEquals(API_ID, r.getAttributes().get("ApiId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dataSourceCamelisesNestedRelationalConfig() {
|
||||
DataSource ds = new DataSource();
|
||||
ds.setName("accountDB");
|
||||
ds.setDataSourceArn("arn:ds");
|
||||
when(appSync.createDataSource(eq(API_ID), any(), eq("eu-west-1"))).thenReturn(ds);
|
||||
StackResource r = resource("AWS::AppSync::DataSource", "GraphQlDsAccountDb");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("ApiId", API_ID)
|
||||
.put("Name", "accountDB")
|
||||
.put("Type", "RELATIONAL_DATABASE");
|
||||
ObjectNode relational = props.putObject("RelationalDatabaseConfig");
|
||||
relational.put("RelationalDatabaseSourceType", "RDS_HTTP_ENDPOINT");
|
||||
relational.putObject("RdsHttpEndpointConfig")
|
||||
.put("AwsRegion", "eu-west-1")
|
||||
.put("DbClusterIdentifier", "ls-db-cluster")
|
||||
.put("DatabaseName", "accountdb")
|
||||
.put("AwsSecretStoreArn", "arn:aws:secretsmanager:eu-west-1:000000000000:secret:db");
|
||||
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class);
|
||||
verify(appSync).createDataSource(eq(API_ID), captor.capture(), eq("eu-west-1"));
|
||||
Map<?, ?> config = (Map<?, ?>) captor.getValue().get("relationalDatabaseConfig");
|
||||
assertEquals("RDS_HTTP_ENDPOINT", config.get("relationalDatabaseSourceType"));
|
||||
Map<?, ?> rds = (Map<?, ?>) config.get("rdsHttpEndpointConfig");
|
||||
assertEquals("ls-db-cluster", rds.get("dbClusterIdentifier"));
|
||||
assertEquals("accountdb", rds.get("databaseName"));
|
||||
assertEquals("arn:aws:secretsmanager:eu-west-1:000000000000:secret:db", rds.get("awsSecretStoreArn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dataSourceUpdateReusesTheNameItAlreadyHas() {
|
||||
DataSource ds = new DataSource();
|
||||
ds.setName("accountDB");
|
||||
ds.setDataSourceArn("arn:ds");
|
||||
when(appSync.updateDataSource(eq(API_ID), eq("accountDB"), any())).thenReturn(ds);
|
||||
StackResource r = resource("AWS::AppSync::DataSource", "GraphQlDsAccountDb");
|
||||
r.getAttributes().put("ApiId", API_ID);
|
||||
r.getAttributes().put("Name", "accountDB");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("ApiId", API_ID)
|
||||
.put("Name", "accountDB")
|
||||
.put("Type", "RELATIONAL_DATABASE");
|
||||
|
||||
provisioner.provision(r, props, updateCtx("arn:ds"));
|
||||
|
||||
verify(appSync).updateDataSource(eq(API_ID), eq("accountDB"), any());
|
||||
verify(appSync, never()).createDataSource(anyString(), any(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void dataSourceMovedToAnotherApiIsCreatedNotUpdated() {
|
||||
DataSource ds = new DataSource();
|
||||
ds.setName("accountDB");
|
||||
ds.setDataSourceArn("arn:ds:new");
|
||||
when(appSync.createDataSource(eq("other-api"), any(), eq("eu-west-1"))).thenReturn(ds);
|
||||
StackResource r = resource("AWS::AppSync::DataSource", "GraphQlDsAccountDb");
|
||||
r.getAttributes().put("ApiId", API_ID);
|
||||
r.getAttributes().put("Name", "accountDB");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("ApiId", "other-api")
|
||||
.put("Name", "accountDB")
|
||||
.put("Type", "RELATIONAL_DATABASE");
|
||||
|
||||
provisioner.provision(r, props, updateCtx("arn:ds"));
|
||||
|
||||
// The entity the resource pointed at lives in an API this resource no longer names, so
|
||||
// updating it would mutate a data source belonging to a different API.
|
||||
verify(appSync).createDataSource(eq("other-api"), any(), eq("eu-west-1"));
|
||||
verify(appSync, never()).updateDataSource(anyString(), anyString(), any());
|
||||
}
|
||||
|
||||
// ── FunctionConfiguration ────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void functionPublishesFunctionIdAndCarriesTheJsRuntime() {
|
||||
FunctionConfiguration fn = new FunctionConfiguration();
|
||||
fn.setFunctionId("fn123");
|
||||
fn.setName("Query_getMessages_0");
|
||||
fn.setDataSourceName("accountDB");
|
||||
fn.setFunctionArn("arn:aws:appsync:eu-west-1:000000000000:apis/" + API_ID + "/functions/fn123");
|
||||
when(appSync.createFunction(eq(API_ID), any(), eq("eu-west-1"))).thenReturn(fn);
|
||||
StackResource r = resource("AWS::AppSync::FunctionConfiguration", "GraphQlFnGetMessages");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("ApiId", API_ID)
|
||||
.put("Name", "Query_getMessages_0")
|
||||
.put("DataSourceName", "accountDB")
|
||||
.put("FunctionVersion", "2018-05-29")
|
||||
.put("Code", "export function request() { return {}; }");
|
||||
props.putObject("Runtime").put("Name", "APPSYNC_JS").put("RuntimeVersion", "1.0.0");
|
||||
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class);
|
||||
verify(appSync).createFunction(eq(API_ID), captor.capture(), eq("eu-west-1"));
|
||||
Map<String, Object> request = captor.getValue();
|
||||
assertEquals("2018-05-29", request.get("functionVersion"));
|
||||
assertEquals("export function request() { return {}; }", request.get("code"));
|
||||
assertEquals("APPSYNC_JS", ((Map<?, ?>) request.get("runtime")).get("name"));
|
||||
|
||||
assertEquals(fn.getFunctionArn(), r.getPhysicalId());
|
||||
// The pipeline resolvers reference their functions by Fn::GetAtt FunctionId.
|
||||
assertEquals("fn123", r.getAttributes().get("FunctionId"));
|
||||
assertEquals("arn:aws:appsync:eu-west-1:000000000000:apis/" + API_ID + "/functions/fn123",
|
||||
r.getAttributes().get("FunctionArn"));
|
||||
assertEquals("accountDB", r.getAttributes().get("DataSourceName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void functionUpdateAddressesTheRecordedFunctionId() {
|
||||
FunctionConfiguration fn = new FunctionConfiguration();
|
||||
fn.setFunctionId("fn123");
|
||||
fn.setName("Query_getMessages_0");
|
||||
fn.setFunctionArn("arn:fn");
|
||||
when(appSync.updateFunction(eq(API_ID), eq("fn123"), any())).thenReturn(fn);
|
||||
StackResource r = resource("AWS::AppSync::FunctionConfiguration", "GraphQlFnGetMessages");
|
||||
r.getAttributes().put("ApiId", API_ID);
|
||||
r.getAttributes().put("FunctionId", "fn123");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("ApiId", API_ID)
|
||||
.put("Name", "Query_getMessages_0")
|
||||
.put("DataSourceName", "accountDB");
|
||||
|
||||
provisioner.provision(r, props, updateCtx("arn:fn"));
|
||||
|
||||
// The function id is not derivable from the ARN-shaped physical id alone in the general
|
||||
// case, so it has to come off the attributes recorded at create time.
|
||||
verify(appSync).updateFunction(eq(API_ID), eq("fn123"), any());
|
||||
verify(appSync, never()).createFunction(anyString(), any(), anyString());
|
||||
}
|
||||
|
||||
// ── Resolver ─────────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void pipelineResolverIsProvisionedWithoutADataSource() {
|
||||
Resolver resolver = new Resolver();
|
||||
resolver.setResolverArn("arn:aws:appsync:eu-west-1:000000000000:apis/" + API_ID
|
||||
+ "/types/Query/resolvers/getMessages");
|
||||
when(appSync.createResolver(eq(API_ID), any(), eq("eu-west-1"))).thenReturn(resolver);
|
||||
StackResource r = resource("AWS::AppSync::Resolver", "GraphQlResolverQuerygetMessages");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("ApiId", API_ID)
|
||||
.put("TypeName", "Query")
|
||||
.put("FieldName", "getMessages")
|
||||
.put("Kind", "PIPELINE")
|
||||
.put("Code", "export function request() { return {}; }");
|
||||
props.putObject("Runtime").put("Name", "APPSYNC_JS").put("RuntimeVersion", "1.0.0");
|
||||
props.putObject("PipelineConfig").putArray("Functions").add("fn123");
|
||||
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class);
|
||||
verify(appSync).createResolver(eq(API_ID), captor.capture(), eq("eu-west-1"));
|
||||
Map<String, Object> request = captor.getValue();
|
||||
assertEquals("PIPELINE", request.get("kind"));
|
||||
assertNull(request.get("dataSourceName"));
|
||||
assertEquals(List.of("fn123"), ((Map<?, ?>) request.get("pipelineConfig")).get("functions"));
|
||||
|
||||
assertEquals(resolver.getResolverArn(), r.getPhysicalId());
|
||||
assertEquals("Query", r.getAttributes().get("TypeName"));
|
||||
assertEquals("getMessages", r.getAttributes().get("FieldName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolverUpdateAddressesTypeAndField() {
|
||||
Resolver resolver = new Resolver();
|
||||
resolver.setResolverArn("arn:resolver");
|
||||
when(appSync.updateResolver(eq(API_ID), eq("Query"), eq("getMessages"), any())).thenReturn(resolver);
|
||||
StackResource r = resource("AWS::AppSync::Resolver", "GraphQlResolverQuerygetMessages");
|
||||
r.getAttributes().put("ApiId", API_ID);
|
||||
r.getAttributes().put("TypeName", "Query");
|
||||
r.getAttributes().put("FieldName", "getMessages");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("ApiId", API_ID)
|
||||
.put("TypeName", "Query")
|
||||
.put("FieldName", "getMessages")
|
||||
.put("Kind", "PIPELINE");
|
||||
|
||||
provisioner.provision(r, props, updateCtx("arn:resolver"));
|
||||
|
||||
verify(appSync).updateResolver(eq(API_ID), eq("Query"), eq("getMessages"), any());
|
||||
verify(appSync, never()).createResolver(anyString(), any(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolverOnADifferentFieldIsCreatedNotUpdated() {
|
||||
Resolver resolver = new Resolver();
|
||||
resolver.setResolverArn("arn:resolver:new");
|
||||
when(appSync.createResolver(eq(API_ID), any(), eq("eu-west-1"))).thenReturn(resolver);
|
||||
StackResource r = resource("AWS::AppSync::Resolver", "GraphQlResolverQuerygetMessages");
|
||||
r.getAttributes().put("ApiId", API_ID);
|
||||
r.getAttributes().put("TypeName", "Query");
|
||||
r.getAttributes().put("FieldName", "getMessages");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("ApiId", API_ID)
|
||||
.put("TypeName", "Query")
|
||||
.put("FieldName", "getUsers")
|
||||
.put("Kind", "PIPELINE");
|
||||
|
||||
provisioner.provision(r, props, updateCtx("arn:resolver"));
|
||||
|
||||
// TypeName and FieldName are create-only: a resolver on a new field is a new resolver.
|
||||
verify(appSync).createResolver(eq(API_ID), any(), eq("eu-west-1"));
|
||||
verify(appSync, never()).updateResolver(anyString(), anyString(), anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolverWithoutTypeOrFieldIsRefused() {
|
||||
StackResource r = resource("AWS::AppSync::Resolver", "GraphQlResolver");
|
||||
ObjectNode props = mapper.createObjectNode().put("ApiId", API_ID).put("TypeName", "Query");
|
||||
|
||||
assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx()));
|
||||
verify(appSync, never()).createResolver(anyString(), any(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolverCodeFromS3IsRefused() {
|
||||
StackResource r = resource("AWS::AppSync::Resolver", "GraphQlResolver");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("ApiId", API_ID)
|
||||
.put("TypeName", "Query")
|
||||
.put("FieldName", "getMessages")
|
||||
.put("CodeS3Location", "s3://bucket/resolver.js");
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx()));
|
||||
|
||||
assertTrue(e.getMessage().contains("CodeS3Location"), e.getMessage());
|
||||
}
|
||||
|
||||
// ── ApiKey ───────────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void apiKeyPublishesTheKeyAndItsId() {
|
||||
ApiKey key = new ApiKey();
|
||||
key.setId("da2-abcdefghijklmnopqrstuvwxyz");
|
||||
key.setApiId(API_ID);
|
||||
when(appSync.createApiKey(eq(API_ID), any())).thenReturn(key);
|
||||
StackResource r = resource("AWS::AppSync::ApiKey", "GraphQlApiqvalia");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("ApiId", API_ID)
|
||||
.put("Description", "qvalia")
|
||||
.put("Expires", 1820570400L);
|
||||
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class);
|
||||
verify(appSync).createApiKey(eq(API_ID), captor.capture());
|
||||
// Expires must stay a number: the service parses it as an epoch second.
|
||||
assertEquals(1820570400L, captor.getValue().get("expires"));
|
||||
assertEquals("qvalia", captor.getValue().get("description"));
|
||||
|
||||
assertEquals("da2-abcdefghijklmnopqrstuvwxyz", r.getPhysicalId());
|
||||
assertEquals("da2-abcdefghijklmnopqrstuvwxyz", r.getAttributes().get("ApiKey"));
|
||||
assertEquals("da2-abcdefghijklmnopqrstuvwxyz", r.getAttributes().get("ApiKeyId"));
|
||||
assertEquals("arn:aws:appsync:eu-west-1:000000000000:apis/" + API_ID
|
||||
+ "/apikeys/da2-abcdefghijklmnopqrstuvwxyz", r.getAttributes().get("Arn"));
|
||||
}
|
||||
|
||||
// ── Deletes ──────────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void deleteApiDeletesByPhysicalId() {
|
||||
StackResource r = resource("AWS::AppSync::GraphQLApi", "GraphQlApi");
|
||||
r.setPhysicalId(API_ID);
|
||||
|
||||
provisioner.delete(r, "eu-west-1");
|
||||
|
||||
verify(appSync).deleteGraphqlApi(API_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteDataSourceUsesTheRecordedApiIdAndName() {
|
||||
StackResource r = resource("AWS::AppSync::DataSource", "GraphQlDsAccountDb");
|
||||
r.setPhysicalId("arn:aws:appsync:eu-west-1:000000000000:apis/" + API_ID + "/datasources/accountDB");
|
||||
r.getAttributes().put("ApiId", API_ID);
|
||||
r.getAttributes().put("Name", "accountDB");
|
||||
|
||||
provisioner.delete(r, "eu-west-1");
|
||||
|
||||
// DeleteDataSource takes the api id and the name; the ARN that is the physical id is not it.
|
||||
verify(appSync).deleteDataSource(API_ID, "accountDB");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteResolverUsesTheRecordedTypeAndField() {
|
||||
StackResource r = resource("AWS::AppSync::Resolver", "GraphQlResolver");
|
||||
r.setPhysicalId("arn:resolver");
|
||||
r.getAttributes().put("ApiId", API_ID);
|
||||
r.getAttributes().put("TypeName", "Query");
|
||||
r.getAttributes().put("FieldName", "getMessages");
|
||||
|
||||
provisioner.delete(r, "eu-west-1");
|
||||
|
||||
verify(appSync).deleteResolver(API_ID, "Query", "getMessages");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteFunctionUsesTheRecordedFunctionId() {
|
||||
StackResource r = resource("AWS::AppSync::FunctionConfiguration", "GraphQlFn");
|
||||
r.setPhysicalId("arn:fn");
|
||||
r.getAttributes().put("ApiId", API_ID);
|
||||
r.getAttributes().put("FunctionId", "fn123");
|
||||
|
||||
provisioner.delete(r, "eu-west-1");
|
||||
|
||||
verify(appSync).deleteFunction(API_ID, "fn123");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteSchemaTouchesNothingBecauseItGoesWithItsApi() {
|
||||
StackResource r = resource("AWS::AppSync::GraphQLSchema", "GraphQlSchema");
|
||||
r.setPhysicalId(API_ID + "GraphQLSchema");
|
||||
r.getAttributes().put("ApiId", API_ID);
|
||||
|
||||
provisioner.delete(r, "eu-west-1");
|
||||
|
||||
verifyNoInteractions(appSync);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteToleratesAnAlreadyGoneResolver() {
|
||||
doThrow(new AwsException("NotFoundException", "Resolver not found", 404))
|
||||
.when(appSync).deleteResolver(API_ID, "Query", "getMessages");
|
||||
StackResource r = resource("AWS::AppSync::Resolver", "GraphQlResolver");
|
||||
r.setPhysicalId("arn:resolver");
|
||||
r.getAttributes().put("ApiId", API_ID);
|
||||
r.getAttributes().put("TypeName", "Query");
|
||||
r.getAttributes().put("FieldName", "getMessages");
|
||||
|
||||
// A DeleteStack retry must not fail on a resolver the first attempt already removed.
|
||||
provisioner.delete(r, "eu-west-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletePropagatesAFailureThatIsNotAlreadyGone() {
|
||||
doThrow(new AwsException("ConcurrentModificationException", "Schema is being modified", 409))
|
||||
.when(appSync).deleteDataSource(API_ID, "accountDB");
|
||||
StackResource r = resource("AWS::AppSync::DataSource", "GraphQlDs");
|
||||
r.setPhysicalId("arn:ds");
|
||||
r.getAttributes().put("ApiId", API_ID);
|
||||
r.getAttributes().put("Name", "accountDB");
|
||||
|
||||
// Anything but "already gone" has to reach the stack as DELETE_FAILED.
|
||||
assertThrows(AwsException.class, () -> provisioner.delete(r, "eu-west-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteWithoutRecordedIdentityDoesNotThrow() {
|
||||
StackResource r = resource("AWS::AppSync::DataSource", "GraphQlDs");
|
||||
r.setPhysicalId("arn:ds");
|
||||
|
||||
provisioner.delete(r, "eu-west-1");
|
||||
|
||||
verifyNoInteractions(appSync);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceTypesCoverTheSixAppSyncTypes() {
|
||||
assertEquals(Set.of("AWS::AppSync::GraphQLApi", "AWS::AppSync::GraphQLSchema",
|
||||
"AWS::AppSync::DataSource", "AWS::AppSync::FunctionConfiguration",
|
||||
"AWS::AppSync::Resolver", "AWS::AppSync::ApiKey"),
|
||||
provisioner.resourceTypes());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user