Add limit CRUD support

This patch is the limit part for unified limit CRUD support.

Change-Id: I1b486b5a3dd641e932b892b8bdda03997719d893
This commit is contained in:
wangxiyuan 2018-10-11 15:24:42 +08:00
parent b36487a1c3
commit 17786215ed
4 changed files with 190 additions and 0 deletions

View File

@ -15,6 +15,7 @@ from openstack.identity.v3 import credential as _credential
from openstack.identity.v3 import domain as _domain
from openstack.identity.v3 import endpoint as _endpoint
from openstack.identity.v3 import group as _group
from openstack.identity.v3 import limit as _limit
from openstack.identity.v3 import policy as _policy
from openstack.identity.v3 import project as _project
from openstack.identity.v3 import region as _region
@ -1047,3 +1048,69 @@ class Proxy(proxy.Proxy):
"""
self._delete(_registered_limit.RegisteredLimit, registered_limit,
ignore_missing=ignore_missing)
def limits(self, **query):
"""Retrieve a generator of limits
:param kwargs \*\*query: Optional query parameters to be sent to limit
the limits being returned.
:returns: A generator of limits instances.
:rtype: :class:`~openstack.identity.v3.limit.Limit`
"""
return self._list(_limit.Limit, paginated=False,
**query)
def get_limit(self, limit):
"""Get a single limit
:param limit: The value can be the ID of a limit
or a :class:`~openstack.identity.v3.limit.Limit` instance.
:returns: One :class:
`~openstack.identity.v3.limit.Limit`
:raises: :class:`~openstack.exceptions.ResourceNotFound` when no
resource can be found.
"""
return self._get(_limit.Limit, limit)
def create_limit(self, **attrs):
"""Create a new limit from attributes
:param dict attrs: Keyword arguments which will be used to create
a :class:`~openstack.identity.v3.limit.Limit`, comprised of the
properties on the Limit class.
:returns: The results of limit creation.
:rtype: :class:`~openstack.identity.v3.limit.Limit`
"""
return self._create(_limit.Limit, **attrs)
def update_limit(self, limit, **attrs):
"""Update a limit
:param limit: Either the ID of a limit. or a
:class:`~openstack.identity.v3.limit.Limit` instance.
:param dict kwargs: The attributes to update on the limit represented
by ``value``.
:returns: The updated limit.
:rtype: :class:`~openstack.identity.v3.limit.Limit`
"""
return self._update(_limit.Limit,
limit, **attrs)
def delete_limit(self, limit, ignore_missing=True):
"""Delete a limit
:param limit: The value can be either the ID of a limit or a
:class:`~openstack.identity.v3.limit.Limit` instance.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be raised when
the limit does not exist. When set to ``True``, no exception will
be thrown when attempting to delete a nonexistent limit.
:returns: ``None``
"""
self._delete(limit.Limit, limit,
ignore_missing=ignore_missing)

View File

@ -0,0 +1,58 @@
# 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 openstack import resource
class Limit(resource.Resource):
resource_key = 'limit'
resources_key = 'limits'
base_path = '/limits'
# capabilities
allow_create = True
allow_fetch = True
allow_commit = True
allow_delete = True
allow_list = True
commit_method = 'PATCH'
commit_jsonpatch = True
_query_mapping = resource.QueryParameters(
'service_id', 'region_id', 'resource_name', 'project_id')
# Properties
#: User-facing description of the registered_limit. *Type: string*
description = resource.Body('description')
#: The links for the registered_limit resource.
links = resource.Body('links')
#: ID of service. *Type: string*
service_id = resource.Body('service_id')
#: ID of region, if any. *Type: string*
region_id = resource.Body('region_id')
#: The resource name. *Type: string*
resource_name = resource.Body('resource_name')
#: The resource limit value. *Type: int*
resource_limit = resource.Body('resource_limit')
#: ID of project. *Type: string*
project_id = resource.Body('project_id')
def _prepare_request_body(self, patch, prepend_key):
body = self._body.dirty
if prepend_key and self.resource_key is not None:
if patch:
body = {self.resource_key: body}
else:
# Keystone support bunch create for unified limit. So the
# request body for creating limit is a list instead of dict.
body = {self.resources_key: [body]}
return body

View File

@ -0,0 +1,60 @@
# 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 openstack.tests.unit import base
from openstack.identity.v3 import limit
EXAMPLE = {
"service_id": "8ac43bb0926245cead88676a96c750d3",
"region_id": 'RegionOne',
"resource_name": 'cores',
"resource_limit": 10,
"project_id": 'a8455cdd4249498f99b63d5af2fb4bc8',
"description": "compute cores for project 123",
"links": {"self": "http://example.com/v3/limit_1"}
}
class TestLimit(base.TestCase):
def test_basic(self):
sot = limit.Limit()
self.assertEqual('limit', sot.resource_key)
self.assertEqual('limits', sot.resources_key)
self.assertEqual('/limits', sot.base_path)
self.assertTrue(sot.allow_create)
self.assertTrue(sot.allow_fetch)
self.assertTrue(sot.allow_commit)
self.assertTrue(sot.allow_delete)
self.assertTrue(sot.allow_list)
self.assertEqual('PATCH', sot.commit_method)
self.assertDictEqual(
{
'service_id': 'service_id',
'region_id': 'region_id',
'resource_name': 'resource_name',
'project_id': 'project_id',
'marker': 'marker',
'limit': 'limit'
},
sot._query_mapping._mapping)
def test_make_it(self):
sot = limit.Limit(**EXAMPLE)
self.assertEqual(EXAMPLE['service_id'], sot.service_id)
self.assertEqual(EXAMPLE['region_id'], sot.region_id)
self.assertEqual(EXAMPLE['resource_name'], sot.resource_name)
self.assertEqual(EXAMPLE['resource_limit'], sot.resource_limit)
self.assertEqual(EXAMPLE['project_id'], sot.project_id)
self.assertEqual(EXAMPLE['description'], sot.description)
self.assertEqual(EXAMPLE['links'], sot.links)

View File

@ -0,0 +1,5 @@
---
features:
- |
Added the unified limits basic CRUD methods. It includes two kinds of
resources: `registered_limit` and `limit`.