Adjust Container override methods

Because the object store works differently than what the Resource base
class provides, there are two overrides: create and update. However,
they weren't completed, so while they mostly functioned, they were
returning only the dictionary of headers and not putting that through
the Container initializer.

This change does roughly the same thing as before by sharing code
between the update and create calls, as they only differ in the
actual HTTP call. The base Resource.update method works fine with the
udpate_by_id override now provided, but Resource.create attempts to set
an ID out of the response body, which we do not get on Container.create,
so it overrides and avoids that step.

Three tests were removed from the suite because they were testing the
allow_create flag on instances of Container from the previous chain of
methods. Now that this is all using classmethods like anything else,
these tests don't make sense.

Change-Id: Ia518179517b38cf185bcd070825af15c7875e688
This commit is contained in:
Brian Curtin
2015-02-17 00:12:51 -06:00
committed by Terry Howe
parent 9e44f605d3
commit 84596359d1
2 changed files with 64 additions and 31 deletions

View File

@@ -100,27 +100,78 @@ class Container(resource.Resource):
#: has a copy of the object before any data is sent.
if_none_match = resource.prop("if-none-match")
def _do_create_update(self, session, method):
url = utils.urljoin(self.base_path, self.id)
@classmethod
def _do_create_update(cls, method, session, attrs, resource_id):
"""Helper method to call put and post
The internals of create and update are the same exept for
the session method.
"""
url = utils.urljoin(cls.base_path, resource_id)
# Only send actual headers, not potentially set body values.
headers = self._attrs.copy()
headers = attrs.copy()
for val in ("name", "count", "bytes"):
headers.pop(val, None)
data = method(url, service=self.service, accept=None,
return method(url, service=cls.service, accept=None,
headers=headers).headers
self._reset_dirty()
return data
def create(self, session):
if not self.allow_create:
raise exceptions.MethodNotSupported('create')
@classmethod
def update_by_id(cls, session, resource_id, attrs, path_args=None):
"""Update a Container with the given attributes.
return self._do_create_update(session, session.put)
:param session: The session to use for making this request.
:type session: :class:`~openstack.session.Session`
:param resource_id: This resource's identifier, if needed by
the request. The default is ``None``.
:param dict attrs: The attributes to be sent in the body
of the request.
:param dict path_args: This parameter is sent by the base
class but is ignored for this method.
def update(self, session):
if not self.allow_update:
:return: A ``dict`` representing the response headers.
:raises: :exc:`~openstack.exceptions.MethodNotSupported` if
:data:`Resource.allow_update` is not set to ``True``.
"""
if not cls.allow_update:
raise exceptions.MethodNotSupported('update')
return self._do_create_update(session, session.post)
return cls._do_create_update(session.post, session, attrs,
resource_id)
@classmethod
def create_by_id(cls, session, attrs, resource_id=None):
"""Create a Container from its attributes.
:param session: The session to use for making this request.
:type session: :class:`~openstack.session.Session`
:param dict attrs: The attributes to be sent in the body
of the request.
:param resource_id: This resource's identifier, if needed by
the request. The default is ``None``.
:return: A ``dict`` representing the response headers.
:raises: :exc:`~openstack.exceptions.MethodNotSupported` if
:data:`Resource.allow_create` is not set to ``True``.
"""
if not cls.allow_create:
raise exceptions.MethodNotSupported('create')
return cls._do_create_update(session.put, session, attrs,
resource_id)
def create(self, session):
"""Create a Container from this instance.
:param session: The session to use for making this request.
:type session: :class:`~openstack.session.Session`
:return: This :class:`~openstack.object_store.v1.container.Container`
instance.
:raises: :exc:`~openstack.exceptions.MethodNotSupported` if
:data:`Resource.allow_create` is not set to ``True``.
"""
self.create_by_id(session, self._attrs, self.id)
self._reset_dirty()
return self

View File

@@ -13,7 +13,6 @@
import mock
import testtools
from openstack import exceptions
from openstack.object_store.v1 import container
@@ -179,20 +178,3 @@ class TestContainer(testtools.TestCase):
def test_update(self):
sot = container.Container.new(name=CONTAINER_NAME)
self._test_create_update(sot, sot.update, self.sess.post)
def _test_cant(self, sot, call):
sot.allow_create = False
self.assertRaises(exceptions.MethodNotSupported,
call, self.sess)
def test_cant_create(self):
sot = container.Container.new(name=CONTAINER_NAME)
sot.allow_create = False
self.assertRaises(exceptions.MethodNotSupported,
sot.create, self.sess)
def test_cant_update(self):
sot = container.Container.new(name=CONTAINER_NAME)
sot.allow_update = False
self.assertRaises(exceptions.MethodNotSupported,
sot.update, self.sess)