feature 1417: Add pdu CRUD
[osm/NBI.git] / osm_nbi / tests / clear-all.sh
1 #! /bin/bash
2 # author: Alfonso Tierno
3 # Script that uses the test NBI URL to clean database. See usage
4
5
6 function usage(){
7 echo -e "usage: $0 [OPTIONS]"
8 echo -e "TEST NBI API is used to clean database content, except user admin. Useful for testing."
9 echo -e "NOTE: database is cleaned but not the content of other modules as RO or VCA that must be cleaned manually."
10 echo -e " OPTIONS"
11 echo -e " -h --help: show this help"
12 echo -e " -f --force: Do not ask for confirmation"
13 echo -e " --clean-RO: clean RO content. RO client (openmano) must be installed and configured"
14 echo -e " --clean-VCA: clean VCA content. juju must be installed and configured"
15 echo -e " ENV variable 'OSMNBI_URL' is used for the URL of the NBI server. If missing, it uses" \
16 "'https://\$OSM_HOSTNAME:9999/osm'. If 'OSM_HOSTNAME' is missing, localhost is used"
17 }
18
19
20 function ask_user(){
21 # ask to the user and parse a response among 'y', 'yes', 'n' or 'no'. Case insensitive.
22 # Params: $1 text to ask; $2 Action by default, can be 'y' for yes, 'n' for no, other or empty for not allowed.
23 # Return: true(0) if user type 'yes'; false (1) if user type 'no'
24 read -e -p "$1" USER_CONFIRMATION
25 while true ; do
26 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'y' ] && return 0
27 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'n' ] && return 1
28 [ "${USER_CONFIRMATION,,}" == "yes" ] || [ "${USER_CONFIRMATION,,}" == "y" ] && return 0
29 [ "${USER_CONFIRMATION,,}" == "no" ] || [ "${USER_CONFIRMATION,,}" == "n" ] && return 1
30 read -e -p "Please type 'yes' or 'no': " USER_CONFIRMATION
31 done
32 }
33
34
35 while [ -n "$1" ]
36 do
37 option="$1"
38 shift
39 ( [ "$option" == -h ] || [ "$option" == --help ] ) && usage && exit
40 ( [ "$option" == -f ] || [ "$option" == --force ] ) && OSMNBI_CLEAN_FORCE=yes && continue
41 [ "$option" == --clean-RO ] && OSMNBI_CLEAN_RO=yes && continue
42 [ "$option" == --clean-VCA ] && OSMNBI_CLEAN_VCA=yes && continue
43 echo "Unknown option '$option'. Type $0 --help" 2>&1 && exit 1
44 done
45
46
47 [ -n "$OSMNBI_CLEAN_FORCE" ] || ask_user "Clean database content (y/N)?" n || exit
48 [ -z "$OSM_HOSTNAME" ] && OSM_HOSTNAME=localhost
49 [ -z "$OSMNBI_URL" ] && OSMNBI_URL="https://${OSM_HOSTNAME}:9999/osm"
50
51 if [ -n "$OSMNBI_CLEAN_RO" ]
52 then
53 export OPENMANO_TENANT=osm
54 for dc in `openmano datacenter-list | awk '{print $1}'`
55 do
56 export OPENMANO_DATACENTER=$dc
57 for i in instance-scenario scenario vnf
58 do
59 for f in `openmano $i-list | awk '{print $1}'`
60 do
61 [[ -n "$f" ]] && [[ "$f" != No ]] && openmano ${i}-delete -f ${f}
62 done
63 done
64 done
65 fi
66
67 for item in vim_accounts vims sdns nsrs vnfrs nslcmops nsds vnfds projects
68 do
69 curl --insecure ${OSMNBI_URL}/test/db-clear/${item}
70 echo " ${item}"
71 done
72 # delete all users except admin
73 curl --insecure ${OSMNBI_URL}/test/db-clear/users?username.ne=admin
74
75 if [ -n "$OSMNBI_CLEAN_RO" ]
76 then
77 for dc in `openmano datacenter-list | awk '{print $1}'` ; do openmano datacenter-detach $dc ; done
78 for dc in `openmano datacenter-list --all | awk '{print $1}'` ; do openmano datacenter-delete -f $dc ; done
79 for dc in `openmano sdn-controller-list | awk '{print $1}'` ; do openmano sdn-controller-delete -f $dc ; done
80 fi
81
82 if [ -n "$OSMNBI_CLEAN_VCA" ]
83 then
84 juju destroy-model -y default
85 juju add-model default
86 fi