@@ -64,16 +64,15 @@ runs:
6464 API_URL_BASE="https://api.localstack.cloud/v1/compute/instances"
6565
6666 source ${{ github.action_path }}/../retry-function.sh
67+
6768 fetch_instances() {
6869 local list_response
6970 list_response=$(curl --fail-with-body -s -X GET \
7071 -H "$AUTH_HEADER" \
7172 -H "$CONTENT_TYPE_HEADER" \
7273 "$API_URL_BASE")
73- if [ $? -ne 0 ]; then
74- echo "API returned an error: $list_response"
75- return 1
76- fi
74+ if [ $? -ne 0 ]; then echo "curl command failed while fetching instances. Response: $list_response" >&2; return 1; fi
75+ if ! check_for_api_error "$list_response" "fetch instances"; then return 1; fi
7776 echo "$list_response"
7877 }
7978
@@ -90,16 +89,18 @@ runs:
9089
9190 delete_instance() {
9291 # We expect a 200 on success or 404 if it's already gone. Other codes are errors.
93- local http_code
94- http_code =$(curl --fail-with-body -s -o /dev/null - w "%{http_code}" -X DELETE \
92+ local response
93+ response =$(curl --fail-with-body -s -w "\n %{http_code}" -X DELETE \
9594 -H "$AUTH_HEADER" \
9695 -H "$CONTENT_TYPE_HEADER" \
9796 "$API_URL_BASE/$previewName")
9897 local exit_code=$?
99- if [ $exit_code -ne 0 ]; then
100- echo "Error deleting instance, curl failed with exit code $exit_code. API response: $http_code" >&2
101- return 1
102- fi
98+ local http_code=$(echo "$response" | tail -n1)
99+ local body=$(echo "$response" | sed '$d')
100+
101+ if [ $exit_code -ne 0 ]; then echo "curl command failed while deleting instance. Response: $body" >&2; return 1; fi
102+ if ! check_for_api_error "$body" "delete instance"; then return 1; fi
103+
103104 if [ "$http_code" -eq 200 ]; then
104105 echo "Instance '$previewName' deleted successfully."
105106 fi
@@ -119,9 +120,10 @@ runs:
119120 -H "$AUTH_HEADER" \
120121 -H "$CONTENT_TYPE_HEADER" \
121122 "$API_URL_BASE")
122- if [ $? -ne 0 ] || ! echo "$response" | jq -e 'has("endpoint_url") and (.endpoint_url | test(".+"))' > /dev/null; then
123- echo "Unable to create preview environment. API response: $response"
124- return 1
123+ if [ $? -ne 0 ]; then echo "curl command failed while creating instance. Response: $response" >&2; return 1; fi
124+ if ! check_for_api_error "$response" "create instance"; then return 1; fi
125+ if ! echo "$response" | jq -e 'has("endpoint_url") and (.endpoint_url | test(".+"))' > /dev/null; then
126+ echo "Invalid response from instance creation API: $response" >&2; return 1;
125127 fi
126128 echo "$response"
127129 }
@@ -169,22 +171,12 @@ runs:
169171 -H "$AUTH_HEADER" \
170172 -H "$CONTENT_TYPE_HEADER" \
171173 "$API_URL_BASE/$previewName/logs")
172- if [ $? -ne 0 ]; then
173- echo "API returned an error while fetching logs: $log_response"
174- return 1
175- fi
176- if [ -z "$log_response" ]; then
177- echo "API returned an empty response when fetching logs."
178- return 1
179- fi
174+ if [ $? -ne 0 ]; then echo "curl command failed while fetching logs. Response: $log_response" >&2; return 1; fi
175+ if ! check_for_api_error "$log_response" "fetch logs"; then return 1; fi
180176
181- # Now, check if jq extracts any content
182- local extracted_content
183- extracted_content=$(echo "$log_response" | jq -r '.[].content')
184- if [ -z "$extracted_content" ]; then
185- echo "No content found in logs or logs are empty after extraction."
186- echo "Raw API response for logs: $log_response" # This will be printed on retry attempts
187- return 1
177+ # A valid log response must be a JSON array.
178+ if ! echo "$log_response" | jq -e 'if type == "array" then true else false end' > /dev/null; then
179+ echo "Invalid response from logs API (expected a JSON array): $log_response" >&2; return 1;
188180 fi
189181
190182 echo "$log_response"
0 commit comments