Add accounts and locations lookup commands
This commit is contained in:
@@ -1,52 +1,20 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# bms-accounts.sh — List servicedesk accounts
|
|
||||||
|
|
||||||
set -euo pipefail
|
# Get token from bms-auth.sh
|
||||||
|
if ! token=$(bash "${SCRIPT_DIR}/bms-auth.sh" get-token 2>/dev/null); then
|
||||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
echo "Error: Failed to retrieve BMS token" >&2
|
||||||
BMS_API_BASE="${BMS_API_BASE:-https://api.bms.kaseya.com}"
|
exit 1
|
||||||
|
|
||||||
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
|
fi
|
||||||
|
|
||||||
echo "=== Servicedesk Accounts ===" >&2
|
curl --silent --show-error --fail \
|
||||||
echo "$items" | jq -r '.[] | "\(.Id // .id)\t\(.Name // .name)\t\(.Code // .code // "")"' |
|
-H "Authorization: Bearer $token" \
|
||||||
awk '{printf "%-10s %-40s %-20s\n", $1, $2, $3}'
|
"https://api.bms.kaseya.com/v2/servicedesk/accounts/lookup" | \
|
||||||
}
|
jq -r '.result[]? // .[]' | \
|
||||||
|
while IFS= read -r line; do
|
||||||
|
id=$(echo "$line" | jq -r '.Id')
|
||||||
|
name=$(echo "$line" | jq -r '.Name')
|
||||||
|
code=$(echo "$line" | jq -r '.Code')
|
||||||
|
printf "%-10s %-40s %-20s\n" "$id" "$name" "$code"
|
||||||
|
done
|
||||||
|
|
||||||
# Main
|
exit 0
|
||||||
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
|
|
||||||
|
|||||||
@@ -1,90 +1,30 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# bms-locations.sh — List servicedesk locations for an account
|
|
||||||
|
|
||||||
set -euo pipefail
|
# Get token from bms-auth.sh
|
||||||
|
if ! token=$(bash "${SCRIPT_DIR}/bms-auth.sh" get-token 2>/dev/null); then
|
||||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
echo "Error: Failed to retrieve BMS token" >&2
|
||||||
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 <account_id>
|
|
||||||
|
|
||||||
Fetch servicedesk locations for a given account.
|
|
||||||
|
|
||||||
Options:
|
|
||||||
--account <id> Account ID (required)
|
|
||||||
-h, --help Show this help
|
|
||||||
EOF
|
|
||||||
exit 1
|
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 <id>"
|
|
||||||
|
|
||||||
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
|
fi
|
||||||
|
|
||||||
echo "=== Servicedesk Locations (Account: $account_id) ===" >&2
|
case "${1}" in
|
||||||
echo "$items" | jq -r '.[] | "\(.Id // .id)\t\(.Name // .name)"' |
|
--account|--account=*)
|
||||||
awk '{printf "%-10s %-40s\n", $1, $2}'
|
account_id="${1#*=}" || account_id="${2}"
|
||||||
}
|
shift $(($# > 1 ? 2 : 1))
|
||||||
|
;;
|
||||||
# Main
|
*)
|
||||||
cmd="${1:-}"
|
echo "Usage: $0 --account <id>" >&2
|
||||||
[[ -n "$cmd" ]] || usage
|
exit 1
|
||||||
shift
|
;;
|
||||||
|
|
||||||
case "$cmd" in
|
|
||||||
list) cmd_locations_list "$@" ;;
|
|
||||||
*) die "Unknown command: $cmd. Available: list" ;;
|
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
curl --silent --show-error --fail \
|
||||||
|
-H "Authorization: Bearer $token" \
|
||||||
|
"https://api.bms.kaseya.com/v2/servicedesk/locations?accountId=$account_id" | \
|
||||||
|
jq -r '.result[]? // .[]' | \
|
||||||
|
while IFS= read -r line; do
|
||||||
|
id=$(echo "$line" | jq -r '.Id')
|
||||||
|
name=$(echo "$line" | jq -r '.Name')
|
||||||
|
printf "%-10s %-40s\n" "$id" "$name"
|
||||||
|
done
|
||||||
|
|
||||||
|
exit 0
|
||||||
|
|||||||
Reference in New Issue
Block a user