blob: 615ccabca7c98ed080e2d361c36941890e83c45d [file] [log] [blame]
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001#!/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
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010022# If operation sufffix is '_BATCH' the the send and get response is split in two sequences,
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020023# one for sending the requests and one for receiving the response
24# but only when using the DMAAP interface
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010025# REST or DMAAP is controlled of the base url of $XX_ADAPTER
26# arg: (PA|ECS|CR|RC GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url>|<correlation-id> [<file>]) | (PA|ECS RESPONSE <correlation-id>)
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020027# (Not for test scripts)
28__do_curl_to_api() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010029 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
30 echo " (${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010031 proxyflag=""
32 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +020033 if [ ! -z "$KUBE_PROXY_PATH" ]; then
BjornMagnussonXA674793d2021-05-06 19:49:17 +020034 if [ $KUBE_PROXY_HTTPX == "http" ]; then
35 proxyflag=" --proxy $KUBE_PROXY_PATH"
36 else
37 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
38 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010039 fi
40 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020041 paramError=0
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010042 input_url=$3
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020043 if [ $# -gt 0 ]; then
44 if [ $1 == "PA" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010045 __ADAPTER=$PA_ADAPTER
46 __ADAPTER_TYPE=$PA_ADAPTER_TYPE
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020047 __RETRY_CODES=$AGENT_RETRY_CODES
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010048 if [ $PMS_VERSION != "V1" ]; then
49 input_url=$PMS_API_PREFIX$3
50 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020051 elif [ $1 == "ECS" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010052 __ADAPTER=$ECS_ADAPTER
53 __ADAPTER_TYPE=$ECS_ADAPTER_TYPE
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020054 __RETRY_CODES=$ECS_RETRY_CODES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010055 elif [ $1 == "CR" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010056 __ADAPTER=$CR_ADAPTER
57 __ADAPTER_TYPE=$CR_ADAPTER_TYPE
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010058 __RETRY_CODES=""
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +010059 elif [ $1 == "RC" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010060 __ADAPTER=$RC_ADAPTER
61 __ADAPTER_TYPE=$RC_ADAPTER_TYPE
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +010062 __RETRY_CODES=""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010063 elif [ $1 == "NGW" ]; then
64 __ADAPTER=$NGW_ADAPTER
65 __ADAPTER_TYPE=$NGW_ADAPTER_TYPE
66 __RETRY_CODES=""
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020067 else
68 paramError=1
69 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010070 if [ $__ADAPTER_TYPE == "MR-HTTP" ]; then
71 __ADAPTER=$MR_ADAPTER_HTTP
72 fi
73 if [ $__ADAPTER_TYPE == "MR-HTTPS" ]; then
74 __ADAPTER=$MR_ADAPTER_HTTPS
75 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020076 fi
77 if [ $# -lt 3 ] || [ $# -gt 4 ]; then
78 paramError=1
79 else
80 timeout=""
81 oper=""
82 file=''
83 httpcode=" -sw %{http_code}"
84 accept=''
85 content=''
86 batch=0
87 if [[ $2 == *"_BATCH" ]]; then
88 batch=1
89 fi
90 if [ $# -gt 3 ]; then
91 content=" -H Content-Type:application/json"
92 fi
93 if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then
94 oper="GET"
95 if [ $# -ne 3 ]; then
96 paramError=1
97 fi
98 elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then
99 oper="PUT"
100 if [ $# -eq 4 ]; then
101 file=" --data-binary @$4"
102 fi
103 accept=" -H accept:application/json"
104 elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then
105 oper="POST"
106 accept=" -H accept:*/*"
107 if [ $# -ne 3 ]; then
108 paramError=1
109 fi
110 elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then
111 oper="DELETE"
112 if [ $# -ne 3 ]; then
113 paramError=1
114 fi
115 elif [ $2 == "RESPONSE" ]; then
116 oper="RESPONSE"
117 if [ $# -ne 3 ]; then
118 paramError=1
119 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100120 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
121 if [ $__ADAPTER_TYPE == "REST" ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200122 paramError=1
123 fi
124 else
125 paramError=1
126 fi
127 fi
128
129 if [ $paramError -eq 1 ]; then
130 ((RES_CONF_FAIL++))
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100131 echo "-Incorrect number of parameters to __do_curl_to_api " $@ >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200132 echo "-Expected: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)" >> $HTTPLOG
133 echo "-Returning response 000" >> $HTTPLOG
134 echo "-000"
135 return 1
136 fi
137
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100138 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
139 if [ $__ADAPTER_TYPE == "REST" ]; then
140 url=" "${__ADAPTER}${input_url}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200141 oper=" -X "$oper
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100142 curlString="curl -k $proxyflag "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200143 echo " CMD: "$curlString >> $HTTPLOG
144 if [ $# -eq 4 ]; then
145 echo " FILE: $(<$4)" >> $HTTPLOG
146 fi
147
148 # Do retry for configured response codes, otherwise only one attempt
149 maxretries=5
150 while [ $maxretries -ge 0 ]; do
151
152 let maxretries=maxretries-1
153 res=$($curlString)
154 retcode=$?
155 if [ $retcode -ne 0 ]; then
156 echo " RETCODE: "$retcode >> $HTTPLOG
157 echo "000"
158 return 1
159 fi
160 retry=0
161 echo " RESP: "$res >> $HTTPLOG
162 status=${res:${#res}-3}
163 if [ ! -z "${__RETRY_CODES}" ]; then
164 for retrycode in $__RETRY_CODES; do
165 if [ $retrycode -eq $status ]; then
166 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED >> $HTTPLOG
167 sleep 1
168 retry=1
169 fi
170 done
171 fi
172 if [ $retry -eq 0 ]; then
173 maxretries=-1
174 fi
175 done
176 echo $res
177 return 0
178 else
179 if [ $oper != "RESPONSE" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100180 requestUrl=$input_url
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200181 if [ $2 == "PUT" ] && [ $# -eq 4 ]; then
182 payload="$(cat $4 | tr -d '\n' | tr -d ' ' )"
183 echo "payload: "$payload >> $HTTPLOG
184 file=" --data-binary "$payload
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100185 elif [ $# -eq 4 ]; then
186 echo " FILE: $(cat $4)" >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200187 fi
188 #urlencode the request url since it will be carried by send-request url
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100189 requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$input_url")
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200190 url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100191 curlString="curl -k $proxyflag -X POST${timeout}${httpcode}${content}${url}${file}"
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200192 echo " CMD: "$curlString >> $HTTPLOG
193 res=$($curlString)
194 retcode=$?
195 if [ $retcode -ne 0 ]; then
196 echo " RETCODE: "$retcode >> $HTTPLOG
197 echo "000"
198 return 1
199 fi
200 echo " RESP: "$res >> $HTTPLOG
201 status=${res:${#res}-3}
202 if [ $status -ne 200 ]; then
203 echo "000"
204 return 1
205 fi
206 cid=${res:0:${#res}-3}
207 if [[ $batch -eq 1 ]]; then
208 echo $cid"200"
209 return 0
210 fi
211 fi
212 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
213 if [ $oper == "RESPONSE" ]; then
214 cid=$3
215 fi
216 url=" "${__ADAPTER}"/receive-response?correlationid="${cid}
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100217 curlString="curl -k $proxyflag -X GET"${timeout}${httpcode}${url}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200218 echo " CMD: "$curlString >> $HTTPLOG
219 res=$($curlString)
220 retcode=$?
221 if [ $retcode -ne 0 ]; then
222 echo " RETCODE: "$retcode >> $HTTPLOG
223 echo "000"
224 return 1
225 fi
226 echo " RESP: "$res >> $HTTPLOG
227 status=${res:${#res}-3}
228 TS=$SECONDS
229 # wait of the reply from the agent/ECS...
230 while [ $status -eq 204 ]; do
231 if [ $(($SECONDS - $TS)) -gt 90 ]; then
232 echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
233 echo "000"
234 return 1
235 fi
236 sleep 0.01
237 echo " CMD: "$curlString >> $HTTPLOG
238 res=$($curlString)
239 if [ $retcode -ne 0 ]; then
240 echo " RETCODE: "$retcode >> $HTTPLOG
241 echo "000"
242 return 1
243 fi
244 echo " RESP: "$res >> $HTTPLOG
245 status=${res:${#res}-3}
246 done
247 if [ $status -eq 200 ]; then
248 body=${res:0:${#res}-3}
249 echo $body
250 return 0
251 fi
252 echo "Status not 200, returning response 000" >> $HTTPLOG
253 echo "0000"
254 return 1
255 fi
256 fi
257}