Skip to content

Commit

Permalink
[WIP] Added handling of deployed environments for submit_message.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
basiliskus committed Oct 24, 2024
1 parent 33e9bdf commit 35e2a95
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 59 deletions.
8 changes: 3 additions & 5 deletions scripts/hurl/rs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

source ./utils.sh

LOCAL_KEY_PATH="$CDCTI_HOME/mock_credentials/organization-trusted-intermediary-private-key-local.pem"

# default values
env=local
root=$CDCTI_HOME/examples/
Expand Down Expand Up @@ -66,10 +64,10 @@ parse_arguments() {

setup_credentials() {
if [ -z "$secret" ] && [ "$client_id" = "flexion" ] && [ "$env" = "local" ]; then
if [ -f "$LOCAL_KEY_PATH" ]; then
secret="$LOCAL_KEY_PATH"
if [ -f "$TI_LOCAL_PRIVATE_KEY_PATH" ]; then
secret="$TI_LOCAL_PRIVATE_KEY_PATH"
else
fail "Local environment key not found at: $LOCAL_KEY_PATH"
fail "Local environment key not found at: $TI_LOCAL_PRIVATE_KEY_PATH"
fi
fi

Expand Down
36 changes: 34 additions & 2 deletions scripts/hurl/submit_message.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Usage: $(basename "$0") -f <message_file.hl7> [-e <environment>]
Options:
-f <FILE> Message file path (required)
-e <ENVIRONMENT> Environment: local|staging|production (Default: $DEFAULT_ENV)
-x <KEY_PATH> Path to the client private key for authentication with RS API
-j <JWT> JWT token for authentication with TI API
-h Display this help and exit
EOF
exit 1
Expand All @@ -21,10 +23,12 @@ parse_arguments() {
# Show help if no arguments
[ $# -eq 0 ] && show_usage

while getopts "e:f:h" opt; do
while getopts "e:f:x:j:h" opt; do
case $opt in
e) env="$OPTARG" ;;
f) file="$OPTARG" ;;
x) private_key="$OPTARG" ;;
j) jwt="$OPTARG" ;;
h) show_usage ;;
?) fail "Invalid option: -$OPTARG" ;;
esac
Expand All @@ -40,7 +44,35 @@ parse_arguments() {
esac
}

setup_credentials() {
# Set default credentials for local environment
if [ "$env" = "local" ] && [ -z "$jwt" ]; then
if [ -f "$RS_LOCAL_JWT_PATH" ]; then
jwt=$(cat "$RS_LOCAL_JWT_PATH")
else
fail "Local JWT file not found at: $RS_LOCAL_JWT_PATH"
fi
fi

if [ "$env" = "local" ] && [ -z "$private_key" ]; then
if [ -f "$TI_LOCAL_PRIVATE_KEY_PATH" ]; then
private_key="$TI_LOCAL_PRIVATE_KEY_PATH"
else
fail "Local environment key not found at: $TI_LOCAL_PRIVATE_KEY_PATH"
fi
fi

# Validate credentials if not local environment
if [ "$env" != "local" ]; then
[ -z "$jwt" ] && fail "JWT (-j) is required for non-local environments"
[ -z "$private_key" ] && fail "Private key (-x) is required for non-local environments"
fi

[ -n "$private_key" ] && [ ! -f "$private_key" ] && fail "Private key file not found: $private_key"
}

check_installed_commands hurl jq az
check_apis "$(get_api_url "$env" "rs")" "$(get_api_url "$env" "ti")"
parse_arguments "$@"
submit_message "$file"
setup_credentials
submit_message "$env" "$file" "$private_key" "$jwt"
8 changes: 3 additions & 5 deletions scripts/hurl/ti.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

source ./utils.sh

LOCAL_JWT_PATH="$CDCTI_HOME/mock_credentials/report-stream-valid-token.jwt"

# default values
env=local
root=$CDCTI_HOME/examples/
Expand Down Expand Up @@ -58,10 +56,10 @@ parse_arguments() {

setup_credentials() {
if [ -z "$jwt" ] && [ "$client" = "report-stream" ] && [ "$env" = "local" ]; then
if [ -f "$LOCAL_JWT_PATH" ]; then
jwt=$(cat "$LOCAL_JWT_PATH")
if [ -f "$RS_LOCAL_JWT_PATH" ]; then
jwt=$(cat "$RS_LOCAL_JWT_PATH")
else
fail "Local JWT file not found at: $LOCAL_JWT_PATH"
fail "Local JWT file not found at: $RS_LOCAL_JWT_PATH"
fi
fi

Expand Down
2 changes: 1 addition & 1 deletion scripts/hurl/update_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ check_apis "$(get_api_url "$env" "rs")" "$(get_api_url "$env" "ti")"
find "$CDCTI_HOME/examples" -type f -name "*$FILE_NAME_SUFFIX_STEP_0.hl7" | while read -r file; do
echo "-----------------------------------------------------------------------------------------------------------"
echo "Submitting message: $file"
submit_message "$file"
submit_message "$env" "$file" "$TI_LOCAL_PRIVATE_KEY_PATH" "$(cat "$RS_LOCAL_JWT_PATH")"
done
114 changes: 68 additions & 46 deletions scripts/hurl/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ TI_API_LCL_URL="http://localhost:8080"
TI_API_STG_URL="https://cdcti-stg-api.azurewebsites.net:443"
TI_API_PRD_URL="https://cdcti-prd-api.azurewebsites.net:443"

TI_LOCAL_PRIVATE_KEY_PATH="$CDCTI_HOME/mock_credentials/organization-trusted-intermediary-private-key-local.pem"
RS_LOCAL_JWT_PATH="$CDCTI_HOME/mock_credentials/report-stream-valid-token.jwt"

fail() {
echo "Error: $1" >&2
exit 1
Expand Down Expand Up @@ -100,16 +103,48 @@ generate_jwt() {
-S "@$secret_path"
}

extract_submission_id() {
# requires: jq
local history_response=$1
echo "$history_response" | jq '.destinations[0].sentReports[0].externalName' | sed 's/.*-\([0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}\)-.*/\1/'
}

download_from_azurite() {
# requires: jq, az
local blob_name=$1
local file_path=$2

echo " Downloading from blob storage '$blob_name' to '$file_path'"
az storage blob download \
--container-name "reports" \
--name "$blob_name" \
--file "$file_path" \
--connection-string "$AZURITE_CONNECTION_STRING" \
--output none || {
echo "Download failed for blob '$blob_name'."
exit 1
}

if [[ "$file_path" == *.fhir ]]; then
echo " Formatting the content for: $file_path"
formatted_content=$(jq '.' "$file_path")
echo "$formatted_content" >"$file_path"
fi
}

check_submission_status() {
# requires: hurl, jq
local submission_id=$1
local env=$1
local submission_id=$2
local private_key=$3

local timeout=180 # 3 minutes
local retry_interval=10 # Retry every 10 seconds

start_time=$(date +%s)

while true; do
history_response=$(./rs.sh history -i "$submission_id")
history_response=$(./rs.sh history -i "$submission_id" -e "$env" -x "$private_key")
overall_status=$(echo "$history_response" | jq -r '.overallStatus')

echo -n " Status: $overall_status"
Expand All @@ -132,43 +167,20 @@ check_submission_status() {
done
}

extract_submission_id() {
# requires: jq
local history_response=$1
echo "$history_response" | jq '.destinations[0].sentReports[0].externalName' | sed 's/.*-\([0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}\)-.*/\1/'
}

download_from_azurite() {
# requires: jq, az
local blob_name=$1
local file_path=$2

echo " Downloading from blob storage '$blob_name' to '$file_path'"
az storage blob download \
--container-name "reports" \
--name "$blob_name" \
--file "$file_path" \
--connection-string "$AZURITE_CONNECTION_STRING" \
--output none || {
echo "Download failed for blob '$blob_name'."
exit 1
}

if [[ "$file_path" == *.fhir ]]; then
echo " Formatting the content for: $file_path"
formatted_content=$(jq '.' "$file_path")
echo "$formatted_content" >"$file_path"
fi
}

submit_message() {
# requires: hurl, jq, az
local file=$1
local env=$1
local file=$2
local private_key=$3
local jwt=$4

local message_file_path=$(dirname "$file")
local message_file_name=$(basename "$file")
local message_base_name="${message_file_name%.hl7}"
message_base_name="${message_base_name%"$FILE_NAME_SUFFIX_STEP_0"}"

echo "Debug: Processing file '$file' in environment '$env'"

msh9=$(awk -F'|' '/^MSH/ { print $9 }' "$file")
case "$msh9" in
"ORU^R01^ORU_R01")
Expand All @@ -187,35 +199,45 @@ submit_message() {

echo "Assuming receivers are '$first_leg_receiver' and '$second_leg_receiver' because of MSH-9 value '$msh9'"

waters_response=$(./rs.sh waters -f "$message_file_name" -r "$message_file_path")
waters_response=$(./rs.sh waters -f "$message_file_name" -r "$message_file_path" -e "$env" -x "$private_key")
submission_id=$(echo "$waters_response" | jq -r '.id')

echo "[First leg] Checking submission status for ID: $submission_id"
if ! check_submission_status "$submission_id"; then
if ! check_submission_status "$env" "$submission_id" "$private_key"; then
echo "Failed to deliver the first leg of the message. Skipping the next steps."
return
fi

inbound_submission_id=$(extract_submission_id "$history_response")
translated_blob_name="ready/$first_leg_receiver/$inbound_submission_id.fhir"
translated_file_path="$message_file_path/$message_base_name$FILE_NAME_SUFFIX_STEP_1.fhir"
download_from_azurite "$translated_blob_name" "$translated_file_path"

metadata_response=$(./ti.sh metadata -i "$inbound_submission_id")
if [ "$env" = "local" ]; then
translated_blob_name="ready/$first_leg_receiver/$inbound_submission_id.fhir"
translated_file_path="$message_file_path/$message_base_name$FILE_NAME_SUFFIX_STEP_1.fhir"
echo "Debug: Downloading translated file in local environment"
download_from_azurite "$translated_blob_name" "$translated_file_path"
fi

metadata_response=$(./ti.sh metadata -i "$inbound_submission_id" -e "$env" -j "$jwt")
outbound_submission_id=$(echo "$metadata_response" | jq -r '.issue[] | select(.details.text == "outbound submission id") | .diagnostics')

transformed_blob_name="receive/flexion.etor-service-sender/$outbound_submission_id.fhir"
transformed_file_path="$message_file_path/$message_base_name$FILE_NAME_SUFFIX_STEP_2.fhir"
download_from_azurite "$transformed_blob_name" "$transformed_file_path"
if [ "$env" = "local" ]; then
transformed_blob_name="receive/flexion.etor-service-sender/$outbound_submission_id.fhir"
transformed_file_path="$message_file_path/$message_base_name$FILE_NAME_SUFFIX_STEP_2.fhir"
echo "Debug: Downloading transformed file in local environment"
download_from_azurite "$transformed_blob_name" "$transformed_file_path"
fi

echo "[Second leg] Checking submission status for ID: $outbound_submission_id"
if ! check_submission_status "$outbound_submission_id"; then
if ! check_submission_status "$env" "$outbound_submission_id" "$private_key"; then
echo "Failed to deliver the second leg of the message. Skipping the next steps."
return
fi

final_submission_id=$(extract_submission_id "$history_response")
final_blob_name="ready/$second_leg_receiver/$final_submission_id.hl7"
final_file_path="$message_file_path/$message_base_name$FILE_NAME_SUFFIX_STEP_3.hl7"
download_from_azurite "$final_blob_name" "$final_file_path"
if [ "$env" = "local" ]; then
final_submission_id=$(extract_submission_id "$history_response")
final_blob_name="ready/$second_leg_receiver/$final_submission_id.hl7"
final_file_path="$message_file_path/$message_base_name$FILE_NAME_SUFFIX_STEP_3.hl7"
echo "Debug: Downloading final file in local environment"
download_from_azurite "$final_blob_name" "$final_file_path"
fi
}

0 comments on commit 35e2a95

Please sign in to comment.