#!/usr/bin/env bash
# ======================================================================
#
# PROOF OF CONCEPT
# DNS Client to access Infoblox DNS
#
# requires
#  - curl
#
# optional:
#  - jq
#
# ----------------------------------------------------------------------
# DOC: https://www.infoblox.com/wp-content/uploads/infoblox-deployment-infoblox-rest-api.pdf
# ----------------------------------------------------------------------
# 2021-01-20  v0.0  <axel.hahn@iml.unibe.ch>
# 2022-01-14  v1.0  <axel.hahn@iml.unibe.ch>  1st public version
# 2022-03-03  v1.1  <axel.hahn@iml.unibe.ch>  colored output if jq is found
# 2022-06-14  v1.2  <axel.hahn@iml.unibe.ch>  debug comments are written to stderr 
# 2022-09-14  v1.3  <axel.hahn@iml.unibe.ch>  show non JSON data as error message
# ======================================================================


# ----------------------------------------------------------------------
# CONFIG
# ----------------------------------------------------------------------

. "$( dirname $0 )"/inc_ib_config.sh || exit 1

# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------

# ----- read cli params
method=GET
if echo "$1" | grep -E "^(GET|PUT|POST|DELETE)$" >/dev/null
then
    method=$1
    shift 1
fi

relUrl="$1"

# ----- make curl request
>&2 echo "; === DNS API :: $method ${relUrl}"

out=$(curl -u $myauth -X $method "${mybaseurl}${relUrl}" 2>/dev/null)
if echo "$out" | head -1 | grep '^[\[\{}]' >/dev/null ; then
    echo "$out" | ( if which jq >/dev/null 2>&1; then jq; else cat; echo ; fi )
else
    echo ERROR:
    echo "$out"
    exit 1
fi

# ----------------------------------------------------------------------