4 # ============LICENSE_START=======================================================
6 # ================================================================================
7 # Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
8 # ================================================================================
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
22 # The script is called with a resource, filepath and an optional argument to
23 # ignore HTTP failure codes which would otherwise indicate a failure.
24 # It invokes a PUT on the resource with the file using curl
25 # Uses aaiconfig.properties for authorization type and url. The HTTP response
26 # code is checked. Responses between 200 and 299 are considered success.
27 # When the ignore failure code parameter is passed, responses outside of
28 # the 200 to 299 range but matching a sub-string of the parameter are
29 # considered success. For example, a parameter value of 412 will consider
30 # responses in the range of 200 to 299 and 412 successes.
32 # method checking parameter list for two strings, and determine if
33 # the second string is a sub-string of the first
37 if test "${string#*$substring}" != "$string"
39 return 0 # $substring is in $string
41 return 1 # $substring is not in $string
49 1. Usage: putTool.sh <resource-path> <json payload file> <optional -display> <optional -XFROMAPPID> <optional -XTRANSID>
50 2. This script requires two arguments, a resource path and a file path to a json file containing the payload.
51 3. Example: resource-path and payload for a particular customer is: business/customers/customer/JohnDoe customerpayload.json
52 4. Adding the optional "-display" argument will display all data returned from the request.
60 # remove leading slash when present
61 RESOURCE=`echo $1 | sed "s,^/,,"`
62 if [ -z $RESOURCE ]; then
63 echo "resource parameter is missing"
64 echo "usage: $0 resource file [expected-failure-codes]"
68 if [ -z $JSONFILE ]; then
69 echo "json file parameter is missing"
70 echo "usage: $0 resource file [expected-failure-codes]"
73 echo `date` " Starting $0 for resource $RESOURCE"
76 XFROMAPPID="AAI-TOOLS"
79 if [ ! -z "$4" ]; then
83 if [ ! -z "$5" ]; then
87 userid=$( id | cut -f2 -d"(" | cut -f1 -d")" )
88 if [ "${userid}" != "aaiadmin" ]; then
89 echo "You must be aaiadmin to run $0. The id used $userid."
94 PROJECT_HOME=/opt/app/aai-resources
95 prop_file=$PROJECT_HOME/resources/etc/appprops/aaiconfig.properties
96 log_dir=$PROJECT_HOME/logs/misc
97 today=$(date +\%Y-\%m-\%d)
100 if [ ${#} -ne 2 ]; then
101 if [ "$3" = "-display" ]; then
107 RESTURL=`grep ^aai.server.url= $prop_file |cut -d'=' -f2 |tr -d "\015"`
108 if [ -z $RESTURL ]; then
109 echo "Property [aai.server.url] not found in file $prop_file"
113 BASICENABLE=`grep ^aai.tools.enableBasicAuth $prop_file |cut -d'=' -f2 |tr -d "\015"`
114 if [ -z $BASICENABLE ]; then
118 CURLUSER=`grep ^aai.tools.username $prop_file |cut -d'=' -f2 |tr -d "\015"`
119 if [ -z $CURLUSER ]; then
120 echo "Property [aai.tools.username] not found in file $prop_file"
123 CURLPASSWORD=`grep ^aai.tools.password $prop_file |cut -d'=' -f2 |tr -d "\015"`
124 if [ -z $CURLPASSWORD ]; then
125 echo "Property [aai.tools.password] not found in file $prop_file"
131 if [ -f /tmp/$(basename $JSONFILE) ]; then
132 fname=/tmp/$(basename $JSONFILE)
133 elif [ ! -f $JSONFILE ]; then
134 echo "The file $JSONFILE does not exist"
138 if [ $MISSING_PROP = false ]; then
139 if [ $USEBASICAUTH = false ]; then
140 AUTHSTRING="--cert $PROJECT_HOME/resources/etc/auth/aaiClientPublicCert.pem --key $PROJECT_HOME/resources/etc/auth/aaiClientPrivateKey.pem"
142 AUTHSTRING="-u $CURLUSER:$CURLPASSWORD"
144 if [ $RETURNRESPONSE = true ]; then
145 curl --request PUT -k $AUTHSTRING -H "X-FromAppId: $XFROMAPPID" -H "X-TransactionId: $XTRANSID" -H "Accept: application/json" -H "Content-Type: application/json" -T $fname $RESTURL$RESOURCE | jq '.'
148 result=`curl --request PUT -w "%{http_code}" -o /dev/null -k $AUTHSTRING -H "X-FromAppId: $XFROMAPPID" -H "X-TransactionId: $XTRANSID" -H "Accept: application/json" -H "Content-Type: application/json" -T $fname $RESTURL$RESOURCE`
149 #echo "result is $result."
151 if [ $? -eq 0 ]; then
154 if [[ "$result" -ge 200 && $result -lt 300 ]]
156 echo "PUT result is OK, $result"
158 if [ -z $ALLOWHTTPRESPONSES ]; then
159 echo "PUT request failed, response code was $result"
162 contains $ALLOWHTTPRESPONSES $result
165 echo "PUT request failed, unexpected response code was $result"
168 echo "PUT result is expected, $result"
174 echo "PUT request failed, response was $result"
180 echo "FAILED to send request to $RESTURL"
185 echo "usage: $0 resource file [expected-failure-codes]"
189 echo `date` " Done $0, returning $RC"