Files
sysadm/tests/api-test.sh
Kris Moore 97e8af86cf New API call for lifepreserver. This one lists the replication
targets on a system

While here, update api-test.sh to save response to /tmp/api-response, so we dont
need to always cut-n-paste

REST Request:
-------------------------------
PUT /sysadm/lifepreserver
{
   "action" : "listreplication"
}

REST Response:
-------------------------------
{
    "args": {
        "listreplication": {
            "tank1->192.168.0.9": {
                "dataset": "tank1",
                "frequency": "22",
                "host": "192.168.0.9",
                "port": "22",
                "rdset": "tank/backups",
                "user": "backups"
            }
        }
    }
}

WebSocket Request:
-------------------------------
{
   "namespace" : "sysadm",
   "args" : {
      "action" : "listreplication"
   },
   "id" : "fooid",
   "name" : "lifepreserver"
}

WebSocket Response:
-------------------------------
{
  "args": {
    "listreplication": {
      "tank1->192.168.0.9": {
        "dataset": "tank1",
        "frequency": "22",
        "host": "192.168.0.9",
        "port": "22",
        "rdset": "tank/backups",
        "user": "backups"
      }
    }
  },
  "id": "fooid",
  "name": "response",
  "namespace": "sysadm"
}
2016-01-15 10:40:03 -05:00

106 lines
2.7 KiB
Bash
Executable File

#!/usr/local/bin/bash
# (Yes, yes, this is a bash script)
# Both resty/jsawk use bash'isms
# Set variable to call jsawk utility
JSAWK="./utils/jsawk -j js24"
which npm >/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
echo "Please install npm first"
exit 1
fi
pkg info p5-JSON >/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
echo "Please install p5-JSON first"
exit 1
fi
if [ ! -d "${HOME}/.npm/ws" ] ; then
npm install ws
if [ $? -ne 0 ] ; then
echo "Failed installing ws node module"
exit 1
fi
fi
if [ ! -d "${HOME}/.npm/wss" ] ; then
npm install wss
if [ $? -ne 0 ] ; then
echo "Failed installing wss node module"
exit 1
fi
fi
echo "Enter your username:"
echo -e ">\c"
read fuser
echo ""
echo "Enter your password:"
echo -e ">\c"
read -s fpass
echo ""
echo "Enter the namespace:"
echo -e "(sysadm)>\c"
read namesp
if [ -z "$namesp" ] ; then
namesp="sysadm"
fi
echo ""
echo "Enter the class name:"
echo -e "(lifepreserver)>\c"
read name
if [ -z "$name" ] ; then
name="lifepreserver"
fi
echo ""
echo "Enter the payload json:"
echo -e "{ \"action\":\"listcron\" }>\c"
read payload
if [ -z "$payload" ] ; then
payload="{ \"action\":\"listcron\" }"
fi
echo ""
# Source our resty functions
. ./utils/resty -W "https://127.0.0.1:12151" -H "Accept: application/json" -H "Content-Type: application/json" -u ${fuser}:${fpass}
# Save output to a file in addition to stdout
ofile="/tmp/api-response"
echo "" > /tmp/api-response
# Check the reply of this REST query
echo "" | tee -a $ofile
echo "REST Request:" | tee -a $ofile
echo "-------------------------------" | tee -a $ofile
echo "PUT /${namesp}/${name}" | tee -a $ofile
echo "${payload}" | perl -0007 -MJSON -ne'print to_json(from_json($_, {allow_nonref=>1}),{pretty=>1})."\n"' | tee -a $ofile
echo "" | tee -a $ofile
echo "REST Response:" | tee -a $ofile
echo "-------------------------------" | tee -a $ofile
PUT /${namesp}/${name} "${payload}" -v -k 2>/tmp/.rstErr | tee -a $ofile
if [ $? -ne 0 ] ; then
echo "Failed.. Error output:"
cat /tmp/.rstErr
fi
# Now check the response via WebSockets
export NODE_TLS_REJECT_UNAUTHORIZED=0
echo "" | tee -a $ofile
echo "WebSocket Request:" | tee -a $ofile
echo "-------------------------------" | tee -a $ofile
echo "{ \"namespace\":\"${namesp}\", \"name\":\"${name}\", \"id\":\"fooid\", \"args\":${payload} }" | perl -0007 -MJSON -ne'print to_json(from_json($_, {allow_nonref=>1}),{pretty=>1})."\n"' | tee -a $ofile
echo "" | tee -a $ofile
echo "WebSocket Response:" | tee -a $ofile
echo "-------------------------------" | tee -a $ofile
echo "{ \"namespace\":\"${namesp}\", \"name\":\"${name}\", \"id\":\"fooid\", \"args\":${payload} }" | node sendwebsocket.js "$fuser" "$fpass" | tee -a $ofile