Merge "Refactor rest-client and identity v2-client"
diff --git a/tempest/common/rest_client.py b/tempest/common/rest_client.py
index 033fe70..212d41d 100644
--- a/tempest/common/rest_client.py
+++ b/tempest/common/rest_client.py
@@ -38,7 +38,21 @@
 
 
 class RestClient(object):
+
     TYPE = "json"
+
+    # This is used by _parse_resp method
+    # Redefine it for purposes of your xml service client
+    # List should contain top-xml_tag-names of data, which is like list/array
+    # For example, in keystone it is users, roles, tenants and services
+    # All of it has children with same tag-names
+    list_tags = []
+
+    # This is used by _parse_resp method too
+    # Used for selection of dict-like xmls,
+    # like metadata for Vms in nova, and volumes in cinder
+    dict_tags = ["metadata", ]
+
     LOG = logging.getLogger(__name__)
 
     def __init__(self, auth_provider):
@@ -49,6 +63,9 @@
         # The version of the API this client implements
         self.api_version = None
         self._skip_path = False
+        # NOTE(vponomaryov): self.headers is deprecated now.
+        # should be removed after excluding it from all use places.
+        # Insted of this should be used 'get_headers' method
         self.headers = {'Content-Type': 'application/%s' % self.TYPE,
                         'Accept': 'application/%s' % self.TYPE}
         self.build_interval = CONF.compute.build_interval
@@ -65,6 +82,19 @@
         self.http_obj = http.ClosingHttp(
             disable_ssl_certificate_validation=dscv)
 
+    def _get_type(self):
+        return self.TYPE
+
+    def get_headers(self, accept_type=None, send_type=None):
+        # This method should be used instead of
+        # deprecated 'self.headers'
+        if accept_type is None:
+            accept_type = self._get_type()
+        if send_type is None:
+            send_type = self._get_type()
+        return {'Content-Type': 'application/%s' % send_type,
+                'Accept': 'application/%s' % accept_type}
+
     def __str__(self):
         STRING_LIMIT = 80
         str_format = ("config:%s, service:%s, base_url:%s, "
@@ -74,7 +104,7 @@
                              self.filters, self.build_interval,
                              self.build_timeout,
                              str(self.token)[0:STRING_LIMIT],
-                             str(self.headers)[0:STRING_LIMIT])
+                             str(self.get_headers())[0:STRING_LIMIT])
 
     def _get_region(self, service):
         """
@@ -150,7 +180,7 @@
                 details = pattern.format(read_code, expected_code)
                 raise exceptions.InvalidHttpSuccessCode(details)
 
-    def post(self, url, body, headers):
+    def post(self, url, body, headers=None):
         return self.request('POST', url, headers, body)
 
     def get(self, url, headers=None):
@@ -159,10 +189,10 @@
     def delete(self, url, headers=None, body=None):
         return self.request('DELETE', url, headers, body)
 
-    def patch(self, url, body, headers):
+    def patch(self, url, body, headers=None):
         return self.request('PATCH', url, headers, body)
 
-    def put(self, url, body, headers):
+    def put(self, url, body, headers=None):
         return self.request('PUT', url, headers, body)
 
     def head(self, url, headers=None):
@@ -218,7 +248,48 @@
                                hashlib.md5(str_body).hexdigest())
 
     def _parse_resp(self, body):
-        return json.loads(body)
+        if self._get_type() is "json":
+            body = json.loads(body)
+
+            # We assume, that if the first value of the deserialized body's
+            # item set is a dict or a list, that we just return the first value
+            # of deserialized body.
+            # Essentially "cutting out" the first placeholder element in a body
+            # that looks like this:
+            #
+            #  {
+            #    "users": [
+            #      ...
+            #    ]
+            #  }
+            try:
+                # Ensure there are not more than one top-level keys
+                if len(body.keys()) > 1:
+                    return body
+                # Just return the "wrapped" element
+                first_key, first_item = body.items()[0]
+                if isinstance(first_item, (dict, list)):
+                    return first_item
+            except (ValueError, IndexError):
+                pass
+            return body
+        elif self._get_type() is "xml":
+            element = etree.fromstring(body)
+            if any(s in element.tag for s in self.dict_tags):
+                # Parse dictionary-like xmls (metadata, etc)
+                dictionary = {}
+                for el in element.getchildren():
+                    dictionary[u"%s" % el.get("key")] = u"%s" % el.text
+                return dictionary
+            if any(s in element.tag for s in self.list_tags):
+                # Parse list-like xmls (users, roles, etc)
+                array = []
+                for child in element.getchildren():
+                    array.append(xml_to_json(child))
+                return array
+
+            # Parse one-item-like xmls (user, role, etc)
+            return xml_to_json(element)
 
     def response_checker(self, method, url, headers, body, resp, resp_body):
         if (resp.status in set((204, 205, 304)) or resp.status < 200 or
@@ -248,8 +319,7 @@
         if not resp_body and resp.status >= 400:
             self.LOG.warning("status >= 400 response with empty body")
 
-    def _request(self, method, url,
-                 headers=None, body=None):
+    def _request(self, method, url, headers=None, body=None):
         """A simple HTTP request interface."""
         # Authenticate the request with the auth provider
         req_url, req_headers, req_body = self.auth_provider.auth_request(
@@ -265,12 +335,13 @@
 
         return resp, resp_body
 
-    def request(self, method, url,
-                headers=None, body=None):
+    def request(self, method, url, headers=None, body=None):
         retry = 0
 
         if headers is None:
-            headers = {}
+            # NOTE(vponomaryov): if some client do not need headers,
+            # it should explicitly pass empty dict
+            headers = self.get_headers()
 
         resp, resp_body = self._request(method, url,
                                         headers=headers, body=body)
@@ -390,10 +461,13 @@
         if (not isinstance(resp_body, collections.Mapping) or
                 'retry-after' not in resp):
             return True
-        over_limit = resp_body.get('overLimit', None)
-        if not over_limit:
-            return True
-        return 'exceed' in over_limit.get('message', 'blabla')
+        if self._get_type() is "json":
+            over_limit = resp_body.get('overLimit', None)
+            if not over_limit:
+                return True
+            return 'exceed' in over_limit.get('message', 'blabla')
+        elif self._get_type() is "xml":
+            return 'exceed' in resp_body.get('message', 'blabla')
 
     def wait_for_resource_deletion(self, id):
         """Waits for a resource to be deleted."""
@@ -415,6 +489,11 @@
 
 
 class RestClientXML(RestClient):
+
+    # NOTE(vponomaryov): This is deprecated class
+    # and should be removed after excluding it
+    # from all service clients
+
     TYPE = "xml"
 
     def _parse_resp(self, body):
@@ -440,11 +519,11 @@
         if method == "GET":
             resp, body = self.get(url)
         elif method == "POST":
-            resp, body = self.post(url, body, self.headers)
+            resp, body = self.post(url, body)
         elif method == "PUT":
-            resp, body = self.put(url, body, self.headers)
+            resp, body = self.put(url, body)
         elif method == "PATCH":
-            resp, body = self.patch(url, body, self.headers)
+            resp, body = self.patch(url, body)
         elif method == "HEAD":
             resp, body = self.head(url)
         elif method == "DELETE":
diff --git a/tempest/services/identity/json/identity_client.py b/tempest/services/identity/json/identity_client.py
index c018215..349a9e9 100644
--- a/tempest/services/identity/json/identity_client.py
+++ b/tempest/services/identity/json/identity_client.py
@@ -12,20 +12,23 @@
 
 import json
 
-from tempest.common.rest_client import RestClient
+from tempest.common import rest_client
 from tempest import config
 from tempest import exceptions
 
 CONF = config.CONF
 
 
-class IdentityClientJSON(RestClient):
+class IdentityClientJSON(rest_client.RestClient):
 
     def __init__(self, auth_provider):
         super(IdentityClientJSON, self).__init__(auth_provider)
         self.service = CONF.identity.catalog_type
         self.endpoint_url = 'adminURL'
 
+        # Needed for xml service client
+        self.list_tags = ["roles", "tenants", "users", "services"]
+
     def has_admin_extensions(self):
         """
         Returns True if the KSADM Admin Extensions are supported
@@ -43,9 +46,8 @@
             'name': name,
         }
         post_body = json.dumps({'role': post_body})
-        resp, body = self.post('OS-KSADM/roles', post_body, self.headers)
-        body = json.loads(body)
-        return resp, body['role']
+        resp, body = self.post('OS-KSADM/roles', post_body)
+        return resp, self._parse_resp(body)
 
     def create_tenant(self, name, **kwargs):
         """
@@ -60,30 +62,24 @@
             'enabled': kwargs.get('enabled', True),
         }
         post_body = json.dumps({'tenant': post_body})
-        resp, body = self.post('tenants', post_body, self.headers)
-        body = json.loads(body)
-        return resp, body['tenant']
+        resp, body = self.post('tenants', post_body)
+        return resp, self._parse_resp(body)
 
     def delete_role(self, role_id):
         """Delete a role."""
-        resp, body = self.delete('OS-KSADM/roles/%s' % str(role_id))
-        return resp, body
+        return self.delete('OS-KSADM/roles/%s' % str(role_id))
 
     def list_user_roles(self, tenant_id, user_id):
         """Returns a list of roles assigned to a user for a tenant."""
         url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
         resp, body = self.get(url)
-        body = json.loads(body)
-        return resp, body['roles']
+        return resp, self._parse_resp(body)
 
     def assign_user_role(self, tenant_id, user_id, role_id):
         """Add roles to a user on a tenant."""
-        post_body = json.dumps({})
         resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
-                              (tenant_id, user_id, role_id), post_body,
-                              self.headers)
-        body = json.loads(body)
-        return resp, body['role']
+                              (tenant_id, user_id, role_id), "")
+        return resp, self._parse_resp(body)
 
     def remove_user_role(self, tenant_id, user_id, role_id):
         """Removes a role assignment for a user on a tenant."""
@@ -92,20 +88,17 @@
 
     def delete_tenant(self, tenant_id):
         """Delete a tenant."""
-        resp, body = self.delete('tenants/%s' % str(tenant_id))
-        return resp, body
+        return self.delete('tenants/%s' % str(tenant_id))
 
     def get_tenant(self, tenant_id):
         """Get tenant details."""
         resp, body = self.get('tenants/%s' % str(tenant_id))
-        body = json.loads(body)
-        return resp, body['tenant']
+        return resp, self._parse_resp(body)
 
     def list_roles(self):
         """Returns roles."""
         resp, body = self.get('OS-KSADM/roles')
-        body = json.loads(body)
-        return resp, body['roles']
+        return resp, self._parse_resp(body)
 
     def list_tenants(self):
         """Returns tenants."""
@@ -133,10 +126,8 @@
             'enabled': en,
         }
         post_body = json.dumps({'tenant': post_body})
-        resp, body = self.post('tenants/%s' % tenant_id, post_body,
-                               self.headers)
-        body = json.loads(body)
-        return resp, body['tenant']
+        resp, body = self.post('tenants/%s' % tenant_id, post_body)
+        return resp, self._parse_resp(body)
 
     def create_user(self, name, password, tenant_id, email, **kwargs):
         """Create a user."""
@@ -149,34 +140,28 @@
         if kwargs.get('enabled') is not None:
             post_body['enabled'] = kwargs.get('enabled')
         post_body = json.dumps({'user': post_body})
-        resp, body = self.post('users', post_body, self.headers)
-        body = json.loads(body)
-        return resp, body['user']
+        resp, body = self.post('users', post_body)
+        return resp, self._parse_resp(body)
 
     def update_user(self, user_id, **kwargs):
         """Updates a user."""
         put_body = json.dumps({'user': kwargs})
-        resp, body = self.put('users/%s' % user_id, put_body,
-                              self.headers)
-        body = json.loads(body)
-        return resp, body['user']
+        resp, body = self.put('users/%s' % user_id, put_body)
+        return resp, self._parse_resp(body)
 
     def get_user(self, user_id):
         """GET a user."""
         resp, body = self.get("users/%s" % user_id)
-        body = json.loads(body)
-        return resp, body['user']
+        return resp, self._parse_resp(body)
 
     def delete_user(self, user_id):
         """Delete a user."""
-        resp, body = self.delete("users/%s" % user_id)
-        return resp, body
+        return self.delete("users/%s" % user_id)
 
     def get_users(self):
         """Get the list of users."""
         resp, body = self.get("users")
-        body = json.loads(body)
-        return resp, body['users']
+        return resp, self._parse_resp(body)
 
     def enable_disable_user(self, user_id, enabled):
         """Enables or disables a user."""
@@ -184,21 +169,17 @@
             'enabled': enabled
         }
         put_body = json.dumps({'user': put_body})
-        resp, body = self.put('users/%s/enabled' % user_id,
-                              put_body, self.headers)
-        body = json.loads(body)
-        return resp, body
+        resp, body = self.put('users/%s/enabled' % user_id, put_body)
+        return resp, self._parse_resp(body)
 
     def delete_token(self, token_id):
         """Delete a token."""
-        resp, body = self.delete("tokens/%s" % token_id)
-        return resp, body
+        return self.delete("tokens/%s" % token_id)
 
     def list_users_for_tenant(self, tenant_id):
         """List users for a Tenant."""
         resp, body = self.get('/tenants/%s/users' % tenant_id)
-        body = json.loads(body)
-        return resp, body['users']
+        return resp, self._parse_resp(body)
 
     def get_user_by_username(self, tenant_id, username):
         resp, users = self.list_users_for_tenant(tenant_id)
@@ -215,22 +196,19 @@
             'description': kwargs.get('description')
         }
         post_body = json.dumps({'OS-KSADM:service': post_body})
-        resp, body = self.post('/OS-KSADM/services', post_body, self.headers)
-        body = json.loads(body)
-        return resp, body['OS-KSADM:service']
+        resp, body = self.post('/OS-KSADM/services', post_body)
+        return resp, self._parse_resp(body)
 
     def get_service(self, service_id):
         """Get Service."""
         url = '/OS-KSADM/services/%s' % service_id
         resp, body = self.get(url)
-        body = json.loads(body)
-        return resp, body['OS-KSADM:service']
+        return resp, self._parse_resp(body)
 
     def list_services(self):
         """List Service - Returns Services."""
         resp, body = self.get('/OS-KSADM/services/')
-        body = json.loads(body)
-        return resp, body['OS-KSADM:services']
+        return resp, self._parse_resp(body)
 
     def delete_service(self, service_id):
         """Delete Service."""
@@ -238,7 +216,7 @@
         return self.delete(url)
 
 
-class TokenClientJSON(RestClient):
+class TokenClientJSON(IdentityClientJSON):
 
     def __init__(self):
         super(TokenClientJSON, self).__init__(None)
@@ -261,15 +239,17 @@
             }
         }
         body = json.dumps(creds)
-        resp, body = self.post(self.auth_url, headers=self.headers, body=body)
+        resp, body = self.post(self.auth_url, body=body)
 
         return resp, body['access']
 
     def request(self, method, url, headers=None, body=None):
         """A simple HTTP request interface."""
         if headers is None:
-            headers = {}
-
+            # Always accept 'json', for TokenClientXML too.
+            # Because XML response is not easily
+            # converted to the corresponding JSON one
+            headers = self.get_headers(accept_type="json")
         self._log_request(method, url, headers, body)
         resp, resp_body = self.http_obj.request(url, method,
                                                 headers=headers, body=body)
@@ -282,7 +262,9 @@
             raise exceptions.IdentityError(
                 'Unexpected status code {0}'.format(resp.status))
 
-        return resp, json.loads(resp_body)
+        if isinstance(resp_body, str):
+            resp_body = json.loads(resp_body)
+        return resp, resp_body
 
     def get_token(self, user, password, tenant, auth_data=False):
         """
diff --git a/tempest/services/identity/xml/identity_client.py b/tempest/services/identity/xml/identity_client.py
index 7c36680..81846da 100644
--- a/tempest/services/identity/xml/identity_client.py
+++ b/tempest/services/identity/xml/identity_client.py
@@ -12,58 +12,24 @@
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    License for the specific language governing permissions and limitations
 #    under the License.
-
-import json
-
-from lxml import etree
-
-from tempest.common.rest_client import RestClientXML
 from tempest import config
-from tempest import exceptions
-from tempest.services.compute.xml.common import Document
-from tempest.services.compute.xml.common import Element
-from tempest.services.compute.xml.common import xml_to_json
+from tempest.services.compute.xml import common as xml
+from tempest.services.identity.json import identity_client
 
 CONF = config.CONF
 
 XMLNS = "http://docs.openstack.org/identity/api/v2.0"
 
 
-class IdentityClientXML(RestClientXML):
-
-    def __init__(self, auth_provider):
-        super(IdentityClientXML, self).__init__(auth_provider)
-        self.service = CONF.identity.catalog_type
-        self.endpoint_url = 'adminURL'
-
-    def _parse_array(self, node):
-        array = []
-        for child in node.getchildren():
-            array.append(xml_to_json(child))
-        return array
-
-    def _parse_body(self, body):
-        data = xml_to_json(body)
-        return data
-
-    def has_admin_extensions(self):
-        """
-        Returns True if the KSADM Admin Extensions are supported
-        False otherwise
-        """
-        if hasattr(self, '_has_admin_extensions'):
-            return self._has_admin_extensions
-        resp, body = self.list_roles()
-        self._has_admin_extensions = ('status' in resp and resp.status != 503)
-        return self._has_admin_extensions
+class IdentityClientXML(identity_client.IdentityClientJSON):
+    TYPE = "xml"
 
     def create_role(self, name):
         """Create a role."""
-        create_role = Element("role", xmlns=XMLNS, name=name)
-        resp, body = self.post('OS-KSADM/roles', str(Document(create_role)),
-                               self.headers)
-        body = self._parse_body(etree.fromstring(body))
-        return resp, body
+        create_role = xml.Element("role", xmlns=XMLNS, name=name)
+        resp, body = self.post('OS-KSADM/roles',
+                               str(xml.Document(create_role)))
+        return resp, self._parse_resp(body)
 
     def create_tenant(self, name, **kwargs):
         """
@@ -73,70 +39,18 @@
         enabled <true|false>: Initial tenant status (default is true)
         """
         en = kwargs.get('enabled', 'true')
-        create_tenant = Element("tenant",
-                                xmlns=XMLNS,
-                                name=name,
-                                description=kwargs.get('description', ''),
-                                enabled=str(en).lower())
-        resp, body = self.post('tenants', str(Document(create_tenant)),
-                               self.headers)
-        body = self._parse_body(etree.fromstring(body))
-        return resp, body
-
-    def delete_role(self, role_id):
-        """Delete a role."""
-        resp, body = self.delete('OS-KSADM/roles/%s' % str(role_id),
-                                 self.headers)
-        return resp, body
-
-    def list_user_roles(self, tenant_id, user_id):
-        """Returns a list of roles assigned to a user for a tenant."""
-        url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
-        resp, body = self.get(url, self.headers)
-        body = self._parse_array(etree.fromstring(body))
-        return resp, body
-
-    def assign_user_role(self, tenant_id, user_id, role_id):
-        """Add roles to a user on a tenant."""
-        resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
-                              (tenant_id, user_id, role_id), '', self.headers)
-        body = self._parse_body(etree.fromstring(body))
-        return resp, body
-
-    def remove_user_role(self, tenant_id, user_id, role_id):
-        """Removes a role assignment for a user on a tenant."""
-        return self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
-                           (tenant_id, user_id, role_id), self.headers)
-
-    def delete_tenant(self, tenant_id):
-        """Delete a tenant."""
-        resp, body = self.delete('tenants/%s' % str(tenant_id), self.headers)
-        return resp, body
-
-    def get_tenant(self, tenant_id):
-        """Get tenant details."""
-        resp, body = self.get('tenants/%s' % str(tenant_id), self.headers)
-        body = self._parse_body(etree.fromstring(body))
-        return resp, body
-
-    def list_roles(self):
-        """Returns roles."""
-        resp, body = self.get('OS-KSADM/roles', self.headers)
-        body = self._parse_array(etree.fromstring(body))
-        return resp, body
+        create_tenant = xml.Element("tenant",
+                                    xmlns=XMLNS,
+                                    name=name,
+                                    description=kwargs.get('description', ''),
+                                    enabled=str(en).lower())
+        resp, body = self.post('tenants', str(xml.Document(create_tenant)))
+        return resp, self._parse_resp(body)
 
     def list_tenants(self):
         """Returns tenants."""
-        resp, body = self.get('tenants', self.headers)
-        body = self._parse_array(etree.fromstring(body))
-        return resp, body
-
-    def get_tenant_by_name(self, tenant_name):
-        resp, tenants = self.list_tenants()
-        for tenant in tenants:
-            if tenant['name'] == tenant_name:
-                return tenant
-        raise exceptions.NotFound('No such tenant')
+        resp, body = self.get('tenants')
+        return resp, self._parse_resp(body)
 
     def update_tenant(self, tenant_id, **kwargs):
         """Updates a tenant."""
@@ -144,173 +58,69 @@
         name = kwargs.get('name', body['name'])
         desc = kwargs.get('description', body['description'])
         en = kwargs.get('enabled', body['enabled'])
-        update_tenant = Element("tenant",
-                                xmlns=XMLNS,
-                                id=tenant_id,
-                                name=name,
-                                description=desc,
-                                enabled=str(en).lower())
+        update_tenant = xml.Element("tenant",
+                                    xmlns=XMLNS,
+                                    id=tenant_id,
+                                    name=name,
+                                    description=desc,
+                                    enabled=str(en).lower())
 
         resp, body = self.post('tenants/%s' % tenant_id,
-                               str(Document(update_tenant)),
-                               self.headers)
-        body = self._parse_body(etree.fromstring(body))
-        return resp, body
+                               str(xml.Document(update_tenant)))
+        return resp, self._parse_resp(body)
 
     def create_user(self, name, password, tenant_id, email, **kwargs):
         """Create a user."""
-        create_user = Element("user",
-                              xmlns=XMLNS,
-                              name=name,
-                              password=password,
-                              tenantId=tenant_id,
-                              email=email)
+        create_user = xml.Element("user",
+                                  xmlns=XMLNS,
+                                  name=name,
+                                  password=password,
+                                  tenantId=tenant_id,
+                                  email=email)
         if 'enabled' in kwargs:
             create_user.add_attr('enabled', str(kwargs['enabled']).lower())
 
-        resp, body = self.post('users', str(Document(create_user)),
-                               self.headers)
-        body = self._parse_body(etree.fromstring(body))
-        return resp, body
+        resp, body = self.post('users', str(xml.Document(create_user)))
+        return resp, self._parse_resp(body)
 
     def update_user(self, user_id, **kwargs):
         """Updates a user."""
         if 'enabled' in kwargs:
             kwargs['enabled'] = str(kwargs['enabled']).lower()
-        update_user = Element("user", xmlns=XMLNS, **kwargs)
+        update_user = xml.Element("user", xmlns=XMLNS, **kwargs)
 
         resp, body = self.put('users/%s' % user_id,
-                              str(Document(update_user)),
-                              self.headers)
-        body = self._parse_body(etree.fromstring(body))
-        return resp, body
-
-    def get_user(self, user_id):
-        """GET a user."""
-        resp, body = self.get("users/%s" % user_id, self.headers)
-        body = self._parse_body(etree.fromstring(body))
-        return resp, body
-
-    def delete_user(self, user_id):
-        """Delete a user."""
-        resp, body = self.delete("users/%s" % user_id, self.headers)
-        return resp, body
-
-    def get_users(self):
-        """Get the list of users."""
-        resp, body = self.get("users", self.headers)
-        body = self._parse_array(etree.fromstring(body))
-        return resp, body
+                              str(xml.Document(update_user)))
+        return resp, self._parse_resp(body)
 
     def enable_disable_user(self, user_id, enabled):
         """Enables or disables a user."""
-        enable_user = Element("user", enabled=str(enabled).lower())
+        enable_user = xml.Element("user", enabled=str(enabled).lower())
         resp, body = self.put('users/%s/enabled' % user_id,
-                              str(Document(enable_user)), self.headers)
-        body = self._parse_array(etree.fromstring(body))
-        return resp, body
+                              str(xml.Document(enable_user)), self.headers)
+        return resp, self._parse_resp(body)
 
-    def delete_token(self, token_id):
-        """Delete a token."""
-        resp, body = self.delete("tokens/%s" % token_id, self.headers)
-        return resp, body
-
-    def list_users_for_tenant(self, tenant_id):
-        """List users for a Tenant."""
-        resp, body = self.get('/tenants/%s/users' % tenant_id, self.headers)
-        body = self._parse_array(etree.fromstring(body))
-        return resp, body
-
-    def get_user_by_username(self, tenant_id, username):
-        resp, users = self.list_users_for_tenant(tenant_id)
-        for user in users:
-            if user['name'] == username:
-                return user
-        raise exceptions.NotFound('No such user')
-
-    def create_service(self, name, type, **kwargs):
+    def create_service(self, name, service_type, **kwargs):
         """Create a service."""
         OS_KSADM = "http://docs.openstack.org/identity/api/ext/OS-KSADM/v1.0"
-        create_service = Element("service",
-                                 xmlns=OS_KSADM,
-                                 name=name,
-                                 type=type,
-                                 description=kwargs.get('description'))
+        create_service = xml.Element("service",
+                                     xmlns=OS_KSADM,
+                                     name=name,
+                                     type=service_type,
+                                     description=kwargs.get('description'))
         resp, body = self.post('OS-KSADM/services',
-                               str(Document(create_service)),
-                               self.headers)
-        body = self._parse_body(etree.fromstring(body))
-        return resp, body
-
-    def list_services(self):
-        """Returns services."""
-        resp, body = self.get('OS-KSADM/services', self.headers)
-        body = self._parse_array(etree.fromstring(body))
-        return resp, body
-
-    def get_service(self, service_id):
-        """Get Service."""
-        url = '/OS-KSADM/services/%s' % service_id
-        resp, body = self.get(url, self.headers)
-        body = self._parse_body(etree.fromstring(body))
-        return resp, body
-
-    def delete_service(self, service_id):
-        """Delete Service."""
-        url = '/OS-KSADM/services/%s' % service_id
-        return self.delete(url, self.headers)
+                               str(xml.Document(create_service)))
+        return resp, self._parse_resp(body)
 
 
-class TokenClientXML(RestClientXML):
-
-    def __init__(self):
-        super(TokenClientXML, self).__init__(None)
-        auth_url = CONF.identity.uri
-
-        # Normalize URI to ensure /tokens is in it.
-        if 'tokens' not in auth_url:
-            auth_url = auth_url.rstrip('/') + '/tokens'
-
-        self.auth_url = auth_url
+class TokenClientXML(identity_client.TokenClientJSON):
+    TYPE = "xml"
 
     def auth(self, user, password, tenant):
-        passwordCreds = Element("passwordCredentials",
-                                username=user,
-                                password=password)
-        auth = Element("auth", tenantName=tenant)
+        passwordCreds = xml.Element("passwordCredentials",
+                                    username=user,
+                                    password=password)
+        auth = xml.Element("auth", tenantName=tenant)
         auth.append(passwordCreds)
-        resp, body = self.post(self.auth_url, headers=self.headers,
-                               body=str(Document(auth)))
+        resp, body = self.post(self.auth_url, body=str(xml.Document(auth)))
         return resp, body['access']
-
-    def request(self, method, url, headers=None, body=None):
-        """A simple HTTP request interface."""
-        if headers is None:
-            headers = {}
-        # Send XML, accept JSON. XML response is not easily
-        # converted to the corresponding JSON one
-        headers['Accept'] = 'application/json'
-        self._log_request(method, url, headers, body)
-        resp, resp_body = self.http_obj.request(url, method,
-                                                headers=headers, body=body)
-        self._log_response(resp, resp_body)
-
-        if resp.status in [401, 403]:
-            resp_body = json.loads(resp_body)
-            raise exceptions.Unauthorized(resp_body['error']['message'])
-        elif resp.status not in [200, 201]:
-            raise exceptions.IdentityError(
-                'Unexpected status code {0}'.format(resp.status))
-
-        return resp, json.loads(resp_body)
-
-    def get_token(self, user, password, tenant, auth_data=False):
-        """
-        Returns (token id, token data) for supplied credentials
-        """
-        resp, body = self.auth(user, password, tenant)
-
-        if auth_data:
-            return body['token']['id'], body
-        else:
-            return body['token']['id']
diff --git a/tempest/services/object_storage/account_client.py b/tempest/services/object_storage/account_client.py
index e9208b7..efac5f5 100644
--- a/tempest/services/object_storage/account_client.py
+++ b/tempest/services/object_storage/account_client.py
@@ -58,7 +58,7 @@
                 url += 'bulk-delete&'
             url = '?%s%s' % (url, urllib.urlencode(params))
 
-        resp, body = self.delete(url, headers=None, body=data)
+        resp, body = self.delete(url, headers={}, body=data)
         return resp, body
 
     def list_account_metadata(self):
diff --git a/tempest/services/object_storage/container_client.py b/tempest/services/object_storage/container_client.py
index 95b428b..f224407 100644
--- a/tempest/services/object_storage/container_client.py
+++ b/tempest/services/object_storage/container_client.py
@@ -181,7 +181,7 @@
             url += '?'
             url += '&%s' % urllib.urlencode(params)
 
-        resp, body = self.get(url)
+        resp, body = self.get(url, headers={})
         if params and params.get('format') == 'json':
             body = json.loads(body)
         elif params and params.get('format') == 'xml':
diff --git a/tempest/services/object_storage/object_client.py b/tempest/services/object_storage/object_client.py
index ca4f1c1..79c5719 100644
--- a/tempest/services/object_storage/object_client.py
+++ b/tempest/services/object_storage/object_client.py
@@ -51,7 +51,7 @@
         url = "%s/%s" % (str(container), str(object_name))
         if params:
             url += '?%s' % urllib.urlencode(params)
-        resp, body = self.delete(url)
+        resp, body = self.delete(url, headers={})
         return resp, body
 
     def update_object_metadata(self, container, object_name, metadata,
diff --git a/tempest/tests/test_rest_client.py b/tempest/tests/test_rest_client.py
index ead112b..ba43daf 100644
--- a/tempest/tests/test_rest_client.py
+++ b/tempest/tests/test_rest_client.py
@@ -13,11 +13,13 @@
 #    under the License.
 
 import httplib2
+import json
 
 from tempest.common import rest_client
 from tempest import config
 from tempest import exceptions
 from tempest.openstack.common.fixture import mockpatch
+from tempest.services.compute.xml import common as xml
 from tempest.tests import base
 from tempest.tests import fake_auth_provider
 from tempest.tests import fake_config
@@ -26,6 +28,8 @@
 
 class BaseRestClientTestClass(base.TestCase):
 
+    url = 'fake_endpoint'
+
     def _get_region(self):
         return 'fake region'
 
@@ -49,36 +53,33 @@
                                               '_error_checker'))
 
     def test_post(self):
-        __, return_dict = self.rest_client.post('fake_endpoint', {},
-                                                {})
+        __, return_dict = self.rest_client.post(self.url, {}, {})
         self.assertEqual('POST', return_dict['method'])
 
     def test_get(self):
-        __, return_dict = self.rest_client.get('fake_endpoint')
+        __, return_dict = self.rest_client.get(self.url)
         self.assertEqual('GET', return_dict['method'])
 
     def test_delete(self):
-        __, return_dict = self.rest_client.delete('fake_endpoint')
+        __, return_dict = self.rest_client.delete(self.url)
         self.assertEqual('DELETE', return_dict['method'])
 
     def test_patch(self):
-        __, return_dict = self.rest_client.patch('fake_endpoint', {},
-                                                 {})
+        __, return_dict = self.rest_client.patch(self.url, {}, {})
         self.assertEqual('PATCH', return_dict['method'])
 
     def test_put(self):
-        __, return_dict = self.rest_client.put('fake_endpoint', {},
-                                               {})
+        __, return_dict = self.rest_client.put(self.url, {}, {})
         self.assertEqual('PUT', return_dict['method'])
 
     def test_head(self):
         self.useFixture(mockpatch.PatchObject(self.rest_client,
                                               'response_checker'))
-        __, return_dict = self.rest_client.head('fake_endpoint')
+        __, return_dict = self.rest_client.head(self.url)
         self.assertEqual('HEAD', return_dict['method'])
 
     def test_copy(self):
-        __, return_dict = self.rest_client.copy('fake_endpoint')
+        __, return_dict = self.rest_client.copy(self.url)
         self.assertEqual('COPY', return_dict['method'])
 
 
@@ -89,4 +90,143 @@
 
     def test_post(self):
         self.assertRaises(exceptions.NotFound, self.rest_client.post,
-                          'fake_endpoint', {}, {})
+                          self.url, {}, {})
+
+
+class TestRestClientHeadersJSON(TestRestClientHTTPMethods):
+    TYPE = "json"
+
+    def _verify_headers(self, resp):
+        self.assertEqual(self.rest_client._get_type(), self.TYPE)
+        resp = dict((k.lower(), v) for k, v in resp.iteritems())
+        self.assertEqual(self.header_value, resp['accept'])
+        self.assertEqual(self.header_value, resp['content-type'])
+
+    def setUp(self):
+        super(TestRestClientHeadersJSON, self).setUp()
+        self.rest_client.TYPE = self.TYPE
+        self.header_value = 'application/%s' % self.rest_client._get_type()
+
+    def test_post(self):
+        resp, __ = self.rest_client.post(self.url, {})
+        self._verify_headers(resp)
+
+    def test_get(self):
+        resp, __ = self.rest_client.get(self.url)
+        self._verify_headers(resp)
+
+    def test_delete(self):
+        resp, __ = self.rest_client.delete(self.url)
+        self._verify_headers(resp)
+
+    def test_patch(self):
+        resp, __ = self.rest_client.patch(self.url, {})
+        self._verify_headers(resp)
+
+    def test_put(self):
+        resp, __ = self.rest_client.put(self.url, {})
+        self._verify_headers(resp)
+
+    def test_head(self):
+        self.useFixture(mockpatch.PatchObject(self.rest_client,
+                                              'response_checker'))
+        resp, __ = self.rest_client.head(self.url)
+        self._verify_headers(resp)
+
+    def test_copy(self):
+        resp, __ = self.rest_client.copy(self.url)
+        self._verify_headers(resp)
+
+
+class TestRestClientHeadersXML(TestRestClientHeadersJSON):
+    TYPE = "xml"
+
+    # These two tests are needed in one exemplar
+    def test_send_json_accept_xml(self):
+        resp, __ = self.rest_client.get(self.url,
+                                        self.rest_client.get_headers("xml",
+                                                                     "json"))
+        resp = dict((k.lower(), v) for k, v in resp.iteritems())
+        self.assertEqual("application/json", resp["content-type"])
+        self.assertEqual("application/xml", resp["accept"])
+
+    def test_send_xml_accept_json(self):
+        resp, __ = self.rest_client.get(self.url,
+                                        self.rest_client.get_headers("json",
+                                                                     "xml"))
+        resp = dict((k.lower(), v) for k, v in resp.iteritems())
+        self.assertEqual("application/json", resp["accept"])
+        self.assertEqual("application/xml", resp["content-type"])
+
+
+class TestRestClientParseRespXML(BaseRestClientTestClass):
+    TYPE = "xml"
+
+    keys = ["fake_key1", "fake_key2"]
+    values = ["fake_value1", "fake_value2"]
+    item_expected = {key: value for key, value in zip(keys, values)}
+    list_expected = {"body_list": [
+        {keys[0]: values[0]},
+        {keys[1]: values[1]},
+    ]}
+    dict_expected = {"body_dict": {
+        keys[0]: values[0],
+        keys[1]: values[1],
+    }}
+
+    def setUp(self):
+        self.fake_http = fake_http.fake_httplib2()
+        super(TestRestClientParseRespXML, self).setUp()
+        self.rest_client.TYPE = self.TYPE
+
+    def test_parse_resp_body_item(self):
+        body_item = xml.Element("item", **self.item_expected)
+        body = self.rest_client._parse_resp(str(xml.Document(body_item)))
+        self.assertEqual(self.item_expected, body)
+
+    def test_parse_resp_body_list(self):
+        self.rest_client.list_tags = ["fake_list", ]
+        body_list = xml.Element(self.rest_client.list_tags[0])
+        for i in range(2):
+            body_list.append(xml.Element("fake_item",
+                                         **self.list_expected["body_list"][i]))
+        body = self.rest_client._parse_resp(str(xml.Document(body_list)))
+        self.assertEqual(self.list_expected["body_list"], body)
+
+    def test_parse_resp_body_dict(self):
+        self.rest_client.dict_tags = ["fake_dict", ]
+        body_dict = xml.Element(self.rest_client.dict_tags[0])
+
+        for i in range(2):
+            body_dict.append(xml.Element("fake_item", xml.Text(self.values[i]),
+                                         key=self.keys[i]))
+
+        body = self.rest_client._parse_resp(str(xml.Document(body_dict)))
+        self.assertEqual(self.dict_expected["body_dict"], body)
+
+
+class TestRestClientParseRespJSON(TestRestClientParseRespXML):
+    TYPE = "json"
+
+    def test_parse_resp_body_item(self):
+        body = self.rest_client._parse_resp(json.dumps(self.item_expected))
+        self.assertEqual(self.item_expected, body)
+
+    def test_parse_resp_body_list(self):
+        body = self.rest_client._parse_resp(json.dumps(self.list_expected))
+        self.assertEqual(self.list_expected["body_list"], body)
+
+    def test_parse_resp_body_dict(self):
+        body = self.rest_client._parse_resp(json.dumps(self.dict_expected))
+        self.assertEqual(self.dict_expected["body_dict"], body)
+
+    def test_parse_resp_two_top_keys(self):
+        dict_two_keys = self.dict_expected.copy()
+        dict_two_keys.update({"second_key": ""})
+        body = self.rest_client._parse_resp(json.dumps(dict_two_keys))
+        self.assertEqual(dict_two_keys, body)
+
+    def test_parse_resp_one_top_key_without_list_or_dict(self):
+        data = {"one_top_key": "not_list_or_dict_value"}
+        body = self.rest_client._parse_resp(json.dumps(data))
+        self.assertEqual(data, body)