From 7bbd97c0f1fad2cc6b896ea384aec4802bb61e32 Mon Sep 17 00:00:00 2001 From: Aditya-ranjan-16 Date: Wed, 30 Jul 2025 13:46:03 +0530 Subject: [PATCH 1/3] fix: improved error handling --- scripts/reset_iks_api_key.sh | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/scripts/reset_iks_api_key.sh b/scripts/reset_iks_api_key.sh index 0fda79ee..a807d6c3 100755 --- a/scripts/reset_iks_api_key.sh +++ b/scripts/reset_iks_api_key.sh @@ -19,6 +19,18 @@ if [[ -z "${RESOURCE_GROUP_ID}" ]]; then exit 1 fi +# ENVIRONMENT VARIABLE VALIDATION +if [[ -z "${IAM_TOKEN}" ]]; then + echo "Environment variable IAM_TOKEN is not set." >&2 + exit 1 +fi + +if [[ -z "${ACCOUNT_ID}" ]]; then + echo "Environment variable ACCOUNT_ID is not set." >&2 + exit 1 +fi + + get_cloud_endpoint() { iam_cloud_endpoint="${IBMCLOUD_IAM_API_ENDPOINT:-"iam.cloud.ibm.com"}" IBMCLOUD_IAM_API_ENDPOINT=${iam_cloud_endpoint#https://} @@ -48,7 +60,14 @@ fetch_data() { while [ "$url" != "null" ]; do # Fetch data from the API - IAM_RESPONSE=$(curl -s "$url" --header "Authorization: $IAM_TOKEN" --header "Content-Type: application/json") + IAM_RESPONSE=$(curl -s "$url" --header "Authorization: Bearer $IAM_TOKEN" --header "Content-Type: application/json") + + # check if the response is valid JSON. + if ! echo "${IAM_RESPONSE}" | jq -e . >/dev/null 2>&1; then + echo "Error: API did not return valid JSON." >&2 + echo "Response was: ${IAM_RESPONSE}" >&2 + exit 1 + fi ERROR_MESSAGE=$(echo "${IAM_RESPONSE}" | jq 'has("errors")') if [[ "${ERROR_MESSAGE}" != false ]]; then From d2589b9c198529dcb2a5e777708414b310ea4299 Mon Sep 17 00:00:00 2001 From: Aditya-ranjan-16 Date: Thu, 31 Jul 2025 13:19:48 +0530 Subject: [PATCH 2/3] fix --- scripts/reset_iks_api_key.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/reset_iks_api_key.sh b/scripts/reset_iks_api_key.sh index a807d6c3..381accfb 100755 --- a/scripts/reset_iks_api_key.sh +++ b/scripts/reset_iks_api_key.sh @@ -60,7 +60,7 @@ fetch_data() { while [ "$url" != "null" ]; do # Fetch data from the API - IAM_RESPONSE=$(curl -s "$url" --header "Authorization: Bearer $IAM_TOKEN" --header "Content-Type: application/json") + IAM_RESPONSE=$(curl -s "$url" --header "Authorization: $IAM_TOKEN" --header "Content-Type: application/json") # check if the response is valid JSON. if ! echo "${IAM_RESPONSE}" | jq -e . >/dev/null 2>&1; then From f954f5edf255b88ac448846579c1254bb17bd464 Mon Sep 17 00:00:00 2001 From: Aditya-ranjan-16 Date: Mon, 4 Aug 2025 13:13:19 +0530 Subject: [PATCH 3/3] fix --- scripts/reset_iks_api_key.sh | 57 ++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/scripts/reset_iks_api_key.sh b/scripts/reset_iks_api_key.sh index 381accfb..c83af2cf 100755 --- a/scripts/reset_iks_api_key.sh +++ b/scripts/reset_iks_api_key.sh @@ -57,24 +57,51 @@ reset=true # Function to fetch data and handle pagination fetch_data() { local url="$IAM_URL" + local fetch_attempt=0 + local retry_wait_time=5 while [ "$url" != "null" ]; do - # Fetch data from the API - IAM_RESPONSE=$(curl -s "$url" --header "Authorization: $IAM_TOKEN" --header "Content-Type: application/json") - - # check if the response is valid JSON. - if ! echo "${IAM_RESPONSE}" | jq -e . >/dev/null 2>&1; then - echo "Error: API did not return valid JSON." >&2 - echo "Response was: ${IAM_RESPONSE}" >&2 - exit 1 - fi + fetch_attempt=0 + # Retry loop for each API call + while [ $fetch_attempt -lt $MAX_ATTEMPTS ]; do + + # Fetch data from the API + IAM_RESPONSE=$(curl -s "$url" --header "Authorization: $IAM_TOKEN" --header "Content-Type: application/json") + + # check if the response is valid JSON. + if ! echo "${IAM_RESPONSE}" | jq -e . >/dev/null 2>&1; then + echo "Error: API did not return valid JSON on attempt $((fetch_attempt + 1))." >&2 + echo "Response was: ${IAM_RESPONSE}" >&2 + fetch_attempt=$((fetch_attempt + 1)) + + if [ $fetch_attempt -lt $MAX_ATTEMPTS ]; then + echo "Retrying in ${retry_wait_time} seconds..." >&2 + sleep $retry_wait_time + continue + else + echo "Maximum retry attempts reached for fetching data." >&2 + exit 1 + fi + fi - ERROR_MESSAGE=$(echo "${IAM_RESPONSE}" | jq 'has("errors")') - if [[ "${ERROR_MESSAGE}" != false ]]; then - echo "${IAM_RESPONSE}" | jq '.errors' - echo "Could not obtain api keys" - exit 1 - fi + ERROR_MESSAGE=$(echo "${IAM_RESPONSE}" | jq 'has("errors")') + if [[ "${ERROR_MESSAGE}" != false ]]; then + echo "API returned errors on attempt $((fetch_attempt + 1)):" >&2 + echo " ${IAM_RESPONSE}" >&2 + fetch_attempt=$((fetch_attempt + 1)) + + if [ $fetch_attempt -lt $MAX_ATTEMPTS ]; then + echo "Retrying in ${retry_wait_time} seconds..." >&2 + sleep $retry_wait_time + continue + else + echo "Maximum retry attempts reached. Could not obtain api keys." >&2 + exit 1 + fi + fi + # Success - break out of retry loop + break + done next_url=$(echo "${IAM_RESPONSE}" | jq -r '.next') key_descriptions=$(echo "$IAM_RESPONSE" | jq -r --arg name "${APIKEY_KEY_NAME}" '.apikeys | .[] | select(.name == $name) | .description')