com.amazonaws.athena.connector.integ.ConnectorStackProvider Maven / Gradle / Ivy
/*-
* #%L
* Amazon Athena Query Federation Integ Test
* %%
* Copyright (C) 2019 - 2020 Amazon Web Services
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.amazonaws.athena.connector.integ;
import com.amazonaws.athena.connector.integ.data.TestConfig;
import org.testng.internal.collections.Pair;
import software.amazon.awscdk.core.App;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.services.iam.PolicyDocument;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
/**
* Responsible for providing a CloudFormation stack for the connector being tested.
*/
public abstract class ConnectorStackProvider
{
private static final int LAMBDA_FUNCTION_MAX_LENGTH = 64;
private final String cloudFormationStackName;
private final TestConfig testConfig;
private final App theApp;
private final String lambdaFunctionName;
public ConnectorStackProvider(String stackName, TestConfig testConfig)
{
final String randomUuidStr = UUID.randomUUID().toString();
theApp = new App();
cloudFormationStackName = String.format("integration-%s-%s", stackName, randomUuidStr);
// Lambda Function's name cannot exceed 64 bytes.
lambdaFunctionName = String.format("%s_%s", stackName.toLowerCase(), randomUuidStr
.replace('-', '_'))
.substring(0, Math.min(LAMBDA_FUNCTION_MAX_LENGTH, stackName.length() + randomUuidStr.length() + 1));
this.testConfig = testConfig;
}
/**
* Must be overridden to facilitate getting the lambda function's IAM access policy. The latter sets up
* access to multiple connector-specific AWS services (e.g. DynamoDB, Elasticsearch etc...)
* @return A policy document object.
*/
protected abstract Optional getAccessPolicy();
/**
* Must be overridden to facilitate the setting of the lambda function's environment variables key-value pairs
* (e.g. "connection_string":"redshift://jdbc:redshift://..."). See individual connector for the expected list of
* environment variables.
*/
protected abstract void setEnvironmentVars(final Map environmentVars);
/**
* Must be overridden (can be a no-op) to facilitate the creation of a connector-specific CloudFormation stack
* resource (e.g. DB table) using AWS CDK.
* @param stack The current CloudFormation stack.
*/
protected abstract void setSpecificResource(final Stack stack);
/**
* Gets the name of the lambda function generated by the Integration-Test module.
* @return The name of the lambda function.
*/
protected String getLambdaFunctionName()
{
return lambdaFunctionName;
}
/**
* Gets the CloudFormation stack programmatically using AWS CDK.
* @return CloudFormation stack object.
*/
protected Pair getStack()
{
ConnectorStackAttributesProvider attributesProvider = new ConnectorStackAttributesProvider(theApp,
cloudFormationStackName, lambdaFunctionName, testConfig, getAccessPolicy(), getEnvironmentVars());
ConnectorStackFactory stackFactory = new ConnectorStackFactory(attributesProvider.getAttributes());
final Stack theStack = stackFactory.createStack();
setSpecificResource(theStack);
return new Pair<>(theApp, theStack);
}
/**
* Gets the specific connectors' environment variables.
* @return A Map containing the environment variables key-value pairs.
*/
private Map getEnvironmentVars()
{
final Map environmentVars = new HashMap<>();
// Set connector-specific environment variables.
setEnvironmentVars(environmentVars);
return environmentVars;
}
}