From 487fac18815ff364acab2627f4b7d94a971ad9f5 Mon Sep 17 00:00:00 2001 From: Bryan Barajas Date: Thu, 18 Jul 2024 21:56:58 +0000 Subject: [PATCH] FIX - Biganimal module - handle endpoint forward slashes --- .../biganimal/modules/biganimal/main.tf | 11 +++++------ .../biganimal/modules/biganimal/outputs.tf | 4 ++++ .../biganimal/modules/biganimal/variables.tf | 16 +++++++++++----- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/edbterraform/data/terraform/biganimal/modules/biganimal/main.tf b/edbterraform/data/terraform/biganimal/modules/biganimal/main.tf index d448af92..d6343f28 100644 --- a/edbterraform/data/terraform/biganimal/modules/biganimal/main.tf +++ b/edbterraform/data/terraform/biganimal/modules/biganimal/main.tf @@ -143,13 +143,13 @@ resource "toolbox_external" "api_biganimal" { <&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 @@ -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 @@ -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" @@ -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 diff --git a/edbterraform/data/terraform/biganimal/modules/biganimal/outputs.tf b/edbterraform/data/terraform/biganimal/modules/biganimal/outputs.tf index 058b477e..749a1cff 100644 --- a/edbterraform/data/terraform/biganimal/modules/biganimal/outputs.tf +++ b/edbterraform/data/terraform/biganimal/modules/biganimal/outputs.tf @@ -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 } diff --git a/edbterraform/data/terraform/biganimal/modules/biganimal/variables.tf b/edbterraform/data/terraform/biganimal/modules/biganimal/variables.tf index 0ec325e7..e57b68b9 100644 --- a/edbterraform/data/terraform/biganimal/modules/biganimal/variables.tf +++ b/edbterraform/data/terraform/biganimal/modules/biganimal/variables.tf @@ -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]}" @@ -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 ] } @@ -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 ]];