#!/bin/bash

. ./common-vars
. ./admin-credentials.rc

for PARTICIPANT in `seq ${START} ${MAX}` ; do

    OPENSTACK_USER=hackfest-$1
    PROJECT=$OPENSTACK_USER

    echo Cleaning up ${OPENSTACK_USER}

    PROJECT_ID=`openstack project list | grep $PROJECT | awk '{print $2}'`
    if [ "${PROJECT_ID}" != "" ]; then

        for RBAC in `openstack network rbac list -f value -c ID`; do
            openstack network rbac show $RBAC -f value | grep $PROJECT_ID 2> /dev/null
            if [ $? -eq 0 ] ; then
                echo "Deleting RBAC policy $RBAC"
                openstack network rbac delete $RBAC
            fi
        done
    fi

    echo "Deleting OpenStack project: $PROJECT"
    openstack project delete --domain ${ADMIN_DOMAIN} $PROJECT

    echo "Deleting OpenStack User: $OPENSTACK_USER"
    openstack user delete --domain ${ADMIN_DOMAIN} $OPENSTACK_USER
done