Skip to content

Commit

Permalink
#825: Allow passing explicit connection to 'Bucket.{list_blobs,make_p…
Browse files Browse the repository at this point in the history
…ublic}'.
  • Loading branch information
tseaver committed May 6, 2015
1 parent f97c62c commit 5ed6c7e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
30 changes: 22 additions & 8 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)
28 changes: 21 additions & 7 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -999,15 +1013,15 @@ 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
with _monkey_defaults(connection=connection):
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')
Expand All @@ -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):
Expand Down

0 comments on commit 5ed6c7e

Please sign in to comment.