Fix Bug 774 - NBI+Keystone: Trying to obtain a token with user+password+project gives...
[osm/NBI.git] / osm_nbi / tests / clear-all.sh
1 #! /bin/bash
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # author: Alfonso Tierno
17 # Script that uses the test NBI URL to clean database. See usage
18
19
20 function usage(){
21 echo -e "usage: $0 [OPTIONS]"
22 echo -e "TEST NBI API is used to clean database content, except user admin. Useful for testing."
23 echo -e "NOTE: database is cleaned but not the content of other modules as RO or VCA that must be cleaned manually."
24 echo -e " OPTIONS"
25 echo -e " -h --help: show this help"
26 echo -e " -f --force: Do not ask for confirmation"
27 echo -e " --completely: It cleans also user admin. NBI will need to be restarted to init database"
28 echo -e " --clean-RO: clean RO content. RO client (openmano) must be installed and configured"
29 echo -e " --clean-VCA: clean VCA content. juju must be installed and configured"
30 echo -e " ENV variable 'OSMNBI_URL' is used for the URL of the NBI server. If missing, it uses" \
31 "'https://\$OSM_HOSTNAME:9999/osm'. If 'OSM_HOSTNAME' is missing, localhost is used"
32 }
33
34
35 function ask_user(){
36 # ask to the user and parse a response among 'y', 'yes', 'n' or 'no'. Case insensitive.
37 # Params: $1 text to ask; $2 Action by default, can be 'y' for yes, 'n' for no, other or empty for not allowed.
38 # Return: true(0) if user type 'yes'; false (1) if user type 'no'
39 read -e -p "$1" USER_CONFIRMATION
40 while true ; do
41 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'y' ] && return 0
42 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'n' ] && return 1
43 [ "${USER_CONFIRMATION,,}" == "yes" ] || [ "${USER_CONFIRMATION,,}" == "y" ] && return 0
44 [ "${USER_CONFIRMATION,,}" == "no" ] || [ "${USER_CONFIRMATION,,}" == "n" ] && return 1
45 read -e -p "Please type 'yes' or 'no': " USER_CONFIRMATION
46 done
47 }
48
49
50 while [ -n "$1" ]
51 do
52 option="$1"
53 shift
54 ( [ "$option" == -h ] || [ "$option" == --help ] ) && usage && exit
55 ( [ "$option" == -f ] || [ "$option" == --force ] ) && OSMNBI_CLEAN_FORCE=yes && continue
56 [ "$option" == --completely ] && OSMNBI_COMPLETELY=yes && continue
57 [ "$option" == --clean-RO ] && OSMNBI_CLEAN_RO=yes && continue
58 [ "$option" == --clean-VCA ] && OSMNBI_CLEAN_VCA=yes && continue
59 echo "Unknown option '$option'. Type $0 --help" 2>&1 && exit 1
60 done
61
62
63 [ -n "$OSMNBI_CLEAN_FORCE" ] || ask_user "Clean database content (y/N)?" n || exit
64 [ -z "$OSM_HOSTNAME" ] && OSM_HOSTNAME=localhost
65 [ -z "$OSMNBI_URL" ] && OSMNBI_URL="https://${OSM_HOSTNAME}:9999/osm"
66
67 if [ -n "$OSMNBI_CLEAN_RO" ]
68 then
69 export OPENMANO_TENANT=osm
70 for dc in `openmano datacenter-list | awk '{print $1}'`
71 do
72 export OPENMANO_DATACENTER=$dc
73 for i in instance-scenario scenario vnf
74 do
75 for f in `openmano $i-list | awk '{print $1}'`
76 do
77 [[ -n "$f" ]] && [[ "$f" != No ]] && openmano ${i}-delete -f ${f}
78 done
79 done
80 done
81 fi
82
83 for item in vim_accounts wim_accounts sdns nsrs vnfrs nslcmops nsds vnfds projects pdus nsts nsis nsilcmops # vims
84 do
85 curl --insecure ${OSMNBI_URL}/test/db-clear/${item}
86 done
87 curl --insecure ${OSMNBI_URL}/test/fs-clear
88 if [ -n "$OSMNBI_COMPLETELY" ] ; then
89 curl --insecure ${OSMNBI_URL}/test/db-clear/users
90 curl --insecure ${OSMNBI_URL}/test/db-clear/admin
91 else
92 # delete all users except admin
93 curl --insecure ${OSMNBI_URL}/test/db-clear/users?username.ne=admin
94 fi
95
96 if [ -n "$OSMNBI_CLEAN_RO" ]
97 then
98 for dc in `openmano datacenter-list | awk '{print $1}'` ; do openmano datacenter-detach $dc ; done
99 for dc in `openmano datacenter-list --all | awk '{print $1}'` ; do openmano datacenter-delete -f $dc ; done
100 for dc in `openmano sdn-controller-list | awk '{print $1}'` ; do openmano sdn-controller-delete -f $dc ; done
101 for dc in `openmano wim-list | awk '{print $1}'` ; do openmano wim-detach $dc ; done
102 for dc in `openmano wim-list --all | awk '{print $1}'` ; do openmano wim-delete -f $dc ; done
103 fi
104
105 if [ -n "$OSMNBI_CLEAN_VCA" ]
106 then
107 for juju_model in `juju models | grep lxd | grep -v controller | grep -v default | awk '{print$1}'`
108 do
109 echo
110 echo juju destroy-model $juju_model
111 juju destroy-model -y $juju_model
112 done
113 # juju destroy-model -y default
114 # juju add-model default
115 fi