186 lines
5.4 KiB
Bash
186 lines
5.4 KiB
Bash
#!/usr/bin/env bash
|
|
# bms-templates.sh — Kaseya BMS template lookups (tickets, notes, timelogs)
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
BMS_API_BASE="${BMS_API_BASE:-https://api.bms.kaseya.com}"
|
|
|
|
# ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
|
|
die() { echo "ERROR: $*" >&2; exit 1; }
|
|
|
|
get_token() {
|
|
bash "${SCRIPT_DIR}/bms-auth.sh" get-token
|
|
}
|
|
|
|
bms_curl() {
|
|
local method="$1"; shift
|
|
local path="$1"; shift
|
|
local token
|
|
token=$(get_token)
|
|
curl -sf -X "$method" \
|
|
"${BMS_API_BASE}${path}" \
|
|
-H "Authorization: Bearer ${token}" \
|
|
-H "Accept: application/json" \
|
|
-H "Content-Type: application/json" \
|
|
"$@"
|
|
}
|
|
|
|
# ─── Formatters ──────────────────────────────────────────────────────────────
|
|
|
|
format_template_list() {
|
|
jq -r '
|
|
(.Data // .Items // .) |
|
|
if type == "array" then .[] else . end |
|
|
[
|
|
(.Id // "-"),
|
|
(.Name // "(unnamed)"),
|
|
("Q:" + ((.QueueId // "-") | tostring)),
|
|
("Pri:" + ((.PriorityId // "-") | tostring)),
|
|
("Type:" + ((.IssueTypeId // .TypeId // "-") | tostring))
|
|
] | @tsv
|
|
' | sort -n | column -t -s $'\t'
|
|
}
|
|
|
|
format_simple_lookup() {
|
|
# Generic: Id + Name for note/timelog templates
|
|
jq -r '
|
|
(.Data // .Items // .) |
|
|
if type == "array" then .[] else . end |
|
|
"\(.Id // "-")\t\(.Name // .Text // "(unnamed)")"
|
|
' | sort -n | column -t -s $'\t'
|
|
}
|
|
|
|
# ─── Templates: Tickets ───────────────────────────────────────────────────────
|
|
|
|
cmd_templates_tickets_list() {
|
|
local format="table"
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--format|-f) format="$2"; shift 2 ;;
|
|
*) die "Unknown option: $1" ;;
|
|
esac
|
|
done
|
|
|
|
echo "=== Ticket Templates ===" >&2
|
|
local response
|
|
response=$(bms_curl GET "/v2/servicedesk/templates/tickets/lookup")
|
|
|
|
if [[ "$format" == "json" ]]; then
|
|
echo "$response" | jq .
|
|
else
|
|
echo "$response" | format_template_list
|
|
fi
|
|
}
|
|
|
|
cmd_templates_tickets_get() {
|
|
local template_id="${1:-}"
|
|
[[ -n "$template_id" ]] || die "Usage: bms templates tickets get <templateId>"
|
|
|
|
bms_curl GET "/v2/servicedesk/templates/tickets/${template_id}" | jq .
|
|
}
|
|
|
|
# ─── Templates: Notes ────────────────────────────────────────────────────────
|
|
|
|
cmd_templates_notes_list() {
|
|
local format="table"
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--format|-f) format="$2"; shift 2 ;;
|
|
*) die "Unknown option: $1" ;;
|
|
esac
|
|
done
|
|
|
|
echo "=== Note Templates ===" >&2
|
|
local response
|
|
response=$(bms_curl GET "/v2/servicedesk/templates/notes/lookup")
|
|
|
|
if [[ "$format" == "json" ]]; then
|
|
echo "$response" | jq .
|
|
else
|
|
echo "$response" | format_simple_lookup
|
|
fi
|
|
}
|
|
|
|
# ─── Templates: Timelogs ─────────────────────────────────────────────────────
|
|
|
|
cmd_templates_timelogs_list() {
|
|
local format="table"
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--format|-f) format="$2"; shift 2 ;;
|
|
*) die "Unknown option: $1" ;;
|
|
esac
|
|
done
|
|
|
|
echo "=== Timelog Templates ===" >&2
|
|
local response
|
|
response=$(bms_curl GET "/v2/servicedesk/templates/timelogs/lookup")
|
|
|
|
if [[ "$format" == "json" ]]; then
|
|
echo "$response" | jq .
|
|
else
|
|
echo "$response" | format_simple_lookup
|
|
fi
|
|
}
|
|
|
|
# ─── Dispatch ────────────────────────────────────────────────────────────────
|
|
|
|
usage_templates() {
|
|
cat >&2 <<'EOF'
|
|
Usage: bms templates <resource> <subcommand> [options]
|
|
|
|
Resources and subcommands:
|
|
tickets list List all ticket templates
|
|
tickets get <id> Get full details for a ticket template
|
|
notes list List all note templates
|
|
timelogs list List all timelog templates
|
|
|
|
Options:
|
|
--format json Output raw JSON instead of table
|
|
|
|
Examples:
|
|
bms templates tickets list
|
|
bms templates tickets get 42
|
|
bms templates notes list
|
|
bms templates timelogs list
|
|
EOF
|
|
exit 1
|
|
}
|
|
|
|
resource="${1:-}"
|
|
[[ -n "$resource" ]] || usage_templates
|
|
shift
|
|
|
|
case "$resource" in
|
|
tickets|ticket)
|
|
subcmd="${1:-list}"
|
|
[[ $# -gt 0 ]] && shift
|
|
case "$subcmd" in
|
|
list) cmd_templates_tickets_list "$@" ;;
|
|
get) cmd_templates_tickets_get "$@" ;;
|
|
*) die "Unknown tickets template subcommand: $subcmd (available: list, get)" ;;
|
|
esac
|
|
;;
|
|
notes|note)
|
|
subcmd="${1:-list}"
|
|
[[ $# -gt 0 ]] && shift
|
|
case "$subcmd" in
|
|
list) cmd_templates_notes_list "$@" ;;
|
|
*) die "Unknown notes template subcommand: $subcmd (available: list)" ;;
|
|
esac
|
|
;;
|
|
timelogs|timelog)
|
|
subcmd="${1:-list}"
|
|
[[ $# -gt 0 ]] && shift
|
|
case "$subcmd" in
|
|
list) cmd_templates_timelogs_list "$@" ;;
|
|
*) die "Unknown timelogs template subcommand: $subcmd (available: list)" ;;
|
|
esac
|
|
;;
|
|
*)
|
|
die "Unknown template resource: $resource (available: tickets, notes, timelogs)"
|
|
;;
|
|
esac
|