From 74516104a106fd992171a402c5d8be1071210946 Mon Sep 17 00:00:00 2001
From: Thomas Goirand <thomas@goirand.fr>
Date: Mon, 12 Oct 2015 21:43:48 +0000
Subject: [PATCH] Setup both v1 and v2 endpoints.

Rewritten-From: 49e1e71606fd2c4569b47bb28fe2ca9c13304d77
---
 trusty/debian/changelog              |  6 ++++
 trusty/debian/cinder-api.postinst.in | 53 +++++++++++++++++++++++++++-
 2 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/trusty/debian/changelog b/trusty/debian/changelog
index a7a41940a..a880f0335 100644
--- a/trusty/debian/changelog
+++ b/trusty/debian/changelog
@@ -1,3 +1,9 @@
+cinder (2:7.0.0~rc2-2) experimental; urgency=medium
+
+  * Setup both v1 and v2 endpoints.
+
+ -- Thomas Goirand <zigo@debian.org>  Mon, 12 Oct 2015 21:41:08 +0000
+
 cinder (2:7.0.0~rc2-1) experimental; urgency=medium
 
   * cinder-api depends on python-openstackclient.
diff --git a/trusty/debian/cinder-api.postinst.in b/trusty/debian/cinder-api.postinst.in
index 3868b9ae5..8f72438cd 100644
--- a/trusty/debian/cinder-api.postinst.in
+++ b/trusty/debian/cinder-api.postinst.in
@@ -6,7 +6,58 @@ set -e
 
 if [ "$1" = "configure" ] || [ "$1" = "reconfigure" ] ; then
 	. /usr/share/debconf/confmodule
-	pkgos_register_endpoint_postinst cinder cinderv2 volumev2 "Cinder Volume Service" 8776 /v2/'%(tenant_id)s'
+
+	# We need special handling for Cinder, as it needs 2 endpoints
+	PKG_NAME=cinder
+	db_get ${PKG_NAME}/register-endpoint
+	if [ "${RET}" = "true" ] ; then
+		# Setup the v1 endpoint
+		db_get ${PKG_NAME}/keystone-ip
+		KEYSTONE_ENDPOINT_IP=`echo ${RET} | egrep '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'`
+		db_get ${PKG_NAME}/keystone-auth-token
+		AUTH_TOKEN=${RET}
+		db_get ${PKG_NAME}/endpoint-ip
+		PKG_ENDPOINT_IP=`echo ${RET} | egrep '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'`
+		db_get ${PKG_NAME}/region-name
+		REGION_NAME=${RET}
+
+		if [ -n "${KEYSTONE_ENDPOINT_IP}" ] && [ -n "${PKG_ENDPOINT_IP}" ] && [ -n "${REGION_NAME}" ] && [ -n "${AUTH_TOKEN}" ] ; then
+		        SERVICE_NAME=cinder
+		        SERVICE_TYPE=volume
+			SERVICE_DESC="OpenStack Block Storage"
+			SERVICE_PORT=8776
+			SERVICE_URL=/v1/'%(tenant_id)s'
+			echo "Registering service and endpoints for ${SERVICE_NAME} with type ${SERVICE_TYPE} at http://${PKG_ENDPOINT_IP}:${SERVICE_PORT}${SERVICE_URL}"
+			openstack --os-token ${AUTH_TOKEN} --os-url=http://${KEYSTONE_ENDPOINT_IP}:35357/v2.0/ service create \
+				--name=${SERVICE_NAME} --description="${SERVICE_DESC}" ${SERVICE_TYPE}
+			openstack --os-token ${AUTH_TOKEN} --os-url=http://${KEYSTONE_ENDPOINT_IP}:35357/v2.0/ endpoint create \
+				--region "${REGION_NAME}" \
+				--publicurl=http://${PKG_ENDPOINT_IP}:${SERVICE_PORT}${SERVICE_URL} \--publicurl=http://${PKG_ENDPOINT_IP}:${SERVICE_PORT}${SERVICE_URL} \
+				--internalurl=http://${PKG_ENDPOINT_IP}:${SERVICE_PORT}${SERVICE_URL} \
+				--adminurl=http://${PKG_ENDPOINT_IP}:${SERVICE_PORT}${SERVICE_URL} ${SERVICE_NAME}
+		        SERVICE_NAME=cinderv2
+		        SERVICE_TYPE=volumev2
+			SERVICE_DESC="OpenStack Block Storage v2"
+			SERVICE_PORT=8776
+			SERVICE_URL=/v2/'%(tenant_id)s'
+			echo "Registering service and endpoints for ${SERVICE_NAME} with type ${SERVICE_TYPE} at http://${PKG_ENDPOINT_IP}:${SERVICE_PORT}${SERVICE_URL}"
+			openstack --os-token ${AUTH_TOKEN} --os-url=http://${KEYSTONE_ENDPOINT_IP}:35357/v2.0/ service create \
+				--name=${SERVICE_NAME} --description="${SERVICE_DESC}" ${SERVICE_TYPE}
+			openstack --os-token ${AUTH_TOKEN} --os-url=http://${KEYSTONE_ENDPOINT_IP}:35357/v2.0/ endpoint create \
+				--region "${REGION_NAME}" \
+				--publicurl=http://${PKG_ENDPOINT_IP}:${SERVICE_PORT}${SERVICE_URL} \--publicurl=http://${PKG_ENDPOINT_IP}:${SERVICE_PORT}${SERVICE_URL} \
+				--internalurl=http://${PKG_ENDPOINT_IP}:${SERVICE_PORT}${SERVICE_URL} \
+				--adminurl=http://${PKG_ENDPOINT_IP}:${SERVICE_PORT}${SERVICE_URL} ${SERVICE_NAME}
+#			pkgos_register_endpoint_postinst cinder cinder volume "Cinder Volume Service" 8776 /v1/'%(tenant_id)s'
+#			pkgos_register_endpoint_postinst cinder cinderv2 volumev2 "Cinder Volume Service" 8776 /v2/'%(tenant_id)s'
+		else
+			echo "Problem in endpoint parameter (IPs or otherwise)."
+		fi
+		db_unregister ${PKG_NAME}/register-endpoint
+	else
+		echo "Will not register "${SERVICE_NAME}" endpoint this time (no user request for it)."
+	fi
+	db_unregister ${PKG_NAME}/keystone-auth-token
 	db_stop
 fi
 
-- 
2.45.2