support for v20
[aai/resources.git] / aai-resources / src / main / scripts / putTool.sh
1 #!/bin/ksh
2
3 ###
4 # ============LICENSE_START=======================================================
5 # org.onap.aai
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=========================================================
19 ###
20
21 #
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.
31 #
32 # method checking parameter list for two strings, and determine if
33 # the second string is a sub-string of the first
34 contains() {
35     string="$1"
36     substring="$2"
37     if test "${string#*$substring}" != "$string"
38     then
39         return 0    # $substring is in $string
40     else
41         return 1    # $substring is not in $string
42     fi
43 }
44
45 display_usage() {
46         cat <<EOF
47         Usage: $0 [options]
48
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.
53 EOF
54 }
55 if [ $# -eq 0 ]; then
56         display_usage
57         exit 1
58 fi
59
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]"
65         exit 1
66 fi
67 JSONFILE=$2
68 if [ -z $JSONFILE ]; then
69         echo "json file parameter is missing"
70         echo "usage: $0 resource file [expected-failure-codes]"
71         exit 1
72 fi
73 echo `date` "   Starting $0 for resource $RESOURCE"
74 ALLOWHTTPRESPONSES=$3
75
76 XFROMAPPID="AAI-TOOLS"
77 XTRANSID=`uuidgen`
78
79 if [ ! -z "$4" ]; then
80    XFROMAPPID=$4
81 fi
82
83 if [ ! -z "$5" ]; then
84    XTRANSID=$5
85 fi
86
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."
90     exit 1
91 fi
92
93
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)
98
99 RETURNRESPONSE=false
100 if [ ${#} -ne 2 ]; then
101     if [ "$3" = "-display" ]; then
102         RETURNRESPONSE=true
103     fi
104 fi
105
106 MISSING_PROP=false
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"
110         MISSING_PROP=true
111 fi
112 USEBASICAUTH=false
113 BASICENABLE=`grep ^aai.tools.enableBasicAuth $prop_file |cut -d'=' -f2 |tr -d "\015"`
114 if [ -z $BASICENABLE ]; then
115         USEBASICAUTH=false
116 else
117         USEBASICAUTH=true
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"
121                 MISSING_PROP=true
122         fi
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"
126                 MISSING_PROP=true
127         fi
128 fi
129
130 fname=$JSONFILE
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"
135         exit -1
136 fi
137
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"
141         else
142                 AUTHSTRING="-u $CURLUSER:$CURLPASSWORD"
143         fi
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 '.'
146                         RC=$?
147                 else
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."
150                 RC=0;
151                 if [ $? -eq 0 ]; then
152                 case $result in
153                         +([0-9])?)
154                                 if [[ "$result" -ge 200 && $result -lt 300 ]]
155                                 then
156                                         echo "PUT result is OK,  $result"
157                                 else
158                                         if [ -z $ALLOWHTTPRESPONSES ]; then
159                                                 echo "PUT request failed, response code was  $result"
160                                                 RC=$result
161                                         else
162                                                 contains $ALLOWHTTPRESPONSES $result
163                                                 if [ $? -ne 0 ]
164                                                 then
165                                                         echo "PUT request failed, unexpected response code was  $result"
166                                                         RC=$result
167                                                 else
168                                                         echo "PUT result is expected,  $result"
169                                                 fi
170                                         fi
171                                 fi
172                                 ;;
173                         *)
174                                 echo "PUT request failed, response was $result"
175                                 RC=-1
176                                 ;;
177
178                 esac
179                 else
180                 echo "FAILED to send request to $RESTURL"
181                 RC=-1
182                 fi
183         fi      
184 else
185         echo "usage: $0 resource file [expected-failure-codes]"
186         RC=-1
187 fi
188
189 echo `date` "   Done $0, returning $RC"
190 exit $RC