@@ -18,77 +18,116 @@ To simplify RESTCONF operations, create a `curl.sh` wrapper script:
1818#! /bin/sh
1919# RESTCONF CLI wrapper for curl
2020
21- # Show usage and exit
21+ HOST=${HOST:- infix.local}
22+ DATASTORE=running
23+ AUTH=admin:admin
24+
2225usage ()
2326{
24- cat << -EOF >&2
25- Usage: $0 [-h HOST] [-d DATASTORE] [-u USER:PASS] METHOD PATH [CURL_ARGS...]
26-
27- Options:
28- -h HOST Target host (default: infix.local)
29- -d DS Datastore: running, operational, startup (default: running)
30- -u CREDS Credentials as user:pass (default: admin:admin)
31-
32- Methods: GET, POST, PUT, PATCH, DELETE
33- EOF
34- exit " $1 "
27+ cat << EOF >&2
28+ Usage: $0 [-v] [-h HOST] [-d DATASTORE] [-u USER:PASS] METHOD XPATH [CURL_ARGS...]
29+
30+ Options:
31+ -h HOST Target host (default: infix.local)
32+ -d DS Datastore: running, operational, startup (default: running)
33+ -u CREDS Credentials as user:pass (default: admin:admin)
34+ -v Verbose mode, use it to display variables and curl command
35+
36+ Methods: GET, POST, PUT, PATCH, DELETE
37+ EOF
38+ exit " $1 "
3539}
3640
37- # Default values
38- HOST=${HOST:- infix.local}
39- DATASTORE=running
40- AUTH=admin:admin
41+ check ()
42+ {
43+ hint=" Note: URL may be missing a module prefix (e.g., ietf-system:system)"
44+ resp=" $1 "
45+ url=" $2 "
46+
47+ if ! command -v jq > /dev/null 2>&1 ; then
48+ printf " %s\n" " $resp "
49+ return
50+ fi
51+
52+ case " $resp " in
53+ * " Syntax error" * )
54+ path_only=" ${url#*// } "
55+
56+ # Check for common URL error(s), e.g., missing module prefix
57+ case " $path_only " in
58+ * " :" * )
59+ printf " %s\n" " $resp " | jq .
60+ ;;
61+ * )
62+ printf " %s\n" " $resp " | jq --arg hint " $hint " \
63+ ' .["ietf-restconf:errors"].error[] |= . + {comment: $hint}'
64+ ;;
65+ esac
66+ ;;
67+ * )
68+ printf " %s\n" " $resp " | jq .
69+ ;;
70+ esac
71+ }
4172
42- # Parse options
43- while getopts " h:d:u: " opt; do
44- case $opt in
45- h) HOST =" $OPTARG " ;;
46- d) DATASTORE =" $OPTARG " ;;
47- u) AUTH= " $OPTARG " ;;
48- * ) usage 1 ;;
49- esac
73+ while getopts " h:d:u:v " opt ; do
74+ case $ opt in
75+ h) HOST= " $OPTARG " ;;
76+ d) DATASTORE =" $OPTARG " ;;
77+ u) AUTH =" $OPTARG " ;;
78+ v) VERBOSE=1 ;;
79+ * ) usage 1 ;;
80+ esac
5081done
5182shift $(( OPTIND - 1 ))
5283
53- # Validate required arguments
5484if [ $# -lt 2 ]; then
55- echo " Error: METHOD and PATH are required" >&2
56- usage 1
85+ echo " Error: METHOD and XPATH are required" >&2
86+ usage 1
5787fi
5888
5989METHOD=$1
60- PATH =$2
90+ XPATH =$2
6191shift 2
6292
63- # Ensure PATH starts with /
64- case " $PATH " in
65- /* ) ;;
66- * ) PATH =" /$PATH " ;;
93+ # Ensure XPATH starts with /
94+ case " $XPATH " in
95+ /* ) ;;
96+ * ) XPATH =" /$XPATH " ;;
6797esac
6898
69- # Build URL based on datastore
7099case " $DATASTORE " in
71- running|startup)
72- URL=" https://${HOST} /restconf/data${PATH } "
73- ;;
74- operational)
75- URL=" https://${HOST} /restconf/data${PATH } "
76- ;;
77- * )
78- echo " Error: Invalid datastore '$DATASTORE '. Use: running, operational, or startup" >&2
79- exit 1
80- ;;
100+ running|startup)
101+ URL=" https://${HOST} /restconf/data${XPATH } "
102+ ;;
103+ operational)
104+ URL=" https://${HOST} /restconf/data${XPATH } "
105+ ;;
106+ * )
107+ echo " Error: Invalid datastore '$DATASTORE '. Use: running, operational, or startup" >&2
108+ exit 1
109+ ;;
81110esac
82111
83- # Execute curl with all remaining arguments passed through
84- exec /usr/bin/curl \
85- --insecure \
86- --user " ${AUTH} " \
87- --request " ${METHOD} " \
88- --header " Content-Type: application/yang-data+json" \
89- --header " Accept: application/yang-data+json" \
90- " $@ " \
91- " ${URL} "
112+ if [ " $VERBOSE " ]; then
113+ echo " DS : $DATASTORE "
114+ echo " OP : $METHOD "
115+ echo " XPATH : $XPATH "
116+ echo " AUTH : $AUTH "
117+ echo " => URL : $URL "
118+ set -x
119+ fi
120+
121+ RESP=$( /usr/bin/curl --silent \
122+ --insecure \
123+ --user " ${AUTH} " \
124+ --request " ${METHOD} " \
125+ --header " Content-Type: application/yang-data+json" \
126+ --header " Accept: application/yang-data+json" \
127+ " $@ " \
128+ " ${URL} " )
129+
130+ check " $RESP " " $URL "
92131```
93132
94133Make it executable:
0 commit comments