#!/usr/bin/env bash set -euo pipefail CONFIG_PATH="/home/node/.openclaw/workspace/.clawdbot/credentials/portainer/config.json" err() { echo "Error: $*" >&2 exit 1 } require_cmd() { command -v "$1" >/dev/null 2>&1 || err "Required command not found: $1" } usage() { cat >&2 <<'EOF' Usage: get-endpoint-id.sh "" Looks up a Portainer endpoint (environment) by exact name and prints its ID. Requires config at: /home/node/.openclaw/workspace/.clawdbot/credentials/portainer/config.json EOF exit 2 } [[ $# -eq 1 ]] || usage ENDPOINT_NAME="$1" require_cmd curl require_cmd jq [[ -f "$CONFIG_PATH" ]] || err "Missing config file: $CONFIG_PATH" BASE_URL="$(jq -r '.base_url // empty' "$CONFIG_PATH")" API_KEY="$(jq -r '.api_key // empty' "$CONFIG_PATH")" [[ -n "$BASE_URL" ]] || err "config.base_url is missing" [[ -n "$API_KEY" ]] || err "config.api_key is missing" BASE_URL="${BASE_URL%/}" response="$(curl -sS -w $'\n%{http_code}' \ -H "X-API-Key: $API_KEY" \ "$BASE_URL/api/endpoints")" http_code="$(printf '%s' "$response" | tail -n1)" body="$(printf '%s' "$response" | sed '$d')" if [[ "$http_code" -lt 200 || "$http_code" -ge 300 ]]; then msg="$(printf '%s' "$body" | jq -r '.message // empty' 2>/dev/null || true)" [[ -n "$msg" ]] || msg="$body" err "Portainer API request failed (HTTP $http_code): $msg" fi endpoint_id="$(printf '%s' "$body" | jq -r --arg name "$ENDPOINT_NAME" '.[] | select(.Name == $name) | .Id' | head -n1)" [[ -n "$endpoint_id" ]] || err "No endpoint found with name: $ENDPOINT_NAME" printf '%s\n' "$endpoint_id"