From 5ed6c7e3cec6f97821facf6f2c329f624430e95b Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Fri, 1 May 2015 16:52:26 -0400 Subject: [PATCH] #825: Allow passing explicit connection to 'Bucket.{list_blobs,make_public}'. --- gcloud/storage/bucket.py | 30 ++++++++++++++++++++++-------- gcloud/storage/test_bucket.py | 28 +++++++++++++++++++++------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index fe533ba34e55..c8afd69f4a8e 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -266,7 +266,7 @@ def get_blob(self, blob_name, connection=None): def list_blobs(self, max_results=None, page_token=None, prefix=None, delimiter=None, versions=None, - projection='noAcl', fields=None): + projection='noAcl', fields=None, connection=None): """Return an iterator used to find blobs in the bucket. :type max_results: integer or ``NoneType`` @@ -298,6 +298,11 @@ def list_blobs(self, max_results=None, page_token=None, prefix=None, and the language of each blob returned: 'items/contentLanguage,nextPageToken' + :type connection: :class:`gcloud.storage.connection.Connection` or + ``NoneType`` + :param connection: Optional. The connection to use when sending + requests. If not provided, falls back to default. + :rtype: :class:`_BlobIterator`. :returns: An iterator of blobs. """ @@ -320,7 +325,8 @@ def list_blobs(self, max_results=None, page_token=None, prefix=None, if fields is not None: extra_params['fields'] = fields - result = self._iterator_class(self, extra_params=extra_params) + result = self._iterator_class( + self, extra_params=extra_params, connection=connection) # Page token must be handled specially since the base `Iterator` # class has it as a reserved property. if page_token is not None: @@ -853,7 +859,7 @@ def disable_website(self): """ return self.configure_website(None, None) - def make_public(self, recursive=False, future=False): + def make_public(self, recursive=False, future=False, connection=None): """Make a bucket public. :type recursive: boolean @@ -863,18 +869,26 @@ def make_public(self, recursive=False, future=False): :type future: boolean :param future: If True, this will make all objects created in the future public as well. + + :type connection: :class:`gcloud.storage.connection.Connection` or + ``NoneType`` + :param connection: Optional. The connection to use when sending + requests. If not provided, falls back to default. """ + connection = _require_connection(connection) + self.acl.all().grant_read() - self.acl.save() + self.acl.save(connection=connection) if future: doa = self.default_object_acl if not doa.loaded: - doa.reload() + doa.reload(connection=connection) doa.all().grant_read() - doa.save() + doa.save(connection=connection) if recursive: - for blob in self: + for blob in self.list_blobs(projection='full', + connection=connection): blob.acl.all().grant_read() - blob.save_acl() + blob.acl.save(connection=connection) diff --git a/gcloud/storage/test_bucket.py b/gcloud/storage/test_bucket.py index 6a74fbccd8f2..ea46d9d157a0 100644 --- a/gcloud/storage/test_bucket.py +++ b/gcloud/storage/test_bucket.py @@ -337,6 +337,18 @@ def test_list_blobs_explicit(self): self.assertEqual(kw['path'], '/b/%s/o' % NAME) self.assertEqual(kw['query_params'], EXPECTED) + def test_list_blobs_w_explicit_connection(self): + NAME = 'name' + connection = _Connection({'items': []}) + bucket = self._makeOne(NAME, None) + iterator = bucket.list_blobs(connection=connection) + blobs = list(iterator) + self.assertEqual(blobs, []) + kw, = connection._requested + self.assertEqual(kw['method'], 'GET') + self.assertEqual(kw['path'], '/b/%s/o' % NAME) + self.assertEqual(kw['query_params'], {'projection': 'noAcl'}) + def test_delete_default_miss(self): from gcloud.exceptions import NotFound NAME = 'name' @@ -906,7 +918,7 @@ def test_make_public_defaults(self): permissive = [{'entity': 'allUsers', 'role': _ACLEntity.READER_ROLE}] after = {'acl': permissive, 'defaultObjectAcl': []} connection = _Connection(after) - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME, None) bucket.acl.loaded = True bucket.default_object_acl.loaded = True with _monkey_defaults(connection=connection): @@ -935,7 +947,7 @@ def _make_public_w_future_helper(self, default_object_acl_loaded=True): # We return the same value for default_object_acl.reload() # to consume. connection = _Connection(after1, after1, after2) - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME, None) bucket.acl.loaded = True bucket.default_object_acl.loaded = default_object_acl_loaded with _monkey_defaults(connection=connection): @@ -980,14 +992,16 @@ def __init__(self, bucket, name): def acl(self): return self + # Faux ACL methods def all(self): return self def grant_read(self): self._granted = True - def save_acl(self): - _saved.append((self._bucket, self._name, self._granted)) + def save(self, connection=None): + _saved.append( + (self._bucket, self._name, self._granted, connection)) class _Iterator(_BlobIterator): def get_items_from_response(self, response): @@ -999,7 +1013,7 @@ def get_items_from_response(self, response): permissive = [{'entity': 'allUsers', 'role': _ACLEntity.READER_ROLE}] after = {'acl': permissive, 'defaultObjectAcl': []} connection = _Connection(after, {'items': [{'name': BLOB_NAME}]}) - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME, None) bucket.acl.loaded = True bucket.default_object_acl.loaded = True bucket._iterator_class = _Iterator @@ -1007,7 +1021,7 @@ def get_items_from_response(self, response): bucket.make_public(recursive=True) self.assertEqual(list(bucket.acl), permissive) self.assertEqual(list(bucket.default_object_acl), []) - self.assertEqual(_saved, [(bucket, BLOB_NAME, True)]) + self.assertEqual(_saved, [(bucket, BLOB_NAME, True, connection)]) kw = connection._requested self.assertEqual(len(kw), 2) self.assertEqual(kw[0]['method'], 'PATCH') @@ -1016,7 +1030,7 @@ def get_items_from_response(self, response): self.assertEqual(kw[0]['query_params'], {'projection': 'full'}) self.assertEqual(kw[1]['method'], 'GET') self.assertEqual(kw[1]['path'], '/b/%s/o' % NAME) - self.assertEqual(kw[1]['query_params'], {'projection': 'noAcl'}) + self.assertEqual(kw[1]['query_params'], {'projection': 'full'}) class _Connection(object):