#!/bin/sh

set -e

if ! [ -r /root/oci-openrc ] ; then
	echo "Cannot read /root/oci-openrc: exiting."
	exit 1
fi

. /root/oci-openrc

# Tempest runs with dynamic credentials, meaning that the per test class
# projects it creates inherit neutron's default quotas, which are raised
# in puppet (QUOTAS/quota_router and QUOTAS/quota_floatingip of
# neutron.conf). The heat plugin though authenticates as the zabbix
# project, and tempest's alt credentials are the tempest2 project, so
# the quota of both projects must be raised on their own.

for PROJECT in zabbix tempest2 ; do
	PROJECT_ID=$(openstack project show ${PROJECT} --format value -c id)
	echo "-> Raising neutron routers and floating IPs quota of the ${PROJECT} project"
	ROUTERS_NBR=$(openstack quota show ${PROJECT_ID} -c routers --format value)
	# The floating IPs column is spelled floating_ips on newer releases of
	# openstackclient, and floating-ips on older ones (ex: Xena).
	if openstack quota show ${PROJECT_ID} | grep -q floating_ips ; then
		FLOATINGIPS_NBR=$(openstack quota show ${PROJECT_ID} -c floating_ips --format value)
	else
		FLOATINGIPS_NBR=$(openstack quota show ${PROJECT_ID} -c floating-ips --format value)
	fi
	if [ "${ROUTERS_NBR}" -lt 100 ] || [ "${FLOATINGIPS_NBR}" -lt 200 ] ; then
		openstack quota set --routers 100 --floating-ips 200 ${PROJECT_ID}
	else
		echo "Routers and floating IPs quota of the ${PROJECT} project already raised"
	fi
done
