3 # ============LICENSE_START=======================================================
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
25 # The script is called with a resource, filepath, an optional argument to
26 # ignore HTTP failure codes which would otherwise indicate a failure,
27 # and an optional argument to display more data.
28 # It invokes a PUT on the resource with the file using curl
29 # Uses aaiconfig.properties for authorization type and url. The HTTP response
30 # code is checked. Responses between 200 and 299 are considered success.
31 # When the ignore failure code parameter is passed, responses outside of
32 # the 200 to 299 range but matching a sub-string of the parameter are
33 # considered success. For example, a parameter value of 412 will consider
34 # responses in the range of 200 to 299 and 412 successes.
36 # method checking parameter list for two strings, and determine if
37 # the second string is a sub-string of the first
41 if test "${string#*$substring}" != "$string"
43 return 0 # $substring is in $string
45 return 1 # $substring is not in $string
53 1. Usage: putTool.sh <resource-path> <json payload file> <optional HTTP Response code> <optional -display>
54 2. This script requires two arguments, a resource path and a file path to a json file containing the payload.
55 3. Example: query?format=xxxx customquery.json (possible formats are simple, raw, console, count, graphson, id, pathed, resource and resource_and_url)
56 4. Adding the optional HTTP Response code will allow the script to ignore HTTP failure codes that match the input parameter.
57 5. Adding the optional "-display" argument will display all data returned from the request, instead of just a response code.
66 # remove leading slash when present
67 RESOURCE=`echo $1 | sed "s,^/,,"`
68 if [ -z $RESOURCE ]; then
69 echo "resource parameter is missing"
70 echo "usage: $0 resource file [expected-failure-codes]"
74 if [ -z $JSONFILE ]; then
75 echo "json file parameter is missing"
76 echo "usage: $0 resource file [expected-failure-codes]"
79 echo `date` " Starting $0 for resource $RESOURCE"
82 XFROMAPPID="AAI-TOOLS"
85 userid=$( id | cut -f2 -d"(" | cut -f1 -d")" )
86 if [ "${userid}" != "aaiadmin" ]; then
87 echo "You must be aaiadmin to run $0. The id used $userid."
91 . /etc/profile.d/aai.sh
92 PROJECT_HOME=/opt/app/aai-traversal
93 prop_file=$PROJECT_HOME/resources/etc/appprops/aaiconfig.properties
94 log_dir=$PROJECT_HOME/logs/misc
95 today=$(date +\%Y-\%m-\%d)
98 if [ ${#} -ne 2 ]; then
99 if [ "$3" = "-display" ]; then
103 if [ ${#} -ne 3 ]; then
104 if [ "$4" = "-display" ]; then
110 RESTURL=`grep ^aai.server.url= $prop_file |cut -d'=' -f2 |tr -d "\015"`
111 if [ -z $RESTURL ]; then
112 echo "Property [aai.server.url] not found in file $prop_file"
116 BASICENABLE=`grep ^aai.tools.enableBasicAuth $prop_file |cut -d'=' -f2 |tr -d "\015"`
117 if [ -z $BASICENABLE ]; then
121 CURLUSER=`grep ^aai.tools.username $prop_file |cut -d'=' -f2 |tr -d "\015"`
122 if [ -z $CURLUSER ]; then
123 echo "Property [aai.tools.username] not found in file $prop_file"
126 CURLPASSWORD=`grep ^aai.tools.password $prop_file |cut -d'=' -f2 |tr -d "\015"`
127 if [ -z $CURLPASSWORD ]; then
128 echo "Property [aai.tools.password] not found in file $prop_file"
133 if [ $MISSING_PROP = false ]; then
134 if [ $USEBASICAUTH = false ]; then
135 AUTHSTRING="--cert $PROJECT_HOME/resources/etc/auth/aaiClientPublicCert.pem --key $PROJECT_HOME/resources/etc/auth/aaiClientPrivateKey.pem"
137 AUTHSTRING="-u $CURLUSER:$CURLPASSWORD"
140 if [ $RETURNRESPONSE = true ]; then
141 curl --request PUT -sL -k $AUTHSTRING -H "Content-Type: application/json" -H "X-FromAppId: $XFROMAPPID" -H "X-TransactionId: $XTRANSID" -H "Accept: application/json" -T $JSONFILE $RESTURL$RESOURCE | python -mjson.tool
144 result=`curl --request PUT -sL -w "%{http_code}" -o /dev/null -k $AUTHSTRING -H "Content-Type: application/json" -H "X-FromAppId: $XFROMAPPID" -H "X-TransactionId: $XTRANSID" -H "Accept: application/json" -T $JSONFILE $RESTURL$RESOURCE`
145 #echo "result is $result."
147 if [ $? -eq 0 ]; then
150 #if [[ "$result" -eq 412 || "$result" -ge 200 && $result -lt 300 ]]
151 if [[ "$result" -ge 200 && $result -lt 300 ]]
153 echo "PUT result is OK, $result"
155 if [ -z $ALLOWHTTPRESPONSES ]; then
156 echo "PUT request failed, response code was $result"
159 contains $ALLOWHTTPRESPONSES $result
162 echo "PUT request failed, unexpected response code was $result"
165 echo "PUT result is expected, $result"
171 echo "PUT request failed, response was $result"
177 echo "FAILED to send request to $RESTURL"
182 echo "usage: $0 resource file [expected-failure-codes]"
186 echo `date` " Done $0, returning $RC"