be273a21956209f71db0d8b76b1bb5c54cebd74a
[aai/traversal.git] / aai-traversal / src / main / scripts / putTool.sh
1 #!/bin/ksh
2 #
3 # ============LICENSE_START=======================================================
4 # org.onap.aai
5 # ================================================================================
6 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
7 # ================================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #    http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ============LICENSE_END=========================================================
20 #
21 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 #
23
24 #
25 <<<<<<< HEAD
26 # The script is called with a resource, filepath and an optional argument to
27 # ignore HTTP failure codes which would otherwise indicate a failure.
28 =======
29 # The script is called with a resource, filepath, an optional argument to
30 # ignore HTTP failure codes which would otherwise indicate a failure,
31 # and an optional argument to display more data.
32 >>>>>>> codecloud/release/1802
33 # It invokes a PUT on the resource with the file using curl
34 # Uses aaiconfig.properties for authorization type and url. The HTTP response
35 # code is checked. Responses between 200 and 299 are considered success.
36 # When the ignore failure code parameter is passed, responses outside of
37 # the 200 to 299 range but matching a sub-string of the parameter are
38 # considered success. For example, a parameter value of 412 will consider
39 # responses in the range of 200 to 299 and 412 successes.
40 #
41 # method checking parameter list for two strings, and determine if
42 # the second string is a sub-string of the first
43 contains() {
44     string="$1"
45     substring="$2"
46     if test "${string#*$substring}" != "$string"
47     then
48         return 0    # $substring is in $string
49     else
50         return 1    # $substring is not in $string
51     fi
52 }
53
54 <<<<<<< HEAD
55 =======
56 display_usage() {
57         cat <<EOF
58         Usage: $0 [options]
59
60         1. Usage: putTool.sh <resource-path> <json payload file> <optional HTTP Response code> <optional -display>
61         2. This script requires two arguments, a resource path and a file path to a json file containing the payload.
62         3. Example: query?format=xxxx customquery.json (possible formats are simple, raw, console, count, graphson, id, pathed, resource and resource_and_url)
63         4. Adding the optional HTTP Response code will allow the script to ignore HTTP failure codes that match the input parameter.
64         5. Adding the optional "-display" argument will display all data returned from the request, instead of just a response code.
65                 
66 EOF
67 }
68 if [ $# -eq 0 ]; then
69         display_usage
70         exit 1
71 fi
72
73 >>>>>>> codecloud/release/1802
74 # remove leading slash when present
75 RESOURCE=`echo $1 | sed "s,^/,,"`
76 if [ -z $RESOURCE ]; then
77         echo "resource parameter is missing"
78         echo "usage: $0 resource file [expected-failure-codes]"
79         exit 1
80 fi
81 JSONFILE=$2
82 if [ -z $JSONFILE ]; then
83         echo "json file parameter is missing"
84         echo "usage: $0 resource file [expected-failure-codes]"
85         exit 1
86 fi
87 echo `date` "   Starting $0 for resource $RESOURCE"
88 ALLOWHTTPRESPONSES=$3
89
90 XFROMAPPID="AAI-TOOLS"
91 XTRANSID=`uuidgen`
92
93 userid=$( id | cut -f2 -d"(" | cut -f1 -d")" )
94 if [ "${userid}" != "aaiadmin" ]; then
95     echo "You must be aaiadmin to run $0. The id used $userid."
96     exit 1
97 fi
98
99 . /etc/profile.d/aai.sh
100 PROJECT_HOME=/opt/app/aai-traversal
101 prop_file=$PROJECT_HOME/bundleconfig/etc/appprops/aaiconfig.properties
102 log_dir=$PROJECT_HOME/logs/misc
103 today=$(date +\%Y-\%m-\%d)
104
105 <<<<<<< HEAD
106 =======
107 RETURNRESPONSE=false
108 if [ ${#} -ne 2 ]; then
109     if [ "$3" = "-display" ]; then
110         RETURNRESPONSE=true
111     fi
112 fi
113 if [ ${#} -ne 3 ]; then
114     if [ "$4" = "-display" ]; then
115         RETURNRESPONSE=true
116     fi
117 fi
118
119 >>>>>>> codecloud/release/1802
120 MISSING_PROP=false
121 RESTURL=`grep ^aai.server.url= $prop_file |cut -d'=' -f2 |tr -d "\015"`
122 if [ -z $RESTURL ]; then
123         echo "Property [aai.server.url] not found in file $prop_file"
124         MISSING_PROP=true
125 fi
126 USEBASICAUTH=false
127 BASICENABLE=`grep ^aai.tools.enableBasicAuth $prop_file |cut -d'=' -f2 |tr -d "\015"`
128 if [ -z $BASICENABLE ]; then
129         USEBASICAUTH=false
130 else
131         USEBASICAUTH=true
132         CURLUSER=`grep ^aai.tools.username $prop_file |cut -d'=' -f2 |tr -d "\015"`
133         if [ -z $CURLUSER ]; then
134                 echo "Property [aai.tools.username] not found in file $prop_file"
135                 MISSING_PROP=true
136         fi
137         CURLPASSWORD=`grep ^aai.tools.password $prop_file |cut -d'=' -f2 |tr -d "\015"`
138         if [ -z $CURLPASSWORD ]; then
139                 echo "Property [aai.tools.password] not found in file $prop_file"
140                 MISSING_PROP=true
141         fi
142 fi
143
144 if [ $MISSING_PROP = false ]; then
145         if [ $USEBASICAUTH = false ]; then
146                 AUTHSTRING="--cert $PROJECT_HOME/bundleconfig/etc/auth/aaiClientPublicCert.pem --key $PROJECT_HOME/bundleconfig/etc/auth/aaiClientPrivateKey.pem"
147         else
148                 AUTHSTRING="-u $CURLUSER:$CURLPASSWORD"
149         fi
150 <<<<<<< HEAD
151         result=`curl --request PUT -sL -w "%{http_code}" -o /dev/null -k $AUTHSTRING -H "X-FromAppId: $XFROMAPPID" -H "X-TransactionId: $XTRANSID" -H "Accept: application/json" -T $JSONFILE $RESTURL$RESOURCE`
152         #echo "result is $result."
153         RC=0;
154         if [ $? -eq 0 ]; then
155 =======
156         
157         if [ $RETURNRESPONSE = true ]; then
158                         curl --request PUT -sL -k $AUTHSTRING -H "X-FromAppId: $XFROMAPPID" -H "X-TransactionId: $XTRANSID" -H "Accept: application/json" -T $JSONFILE $RESTURL$RESOURCE | python -mjson.tool
159                         RC=$?
160                 else
161                 result=`curl --request PUT -sL -w "%{http_code}" -o /dev/null -k $AUTHSTRING -H "X-FromAppId: $XFROMAPPID" -H "X-TransactionId: $XTRANSID" -H "Accept: application/json" -T $JSONFILE $RESTURL$RESOURCE`
162                 #echo "result is $result."
163                 RC=0;
164                 if [ $? -eq 0 ]; then
165 >>>>>>> codecloud/release/1802
166                 case $result in
167                         +([0-9])?)
168                                 #if [[ "$result" -eq 412 || "$result" -ge 200 && $result -lt 300 ]]
169                                 if [[ "$result" -ge 200 && $result -lt 300 ]]
170                                 then
171                                         echo "PUT result is OK,  $result"
172                                 else
173                                         if [ -z $ALLOWHTTPRESPONSES ]; then
174                                                 echo "PUT request failed, response code was  $result"
175                                                 RC=$result
176                                         else
177                                                 contains $ALLOWHTTPRESPONSES $result
178                                                 if [ $? -ne 0 ]
179                                                 then
180                                                         echo "PUT request failed, unexpected response code was  $result"
181                                                         RC=$result
182                                                 else
183                                                         echo "PUT result is expected,  $result"
184                                                 fi
185                                         fi
186                                 fi
187                                 ;;
188                         *)
189                                 echo "PUT request failed, response was $result"
190                                 RC=-1
191                                 ;;
192
193                 esac
194 <<<<<<< HEAD
195         else
196                 echo "FAILED to send request to $RESTURL"
197                 RC=-1
198         fi
199 =======
200                 else
201                 echo "FAILED to send request to $RESTURL"
202                 RC=-1
203                 fi
204         fi      
205 >>>>>>> codecloud/release/1802
206 else
207         echo "usage: $0 resource file [expected-failure-codes]"
208         RC=-1
209 fi
210
211 echo `date` "   Done $0, returning $RC"
212 exit $RC