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 |
| 22 | # If operation prefix is '_BATCH' the the send and get response is split in two sequences, |
| 23 | # one for sending the requests and one for receiving the response |
| 24 | # but only when using the DMAAP interface |
| 25 | # REST or DMAAP is controlled of the base url of $ADAPTER |
| 26 | # arg: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>) |
| 27 | # (Not for test scripts) |
| 28 | __do_curl_to_api() { |
| 29 | echo "(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG |
| 30 | paramError=0 |
| 31 | |
| 32 | if [ $# -gt 0 ]; then |
| 33 | if [ $1 == "PA" ]; then |
| 34 | __ADAPTER=$ADAPTER |
| 35 | __RESTBASE=$RESTBASE |
| 36 | __RESTBASE_SECURE=$RESTBASE_SECURE |
| 37 | __RETRY_CODES=$AGENT_RETRY_CODES |
| 38 | elif [ $1 == "ECS" ]; then |
| 39 | __ADAPTER=$ECS_ADAPTER |
| 40 | __RESTBASE=$ECS_RESTBASE |
| 41 | __RESTBASE_SECURE=$ECS_RESTBASE_SECURE |
| 42 | __RETRY_CODES=$ECS_RETRY_CODES |
| 43 | else |
| 44 | paramError=1 |
| 45 | fi |
| 46 | fi |
| 47 | if [ $# -lt 3 ] || [ $# -gt 4 ]; then |
| 48 | paramError=1 |
| 49 | else |
| 50 | timeout="" |
| 51 | oper="" |
| 52 | file='' |
| 53 | httpcode=" -sw %{http_code}" |
| 54 | accept='' |
| 55 | content='' |
| 56 | batch=0 |
| 57 | if [[ $2 == *"_BATCH" ]]; then |
| 58 | batch=1 |
| 59 | fi |
| 60 | if [ $# -gt 3 ]; then |
| 61 | content=" -H Content-Type:application/json" |
| 62 | fi |
| 63 | if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then |
| 64 | oper="GET" |
| 65 | if [ $# -ne 3 ]; then |
| 66 | paramError=1 |
| 67 | fi |
| 68 | elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then |
| 69 | oper="PUT" |
| 70 | if [ $# -eq 4 ]; then |
| 71 | file=" --data-binary @$4" |
| 72 | fi |
| 73 | accept=" -H accept:application/json" |
| 74 | elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then |
| 75 | oper="POST" |
| 76 | accept=" -H accept:*/*" |
| 77 | if [ $# -ne 3 ]; then |
| 78 | paramError=1 |
| 79 | fi |
| 80 | elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then |
| 81 | oper="DELETE" |
| 82 | if [ $# -ne 3 ]; then |
| 83 | paramError=1 |
| 84 | fi |
| 85 | elif [ $2 == "RESPONSE" ]; then |
| 86 | oper="RESPONSE" |
| 87 | if [ $# -ne 3 ]; then |
| 88 | paramError=1 |
| 89 | fi |
| 90 | if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then |
| 91 | paramError=1 |
| 92 | fi |
| 93 | else |
| 94 | paramError=1 |
| 95 | fi |
| 96 | fi |
| 97 | |
| 98 | if [ $paramError -eq 1 ]; then |
| 99 | ((RES_CONF_FAIL++)) |
| 100 | echo "-Incorrect number of parameters to __do_curl_agent " $@ >> $HTTPLOG |
| 101 | echo "-Expected: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)" >> $HTTPLOG |
| 102 | echo "-Returning response 000" >> $HTTPLOG |
| 103 | echo "-000" |
| 104 | return 1 |
| 105 | fi |
| 106 | |
| 107 | if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then |
| 108 | url=" "${__ADAPTER}${3} |
| 109 | oper=" -X "$oper |
| 110 | curlString="curl -k "${oper}${timeout}${httpcode}${accept}${content}${url}${file} |
| 111 | echo " CMD: "$curlString >> $HTTPLOG |
| 112 | if [ $# -eq 4 ]; then |
| 113 | echo " FILE: $(<$4)" >> $HTTPLOG |
| 114 | fi |
| 115 | |
| 116 | # Do retry for configured response codes, otherwise only one attempt |
| 117 | maxretries=5 |
| 118 | while [ $maxretries -ge 0 ]; do |
| 119 | |
| 120 | let maxretries=maxretries-1 |
| 121 | res=$($curlString) |
| 122 | retcode=$? |
| 123 | if [ $retcode -ne 0 ]; then |
| 124 | echo " RETCODE: "$retcode >> $HTTPLOG |
| 125 | echo "000" |
| 126 | return 1 |
| 127 | fi |
| 128 | retry=0 |
| 129 | echo " RESP: "$res >> $HTTPLOG |
| 130 | status=${res:${#res}-3} |
| 131 | if [ ! -z "${__RETRY_CODES}" ]; then |
| 132 | for retrycode in $__RETRY_CODES; do |
| 133 | if [ $retrycode -eq $status ]; then |
| 134 | echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED >> $HTTPLOG |
| 135 | sleep 1 |
| 136 | retry=1 |
| 137 | fi |
| 138 | done |
| 139 | fi |
| 140 | if [ $retry -eq 0 ]; then |
| 141 | maxretries=-1 |
| 142 | fi |
| 143 | done |
| 144 | echo $res |
| 145 | return 0 |
| 146 | else |
| 147 | if [ $oper != "RESPONSE" ]; then |
| 148 | requestUrl=$3 |
| 149 | if [ $2 == "PUT" ] && [ $# -eq 4 ]; then |
| 150 | payload="$(cat $4 | tr -d '\n' | tr -d ' ' )" |
| 151 | echo "payload: "$payload >> $HTTPLOG |
| 152 | file=" --data-binary "$payload |
| 153 | fi |
| 154 | #urlencode the request url since it will be carried by send-request url |
| 155 | requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$3") |
| 156 | url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper} |
| 157 | curlString="curl -k -X POST${timeout}${httpcode}${content}${url}${file}" |
| 158 | echo " CMD: "$curlString >> $HTTPLOG |
| 159 | res=$($curlString) |
| 160 | retcode=$? |
| 161 | if [ $retcode -ne 0 ]; then |
| 162 | echo " RETCODE: "$retcode >> $HTTPLOG |
| 163 | echo "000" |
| 164 | return 1 |
| 165 | fi |
| 166 | echo " RESP: "$res >> $HTTPLOG |
| 167 | status=${res:${#res}-3} |
| 168 | if [ $status -ne 200 ]; then |
| 169 | echo "000" |
| 170 | return 1 |
| 171 | fi |
| 172 | cid=${res:0:${#res}-3} |
| 173 | if [[ $batch -eq 1 ]]; then |
| 174 | echo $cid"200" |
| 175 | return 0 |
| 176 | fi |
| 177 | fi |
| 178 | if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then |
| 179 | if [ $oper == "RESPONSE" ]; then |
| 180 | cid=$3 |
| 181 | fi |
| 182 | url=" "${__ADAPTER}"/receive-response?correlationid="${cid} |
| 183 | curlString="curl -k -X GET"${timeout}${httpcode}${url} |
| 184 | echo " CMD: "$curlString >> $HTTPLOG |
| 185 | res=$($curlString) |
| 186 | retcode=$? |
| 187 | if [ $retcode -ne 0 ]; then |
| 188 | echo " RETCODE: "$retcode >> $HTTPLOG |
| 189 | echo "000" |
| 190 | return 1 |
| 191 | fi |
| 192 | echo " RESP: "$res >> $HTTPLOG |
| 193 | status=${res:${#res}-3} |
| 194 | TS=$SECONDS |
| 195 | # wait of the reply from the agent/ECS... |
| 196 | while [ $status -eq 204 ]; do |
| 197 | if [ $(($SECONDS - $TS)) -gt 90 ]; then |
| 198 | echo " RETCODE: (timeout after 90s)" >> $HTTPLOG |
| 199 | echo "000" |
| 200 | return 1 |
| 201 | fi |
| 202 | sleep 0.01 |
| 203 | echo " CMD: "$curlString >> $HTTPLOG |
| 204 | res=$($curlString) |
| 205 | if [ $retcode -ne 0 ]; then |
| 206 | echo " RETCODE: "$retcode >> $HTTPLOG |
| 207 | echo "000" |
| 208 | return 1 |
| 209 | fi |
| 210 | echo " RESP: "$res >> $HTTPLOG |
| 211 | status=${res:${#res}-3} |
| 212 | done |
| 213 | if [ $status -eq 200 ]; then |
| 214 | body=${res:0:${#res}-3} |
| 215 | echo $body |
| 216 | return 0 |
| 217 | fi |
| 218 | echo "Status not 200, returning response 000" >> $HTTPLOG |
| 219 | echo "0000" |
| 220 | return 1 |
| 221 | fi |
| 222 | fi |
| 223 | } |