Skip to content

Commit

Permalink
Add DB mocks and sample RTM scenario (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-bigorajski authored Aug 6, 2024
1 parent 2e8d7ad commit 83c5eb6
Show file tree
Hide file tree
Showing 21 changed files with 1,112 additions and 8 deletions.
35 changes: 32 additions & 3 deletions designer/application-customizations.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,32 @@
scenarioTypes {
"streaming" {
# customize Flink streaming scenario type

modelConfig: {
modelConfig {
components {
customersDataEnricher {
providerType: databaseEnricher
config: {
databaseQueryEnricher {
name: "customers-data-query-enricher"
dbPool: ${rtmNearPosExampleDatabasePool} #refers to your database pool definition
}
databaseLookupEnricher {
name: "customers-data-lookup-enricher"
dbPool: ${rtmNearPosExampleDatabasePool}
}
}
}
posDataEnricher {
providerType: databaseEnricher
config: {
databaseLookupEnricher {
name: "pos-data-lookup-enricher"
dbPool: ${rtmNearPosExampleDatabasePool}
}
}
}
"customerProfileOffers" {
providerType: "openAPI"
providerType: "openAPI"
url: "http://mocks:8080/__admin/files/openapi/CustomerApi.yaml"
rootUrl: "http://mocks:8080/"
namePattern: "get.*"
Expand All @@ -23,3 +44,11 @@ scenarioTypes {
# customize Lite request-response scenario type
}
}

rtmNearPosExampleDatabasePool {
driverClassName: "org.postgresql.Driver"
url: "jdbc:postgresql://mocks:5432/mocks"
username: "mocks"
password: "mocks_pass"
schema: "rtm_near_pos_example"
}
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
deploy:
resources:
limits:
memory: 128M
memory: 256M

### Quickstart mocks
mocks:
Expand Down Expand Up @@ -86,7 +86,7 @@ services:
condition: service_started
mocks:
condition: service_healthy
expose:
expose:
- 8181
healthcheck:
test: [ "CMD-SHELL", "curl localhost:8080/api/app/healthCheck" ]
Expand Down
8 changes: 8 additions & 0 deletions flink/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
ARG FLINK_VERSION

FROM curlimages/curl:8.9.1 AS lib_provider

# Adding custom libraries ('add other libraries' section):
# https://nightlies.apache.org/flink/flink-docs-master/docs/deployment/resource-providers/standalone/docker/#further-customization
WORKDIR /libs
RUN curl -k --output /libs/postgresql-42.6.0.jar https://repo1.maven.org/maven2/org/postgresql/postgresql/42.6.0/postgresql-42.6.0.jar

FROM flink:${FLINK_VERSION}

USER root
RUN echo '#!/bin/sh' > /ex-docker-entrypoint.sh && \
echo 'export FLINK_PROPERTIES=$(cat /opt/flink/conf/flink-properties.yml) && /docker-entrypoint.sh "$@"' >> /ex-docker-entrypoint.sh && \
chmod +x /ex-docker-entrypoint.sh
COPY --from=lib_provider /libs/ /opt/flink/lib/

USER flink
COPY flink-properties.yml /opt/flink/conf/
Expand Down
17 changes: 14 additions & 3 deletions mocks/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM holomekc/wiremock-gui:3.8.1 AS wiremock

RUN apt-get update && \
RUN apt-get update && \
apt-get install -y wget && \
wget -P /var/wiremock/extensions https://repo1.maven.org/maven2/org/wiremock/extensions/wiremock-faker-extension-standalone/0.2.0/wiremock-faker-extension-standalone-0.2.0.jar

Expand All @@ -12,8 +12,13 @@ CMD ["/sbin/my_init"]
# install
USER root

RUN apt-get update -y && \
RUN apt-get install -y --no-install-recommends curl ca-certificates && \
install -d /usr/share/postgresql-common/pgdg && \
curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc && \
echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list && \
apt-get update -y && \
apt -y install openjdk-11-jre-headless && \
apt -y install postgresql-16 && \
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# WIREMOCK
Expand All @@ -24,7 +29,13 @@ COPY http-service/mocks /home/wiremock/
COPY http-service/scripts /etc/service/http-service
RUN mv /etc/service/http-service/run-wiremock.sh /etc/service/http-service/run

# POSTGRES
COPY db /home/postgres
RUN mkdir /etc/service/postgres && \
mv /home/postgres/scripts/run_postgres.sh /etc/service/postgres/run

EXPOSE 8080
EXPOSE 5432

HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=30 \
CMD (curl -f http://localhost:8080/__admin/) || exit 1
CMD (curl -f http://localhost:8080/__admin/ && pg_isready -d mocks -U mocks) || exit 1
61 changes: 61 additions & 0 deletions mocks/db/mocks/__ddl/rtm_near_pos_example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
-- cleanup for the sake idempotent run
drop table if exists contact_history;
drop table if exists blocked_list;
drop table if exists client;
drop table if exists pos;
drop type if exists consent_enum;
drop type if exists client_type_enum;

create table pos
(
id SERIAL PRIMARY KEY,
location_lat NUMERIC(10, 6) NOT NULL,
location_lon NUMERIC(10, 6) NOT NULL,
open_hour TIME NOT NULL,
close_hour TIME NOT NULL
);

create type consent_enum AS ENUM ('SMS', 'EMAIL', 'PUSH', 'SMS_EMAIL', 'EMAIL_PUSH', 'SMS_PUSH', 'SMS_EMAIL_PUSH');
create type client_type_enum AS ENUM ('INDIVIDUAL', 'BUSINESS');

create table client
(
id SERIAL PRIMARY KEY,
pos_id SERIAL REFERENCES pos(id) NOT NULL,
msisdn CHAR(11),
email VARCHAR(100),
consents consent_enum,
client_type client_type_enum NOT NULL
);

create table blocked_list(
client_id INT PRIMARY KEY references client(id)
);

create table contact_history(
id SERIAL PRIMARY KEY,
client_id INT NOT NULL references client(id),
event_time TIMESTAMP NOT NULL
);

---- POS
insert into pos(id, location_lat, location_lon, open_hour, close_hour) VALUES (1, 52.237049, 21.017532, '00:00:00', '23:59:59');
insert into pos(id, location_lat, location_lon, open_hour, close_hour) VALUES (2, 50.049683, 19.944544, '08:00:00', '15:00:00');
insert into pos(id, location_lat, location_lon, open_hour, close_hour) VALUES (3, 51.107883, 17.038538, '00:00:00', '23:59:59');

---- Clients
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (1, 1, '48500500500', '[email protected]', 'SMS', 'INDIVIDUAL');
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (2, 1, '48500500501', '[email protected]', 'SMS_EMAIL', 'BUSINESS');
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (3, 1, '48500500502', '[email protected]', 'PUSH', 'INDIVIDUAL');
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (4, 2, '48500500503', '[email protected]', 'SMS_EMAIL_PUSH', 'INDIVIDUAL');
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (5, 2, '48500500504', '[email protected]', 'EMAIL', 'BUSINESS');
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (6, 2, '48500500505', '[email protected]', null, 'BUSINESS');
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (7, 2, '48500500506', '[email protected]', 'EMAIL_PUSH', 'INDIVIDUAL');
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (8, 3, '48500500507', '[email protected]', 'SMS_PUSH', 'BUSINESS');
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (9, 3, '48500500508', '[email protected]', 'EMAIL', 'INDIVIDUAL');

---- Blocked
insert into blocked_list(client_id) values (5);

-- Contact history
insert into contact_history(client_id, event_time) VALUES (9, NOW() - INTERVAL '1 minutes');
6 changes: 6 additions & 0 deletions mocks/db/scripts/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash -e

strip_extension() {
local file="$1"
echo "${file%.*}"
}
42 changes: 42 additions & 0 deletions mocks/db/scripts/configure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash -e

cd "$(dirname "$0")"
. postgres_operations.sh
. common.sh

init_db() {
init_bg_log_file
init_data_dir
init_custom_conf_dir
configure_pg_config
configure_authentication
}

configure_users() {
create_user
create_custom_database
grant_privileges
alter_pg_user_pass
}

execute_ddls() {
local schema_name
local ddl_content
for file in "$PG_DDL_DIR"/*; do
if [ -f "$file" ]; then
schema_name=$(basename "$(strip_extension "$file")")
echo "Creating schema: $schema_name"
create_schema "$PG_USER" "$schema_name"
ddl_content=$(wrap_sql_with_current_schema "$schema_name" "$(cat "$file")")
echo "Executing ddl: $file with content: $ddl_content"
echo "$ddl_content" | execute_sql "" "$PG_USER" "$PG_PASS"
fi
done
}

init_db
start_bg
wait_until_started
configure_users
execute_ddls
stop
130 changes: 130 additions & 0 deletions mocks/db/scripts/postgres_operations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/bin/bash -e

init_data_dir() {
if [ ! -e "$PG_DATA_DIR" ]; then
mkdir -p "$PG_DATA_DIR"
chown postgres "$PG_DATA_DIR"
/sbin/setuser postgres "$PG_BIN_DIR"/initdb -D "$PG_DATA_DIR"
fi
}

init_custom_conf_dir() {
if [ ! -e "$PG_CUSTOM_CONF_DIR" ]; then
mkdir -p "$PG_CUSTOM_CONF_DIR"
chown postgres "$PG_CUSTOM_CONF_DIR"
fi
}

configure_authentication() {
if [ ! -f "$PG_HBA_FILE" ]; then
cp "$PG_DATA_DIR/pg_hba.conf" "$PG_HBA_FILE"
chown postgres "$PG_HBA_FILE"
echo "#<Custom configuration>" >> "$PG_HBA_FILE"
echo "host all all all md5" >> "$PG_HBA_FILE"
fi
}

configure_pg_config() {
if [ ! -f "$PG_CONF_FILE" ]; then
cp "$PG_DATA_DIR/postgresql.conf" "$PG_CONF_FILE"
chown postgres "$PG_CONF_FILE"
echo "#<Custom configuration>" >> "$PG_CONF_FILE"
echo "listen_addresses = '*'" >> "$PG_CONF_FILE"
fi
}

init_bg_log_file() {
local log_file
log_file="/var/log/postgres_bg.log"
if [ ! -f "$log_file" ]; then
touch "$log_file"
chown postgres "$log_file"
fi
}

wait_until_started() {
local max_startup_timeout_in_s=10
while ! pg_isready >/dev/null 2>&1; do
sleep 1
max_startup_timeout_in_s=$((max_startup_timeout_in_s - 1))
if ((max_startup_timeout_in_s <= 0)); then
echo "Postgres is not started"
exit 1
fi
done
echo "Postgres started"
}

create_custom_database() {
local db_name="${1:-$PG_DB_NAME}"
DB_EXISTS=$(echo "SELECT 1 FROM pg_database WHERE datname='$db_name'" | execute_sql "" "postgres" "" "-tA")
if [ "$DB_EXISTS" != "1" ]; then
echo "CREATE DATABASE \"$db_name\"" | execute_sql "" "postgres" ""
else
echo "DB already exists - creation skipped"
fi
}

create_user() {
ROLE_EXISTS=$(echo "SELECT 1 FROM pg_roles WHERE rolname='$PG_USER'" | execute_sql "" "postgres" "" "-tA")
if [ "$ROLE_EXISTS" != "1" ]; then
echo "CREATE ROLE \"${PG_USER}\" WITH LOGIN PASSWORD '${PG_PASS}';" | execute_sql "" "postgres" ""
else
echo "ROLE already exists - creation skipped"
fi
}

grant_privileges() {
local user="${1:-$PG_USER}"
local db_name="${2:-$PG_DB_NAME}"
execute_sql "" "postgres" "" <<EOF
GRANT ALL PRIVILEGES ON DATABASE "${db_name}" TO "${user}";
ALTER DATABASE "${db_name}" OWNER TO "${user}";
EOF
}

create_schema() {
local user="${1:-$PG_USER}"
local schema_name="${2:-PUBLIC}"
echo "CREATE SCHEMA IF NOT EXISTS \"$schema_name\" AUTHORIZATION \"$user\"" | execute_sql "$PG_DB_NAME" "postgres" ""
}

wrap_sql_with_current_schema() {
local schema_name="${1:-PUBLIC}"
local sql="$2"
cat <<EOF
SET search_path TO $schema_name;
$sql
RESET search_path;
EOF
}

alter_pg_user_pass() {
echo "ALTER ROLE postgres WITH PASSWORD 'postgres';" | execute_sql "" "postgres" ""
}

execute_sql() {
local -r db="${1:-}"
local -r user="${2:-postgres}"
local -r pass="${3:-}"
local opts
read -r -a opts <<<"${@:4}"
local args=("-U" "$user" "-p" "${PG_PORT:-5432}" "-h" "127.0.0.1")
[[ -n "$db" ]] && args+=("-d" "$db")
[[ "${#opts[@]}" -gt 0 ]] && args+=("${opts[@]}")
PGPASSWORD=$pass psql "${args[@]}"
}

start_bg() {
/sbin/setuser postgres "$PG_BIN_DIR"/pg_ctl start -D "$PG_DATA_DIR" -l /var/log/postgres_bg.log
}

start() {
/sbin/setuser postgres "$PG_BIN_DIR"/postgres -D "$PG_DATA_DIR" "--hba_file=$PG_HBA_FILE" "--config-file=$PG_CONF_FILE"
}

stop() {
/sbin/setuser postgres "$PG_BIN_DIR"/pg_ctl stop -w -D "$PG_DATA_DIR"
}

"$@"
17 changes: 17 additions & 0 deletions mocks/db/scripts/run_postgres.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

export PG_DB_NAME="mocks"
export PG_USER="mocks"
export PG_PASS="mocks_pass"

export PG_BIN_DIR="/usr/lib/postgresql/16/bin"
export PG_BASE_DIR="/home/postgres"
export PG_DATA_DIR="$PG_BASE_DIR/data"
export PG_CUSTOM_BIN_DIR="$PG_BASE_DIR/scripts"
export PG_DDL_DIR="$PG_BASE_DIR/mocks/__ddl"
export PG_CUSTOM_CONF_DIR="$PG_BASE_DIR/conf"
export PG_CONF_FILE="$PG_CUSTOM_CONF_DIR/postgresql.conf"
export PG_HBA_FILE="$PG_CUSTOM_CONF_DIR/pg_hba.conf"

"$PG_CUSTOM_BIN_DIR"/configure.sh
exec "$PG_CUSTOM_BIN_DIR"/postgres_operations.sh start
Loading

0 comments on commit 83c5eb6

Please sign in to comment.