Skip to content

Commit

Permalink
Updating Bucket.make_public and ACL.reload accept a client.
Browse files Browse the repository at this point in the history
Towards googleapis#952, removing connection from methods / constructors.
  • Loading branch information
dhermes committed Jul 11, 2015
1 parent 96c2b78 commit eb2cd45
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
28 changes: 23 additions & 5 deletions gcloud/storage/acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,33 @@ def get_entities(self):
self._ensure_loaded()
return list(self.entities.values())

def reload(self, connection=None):
@staticmethod
def _client_or_connection(client):
"""Temporary method to get a connection from a client.
If the client is null, gets the connection from the environment.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
:rtype: :class:`gcloud.storage.connection.Connection`
:returns: The connection determined from the ``client`` or environment.
"""
if client is None:
return _require_connection()
else:
return client.connection

def reload(self, client=None):
"""Reload the ACL data from Cloud Storage.
:type connection: :class:`gcloud.storage.connection.Connection` or None
:param connection: explicit connection to use for API request;
defaults to instance property.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
"""
path = self.reload_path
connection = _require_connection(connection)
connection = self._client_or_connection(client)

self.entities.clear()

Expand Down
13 changes: 6 additions & 7 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ def disable_website(self):
"""
return self.configure_website(None, None)

def make_public(self, recursive=False, future=False, connection=None):
def make_public(self, recursive=False, future=False, client=None):
"""Make a bucket public.
If ``recursive=True`` and the bucket contains more than 256
Expand All @@ -845,20 +845,19 @@ def make_public(self, recursive=False, future=False, connection=None):
: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.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
"""
connection = _require_connection(connection)
connection = self._client_or_connection(client)

self.acl.all().grant_read()
self.acl.save(connection=connection)

if future:
doa = self.default_object_acl
if not doa.loaded:
doa.reload(connection=connection)
doa.reload(client=client)
doa.all().grant_read()
doa.save(connection=connection)

Expand Down
15 changes: 12 additions & 3 deletions gcloud/storage/test_acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,12 @@ def test_reload_missing_w_explicit_connection(self):
# https://github.com/GoogleCloudPlatform/gcloud-python/issues/652
ROLE = 'role'
connection = _Connection({})
client = _Client(connection)
acl = self._makeOne()
acl.reload_path = '/testing/acl'
acl.loaded = True
acl.entity('allUsers', ROLE)
acl.reload(connection=connection)
acl.reload(client=client)
self.assertEqual(list(acl), [])
kw = connection._requested
self.assertEqual(len(kw), 1)
Expand All @@ -557,11 +558,12 @@ def test_reload_empty_result_clears_local_w_implicit_connection(self):
def test_reload_empty_result_clears_local_w_explicit_connection(self):
ROLE = 'role'
connection = _Connection({'items': []})
client = _Client(connection)
acl = self._makeOne()
acl.reload_path = '/testing/acl'
acl.loaded = True
acl.entity('allUsers', ROLE)
acl.reload(connection=connection)
acl.reload(client=client)
self.assertTrue(acl.loaded)
self.assertEqual(list(acl), [])
kw = connection._requested
Expand Down Expand Up @@ -590,10 +592,11 @@ def test_reload_nonempty_result_w_explicit_connection(self):
ROLE = 'role'
connection = _Connection(
{'items': [{'entity': 'allUsers', 'role': ROLE}]})
client = _Client(connection)
acl = self._makeOne()
acl.reload_path = '/testing/acl'
acl.loaded = True
acl.reload(connection=connection)
acl.reload(client=client)
self.assertTrue(acl.loaded)
self.assertEqual(list(acl), [{'entity': 'allUsers', 'role': ROLE}])
kw = connection._requested
Expand Down Expand Up @@ -870,3 +873,9 @@ def api_request(self, **kw):
raise NotFound('miss')
else:
return response


class _Client(object):

def __init__(self, connection):
self.connection = connection
3 changes: 2 additions & 1 deletion gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,14 +1026,15 @@ def test_make_public_recursive_too_many(self):
],
}
connection = _Connection(AFTER, GET_BLOBS_RESP)
client = _Client(connection)
bucket = self._makeOne(NAME)
bucket.acl.loaded = True
bucket.default_object_acl.loaded = True

# Make the Bucket refuse to make_public with 2 objects.
bucket._MAX_OBJECTS_FOR_ITERATION = 1
self.assertRaises(ValueError, bucket.make_public, recursive=True,
connection=connection)
client=client)


class _Connection(object):
Expand Down

0 comments on commit eb2cd45

Please sign in to comment.