VCD feature 7193-provider_nerwork
[osm/NBI.git] / osm_nbi / tests / clear-all.sh
index 5ba9d4b..23a87ac 100755 (executable)
@@ -1,16 +1,31 @@
 #! /bin/bash
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
 # author: Alfonso Tierno
 # Script that uses the test NBI URL to clean database. See usage
 
 
 function usage(){
     echo -e "usage: $0 [OPTIONS]"
-    echo -e "TEST NBI API is used to clean database content, except user admin. Useful for testing."
+    echo -e "TEST NBI API is used to clean database content, except admin users,projects and roles. Useful for testing."
     echo -e "NOTE: database is cleaned but not the content of other modules as RO or VCA that must be cleaned manually."
     echo -e "  OPTIONS"
     echo -e "     -h --help:   show this help"
     echo -e "     -f --force:  Do not ask for confirmation"
-    echo -e "     --completely:  It cleans also user admin. NBI will need to be restarted to init database"
+    echo -e "     --completely:  It cleans also admin user/project and roles. It only works for internal" \
+            "authentication. NBI will need to be restarted to init database"
     echo -e "     --clean-RO:  clean RO content. RO client (openmano) must be installed and configured"
     echo -e "     --clean-VCA: clean VCA content. juju  must be installed and configured"
     echo -e "  ENV variable 'OSMNBI_URL' is used for the URL of the NBI server. If missing, it uses" \
@@ -66,28 +81,59 @@ then
     done
 fi
 
-for item in vim_accounts sdns nsrs vnfrs nslcmops nsds vnfds projects nsts # vims
+for item in vim_accounts wim_accounts sdns nsrs vnfrs nslcmops nsds vnfds pdus nsts nsis nsilcmops # vims
 do
     curl --insecure ${OSMNBI_URL}/test/db-clear/${item}
-    echo " ${item}"
 done
+curl --insecure ${OSMNBI_URL}/test/fs-clear
 if [ -n "$OSMNBI_COMPLETELY" ] ; then
-    curl --insecure ${OSMNBI_URL}/test/db-clear/users && echo " ${item}"
-    curl --insecure ${OSMNBI_URL}/test/db-clear/version && echo " ${item}"
-else
-    # delete all users except admin
-    curl --insecure ${OSMNBI_URL}/test/db-clear/users?username.ne=admin
+    # delete all users. It will only works for internal backend
+    curl --insecure ${OSMNBI_URL}/test/db-clear/users
+    curl --insecure ${OSMNBI_URL}/test/db-clear/projects
+    curl --insecure ${OSMNBI_URL}/test/db-clear/roles
 fi
 
+    [[ -z "$OSM_USER" ]] && OSM_USER=admin
+    [[ -z "$OSM_PASSWORD" ]] && OSM_PASSWORD=admin
+    [[ -z "$OSM_PROJECT" ]] && OSM_PROJECT=admin
+
+    TOKEN=`curl --insecure -H "Content-Type: application/yaml" -H "Accept: application/yaml" \
+        --data "{username: '$OSM_USER', password: '$OSM_PASSWORD', project_id: '$OSM_PROJECT'}" \
+        ${OSMNBI_URL}/admin/v1/tokens 2>/dev/null | awk '($1=="_id:"){print $2}'`;
+    echo "TOKEN='$TOKEN'"
+
+    echo "delete users, prujects,roles. Ignore response errors due that own user,project cannot be deleted"
+    for topic in users projects roles
+    do
+        elements=`curl --insecure ${OSMNBI_URL}/admin/v1/$topic -H "Authorization: Bearer $TOKEN" \
+            -H "Accept: application/yaml" 2>/dev/null | awk '($1=="_id:"){print $2};($2=="_id:"){print $3}'`;
+        for element in $elements
+        do
+            # not needed to check if own user, project, etc; because OSM will deny deletion
+            echo deleting $topic _id=$element
+            curl --insecure ${OSMNBI_URL}/admin/v1/$topic/$element -H "Authorization: Bearer $TOKEN" \
+                -H "Accept: application/yaml" -X DELETE 2>/dev/null
+        done
+    done
+
+
 if [ -n "$OSMNBI_CLEAN_RO" ]
 then
     for dc in `openmano datacenter-list | awk '{print $1}'` ; do openmano datacenter-detach $dc ; done
     for dc in `openmano datacenter-list --all | awk '{print $1}'` ; do openmano datacenter-delete -f  $dc ; done
     for dc in `openmano sdn-controller-list | awk '{print $1}'` ; do openmano sdn-controller-delete -f $dc ; done
+    for dc in `openmano wim-list | awk '{print $1}'` ; do openmano wim-detach $dc ; done
+    for dc in `openmano wim-list --all | awk '{print $1}'` ; do openmano wim-delete -f  $dc ; done
 fi
 
 if [ -n "$OSMNBI_CLEAN_VCA" ]
 then
-    juju destroy-model -y default
-    juju add-model default
+    for juju_model in `juju models | grep lxd | grep -v controller | grep -v default | awk '{print$1}'`
+    do
+       echo
+       echo juju destroy-model  $juju_model
+       juju destroy-model -y $juju_model
+    done
+    # juju destroy-model -y default
+    # juju add-model default
 fi