Get container off of an Object if its passed

The previous object operations on object_store proxy were too strict on
requiring a container argument to be passed, and in some cases they were
too loose.

When an Object is passed, look to see if it has a container attribute
already set and use it if so. If that doesn't work out, look at the
container argument, which that that point would have to be specified. If
we get a container out of that, use it. After this, we won't be able to
do any object operations with a container, so raise ValueError
specifying that a container has to be provided.

Change-Id: I6b587831321998245815fccf8f822025e83141c5
Closes-Bug: 1487147
This commit is contained in:
Brian Curtin 2015-08-20 12:52:37 -05:00
parent 250a4d23d5
commit 43f5a00ce1
1 changed files with 18 additions and 10 deletions

View File

@ -131,6 +131,16 @@ class Proxy(proxy.BaseProxy):
ob.container = container.name
yield ob
def _get_container_name(self, value, container):
if isinstance(value, _obj.Object):
if value.container is not None:
return value.container
if container is not None:
container = _container.Container.from_id(container)
return container.name
raise ValueError("container must be specified")
def get_object(self, value, container=None):
"""Get the data associated with an object
@ -146,11 +156,11 @@ class Proxy(proxy.BaseProxy):
:raises: :class:`~openstack.exceptions.ResourceNotFound`
when no resource can be found.
"""
container = _container.Container.from_id(container)
container_name = self._get_container_name(value, container)
# TODO(brian): s/_obj/obj once other changes propogate
return self._get(_obj.Object, value,
path_args={"container": container.name})
path_args={"container": container_name})
def save_object(self, obj, path):
"""Save the data contained inside an object to disk.
@ -177,13 +187,11 @@ class Proxy(proxy.BaseProxy):
:rtype: :class:`~openstack.object_store.v1.container.Container`
"""
container = attrs.pop("container", None)
if container is None:
raise ValueError("container must be specified")
container = _container.Container.from_id(container)
container_name = self._get_container_name(None, container)
# TODO(brian): s/_container/container once other changes propogate
return self._create(_obj.Object,
path_args={"container": container.name}, **attrs)
path_args={"container": container_name}, **attrs)
def copy_object(self):
"""Copy an object."""
@ -206,11 +214,11 @@ class Proxy(proxy.BaseProxy):
:returns: ``None``
"""
container = _container.Container.from_id(container)
container_name = self._get_container_name(value, container)
# TODO(brian): s/_obj/obj once other changes propogate
self._delete(_obj.Object, value, ignore_missing=ignore_missing,
path_args={"container": container.name})
path_args={"container": container_name})
def get_object_metadata(self, value, container=None):
"""Get metatdata for an object
@ -226,10 +234,10 @@ class Proxy(proxy.BaseProxy):
:raises: :class:`~openstack.exceptions.ResourceNotFound`
when no resource can be found.
"""
container = _container.Container.from_id(container)
container_name = self._get_container_name(value, container)
return self._head(_obj.Object, value,
path_args={"container": container.name})
path_args={"container": container_name})
def set_object_metadata(self, obj):
"""Set metatdata for an object.