#!/bin/bash

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

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

    OPENSTACK_USER=hackfest-${PARTICIPANT}
    PROJECT=hackfest-${PARTICIPANT}
    PASSWORD=hackfest

    echo "Creating OpenStack project: $PROJECT"
    openstack project create --domain ${ADMIN_DOMAIN} $PROJECT
    PROJECT_ID=`openstack project list | grep $PROJECT | awk '{print $2}'`
    openstack quota set --cores 64 --ram 131072 $PROJECT_ID

    echo "Creating OpenStack User"
    openstack user create --domain ${ADMIN_DOMAIN} --password $PASSWORD $OPENSTACK_USER
    openstack role add --user $OPENSTACK_USER --project $PROJECT member

    echo "Creating OpenStack Network RBAC policy access_as_external, for network $NETWORK and project $PROJECT"
    openstack network rbac create \
        --target-project $PROJECT \
        --type network \
        --action access_as_external \
        $NETWORK

    echo "Creating OpenStack Network RBAC policy access_as_shared, for network $NETWORK and project $PROJECT"
    openstack network rbac create \
        --target-project $PROJECT \
        --type network \
        --action access_as_shared \
        $NETWORK

    echo "Adding security groups"
    for i in $(openstack --os-username=$OPENSTACK_USER --os-password=$PASSWORD --os-project-id=$PROJECT_ID security group list | awk '/default/{ print $2 }'); do
        openstack --os-username=$OPENSTACK_USER --os-password=$PASSWORD --os-project-id=$PROJECT_ID security group rule create $i --protocol icmp --remote-ip 0.0.0.0/0
        openstack --os-username=$OPENSTACK_USER --os-password=$PASSWORD --os-project-id=$PROJECT_ID security group rule create $i --protocol udp --remote-ip 0.0.0.0/0
        openstack --os-username=$OPENSTACK_USER --os-password=$PASSWORD --os-project-id=$PROJECT_ID security group rule create $i --protocol tcp --remote-ip 0.0.0.0/0
    done

done