Skip to content

Commit

Permalink
FIX - Biganimal module - handle endpoint forward slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
bryan-bar committed Jul 18, 2024
1 parent 5d71722 commit 487fac1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
11 changes: 5 additions & 6 deletions edbterraform/data/terraform/biganimal/modules/biganimal/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ resource "toolbox_external" "api_biganimal" {
<<EOT
set -eou pipefail
URI="$${BA_API_URI:=https://portal.biganimal.com/api/v3/}"
URI="${data.external.ba_api_access.result.ba_api_uri}"
create_stage() {
ENDPOINT="projects/${var.project.id}/clusters"
REQUEST_TYPE="POST"
DATA='${jsonencode(local.API_DATA)}'
if ! RESULT=$(curl --silent --show-error --fail-with-body --location --request $REQUEST_TYPE --header "content-type: application/json" --header "$AUTH_HEADER" --url "$URI$ENDPOINT" --data "$DATA" 2>&1)
if ! RESULT=$(curl --silent --show-error --fail-with-body --location --request $REQUEST_TYPE --header "content-type: application/json" --header "$AUTH_HEADER" --url "$URI/$ENDPOINT" --data "$DATA" 2>&1)
then
RC="$${PIPESTATUS[0]}"
printf "URI: %s\n" "$URI" 1>&2
Expand All @@ -166,7 +166,7 @@ resource "toolbox_external" "api_biganimal" {
delete_stage() {
ENDPOINT="projects/${var.project.id}/clusters/$1"
REQUEST_TYPE="DELETE"
if ! RESULT=$(curl --silent --show-error --fail-with-body --location --request $REQUEST_TYPE --header "content-type: application/json" --header "$AUTH_HEADER" --url "$URI$ENDPOINT" 2>&1)
if ! RESULT=$(curl --silent --show-error --fail-with-body --location --request $REQUEST_TYPE --header "content-type: application/json" --header "$AUTH_HEADER" --url "$URI/$ENDPOINT" 2>&1)
then
RC="$${PIPESTATUS[0]}"
printf "%s\n" "$RESULT" 1>&2
Expand Down Expand Up @@ -242,8 +242,7 @@ resource "toolbox_external" "api_status" {
AUTH_HEADER="authorization: Bearer $BA_BEARER_TOKEN"
fi
URI="$${BA_API_URI:=https://portal.biganimal.com/api/v3/}"
URI="${data.external.ba_api_access.result.ba_api_uri}"
# Check cluster status
ENDPOINT="projects/${var.project.id}/clusters/${jsondecode(toolbox_external.api_biganimal.0.result.data).clusterId}"
REQUEST_TYPE="GET"
Expand All @@ -254,7 +253,7 @@ resource "toolbox_external" "api_status" {
SLEEP_TIME=15
while [[ $PHASE != *"healthy"* ]]
do
if ! RESULT=$(curl --silent --show-error --fail-with-body --location --request $REQUEST_TYPE --header "content-type: application/json" --header "$AUTH_HEADER" --url "$URI$ENDPOINT" 2>&1)
if ! RESULT=$(curl --silent --show-error --fail-with-body --location --request $REQUEST_TYPE --header "content-type: application/json" --header "$AUTH_HEADER" --url "$URI/$ENDPOINT" 2>&1)
then
RC="$${PIPESTATUS[0]}"
printf "%s\n" "$RESULT" 1>&2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ locals {
dbname = [ for uri_split in local.uri_split : split("/", uri_split.path)[1] ]
}

output "api_uri" {
value = data.external.ba_api_access.result.ba_api_uri
}

output "project_id" {
value = var.project.id
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,17 @@ data "external" "ba_api_access" {
fi
# Check for a valid project id, uri, and access to the endpoint
URI="$${BA_API_URI:=https://portal.biganimal.com/api/v3/}"
URI="$${BA_API_URI:=https://portal.biganimal.com/api/v3}"
# Strip trailing slashes, only required for direct api calls, not for the provider.
while [[ "$URI" == */ ]]
do
URI="$${URI%/}"
done
PROJECT_ID="${var.project.id}"
ENDPOINT="projects/$PROJECT_ID/cloud-providers"
REQUEST_TYPE="GET"
if ! RESPONSE=$(curl --silent --show-error --fail-with-body --location --request $REQUEST_TYPE --header "content-type: application/json" --header "$AUTH_HEADER" --url "$URI$ENDPOINT" 2>&1) || \
if ! RESPONSE=$(curl --silent --show-error --fail-with-body --location --request $REQUEST_TYPE --header "content-type: application/json" --header "$AUTH_HEADER" --url "$URI/$ENDPOINT" 2>&1) || \
! RESULT=$(printf "$RESPONSE" | jq -er .data | jq -er tostring 2>&1)
then
RC="$${PIPESTATUS[0]}"
Expand All @@ -224,7 +230,7 @@ data "external" "ba_api_access" {
exit "$RC"
fi
printf '{"data":"%s"}' "$(printf "$RESULT" | base64 -w 0)"
printf '{"data":"%s","ba_api_uri":"%s"}' "$(printf "$RESULT" | base64 -w 0)" "$URI"
EOT
]
}
Expand Down Expand Up @@ -388,11 +394,11 @@ resource "toolbox_external" "witness_node_params" {
AUTH_HEADER="authorization: Bearer $BA_BEARER_TOKEN"
fi
URI="$${BA_API_URI:=https://portal.biganimal.com/api/v3/}"
URI="${ data.external.ba_api_access.result.ba_api_uri }"
ENDPOINT="projects/${var.project.id}/utils/calculate-witness-group-params"
REQUEST_TYPE="PUT"
DATA='{"provider":{"cloudProviderId":"${ each.value.cloud_account ? each.value.cloud_service_provider : "bah:${each.value.cloud_service_provider}" }"},"region":{"regionId":"${each.value.region}"}}'
RESULT=$(curl --silent --show-error --fail-with-body --location --request $REQUEST_TYPE --header "content-type: application/json" --header "$AUTH_HEADER" --url "$URI$ENDPOINT" --data "$DATA")
RESULT=$(curl --silent --show-error --fail-with-body --location --request $REQUEST_TYPE --header "content-type: application/json" --header "$AUTH_HEADER" --url "$URI/$ENDPOINT" --data "$DATA")
RC=$?
if [[ $RC -ne 0 ]];
Expand Down

0 comments on commit 487fac1

Please sign in to comment.