From c8b9955e1a8f650a0b85c5c065e73135e4ff168a Mon Sep 17 00:00:00 2001
From: Velmurugan Kumar <velkumar@cisco.com>
Date: Thu, 6 Oct 2016 11:19:26 -0700
Subject: [PATCH] keystone v3 support for all endpoint tests

Change-Id: I4c97fe15c8b87770d97c4954c7d11f4d4307e7c7
---
 cloudpulse/openstack/api/cinder_api.py        | 11 ++++++--
 cloudpulse/openstack/api/glance_api.py        | 16 ++++++------
 cloudpulse/openstack/api/keystone_api.py      | 12 +++++++--
 cloudpulse/openstack/api/neutron_api.py       | 26 ++++++++++++++-----
 cloudpulse/openstack/api/nova_api.py          | 15 ++++++++---
 .../plugins/endpoint_tests/endpoint.py        | 18 ++++++++-----
 6 files changed, 69 insertions(+), 29 deletions(-)

diff --git a/cloudpulse/openstack/api/cinder_api.py b/cloudpulse/openstack/api/cinder_api.py
index fd8c010..5a394a9 100644
--- a/cloudpulse/openstack/api/cinder_api.py
+++ b/cloudpulse/openstack/api/cinder_api.py
@@ -10,12 +10,19 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
-from cinderclient.client import Client
+from cinderclient.client import Client as cinder_client
+from keystoneclient.auth.identity import v3 as keystone_v3
+from keystoneclient import session
 
 
 class CinderHealth(object):
+
     def __init__(self, creds):
-        self.cinderclient = Client(**creds)
+        cacert = creds['cacert']
+        del creds['cacert']
+        auth = keystone_v3.Password(**creds)
+        sess = session.Session(auth=auth, verify=cacert)
+        self.cinderclient = cinder_client(2, session=sess)
 
     def cinder_list(self):
         try:
diff --git a/cloudpulse/openstack/api/glance_api.py b/cloudpulse/openstack/api/glance_api.py
index 9ec3bd4..16bd29b 100644
--- a/cloudpulse/openstack/api/glance_api.py
+++ b/cloudpulse/openstack/api/glance_api.py
@@ -12,18 +12,18 @@
 
 from glanceclient.exc import ClientException
 from glanceclient.v2 import client as glance_client
+from keystoneclient.auth.identity import v3 as keystone_v3
+from keystoneclient import session
 
 
 class GlanceHealth(object):
 
-    def __init__(self, keystone_instance):
-        authtoken = keystone_instance.keystone_return_authtoken()
-        glance_endpoint = (keystone_instance
-                           .keystone_endpoint_find(service_type='image',
-                                                   endpoint_type='internalURL')
-                           )
-        self.glanceclient = glance_client.Client(glance_endpoint,
-                                                 token=authtoken)
+    def __init__(self, creds):
+        cacert = creds['cacert']
+        del creds['cacert']
+        auth = keystone_v3.Password(**creds)
+        sess = session.Session(auth=auth, verify=cacert)
+        self.glanceclient = glance_client.Client('1', session=sess)
 
     def glance_image_list(self):
         try:
diff --git a/cloudpulse/openstack/api/keystone_api.py b/cloudpulse/openstack/api/keystone_api.py
index 9620e25..3d3c1c2 100644
--- a/cloudpulse/openstack/api/keystone_api.py
+++ b/cloudpulse/openstack/api/keystone_api.py
@@ -10,13 +10,21 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
+from keystoneclient.auth.identity import v3 as keystone_v3
+from keystoneclient import client as keystoneclient
 from keystoneclient.exceptions import ClientException
-from keystoneclient.v2_0 import client as keystone_client
+from keystoneclient import session
 
 
 class KeystoneHealth(object):
+
     def __init__(self, creds):
-        self.keystoneclient = keystone_client.Client(**creds)
+        cacert = creds['cacert']
+        del creds['cacert']
+        auth = keystone_v3.Password(**creds)
+        sess = session.Session(auth=auth, verify=cacert)
+        self.keystoneclient = keystoneclient.Client(
+            3, session=sess, auth_url=creds['auth_url'])
 
     def keystone_service_list(self):
         try:
diff --git a/cloudpulse/openstack/api/neutron_api.py b/cloudpulse/openstack/api/neutron_api.py
index 35b4b1a..9afe934 100644
--- a/cloudpulse/openstack/api/neutron_api.py
+++ b/cloudpulse/openstack/api/neutron_api.py
@@ -10,23 +10,37 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
+
+from keystoneclient.auth.identity import v3 as keystone_v3
+from keystoneclient import session
 from neutronclient.common.exceptions import NeutronException
-from neutronclient.v2_0 import client as neutron_client
+from neutronclient.neutron import client as neutronclient
 
 
 class NeutronHealth(object):
+
     def __init__(self, creds):
-        creds['timeout'] = 30
-        creds['ca_cert'] = creds['cacert']
-        self.neutronclient = neutron_client.Client(**creds)
+        # creds['timeout'] = 30
+        cacert = creds['cacert']
+        del creds['cacert']
+        auth = keystone_v3.Password(**creds)
+        sess = session.Session(auth=auth, verify=cacert)
+        self.neutron_client = neutronclient.Client('2.0', session=sess)
 
     def neutron_agent_list(self):
         try:
-            agent_list = self.neutronclient.list_agents()
+            agent_list = self.neutron_client.list_agents()
         except (NeutronException, Exception) as e:
             return (404, e.message, [])
         return (200, "success", agent_list['agents'])
 
+    def neutron_list_networks(self):
+        try:
+            net_list = self.neutron_client.list_networks()
+        except (NeutronException, Exception) as e:
+            return (404, e.message, [])
+        return (200, "success", net_list['networks'])
+
     def network_create(self, network_name):
         try:
             self.neutronclient.format = 'json'
@@ -44,7 +58,7 @@ class NeutronHealth(object):
                 "network_id": network_id,
                 "ip_version": 4,
                 "cidr": network_cidr
-                }
+            }
             res = self.neutronclient.create_subnet({'subnet': subnet})
         except (NeutronException, Exception) as e:
             return (404, e.message, [])
diff --git a/cloudpulse/openstack/api/nova_api.py b/cloudpulse/openstack/api/nova_api.py
index d1dd0a0..73c8cec 100644
--- a/cloudpulse/openstack/api/nova_api.py
+++ b/cloudpulse/openstack/api/nova_api.py
@@ -10,14 +10,21 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
-from novaclient.client import Client
+from keystoneclient.auth.identity import v3 as keystone_v3
+from keystoneclient import session
+from novaclient.client import Client as novaclient
 from novaclient.exceptions import ClientException
 
 
 class NovaHealth(object):
-    def __init__(self, creden):
-        creden['timeout'] = 30
-        self.novaclient = Client(**creden)
+
+    def __init__(self, creds):
+        # creden['timeout'] = 30
+        cacert = creds['cacert']
+        del creds['cacert']
+        auth = keystone_v3.Password(**creds)
+        sess = session.Session(auth=auth, verify=cacert)
+        self.novaclient = novaclient(2, session=sess)
 
     def nova_hypervisor_list(self):
         try:
diff --git a/cloudpulse/scenario/plugins/endpoint_tests/endpoint.py b/cloudpulse/scenario/plugins/endpoint_tests/endpoint.py
index 96f2433..6d6184c 100644
--- a/cloudpulse/scenario/plugins/endpoint_tests/endpoint.py
+++ b/cloudpulse/scenario/plugins/endpoint_tests/endpoint.py
@@ -57,11 +57,16 @@ class endpoint_scenario(base.Scenario):
         importutils.import_module('keystonemiddleware.auth_token')
         creds = {}
         creds['username'] = cfg.CONF.keystone_authtoken.username
-        creds['tenant_name'] = cfg.CONF.keystone_authtoken.project_name
         creds['password'] = cfg.CONF.keystone_authtoken.password
+        creds['project_name'] = cfg.CONF.keystone_authtoken.project_name
         creds['auth_url'] = cfg.CONF.keystone_authtoken.auth_uri
         creds['cacert'] = cfg.CONF.keystone_authtoken.cafile
-        creds['endpoint_type'] = 'internalURL'
+        if cfg.CONF.keystone_authtoken.project_domain_id:
+            creds[
+                'project_domain_id'] = (cfg.CONF.keystone_authtoken.
+                                        project_domain_id)
+            creds[
+                'user_domain_id'] = cfg.CONF.keystone_authtoken.user_domain_id
         return creds
 
     def _get_nova_v2_credentials(self):
@@ -78,7 +83,7 @@ class endpoint_scenario(base.Scenario):
 
     @base.scenario(admin_only=False, operator=False)
     def nova_endpoint(self, *args, **kwargs):
-        creds = self._get_nova_v2_credentials()
+        creds = self._get_credentials()
         nova = NovaHealth(creds)
         return nova.nova_service_list()
 
@@ -86,7 +91,7 @@ class endpoint_scenario(base.Scenario):
     def neutron_endpoint(self, *args, **kwargs):
         creds = self._get_credentials()
         neutron = NeutronHealth(creds)
-        return neutron.neutron_agent_list()
+        return neutron.neutron_list_networks()
 
     @base.scenario(admin_only=False, operator=False)
     def keystone_endpoint(self, *args, **kwargs):
@@ -97,13 +102,12 @@ class endpoint_scenario(base.Scenario):
     @base.scenario(admin_only=False, operator=False)
     def glance_endpoint(self, *args, **kwargs):
         creds = self._get_credentials()
-        keystone = KeystoneHealth(creds)
-        glance = GlanceHealth(keystone)
+        glance = GlanceHealth(creds)
         return glance.glance_image_list()
 
     @base.scenario(admin_only=False, operator=False)
     def cinder_endpoint(self, *args, **kwargs):
-        creds = self._get_nova_v2_credentials()
+        creds = self._get_credentials()
         cinder = CinderHealth(creds)
         return cinder.cinder_list()