Merge "add the ability to get an object back from swift"

This commit is contained in:
Jenkins 2016-01-22 15:26:29 +00:00 committed by Gerrit Code Review
commit 53290944fb
5 changed files with 62 additions and 1 deletions

View File

@ -472,6 +472,11 @@ class ObjectMetadata(task_manager.Task):
return client.swift_client.head_object(**self.args)
class ObjectGet(task_manager.Task):
def main(self, client):
return client.swift_client.get_object(**self.args)
class SubnetCreate(task_manager.Task):
def main(self, client):
return client.neutron_client.create_subnet(**self.args)

View File

@ -3751,6 +3751,31 @@ class OpenStackCloud(object):
"Object metadata fetch failed: %s (%s/%s)" % (
e.http_reason, e.http_host, e.http_path))
def get_object(self, container, obj, query_string=None,
resp_chunk_size=None):
"""Get the headers and body of an object from swift
:param string container: name of the container.
:param string obj: name of the object.
:param string query_string: query args for uri.
(delimiter, prefix, etc.)
:param int resp_chunk_size: chunk size of data to read.
:returns: Tuple (headers, body) of the object, or None if the object
is not found (404)
:raises: OpenStackCloudException on operation error.
"""
try:
return self.manager.submitTask(_tasks.ObjectGet(
container=container, obj=obj, query_string=query_string,
resp_chunk_size=resp_chunk_size))
except swift_exceptions.ClientException as e:
if e.http_status == 404:
return None
raise OpenStackCloudException(
"Object fetch failed: %s (%s/%s)" % (
e.http_reason, e.http_host, e.http_path))
def create_subnet(self, network_name_or_id, cidr, ip_version=4,
enable_dhcp=False, subnet_name=None, tenant_id=None,
allocation_pools=None,

View File

@ -82,7 +82,7 @@ class Task(object):
if type(self._result) == list:
return meta.obj_list_to_dict(self._result)
elif type(self._result) not in (bool, int, float, str, set,
types.GeneratorType):
tuple, types.GeneratorType):
return meta.obj_to_dict(self._result)
else:
return self._result

View File

@ -64,6 +64,8 @@ class TestObject(base.TestCase):
)
self.assertIsNotNone(
self.cloud.get_object_metadata(container_name, name))
self.assertIsNotNone(
self.cloud.get_object(container_name, name))
self.assertEqual(
name,
self.cloud.list_objects(container_name)[0]['name'])

View File

@ -322,3 +322,32 @@ class TestObject(base.TestCase):
self.assertRaises(shade.OpenStackCloudException,
self.cloud.delete_object,
container_name, object_name)
@mock.patch.object(shade.OpenStackCloud, 'swift_client')
def test_get_object(self, mock_swift):
fake_resp = ({'headers': 'yup'}, 'test body')
mock_swift.get_object.return_value = fake_resp
container_name = 'container_name'
object_name = 'object_name'
resp = self.cloud.get_object(container_name, object_name)
self.assertEqual(fake_resp, resp)
@mock.patch.object(shade.OpenStackCloud, 'swift_client')
def test_get_object_not_found(self, mock_swift):
mock_swift.get_object.side_effect = swift_exc.ClientException(
'ERROR', http_status=404)
container_name = 'container_name'
object_name = 'object_name'
self.assertIsNone(self.cloud.get_object(container_name, object_name))
mock_swift.get_object.assert_called_once_with(
container=container_name, obj=object_name,
query_string=None, resp_chunk_size=None)
@mock.patch.object(shade.OpenStackCloud, 'swift_client')
def test_get_object_exception(self, mock_swift):
mock_swift.get_object.side_effect = swift_exc.ClientException("ERROR")
container_name = 'container_name'
object_name = 'object_name'
self.assertRaises(shade.OpenStackCloudException,
self.cloud.get_object,
container_name, object_name)