60c3e1c0028c526799aa668215c52ba3032e0839
[aai/traversal.git] / aai-traversal / src / main / scripts / deleteTool.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 # The script is called with a resource to be deleted.
26 # Uses aaiconfig.properties for authorization type and url. 
27 # It invokes a GET on the resource using curl and parses the resource-version.
28 # If found, prompts the user to continue and invokes DELETE using curl.
29 # responses in the range of 200 to 299 are considered successful
30 #
31
32 # remove leading slash when present
33 RESOURCE=`echo $1 | sed "s,^/,,"`
34 if [ -z $RESOURCE ]; then
35         echo "resource parameter is missing"
36         echo "usage: $0 resource"
37         exit 1
38 fi
39 echo `date` "   Starting $0 for resource $RESOURCE"
40
41 XFROMAPPID="AAI-TOOLS"
42 XTRANSID=`uuidgen`
43 XTRANSID1=`uuidgen`
44
45 userid=$( id | cut -f2 -d"(" | cut -f1 -d")" )
46 if [ "${userid}" != "aaiadmin" ]; then
47     echo "You must be aaiadmin to run $0. The id used $userid."
48     exit 1
49 fi
50
51 . /etc/profile.d/aai.sh
52 PROJECT_HOME=/opt/app/aai-traversal
53 prop_file=$PROJECT_HOME/bundleconfig/etc/appprops/aaiconfig.properties
54 log_dir=$PROJECT_HOME/logs/misc
55 today=$(date +\%Y-\%m-\%d)
56
57 MISSING_PROP=false
58 RESTURL=`grep ^aai.server.url= $prop_file |cut -d'=' -f2 |tr -d "\015"`
59 if [ -z $RESTURL ]; then
60         echo "Property [aai.server.url] not found in file $prop_file"
61         MISSING_PROP=true
62 fi
63 USEBASICAUTH=false
64 BASICENABLE=`grep ^aai.tools.enableBasicAuth $prop_file |cut -d'=' -f2 |tr -d "\015"`
65 if [ -z $BASICENABLE ]; then
66         USEBASICAUTH=false
67 else
68         USEBASICAUTH=true
69         CURLUSER=`grep ^aai.tools.username $prop_file |cut -d'=' -f2 |tr -d "\015"`
70         if [ -z $CURLUSER ]; then
71                 echo "Property [aai.tools.username] not found in file $prop_file"
72                 MISSING_PROP=true
73         fi
74         CURLPASSWORD=`grep ^aai.tools.password $prop_file |cut -d'=' -f2 |tr -d "\015"`
75         if [ -z $CURLPASSWORD ]; then
76                 echo "Property [aai.tools.password] not found in file $prop_file"
77                 MISSING_PROP=true
78         fi
79 fi
80
81 if [ $MISSING_PROP = false ]; then
82         if [ $USEBASICAUTH = false ]; then
83                 AUTHSTRING="--cert $PROJECT_HOME/bundleconfig/etc/auth/aaiClientPublicCert.pem --key $PROJECT_HOME/bundleconfig/etc/auth/aaiClientPrivateKey.pem"
84         else
85                 AUTHSTRING="-u $CURLUSER:$CURLPASSWORD"
86         fi
87         RESOURCEVERSION=`curl --request GET -sL -k $AUTHSTRING -H "X-FromAppId: $XFROMAPPID" -H "X-TransactionId: $XTRANSID1" -H "Accept: application/json" $RESTURL$RESOURCE|awk -F"\"resource-version\":\"" '{print $2}' | cut -d\" -f1`
88         echo "resource-version is" $RESOURCEVERSION
89         if [ -z $RESOURCEVERSION ]; then
90                 echo "failed to get resource-version for $RESOURCE"
91                         echo `date` "   Done $0, returning -1"
92                         exit -1
93         fi
94         echo "Are you sure you would like to delete $RESOURCE? (y, n)"
95         read USERINPUT
96         if [ "$USERINPUT" != "y" ]; then
97                         echo "user chose to exit before delete"
98                         echo `date` "   Done $0, returning -1"
99                         exit -1
100         fi
101                         
102         result=`curl --request DELETE -sL -w "%{http_code}" -o /dev/null -k $AUTHSTRING -H "X-FromAppId: $XFROMAPPID" -H "X-TransactionId: $XTRANSID" -H "Accept: application/json" $RESTURL$RESOURCE?resource-version=$RESOURCEVERSION`
103         echo "result is $result."
104         RC=0;
105         if [ $? -eq 0 ]; then
106                 case $result in
107                         +([0-9])?)
108                                 if [[ "$result" -ge 200 && $result -lt 300 ]]
109                                 then
110                                         echo "DELETE result is OK,  $result"
111                                 else
112                                         echo "failed DELETE request, response code was  $result"
113                                         RC=$result
114                                 fi
115                                 ;;
116                         *)
117                                 echo "DELETE request failed, response was $result"
118                                 RC=-1
119                                 ;;
120
121                 esac
122         else
123                 echo "FAILED to send request to $RESTURL"
124                 RC=-1
125         fi
126 else
127         echo "usage: $0 resource"
128         RC=-1
129 fi
130
131 echo
132 echo `date` "   Done $0, returning $RC"
133 exit $RC