Merge "RestClient to target specific services in Keystone catalog"
diff --git a/tempest/common/rest_client.py b/tempest/common/rest_client.py
index 18f86a7..690738f 100644
--- a/tempest/common/rest_client.py
+++ b/tempest/common/rest_client.py
@@ -7,12 +7,13 @@
 
 class RestClient(object):
 
-    def __init__(self, config, user, key, auth_url, tenant_name=None):
+    def __init__(self, config, user, key, auth_url, service, tenant_name=None):
         self.config = config
         if self.config.env.authentication == 'keystone_v2':
             self.token, self.base_url = self.keystone_v2_auth(user,
                                                               key,
                                                               auth_url,
+                                                              service,
                                                               tenant_name)
         else:
             self.token, self.base_url = self.basic_auth(user,
@@ -35,7 +36,7 @@
         except:
             raise
 
-    def keystone_v2_auth(self, user, api_key, auth_url, tenant_name):
+    def keystone_v2_auth(self, user, api_key, auth_url, service, tenant_name):
         """
         Provides authentication via Keystone 2.0
         """
@@ -59,18 +60,25 @@
             try:
                 auth_data = json.loads(body)['access']
                 token = auth_data['token']['id']
-                endpoints = auth_data['serviceCatalog'][0]['endpoints']
-                mgmt_url = endpoints[0]['publicURL']
-
-                #TODO (dwalleck): This is a horrible stopgap.
-                #Need to join strings more cleanly
-                temp = mgmt_url.rsplit('/')
-                service_url = temp[0] + '//' + temp[2] + '/' + temp[3] + '/'
-                management_url = service_url + tenant_name
-                return token, management_url
             except Exception, e:
-                print "Failed to authenticate user: %s" % e
+                print "Failed to obtain token for user: %s" % e
                 raise
+
+            mgmt_url = None
+            for ep in auth_data['serviceCatalog']:
+                if ep["name"] == service:
+                    mgmt_url = ep['endpoints'][0]['publicURL']
+                    break
+
+            if mgmt_url == None:
+                raise exceptions.EndpointNotFound(service)
+
+            #TODO (dwalleck): This is a horrible stopgap.
+            #Need to join strings more cleanly
+            temp = mgmt_url.rsplit('/')
+            service_url = temp[0] + '//' + temp[2] + '/' + temp[3] + '/'
+            management_url = service_url + tenant_name
+            return token, management_url
         elif resp.status == 401:
             raise exceptions.AuthenticationFailure(user=user, password=api_key)
 
diff --git a/tempest/exceptions.py b/tempest/exceptions.py
index 16d6af3..73798eb 100644
--- a/tempest/exceptions.py
+++ b/tempest/exceptions.py
@@ -32,6 +32,14 @@
         self.message = self.msg % kwargs
 
 
+class EndpointNotFound(Exception):
+    def __init__(self, message):
+        self.message = message
+
+    def __str__(self):
+        return repr(self.message)
+
+
 class OverLimit(Exception):
     def __init__(self, message):
         self.message = message
diff --git a/tempest/services/nova/json/extensions_client.py b/tempest/services/nova/json/extensions_client.py
index 3521f14..abd8957 100644
--- a/tempest/services/nova/json/extensions_client.py
+++ b/tempest/services/nova/json/extensions_client.py
@@ -7,7 +7,7 @@
     def __init__(self, config, username, key, auth_url, tenant_name=None):
         self.config = config
         self.client = rest_client.RestClient(config, username, key,
-                                             auth_url, tenant_name)
+                                             auth_url, 'nova', tenant_name)
 
     def list_extensions(self):
         url = 'extensions'
diff --git a/tempest/services/nova/json/flavors_client.py b/tempest/services/nova/json/flavors_client.py
index ba292c3..4d78e66 100644
--- a/tempest/services/nova/json/flavors_client.py
+++ b/tempest/services/nova/json/flavors_client.py
@@ -7,7 +7,7 @@
     def __init__(self, config, username, key, auth_url, tenant_name=None):
         self.config = config
         self.client = rest_client.RestClient(config, username, key,
-                                             auth_url, tenant_name)
+                                             auth_url, 'nova', tenant_name)
 
     def list_flavors(self, params=None):
         url = 'flavors'
diff --git a/tempest/services/nova/json/images_client.py b/tempest/services/nova/json/images_client.py
index 682e66f..12c1774 100644
--- a/tempest/services/nova/json/images_client.py
+++ b/tempest/services/nova/json/images_client.py
@@ -9,7 +9,7 @@
     def __init__(self, config, username, key, auth_url, tenant_name=None):
         self.config = config
         self.client = rest_client.RestClient(config, username, key,
-                                             auth_url, tenant_name)
+                                             auth_url, 'nova', tenant_name)
 
         self.build_interval = self.config.nova.build_interval
         self.build_timeout = self.config.nova.build_timeout
diff --git a/tempest/services/nova/json/limits_client.py b/tempest/services/nova/json/limits_client.py
index 8dd3406..f06235c 100644
--- a/tempest/services/nova/json/limits_client.py
+++ b/tempest/services/nova/json/limits_client.py
@@ -6,7 +6,7 @@
 
     def __init__(self, config, username, key, auth_url, tenant_name=None):
         self.client = rest_client.RestClient(config, username, key,
-                                             auth_url, tenant_name)
+                                             auth_url, 'nova', tenant_name)
 
     def get_limits(self):
         resp, body = self.client.get("limits")
diff --git a/tempest/services/nova/json/servers_client.py b/tempest/services/nova/json/servers_client.py
index 5fac08e..f68d140 100644
--- a/tempest/services/nova/json/servers_client.py
+++ b/tempest/services/nova/json/servers_client.py
@@ -9,7 +9,7 @@
     def __init__(self, config, username, key, auth_url, tenant_name=None):
         self.config = config
         self.client = rest_client.RestClient(config, username, key,
-                                             auth_url, tenant_name)
+                                             auth_url, 'nova', tenant_name)
 
         self.build_interval = self.config.nova.build_interval
         self.build_timeout = self.config.nova.build_timeout