<NME> tenant_usages_client.py
<BEF> ADDFILE
<MSG> port test_simple_tenant_usage into nova v3 part1

This changeset only copies the v2 files into the appropriate v3
directories unchanged. This is being tried in order to make
reviewing of the porting easier as gerrit will display only what
is actually changed for v3 rather than entirely new files.

Partially implements blueprint nova-v3-api-tests

Change-Id: I0ef1d85fe7ce7e9ca836c6e3a5a447df9941eccd

<DFF> @@ -0,0 +1,54 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 NEC Corporation
+# All Rights Reserved.
+#
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    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 urllib
+
+from lxml import etree
+
+from tempest.common.rest_client import RestClientXML
+from tempest.services.compute.xml.common import xml_to_json
+
+
+class TenantUsagesClientXML(RestClientXML):
+
+    def __init__(self, config, username, password, auth_url, tenant_name=None):
+        super(TenantUsagesClientXML, self).__init__(config, username,
+                                                    password, auth_url,
+                                                    tenant_name)
+        self.service = self.config.compute.catalog_type
+
+    def _parse_array(self, node):
+        json = xml_to_json(node)
+        return json
+
+    def list_tenant_usages(self, params=None):
+        url = 'os-simple-tenant-usage'
+        if params:
+            url += '?%s' % urllib.urlencode(params)
+
+        resp, body = self.get(url, self.headers)
+        tenant_usage = self._parse_array(etree.fromstring(body))
+        return resp, tenant_usage['tenant_usage']
+
+    def get_tenant_usage(self, tenant_id, params=None):
+        url = 'os-simple-tenant-usage/%s' % tenant_id
+        if params:
+            url += '?%s' % urllib.urlencode(params)
+
+        resp, body = self.get(url, self.headers)
+        tenant_usage = self._parse_array(etree.fromstring(body))
+        return resp, tenant_usage

{'commit': 'diff --git a/tempest/api/compute/v3/admin/test_simple_tenant_usage.py b/tempest/api/compute/v3/admin/test_simple_tenant_usage.py\nnew file mode 100644\nindex 000000000..a599f0622\n--- /dev/null\n+++ b/tempest/api/compute/v3/admin/test_simple_tenant_usage.py\n@@ -0,0 +1,115 @@\n+# vim: tabstop=4 shiftwidth=4 softtabstop=4\n+\n+# Copyright 2013 NEC Corporation\n+# All Rights Reserved.\n+#\n+#    Licensed under the Apache License, Version 2.0 (the "License"); you may\n+#    not use this file except in compliance with the License. You may obtain\n+#    a copy of the License at\n+#\n+#         http://www.apache.org/licenses/LICENSE-2.0\n+#\n+#    Unless required by applicable law or agreed to in writing, software\n+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\n+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n+#    License for the specific language governing permissions and limitations\n+#    under the License.\n+\n+import datetime\n+\n+from tempest.api.compute import base\n+from tempest import exceptions\n+from tempest.test import attr\n+import time\n+\n+\n+class TenantUsagesTestJSON(base.BaseV2ComputeAdminTest):\n+\n+    _interface = \'json\'\n+\n+    @classmethod\n+    def setUpClass(cls):\n+        super(TenantUsagesTestJSON, cls).setUpClass()\n+        cls.adm_client = cls.os_adm.tenant_usages_client\n+        cls.client = cls.os.tenant_usages_client\n+        cls.identity_client = cls._get_identity_admin_client()\n+\n+        resp, tenants = cls.identity_client.list_tenants()\n+        cls.tenant_id = [tnt[\'id\'] for tnt in tenants if tnt[\'name\'] ==\n+                         cls.client.tenant_name][0]\n+\n+        # Create a server in the demo tenant\n+        resp, server = cls.create_test_server(wait_until=\'ACTIVE\')\n+        time.sleep(2)\n+\n+        now = datetime.datetime.now()\n+        cls.start = cls._parse_strtime(now - datetime.timedelta(days=1))\n+        cls.end = cls._parse_strtime(now + datetime.timedelta(days=1))\n+\n+    @classmethod\n+    def _parse_strtime(cls, at):\n+        # Returns formatted datetime\n+        return at.strftime(\'%Y-%m-%dT%H:%M:%S.%f\')\n+\n+    @attr(type=\'gate\')\n+    def test_list_usage_all_tenants(self):\n+        # Get usage for all tenants\n+        params = {\'start\': self.start,\n+                  \'end\': self.end,\n+                  \'detailed\': int(bool(True))}\n+        resp, tenant_usage = self.adm_client.list_tenant_usages(params)\n+        self.assertEqual(200, resp.status)\n+        self.assertEqual(len(tenant_usage), 8)\n+\n+    @attr(type=\'gate\')\n+    def test_get_usage_tenant(self):\n+        # Get usage for a specific tenant\n+        params = {\'start\': self.start,\n+                  \'end\': self.end}\n+        resp, tenant_usage = self.adm_client.get_tenant_usage(\n+            self.tenant_id, params)\n+\n+        self.assertEqual(200, resp.status)\n+        self.assertEqual(len(tenant_usage), 8)\n+\n+    @attr(type=\'gate\')\n+    def test_get_usage_tenant_with_non_admin_user(self):\n+        # Get usage for a specific tenant with non admin user\n+        params = {\'start\': self.start,\n+                  \'end\': self.end}\n+        resp, tenant_usage = self.client.get_tenant_usage(\n+            self.tenant_id, params)\n+\n+        self.assertEqual(200, resp.status)\n+        self.assertEqual(len(tenant_usage), 8)\n+\n+    @attr(type=[\'negative\', \'gate\'])\n+    def test_get_usage_tenant_with_empty_tenant_id(self):\n+        # Get usage for a specific tenant empty\n+        params = {\'start\': self.start,\n+                  \'end\': self.end}\n+        self.assertRaises(exceptions.NotFound,\n+                          self.adm_client.get_tenant_usage,\n+                          \'\', params)\n+\n+    @attr(type=[\'negative\', \'gate\'])\n+    def test_get_usage_tenant_with_invalid_date(self):\n+        # Get usage for tenant with invalid date\n+        params = {\'start\': self.end,\n+                  \'end\': self.start}\n+        self.assertRaises(exceptions.BadRequest,\n+                          self.adm_client.get_tenant_usage,\n+                          self.tenant_id, params)\n+\n+    @attr(type=[\'negative\', \'gate\'])\n+    def test_list_usage_all_tenants_with_non_admin_user(self):\n+        # Get usage for all tenants with non admin user\n+        params = {\'start\': self.start,\n+                  \'end\': self.end,\n+                  \'detailed\': int(bool(True))}\n+        self.assertRaises(exceptions.Unauthorized,\n+                          self.client.list_tenant_usages, params)\n+\n+\n+class TenantUsagesTestXML(TenantUsagesTestJSON):\n+    _interface = \'xml\'\ndiff --git a/tempest/services/compute/v3/json/tenant_usages_client.py b/tempest/services/compute/v3/json/tenant_usages_client.py\nnew file mode 100644\nindex 000000000..4dd69645f\n--- /dev/null\n+++ b/tempest/services/compute/v3/json/tenant_usages_client.py\n@@ -0,0 +1,47 @@\n+# vim: tabstop=4 shiftwidth=4 softtabstop=4\n+\n+# Copyright 2013 NEC Corporation\n+# All Rights Reserved.\n+#\n+#    Licensed under the Apache License, Version 2.0 (the "License"); you may\n+#    not use this file except in compliance with the License. You may obtain\n+#    a copy of the License at\n+#\n+#         http://www.apache.org/licenses/LICENSE-2.0\n+#\n+#    Unless required by applicable law or agreed to in writing, software\n+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\n+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n+#    License for the specific language governing permissions and limitations\n+#    under the License.\n+\n+import json\n+import urllib\n+\n+from tempest.common.rest_client import RestClient\n+\n+\n+class TenantUsagesClientJSON(RestClient):\n+\n+    def __init__(self, config, username, password, auth_url, tenant_name=None):\n+        super(TenantUsagesClientJSON, self).__init__(\n+            config, username, password, auth_url, tenant_name)\n+        self.service = self.config.compute.catalog_type\n+\n+    def list_tenant_usages(self, params=None):\n+        url = \'os-simple-tenant-usage\'\n+        if params:\n+            url += \'?%s\' % urllib.urlencode(params)\n+\n+        resp, body = self.get(url)\n+        body = json.loads(body)\n+        return resp, body[\'tenant_usages\'][0]\n+\n+    def get_tenant_usage(self, tenant_id, params=None):\n+        url = \'os-simple-tenant-usage/%s\' % tenant_id\n+        if params:\n+            url += \'?%s\' % urllib.urlencode(params)\n+\n+        resp, body = self.get(url)\n+        body = json.loads(body)\n+        return resp, body[\'tenant_usage\']\ndiff --git a/tempest/services/compute/v3/xml/tenant_usages_client.py b/tempest/services/compute/v3/xml/tenant_usages_client.py\nnew file mode 100644\nindex 000000000..cb923243e\n--- /dev/null\n+++ b/tempest/services/compute/v3/xml/tenant_usages_client.py\n@@ -0,0 +1,54 @@\n+# vim: tabstop=4 shiftwidth=4 softtabstop=4\n+\n+# Copyright 2013 NEC Corporation\n+# All Rights Reserved.\n+#\n+#    Licensed under the Apache License, Version 2.0 (the "License"); you may\n+#    not use this file except in compliance with the License. You may obtain\n+#    a copy of the License at\n+#\n+#         http://www.apache.org/licenses/LICENSE-2.0\n+#\n+#    Unless required by applicable law or agreed to in writing, software\n+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\n+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n+#    License for the specific language governing permissions and limitations\n+#    under the License.\n+\n+import urllib\n+\n+from lxml import etree\n+\n+from tempest.common.rest_client import RestClientXML\n+from tempest.services.compute.xml.common import xml_to_json\n+\n+\n+class TenantUsagesClientXML(RestClientXML):\n+\n+    def __init__(self, config, username, password, auth_url, tenant_name=None):\n+        super(TenantUsagesClientXML, self).__init__(config, username,\n+                                                    password, auth_url,\n+                                                    tenant_name)\n+        self.service = self.config.compute.catalog_type\n+\n+    def _parse_array(self, node):\n+        json = xml_to_json(node)\n+        return json\n+\n+    def list_tenant_usages(self, params=None):\n+        url = \'os-simple-tenant-usage\'\n+        if params:\n+            url += \'?%s\' % urllib.urlencode(params)\n+\n+        resp, body = self.get(url, self.headers)\n+        tenant_usage = self._parse_array(etree.fromstring(body))\n+        return resp, tenant_usage[\'tenant_usage\']\n+\n+    def get_tenant_usage(self, tenant_id, params=None):\n+        url = \'os-simple-tenant-usage/%s\' % tenant_id\n+        if params:\n+            url += \'?%s\' % urllib.urlencode(params)\n+\n+        resp, body = self.get(url, self.headers)\n+        tenant_usage = self._parse_array(etree.fromstring(body))\n+        return resp, tenant_usage\n', 'commit_subject': 'port test_simple_tenant_usage into nova v3 part1', 'repo_name': 'openstack/tempest', 'committer': {'name': 'ivan-zhu', 'email': '1c4f8d8439de830606e278fd77f4950f72beb342@linux.vnet.ibm.com', 'time_sec': '1385516009', 'tz_offset': '480', 'date': {'seconds': '1385516009'}}, 'commit_tree': '68a32a5e510ec47aba788bf2ee0cee81ab0a3eff', 'commit_message': 'port test_simple_tenant_usage into nova v3 part1\n\nThis changeset only copies the v2 files into the appropriate v3\ndirectories unchanged. This is being tried in order to make\nreviewing of the porting easier as gerrit will display only what\nis actually changed for v3 rather than entirely new files.\n\nPartially implements blueprint nova-v3-api-tests\n\nChange-Id: I0ef1d85fe7ce7e9ca836c6e3a5a447df9941eccd\n', 'language_name': 'Python', 'license': 'apache-2.0', 'hunks': '@@ -0,0 +1,54 @@\n+# vim: tabstop=4 shiftwidth=4 softtabstop=4\n+\n+# Copyright 2013 NEC Corporation\n+# All Rights Reserved.\n+#\n+#    Licensed under the Apache License, Version 2.0 (the "License"); you may\n+#    not use this file except in compliance with the License. You may obtain\n+#    a copy of the License at\n+#\n+#         http://www.apache.org/licenses/LICENSE-2.0\n+#\n+#    Unless required by applicable law or agreed to in writing, software\n+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\n+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n+#    License for the specific language governing permissions and limitations\n+#    under the License.\n+\n+import urllib\n+\n+from lxml import etree\n+\n+from tempest.common.rest_client import RestClientXML\n+from tempest.services.compute.xml.common import xml_to_json\n+\n+\n+class TenantUsagesClientXML(RestClientXML):\n+\n+    def __init__(self, config, username, password, auth_url, tenant_name=None):\n+        super(TenantUsagesClientXML, self).__init__(config, username,\n+                                                    password, auth_url,\n+                                                    tenant_name)\n+        self.service = self.config.compute.catalog_type\n+\n+    def _parse_array(self, node):\n+        json = xml_to_json(node)\n+        return json\n+\n+    def list_tenant_usages(self, params=None):\n+        url = \'os-simple-tenant-usage\'\n+        if params:\n+            url += \'?%s\' % urllib.urlencode(params)\n+\n+        resp, body = self.get(url, self.headers)\n+        tenant_usage = self._parse_array(etree.fromstring(body))\n+        return resp, tenant_usage[\'tenant_usage\']\n+\n+    def get_tenant_usage(self, tenant_id, params=None):\n+        url = \'os-simple-tenant-usage/%s\' % tenant_id\n+        if params:\n+            url += \'?%s\' % urllib.urlencode(params)\n+\n+        resp, body = self.get(url, self.headers)\n+        tenant_usage = self._parse_array(etree.fromstring(body))\n+        return resp, tenant_usage\n', 'addition_count': 54, 'deletion_count': 0, 'src_file': '/dev/null', 'tgt_file': 'b/tempest/services/compute/v3/xml/tenant_usages_client.py', 'file_extension': '.py', 'before_file': 'ADDFILE', 'message': 'port test_simple_tenant_usage into nova v3 part1\n\nThis changeset only copies the v2 files into the appropriate v3\ndirectories unchanged. This is being tried in order to make\nreviewing of the porting easier as gerrit will display only what\nis actually changed for v3 rather than entirely new files.\n\nPartially implements blueprint nova-v3-api-tests\n\nChange-Id: I0ef1d85fe7ce7e9ca836c6e3a5a447df9941eccd\n', 'author_name': 'ivan-zhu', 'lang': 'py'}