Expose 'requires_id' to get_xxx proxy functions

This patch proposes to relax the constraints on the get() call
interface: we should permit a resource get operation without providing
an ID.

Change-Id: Ic45b04736a7e35f9f57d2f8f0b08675d0c02da8c
This commit is contained in:
tengqm 2016-06-19 23:21:08 -04:00
parent 8a569811d0
commit d7b393d449
4 changed files with 21 additions and 12 deletions

View File

@ -193,7 +193,7 @@ class BaseProxy(object):
return res.create(self.session)
@_check_resource(strict=False)
def _get(self, resource_type, value=None, **attrs):
def _get(self, resource_type, value=None, requires_id=True, **attrs):
"""Get a resource
:param resource_type: The type of resource to get.
@ -214,7 +214,7 @@ class BaseProxy(object):
res = self._get_resource(resource_type, value, **attrs)
try:
return res.get(self.session)
return res.get(self.session, requires_id=requires_id)
except exceptions.NotFoundException as e:
raise exceptions.ResourceNotFound(
message="No %s found for %s" %

View File

@ -509,12 +509,13 @@ class Resource(object):
self._translate_response(response)
return self
def get(self, session):
def get(self, session, requires_id=True):
"""Get a remote resource based on this instance.
:param session: The session to use for making this request.
:type session: :class:`~openstack.session.Session`
:param boolean requires_id: A boolean indicating whether resource ID
should be part of the requested URI.
:return: This :class:`Resource` instance.
:raises: :exc:`~openstack.exceptions.MethodNotSupported` if
:data:`Resource.allow_get` is not set to ``True``.
@ -522,8 +523,7 @@ class Resource(object):
if not self.allow_get:
raise exceptions.MethodNotSupported(self, "get")
request = self._prepare_request()
request = self._prepare_request(requires_id=requires_id)
response = session.get(request.uri, endpoint_filter=self.service)
self._translate_response(response)

View File

@ -290,7 +290,7 @@ class TestProxyGet(testtools.TestCase):
def test_get_resource(self):
rv = self.sot._get(RetrieveableResource, self.res)
self.res.get.assert_called_with(self.session)
self.res.get.assert_called_with(self.session, requires_id=True)
self.assertEqual(rv, self.fake_result)
def test_get_resource_with_args(self):
@ -298,14 +298,14 @@ class TestProxyGet(testtools.TestCase):
rv = self.sot._get(RetrieveableResource, self.res, **args)
self.res._update.assert_called_once_with(**args)
self.res.get.assert_called_with(self.session)
self.res.get.assert_called_with(self.session, requires_id=True)
self.assertEqual(rv, self.fake_result)
def test_get_id(self):
rv = self.sot._get(RetrieveableResource, self.fake_id)
RetrieveableResource.new.assert_called_with(id=self.fake_id)
self.res.get.assert_called_with(self.session)
self.res.get.assert_called_with(self.session, requires_id=True)
self.assertEqual(rv, self.fake_result)
def test_get_not_found(self):

View File

@ -831,10 +831,19 @@ class TestResourceActions(base.TestCase):
def test_get(self):
result = self.sot.get(self.session)
self.sot._prepare_request.assert_called_once_with()
self.sot._prepare_request.assert_called_once_with(requires_id=True)
self.session.get.assert_called_once_with(
self.request.uri,
endpoint_filter=self.service_name)
self.request.uri, endpoint_filter=self.service_name)
self.sot._translate_response.assert_called_once_with(self.response)
self.assertEqual(result, self.sot)
def test_get_not_requires_id(self):
result = self.sot.get(self.session, False)
self.sot._prepare_request.assert_called_once_with(requires_id=False)
self.session.get.assert_called_once_with(
self.request.uri, endpoint_filter=self.service_name)
self.sot._translate_response.assert_called_once_with(self.response)
self.assertEqual(result, self.sot)