From fec6530898023205fd531480e26ad44fbe2b427c Mon Sep 17 00:00:00 2001 From: Eric Loyd Date: Wed, 31 Jul 2024 11:52:00 -0400 Subject: [PATCH] Added ability to apply changes while creating --- api.sh | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/api.sh b/api.sh index 7bae44c..3eb8a11 100755 --- a/api.sh +++ b/api.sh @@ -18,6 +18,7 @@ myOptions[API]="objects" myOptions[APIep]="servicestatus" myOptions[TestMode]="" myOptions[Create]="" +myOptions[Apply]="" myOptions[Options]="" verbose="0" tmpJSON="" @@ -125,12 +126,14 @@ print_help() { -cn|--configname config_name= ... comment | downtime | contact | host | service | hostgroup | ... --create doCreate="true" + --apply If we're creating something, then Apply Configuration -D = -f|--fields JQ-valid list of fields to show= --file load JSON from= --helpopt Show help for command options --help This text -hg|--hostgroup hostgroup= + -hgm hostgroup_members (mainly for creating hostgroups) -h|--host host_name= ... hostgroupmembers | servicegroupmembers > -j|--jq additional valid JQ= @@ -143,6 +146,7 @@ print_help() { -qq Same as --quick but add -o c but also print the first line of the CSV output (fields) --save save JSON to= -sg|--servicegroup servicegroup= + -sgm servicegroup_members (mainly for creating servicegroups) -s|--service service_description= --state 0, 1, or 2 (or other, I suppose) --stype 0, 1 (SOFT or HARD) @@ -182,12 +186,14 @@ while [ -n "$1" ]; do -cg|--contactgroup) myDir[contact_groups]="$2"; shift 2;; -cn|--configname) myDir[config_name]="$2"; shift 2;; --create) myOptions[Create]="true"; shift 1;; + --apply) myOptions[Apply]="true"; shift 1;; -D) myDir[$2]="$3"; shift 3;; -f|--fields) myOptions[Fields]="$2"; shift 2;; --file) myOptions[File]="$2"; shift 2;; --helpopt) print_helpopt;; --help) print_help;; -hg|--hostgroup) myDir[hostgroup_name]="$2"; shift 2;; + -hgm) myDir[hostgroup_members]="$2"; shift 2;; -h|--host) myDir[host_name]="$2"; shift 2;; -j|--jq) myOptions[MoreJQ]="$2"; shift 2;; --key) APIKEY="$2"; shift 2;; @@ -199,6 +205,7 @@ while [ -n "$1" ]; do -Q|--quick) myOptions[Quick]="true"; shift 1;; --save) myOptions[Save]="$2"; shift 2;; -sg|--servicegroup) myDir[servicegroup_name]="$2"; shift 2;; + -sgm) myDir[servicegroup_members]="$2"; shift 2;; -s|--service) myDir[service_description]="$2"; shift 2;; --stats) do_stats; shift 2;; --state) myDir[current_state]="$2"; shift 2;; @@ -241,27 +248,46 @@ do_api() { do_api_post() { api_start="$1" api_command="$2" + api_data="" + [ -n "$3" ] && api_data="$3&applyconfig=0" url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0" do_debug 2 "start=$1 command=$2" - do_debug 1 "Executing: $url" - $curl -XPOST -k "$url" -d "hostgroup_name=${myDir[HG]}&alias=${myDir[HG]}&applyconfig=0" + do_debug 1 "Executing: $curl -XPOST -k \"$url\" -d \"$api_data\"" + [ -z "${myOptions[TestMode]}" ] && $curl -XPOST -k "$url" -d "$api_data" } # At this time, all we can create ia a hostgroup create_hostgroup() { do_debug 1 "about to do an API post call" - do_api_post config hostgroup + api_data="hostgroup_name=${myDir[hostgroup_name]}&alias=${myDir[hostgroup_name]}" + [ -n "${myDir[host_name]}" ] && api_data+="&members=${myDir[host_name]}" + [ -n "${myDir[hostgroup_members]}" ] && api_data+="&hostgroup_members=${myDir[hostgroup_members]}" + do_api_post config hostgroup "$api_data" +} + +create_servicegroup() { + do_debug 1 "about to do an API post call" + api_data="servicegroup_name=${myDir[servicegroup_name]}&alias=${myDir[servicegroup_name]}" + [ -n "${myDir[service_description]}" ] && api_data+="&members=${myDir[service_description]}" + [ -n "${myDir[servicegroup_members]}" ] && api_data+="&servicegroup_members=${myDir[servicegroup_members]}" + do_api_post config servicegroup "$api_data" } do_create() { do_debug 1 "about to do a create_command" - [ -n "${myDir[HG]}" ] && create_hostgroup - exit + [ -n "${myDir[hostgroup_name]}" ] && create_hostgroup + [ -n "${myDir[servicegroup_members]}" ] && create_servicegroup +} + +do_apply() { + do_debug 1 "About to Apply Configuration..." + do_api_post system applyconfig } # If we said "--create" then we want to make something if [ -n "${myOptions[Create]}" ]; then do_create + [ -n "${myOptions[Apply]}" -a -z "${myOptions[TestMode]}" ] && do_apply exit fi