From c2ea0805355a6e9fbbc7a13bd7315f36185e08f8 Mon Sep 17 00:00:00 2001 From: "Marcus A." Date: Tue, 7 Apr 2026 18:37:46 +0000 Subject: [PATCH] Add accounts and locations lookups --- scripts/bms-accounts.sh | 52 +++++++++++++++++++++++ scripts/bms-locations.sh | 90 ++++++++++++++++++++++++++++++++++++++++ scripts/bms.sh | 4 ++ 3 files changed, 146 insertions(+) create mode 100755 scripts/bms-accounts.sh create mode 100755 scripts/bms-locations.sh mode change 100755 => 100644 scripts/bms.sh diff --git a/scripts/bms-accounts.sh b/scripts/bms-accounts.sh new file mode 100755 index 0000000..62a44a9 --- /dev/null +++ b/scripts/bms-accounts.sh @@ -0,0 +1,52 @@ +#!/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 diff --git a/scripts/bms-locations.sh b/scripts/bms-locations.sh new file mode 100755 index 0000000..6315745 --- /dev/null +++ b/scripts/bms-locations.sh @@ -0,0 +1,90 @@ +#!/usr/bin/env bash +# bms-locations.sh — List servicedesk locations for an account + +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; } + +usage() { + cat >&2 <<'EOF' +Usage: bms-locations.sh list --account + +Fetch servicedesk locations for a given account. + +Options: + --account Account ID (required) + -h, --help Show this help +EOF + 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_locations_list() { + local account_id="" + + # Simple arg parsing + while [[ $# -gt 0 ]]; do + case "$1" in + --account) + account_id="$2" + shift 2 + ;; + --account=*) + account_id="${1#*=}" + shift + ;; + -h|--help) + usage + ;; + *) + die "Unknown option: $1" + ;; + esac + done + + [[ -n "$account_id" ]] || die "Missing required --account " + + local response + response=$(bms_curl "/v2/servicedesk/locations?accountId=${account_id}") || die "Failed to fetch locations" + + # Extract the array - response is {"result": [...]} + local items + items=$(echo "$response" | jq -r '.result // .Data // .Items // .' 2>/dev/null) || \ + die "Failed to parse locations response" + + # Check if it's an array + if ! echo "$items" | jq -e 'type == "array"' >/dev/null 2>&1; then + die "Expected an array of locations, got: $(echo "$items" | jq -c . 2>/dev/null || echo "non-JSON")" + fi + + echo "=== Servicedesk Locations (Account: $account_id) ===" >&2 + echo "$items" | jq -r '.[] | "\(.Id // .id)\t\(.Name // .name)"' | + awk '{printf "%-10s %-40s\n", $1, $2}' +} + +# Main +cmd="${1:-}" +[[ -n "$cmd" ]] || usage +shift + +case "$cmd" in + list) cmd_locations_list "$@" ;; + *) die "Unknown command: $cmd. Available: list" ;; +esac diff --git a/scripts/bms.sh b/scripts/bms.sh old mode 100755 new mode 100644 index 0378786..9dccf7f --- a/scripts/bms.sh +++ b/scripts/bms.sh @@ -15,6 +15,8 @@ Usage: bms tickets Manage tickets bms templates Browse ticket/note/timelog templates bms lookup Fetch lookup tables (IDs for statuses, etc.) + bms accounts List servicedesk accounts + bms locations --account List locations for an account Tickets subcommands: list List/search tickets @@ -77,6 +79,8 @@ case "$cmd" in tickets) exec bash "${SCRIPT_DIR}/bms-tickets.sh" "$@" ;; templates) exec bash "${SCRIPT_DIR}/bms-templates.sh" "$@" ;; lookup) exec bash "${SCRIPT_DIR}/bms-lookup.sh" "$@" ;; + accounts) exec bash "${SCRIPT_DIR}/bms-accounts.sh" "$@" ;; + locations) exec bash "${SCRIPT_DIR}/bms-locations.sh" "$@" ;; help|-h|--help) usage ;; *) echo "Unknown command: $cmd" >&2