Merge "Add keystone unified limits client"
diff --git a/releasenotes/notes/limits-client-d656f16a3d3e84fc.yaml b/releasenotes/notes/limits-client-d656f16a3d3e84fc.yaml
new file mode 100644
index 0000000..311eca3
--- /dev/null
+++ b/releasenotes/notes/limits-client-d656f16a3d3e84fc.yaml
@@ -0,0 +1,4 @@
+---
+features:
+  - |
+    Add a new client for keystone's unified limits API to create and update limits.
diff --git a/tempest/clients.py b/tempest/clients.py
index ebf2540..1b05b54 100644
--- a/tempest/clients.py
+++ b/tempest/clients.py
@@ -222,6 +222,8 @@
             self.identity_v3.ApplicationCredentialsClient(**params_v3)
         self.access_rules_client = \
             self.identity_v3.AccessRulesClient(**params_v3)
+        self.identity_limits_client = \
+            self.identity_v3.LimitsClient(**params_v3)
 
         # Token clients do not use the catalog. They only need default_params.
         # They read auth_url, so they should only be set if the corresponding
diff --git a/tempest/lib/services/identity/v3/__init__.py b/tempest/lib/services/identity/v3/__init__.py
index 86fa991..af09fb1 100644
--- a/tempest/lib/services/identity/v3/__init__.py
+++ b/tempest/lib/services/identity/v3/__init__.py
@@ -32,6 +32,7 @@
 from tempest.lib.services.identity.v3.identity_client import IdentityClient
 from tempest.lib.services.identity.v3.inherited_roles_client import \
     InheritedRolesClient
+from tempest.lib.services.identity.v3.limits_client import LimitsClient
 from tempest.lib.services.identity.v3.oauth_consumers_client import \
     OAUTHConsumerClient
 from tempest.lib.services.identity.v3.oauth_token_client import \
@@ -55,7 +56,8 @@
            'DomainConfigurationClient', 'EndPointGroupsClient',
            'EndPointsClient', 'EndPointsFilterClient',
            'GroupsClient', 'IdentityClient', 'InheritedRolesClient',
-           'OAUTHConsumerClient', 'OAUTHTokenClient', 'PoliciesClient',
-           'ProjectsClient', 'ProjectTagsClient', 'RegionsClient',
-           'RoleAssignmentsClient', 'RolesClient', 'ServicesClient',
-           'V3TokenClient', 'TrustsClient', 'UsersClient', 'VersionsClient']
+           'LimitsClient', 'OAUTHConsumerClient', 'OAUTHTokenClient',
+           'PoliciesClient', 'ProjectsClient', 'ProjectTagsClient',
+           'RegionsClient', 'RoleAssignmentsClient', 'RolesClient',
+           'ServicesClient', 'V3TokenClient', 'TrustsClient', 'UsersClient',
+           'VersionsClient']
diff --git a/tempest/lib/services/identity/v3/limits_client.py b/tempest/lib/services/identity/v3/limits_client.py
new file mode 100644
index 0000000..26d04bc
--- /dev/null
+++ b/tempest/lib/services/identity/v3/limits_client.py
@@ -0,0 +1,57 @@
+# Copyright 2021 Red Hat, Inc.
+#
+# 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.
+
+from oslo_serialization import jsonutils as json
+
+from tempest.lib.common import rest_client
+
+
+class LimitsClient(rest_client.RestClient):
+    api_version = "v3"
+
+    def get_registered_limits(self):
+        """Lists all registered limits."""
+        resp, body = self.get('registered_limits')
+        self.expected_success(200, resp.status)
+        return rest_client.ResponseBody(resp, json.loads(body))
+
+    def create_limit(self, region_id, service_id, project_id, resource_name,
+                     default_limit, description=None, domain_id=None):
+        """Creates a limit in keystone."""
+        limit = {
+            'service_id': service_id,
+            'project_id': project_id,
+            'resource_name': resource_name,
+            'resource_limit': default_limit,
+            'region_id': region_id,
+            'description': description or '%s limit for %s' % (
+                resource_name, project_id),
+        }
+        if domain_id:
+            limit['domain_id'] = domain_id
+        post_body = json.dumps({'limits': [limit]})
+        resp, body = self.post('limits', post_body)
+        self.expected_success(201, resp.status)
+        return rest_client.ResponseBody(resp, json.loads(body))
+
+    def update_limit(self, limit_id, resource_limit, description=None):
+        """Updates a limit in keystone by id."""
+
+        limit = {'resource_limit': resource_limit}
+        if description:
+            limit['description'] = description
+        patch_body = json.dumps({'limit': limit})
+        resp, body = self.patch('limits/%s' % limit_id, patch_body)
+        self.expected_success(200, resp.status)
+        return rest_client.ResponseBody(resp, json.loads(body))
diff --git a/tempest/tests/lib/services/identity/v3/test_limit_client.py b/tempest/tests/lib/services/identity/v3/test_limit_client.py
new file mode 100644
index 0000000..07ec6cd
--- /dev/null
+++ b/tempest/tests/lib/services/identity/v3/test_limit_client.py
@@ -0,0 +1,82 @@
+# Copyright 2021 Red Hat, Inc.
+#
+# 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.
+
+from tempest.lib.services.identity.v3 import limits_client
+from tempest.tests.lib import fake_auth_provider
+from tempest.tests.lib.services import base
+
+
+class TestLimitsClient(base.BaseServiceTest):
+    def setUp(self):
+        super(TestLimitsClient, self).setUp()
+        self.client = limits_client.LimitsClient(
+            fake_auth_provider.FakeAuthProvider(),
+            'identity', 'regionOne')
+
+    def test_get_registered_limits(self):
+        fake_result = {'foo': 'bar'}
+        self.check_service_client_function(
+            self.client.get_registered_limits,
+            'tempest.lib.common.rest_client.RestClient.get',
+            fake_result,
+            False,
+            status=200)
+
+    def test_create_limit(self):
+        fake_result = {'foo': 'bar'}
+        self.check_service_client_function(
+            self.client.create_limit,
+            'tempest.lib.common.rest_client.RestClient.post',
+            fake_result,
+            False,
+            region_id='regionOne', service_id='image',
+            project_id='project', resource_name='widgets',
+            default_limit=10,
+            description='Spacely Widgets',
+            status=201)
+
+    def test_create_limit_with_domain(self):
+        fake_result = {'foo': 'bar'}
+        self.check_service_client_function(
+            self.client.create_limit,
+            'tempest.lib.common.rest_client.RestClient.post',
+            fake_result,
+            False,
+            region_id='regionOne', service_id='image',
+            project_id='project', resource_name='widgets',
+            default_limit=10,
+            domain_id='foo',
+            description='Spacely Widgets',
+            status=201)
+
+    def test_update_limit(self):
+        fake_result = {'foo': 'bar'}
+        self.check_service_client_function(
+            self.client.update_limit,
+            'tempest.lib.common.rest_client.RestClient.patch',
+            fake_result,
+            False,
+            limit_id='123', resource_limit=20,
+            status=200)
+
+    def test_update_limit_with_description(self):
+        fake_result = {'foo': 'bar'}
+        self.check_service_client_function(
+            self.client.update_limit,
+            'tempest.lib.common.rest_client.RestClient.patch',
+            fake_result,
+            False,
+            limit_id='123', resource_limit=20,
+            description='new description',
+            status=200)