#!/usr/bin/env bash # bms-accounts.sh — List servicedesk accounts set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" BMS_API_BASE="${BMS_API_BASE:-https://api.bms.kaseya.com}" die() { echo "ERROR: $*" >&2; exit 1; } get_token() { bash "${SCRIPT_DIR}/bms-auth.sh" get-token } bms_curl() { local path="$1"; shift local token token=$(get_token) curl -sf -X GET \ "${BMS_API_BASE}${path}" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" \ "$@" } cmd_accounts_list() { local response response=$(bms_curl "/v2/servicedesk/accounts/lookup") || die "Failed to fetch accounts" # Extract the array - response is {"result": [...]} local items items=$(echo "$response" | jq -r '.result // .Data // .Items // .' 2>/dev/null) || \ die "Failed to parse accounts response" # Check if it's an array if ! echo "$items" | jq -e 'type == "array"' >/dev/null 2>&1; then die "Expected an array of accounts, got: $(echo "$items" | jq -c . 2>/dev/null || echo "non-JSON")" fi echo "=== Servicedesk Accounts ===" >&2 echo "$items" | jq -r '.[] | "\(.Id // .id)\t\(.Name // .name)\t\(.Code // .code // "")"' | awk '{printf "%-10s %-40s %-20s\n", $1, $2, $3}' } # Main cmd="${1:-}" [[ -n "$cmd" ]] || { echo "Usage: bms-accounts.sh list" >&2; exit 1; } case "$cmd" in list) cmd_accounts_list ;; *) die "Unknown command: $cmd. Available: list" ;; esac