BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # ============LICENSE_START=============================================== |
| 4 | # Copyright (C) 2020 Nordix Foundation. All rights reserved. |
| 5 | # ======================================================================== |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # ============LICENSE_END================================================= |
| 18 | # |
| 19 | |
| 20 | # Generic function to query the agent/ECS via the REST or DMAAP interface. |
| 21 | # Used by all other agent/ECS api test functions |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 22 | # If operation sufffix is '_BATCH' the the send and get response is split in two sequences, |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 23 | # one for sending the requests and one for receiving the response |
| 24 | # but only when using the DMAAP interface |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 25 | # REST or DMAAP is controlled of the base url of $XX_ADAPTER |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 26 | # arg: (PA|ECS|CR|RC GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url>|<correlation-id> [<file> [mime-type]]) | (PA|ECS RESPONSE <correlation-id>) |
| 27 | # Default mime type for file is application/json unless specified in parameter mime-type |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 28 | # (Not for test scripts) |
| 29 | __do_curl_to_api() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 30 | TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") |
| 31 | echo " (${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 32 | proxyflag="" |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 33 | if [ ! -z "$KUBE_PROXY_PATH" ]; then |
| 34 | if [ $KUBE_PROXY_HTTPX == "http" ]; then |
| 35 | proxyflag=" --proxy $KUBE_PROXY_PATH" |
| 36 | else |
| 37 | proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH" |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 38 | fi |
| 39 | fi |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 40 | |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 41 | paramError=0 |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 42 | input_url=$3 |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 43 | fname=$4 |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 44 | if [ $# -gt 0 ]; then |
| 45 | if [ $1 == "PA" ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 46 | __ADAPTER=$PA_ADAPTER |
| 47 | __ADAPTER_TYPE=$PA_ADAPTER_TYPE |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 48 | __RETRY_CODES=$AGENT_RETRY_CODES |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 49 | if [ $PMS_VERSION != "V1" ]; then |
| 50 | input_url=$PMS_API_PREFIX$3 |
| 51 | fi |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 52 | elif [ $1 == "ECS" ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 53 | __ADAPTER=$ECS_ADAPTER |
| 54 | __ADAPTER_TYPE=$ECS_ADAPTER_TYPE |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 55 | __RETRY_CODES=$ECS_RETRY_CODES |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 56 | elif [ $1 == "CR" ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 57 | __ADAPTER=$CR_ADAPTER |
| 58 | __ADAPTER_TYPE=$CR_ADAPTER_TYPE |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 59 | __RETRY_CODES="" |
BjornMagnussonXA | de4d0f8 | 2020-11-29 16:04:06 +0100 | [diff] [blame] | 60 | elif [ $1 == "RC" ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 61 | __ADAPTER=$RC_ADAPTER |
| 62 | __ADAPTER_TYPE=$RC_ADAPTER_TYPE |
BjornMagnussonXA | de4d0f8 | 2020-11-29 16:04:06 +0100 | [diff] [blame] | 63 | __RETRY_CODES="" |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 64 | elif [ $1 == "NGW" ]; then |
| 65 | __ADAPTER=$NGW_ADAPTER |
| 66 | __ADAPTER_TYPE=$NGW_ADAPTER_TYPE |
| 67 | __RETRY_CODES="" |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 68 | elif [ $1 == "DMAAPADP" ]; then |
| 69 | __ADAPTER=$DMAAP_ADP_ADAPTER |
| 70 | __ADAPTER_TYPE=$DMAAP_ADP_ADAPTER_TYPE |
| 71 | __RETRY_CODES="" |
| 72 | elif [ $1 == "DMAAPMED" ]; then |
| 73 | __ADAPTER=$DMAAP_MED_ADAPTER |
| 74 | __ADAPTER_TYPE=$DMAAP_MED_ADAPTER_TYPE |
| 75 | __RETRY_CODES="" |
| 76 | elif [ $1 == "MRSTUB" ]; then |
| 77 | __ADAPTER=$MR_STUB_ADAPTER |
| 78 | __ADAPTER_TYPE=$MR_STUB_ADAPTER_TYPE |
| 79 | __RETRY_CODES="" |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 80 | elif [ $1 == "DMAAPMR" ]; then |
| 81 | __ADAPTER=$MR_DMAAP_ADAPTER_HTTP |
| 82 | __ADAPTER_TYPE=$MR_DMAAP_ADAPTER_TYPE |
| 83 | __RETRY_CODES="" |
| 84 | else |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 85 | paramError=1 |
| 86 | fi |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 87 | if [ "$__ADAPTER_TYPE" == "MR-HTTP" ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 88 | __ADAPTER=$MR_ADAPTER_HTTP |
| 89 | fi |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 90 | if [ "$__ADAPTER_TYPE" == "MR-HTTPS" ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 91 | __ADAPTER=$MR_ADAPTER_HTTPS |
| 92 | fi |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 93 | fi |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 94 | if [ $# -lt 3 ] || [ $# -gt 5 ]; then |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 95 | paramError=1 |
| 96 | else |
| 97 | timeout="" |
| 98 | oper="" |
| 99 | file='' |
| 100 | httpcode=" -sw %{http_code}" |
| 101 | accept='' |
| 102 | content='' |
| 103 | batch=0 |
| 104 | if [[ $2 == *"_BATCH" ]]; then |
| 105 | batch=1 |
| 106 | fi |
| 107 | if [ $# -gt 3 ]; then |
| 108 | content=" -H Content-Type:application/json" |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 109 | fname=$4 |
| 110 | if [ $# -gt 4 ]; then |
| 111 | content=" -H Content-Type:"$5 |
| 112 | fi |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 113 | fi |
| 114 | if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then |
| 115 | oper="GET" |
| 116 | if [ $# -ne 3 ]; then |
| 117 | paramError=1 |
| 118 | fi |
| 119 | elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then |
| 120 | oper="PUT" |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 121 | if [ $# -gt 3 ]; then |
| 122 | file=" --data-binary @$fname" |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 123 | fi |
| 124 | accept=" -H accept:application/json" |
| 125 | elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then |
| 126 | oper="POST" |
| 127 | accept=" -H accept:*/*" |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 128 | if [ $# -gt 3 ]; then |
| 129 | file=" --data-binary @$fname" |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 130 | accept=" -H accept:application/json" |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 131 | fi |
| 132 | elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then |
| 133 | oper="DELETE" |
| 134 | if [ $# -ne 3 ]; then |
| 135 | paramError=1 |
| 136 | fi |
| 137 | elif [ $2 == "RESPONSE" ]; then |
| 138 | oper="RESPONSE" |
| 139 | if [ $# -ne 3 ]; then |
| 140 | paramError=1 |
| 141 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 142 | #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then |
| 143 | if [ $__ADAPTER_TYPE == "REST" ]; then |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 144 | paramError=1 |
| 145 | fi |
| 146 | else |
| 147 | paramError=1 |
| 148 | fi |
| 149 | fi |
| 150 | |
| 151 | if [ $paramError -eq 1 ]; then |
| 152 | ((RES_CONF_FAIL++)) |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 153 | echo "-Incorrect number of parameters to __do_curl_to_api " $@ >> $HTTPLOG |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 154 | echo "-Expected: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)" >> $HTTPLOG |
| 155 | echo "-Returning response 000" >> $HTTPLOG |
| 156 | echo "-000" |
| 157 | return 1 |
| 158 | fi |
| 159 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 160 | #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then |
| 161 | if [ $__ADAPTER_TYPE == "REST" ]; then |
| 162 | url=" "${__ADAPTER}${input_url} |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 163 | oper=" -X "$oper |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 164 | curlString="curl -k $proxyflag "${oper}${timeout}${httpcode}${accept}${content}${url}${file} |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 165 | echo " CMD: "$curlString >> $HTTPLOG |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 166 | if [ $# -gt 3 ]; then |
| 167 | echo " FILE: $(<$fname)" >> $HTTPLOG |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 168 | fi |
| 169 | |
| 170 | # Do retry for configured response codes, otherwise only one attempt |
| 171 | maxretries=5 |
| 172 | while [ $maxretries -ge 0 ]; do |
| 173 | |
| 174 | let maxretries=maxretries-1 |
| 175 | res=$($curlString) |
| 176 | retcode=$? |
| 177 | if [ $retcode -ne 0 ]; then |
| 178 | echo " RETCODE: "$retcode >> $HTTPLOG |
| 179 | echo "000" |
| 180 | return 1 |
| 181 | fi |
| 182 | retry=0 |
| 183 | echo " RESP: "$res >> $HTTPLOG |
| 184 | status=${res:${#res}-3} |
| 185 | if [ ! -z "${__RETRY_CODES}" ]; then |
| 186 | for retrycode in $__RETRY_CODES; do |
| 187 | if [ $retrycode -eq $status ]; then |
| 188 | echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED >> $HTTPLOG |
| 189 | sleep 1 |
| 190 | retry=1 |
| 191 | fi |
| 192 | done |
| 193 | fi |
| 194 | if [ $retry -eq 0 ]; then |
| 195 | maxretries=-1 |
| 196 | fi |
| 197 | done |
| 198 | echo $res |
| 199 | return 0 |
| 200 | else |
| 201 | if [ $oper != "RESPONSE" ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 202 | requestUrl=$input_url |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 203 | if [ $2 == "PUT" ] && [ $# -gt 3 ]; then |
| 204 | payload="$(cat $fname | tr -d '\n' | tr -d ' ' )" |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 205 | echo "payload: "$payload >> $HTTPLOG |
| 206 | file=" --data-binary "$payload |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 207 | elif [ $# -gt 3 ]; then |
| 208 | echo " FILE: $(cat $fname)" >> $HTTPLOG |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 209 | fi |
| 210 | #urlencode the request url since it will be carried by send-request url |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 211 | requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$input_url") |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 212 | url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper} |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 213 | curlString="curl -k $proxyflag -X POST${timeout}${httpcode}${content}${url}${file}" |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 214 | echo " CMD: "$curlString >> $HTTPLOG |
| 215 | res=$($curlString) |
| 216 | retcode=$? |
| 217 | if [ $retcode -ne 0 ]; then |
| 218 | echo " RETCODE: "$retcode >> $HTTPLOG |
| 219 | echo "000" |
| 220 | return 1 |
| 221 | fi |
| 222 | echo " RESP: "$res >> $HTTPLOG |
| 223 | status=${res:${#res}-3} |
| 224 | if [ $status -ne 200 ]; then |
| 225 | echo "000" |
| 226 | return 1 |
| 227 | fi |
| 228 | cid=${res:0:${#res}-3} |
| 229 | if [[ $batch -eq 1 ]]; then |
| 230 | echo $cid"200" |
| 231 | return 0 |
| 232 | fi |
| 233 | fi |
| 234 | if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then |
| 235 | if [ $oper == "RESPONSE" ]; then |
| 236 | cid=$3 |
| 237 | fi |
| 238 | url=" "${__ADAPTER}"/receive-response?correlationid="${cid} |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 239 | curlString="curl -k $proxyflag -X GET"${timeout}${httpcode}${url} |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 240 | echo " CMD: "$curlString >> $HTTPLOG |
| 241 | res=$($curlString) |
| 242 | retcode=$? |
| 243 | if [ $retcode -ne 0 ]; then |
| 244 | echo " RETCODE: "$retcode >> $HTTPLOG |
| 245 | echo "000" |
| 246 | return 1 |
| 247 | fi |
| 248 | echo " RESP: "$res >> $HTTPLOG |
| 249 | status=${res:${#res}-3} |
| 250 | TS=$SECONDS |
| 251 | # wait of the reply from the agent/ECS... |
| 252 | while [ $status -eq 204 ]; do |
| 253 | if [ $(($SECONDS - $TS)) -gt 90 ]; then |
| 254 | echo " RETCODE: (timeout after 90s)" >> $HTTPLOG |
| 255 | echo "000" |
| 256 | return 1 |
| 257 | fi |
| 258 | sleep 0.01 |
| 259 | echo " CMD: "$curlString >> $HTTPLOG |
| 260 | res=$($curlString) |
| 261 | if [ $retcode -ne 0 ]; then |
| 262 | echo " RETCODE: "$retcode >> $HTTPLOG |
| 263 | echo "000" |
| 264 | return 1 |
| 265 | fi |
| 266 | echo " RESP: "$res >> $HTTPLOG |
| 267 | status=${res:${#res}-3} |
| 268 | done |
| 269 | if [ $status -eq 200 ]; then |
| 270 | body=${res:0:${#res}-3} |
| 271 | echo $body |
| 272 | return 0 |
| 273 | fi |
| 274 | echo "Status not 200, returning response 000" >> $HTTPLOG |
| 275 | echo "0000" |
| 276 | return 1 |
| 277 | fi |
| 278 | fi |
| 279 | } |