mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-16 02:38:47 +00:00
Basic ListAdd Lambda
Create a basic list adding Lambda. NOTE: This does not include the following which are still needed: 1) Permanent DB Setup 2) Better RDS access control with security groups 3) Proper input parameters for the List add Lambda
This commit is contained in:
parent
bbe0daff43
commit
de0bd51eaf
2
.gitignore
vendored
2
.gitignore
vendored
@ -85,3 +85,5 @@ lint/outputs/
|
|||||||
lint/tmp/
|
lint/tmp/
|
||||||
# lint/reports/
|
# lint/reports/
|
||||||
Lambdas/Lists/src/main/resources/dbProperties.json
|
Lambdas/Lists/src/main/resources/dbProperties.json
|
||||||
|
Lambdas/Lists/target/classes/dbProperties.json
|
||||||
|
Lambdas/Lists/target/classes/META-INF/Lists.kotlin_module
|
||||||
|
|||||||
@ -34,6 +34,11 @@
|
|||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.8.1</version>
|
<version>3.8.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mariadb.jdbc</groupId>
|
||||||
|
<artifactId>mariadb-java-client</artifactId>
|
||||||
|
<version>2.7.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>1.11</maven.compiler.source>
|
<maven.compiler.source>1.11</maven.compiler.source>
|
||||||
|
|||||||
@ -12,14 +12,28 @@ public class DBConnector {
|
|||||||
|
|
||||||
Connection connection;
|
Connection connection;
|
||||||
|
|
||||||
DBConnector() throws IOException, SQLException {
|
DBConnector() throws IOException, SQLException, ClassNotFoundException {
|
||||||
this(loadProperties("dbProperties.json"));
|
this(loadProperties("dbProperties.json"));
|
||||||
}
|
}
|
||||||
|
|
||||||
DBConnector(Properties dbProperties) throws SQLException {
|
DBConnector(Properties dbProperties) throws SQLException, ClassNotFoundException {
|
||||||
|
Class.forName("org.mariadb.jdbc.Driver");
|
||||||
System.out.println(dbProperties);
|
System.out.println(dbProperties);
|
||||||
connection = DriverManager.getConnection(dbProperties.get("url").toString(), dbProperties);
|
System.out.println(DBConnector.buildURL(dbProperties));
|
||||||
|
connection = DriverManager.getConnection(dbProperties.get("url").toString(), dbProperties.get("user").toString(), dbProperties.get("password").toString());
|
||||||
|
System.out.println(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
try {
|
||||||
|
connection.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Connection getConnection() {
|
||||||
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Properties loadProperties(String path) throws IOException {
|
public static Properties loadProperties(String path) throws IOException {
|
||||||
@ -29,4 +43,11 @@ public class DBConnector {
|
|||||||
propertiesJSON.keys().forEachRemaining(key -> toReturn.setProperty(key, propertiesJSON.get(key).toString()));
|
propertiesJSON.keys().forEachRemaining(key -> toReturn.setProperty(key, propertiesJSON.get(key).toString()));
|
||||||
return toReturn;
|
return toReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String buildURL(Properties dbProperties) {
|
||||||
|
String dbURL = dbProperties.get("url").toString();
|
||||||
|
dbURL += "?user=" + dbProperties.get("user").toString();
|
||||||
|
dbURL += "&password=" + dbProperties.get("password").toString();
|
||||||
|
return dbURL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,9 +8,21 @@ public class InputUtils {
|
|||||||
if ((inputMap.get("context") != null) && (inputMap.get("context") instanceof Map<?, ?>)) {
|
if ((inputMap.get("context") != null) && (inputMap.get("context") instanceof Map<?, ?>)) {
|
||||||
contextMap = ((Map<String, Object>) inputMap.get("context"));
|
contextMap = ((Map<String, Object>) inputMap.get("context"));
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("The key \"Context\" must exist and be a map");
|
throw new IllegalArgumentException("The key \"context\" must exist and be a map");
|
||||||
}
|
}
|
||||||
System.out.println(inputMap.get("context"));
|
System.out.println(inputMap.get("context"));
|
||||||
System.out.println(contextMap.get("sub"));
|
System.out.println(contextMap.get("sub"));
|
||||||
|
return contextMap.get("sub").toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, Object> getBody(Map<String, Object> inputMap) {
|
||||||
|
return getMap(inputMap, "body");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, Object> getMap(Map<String, Object> parentMap, String childKey) {
|
||||||
|
if ((parentMap.get(childKey) != null) && (parentMap.get(childKey) instanceof Map<?, ?>)) {
|
||||||
|
return ((Map<String, Object>) parentMap.get(childKey));
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("The key \"" + childKey + "\" must exist and be a map");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,27 @@
|
|||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class ListAdder {
|
public class ListAdder {
|
||||||
|
|
||||||
|
private DBConnector connector;
|
||||||
|
private String cognitoID;
|
||||||
|
|
||||||
|
private final String LIST_CREATE = "INSERT INTO Lists (Name, Owner) VALUES (?, ?)";
|
||||||
|
|
||||||
|
ListAdder(DBConnector connector, String cognitoID) {
|
||||||
|
this.connector = connector;
|
||||||
|
this.cognitoID = cognitoID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(Map<String, Object> bodyMap) throws SQLException {
|
||||||
|
Connection connection = connector.getConnection();
|
||||||
|
PreparedStatement statement = connection.prepareStatement(LIST_CREATE);
|
||||||
|
statement.setString(1, bodyMap.get("Name").toString());
|
||||||
|
statement.setString(2, cognitoID);
|
||||||
|
System.out.println(statement);
|
||||||
|
statement.executeUpdate();
|
||||||
|
connection.commit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,11 +11,17 @@ public class ListsPOST implements RequestHandler<Map<String,Object>, String>{
|
|||||||
public String handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
public String handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
||||||
String cognitoID = InputUtils.getCognitoIDFromBody(inputMap);
|
String cognitoID = InputUtils.getCognitoIDFromBody(inputMap);
|
||||||
try {
|
try {
|
||||||
System.out.println(new DBConnector());
|
DBConnector connector = new DBConnector();
|
||||||
} catch (IOException e) {
|
try {
|
||||||
e.printStackTrace();
|
new ListAdder(connector, cognitoID).add(InputUtils.getBody(inputMap));
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
connector.close();
|
||||||
|
}
|
||||||
|
} catch (IOException|SQLException|ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new RuntimeException(e.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"url": "http://aws.com/something",
|
|
||||||
"user": "aUser",
|
|
||||||
"password": "aPassword"
|
|
||||||
}
|
|
||||||
@ -2,33 +2,17 @@
|
|||||||
#Base script from: https://github.com/NMerz/DoctorsNote/blob/master/AWS%20Setup/Lambda-GatewayInitialization.sh
|
#Base script from: https://github.com/NMerz/DoctorsNote/blob/master/AWS%20Setup/Lambda-GatewayInitialization.sh
|
||||||
|
|
||||||
#NOTE: This has been tested and works; however, the apigateway does not properly show as a trigger in AWS's web UI
|
#NOTE: This has been tested and works; however, the apigateway does not properly show as a trigger in AWS's web UI
|
||||||
#NOTE2: This assumes that the root Gateway and Lambda role have been set up previously (one-time setup) and their values are stored in the constants below
|
#NOTE2: This assumes that the root Gateway and Lambda role have been set up previously (one-time setup) and their values are set in VarSetup.sh
|
||||||
|
|
||||||
#constants
|
echo "Creating a Gateway/Lambda pair."
|
||||||
APIID=datoh7woc9 #rest-api-id is tied to the apigateway while resource-id seems tied to the specific url extension
|
|
||||||
ROOTRESOURCEID=6xrzhzidxh #gateway root should have a consistent resource id which will serve as parent for many apis
|
|
||||||
LAMBDAROLE=arn:aws:iam::569815541706:role/LambdaBasic
|
|
||||||
LANGUAGE=java11
|
|
||||||
DEPLOYSTAGE=Development
|
|
||||||
|
|
||||||
DEBUGFILE=/dev/null
|
REL_SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
|
||||||
|
|
||||||
echo -n "Please enter function/endpoint name: "
|
source ${REL_SCRIPT_DIR}/VarSetup.sh
|
||||||
read functionName
|
|
||||||
echo -n "Please enter method(GET, POST, etc.): "
|
|
||||||
read method
|
|
||||||
|
|
||||||
jarPath=$(find .. -name "${functionName}.jar")
|
|
||||||
if [[ "$jarPath" == "" ]]; then
|
|
||||||
echo "Unable to find file ${functionName}.jar" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
functionPath=${jarPath%/${functionName}.jar}
|
|
||||||
zipPath=${functionPath}.zip
|
|
||||||
|
|
||||||
zip ${zipPath} ${jarPath}
|
RAWLAMBDA=$(aws lambda create-function --function-name ${functionName}${method} --zip-file fileb://${zipPath} --runtime ${LANGUAGE} --role ${LAMBDAROLE} --handler ${functionName}${method} 2>${DEBUGFILE})
|
||||||
|
|
||||||
RAWLAMBDA=$(aws lambda create-function --function-name ${functionName}${method} --zip-file fileb://${zipPath} --runtime ${LANGUAGE} --role ${LAMBDAROLE} --handler ${functionName}${method}.lambda_handler 2>${DEBUGFILE})
|
|
||||||
|
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
echo "Unable to create Lamba" >&2
|
echo "Unable to create Lamba" >&2
|
||||||
|
|||||||
17
Tooling/LambdaUpdate.sh
Normal file
17
Tooling/LambdaUpdate.sh
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
echo "Updating a Lambda."
|
||||||
|
|
||||||
|
REL_SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
|
||||||
|
|
||||||
|
source ${REL_SCRIPT_DIR}/VarSetup.sh
|
||||||
|
|
||||||
|
|
||||||
|
aws lambda update-function-code --function-name ${functionName}${method} --zip-file fileb://${jarPath} 1>${DEBUGFILE} 2>${DEBUGFILE}
|
||||||
|
|
||||||
|
if [[ $? -ne 0 ]]; then
|
||||||
|
echo "Unable to update Lamba" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Update successful."
|
||||||
|
exit 0
|
||||||
26
Tooling/VarSetup.sh
Normal file
26
Tooling/VarSetup.sh
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Meant to be source'd from other scripts that need these vars.
|
||||||
|
|
||||||
|
#constants
|
||||||
|
APIID=datoh7woc9 #rest-api-id is tied to the apigateway while resource-id seems tied to the specific url extension
|
||||||
|
ROOTRESOURCEID=6xrzhzidxh #gateway root should have a consistent resource id which will serve as parent for many apis
|
||||||
|
LAMBDAROLE=arn:aws:iam::569815541706:role/LambdaBasic
|
||||||
|
LANGUAGE=java11
|
||||||
|
DEPLOYSTAGE=Development
|
||||||
|
|
||||||
|
DEBUGFILE=/dev/null
|
||||||
|
|
||||||
|
REL_SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
|
||||||
|
|
||||||
|
echo -n "Please enter base function name: "
|
||||||
|
read functionName
|
||||||
|
echo -n "Please enter method(GET, POST, etc.): "
|
||||||
|
read method
|
||||||
|
|
||||||
|
jarPath=$(find ${REL_SCRIPT_DIR}/.. -name "${functionName}.jar")
|
||||||
|
if [[ "$jarPath" == "" ]]; then
|
||||||
|
echo "Unable to find file ${functionName}.jar" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
functionPath=${jarPath%/${functionName}.jar}
|
||||||
Loading…
Reference in New Issue
Block a user