Skip to content

Commit

Permalink
Updating storage.ACL methods to accept a client.
Browse files Browse the repository at this point in the history
Also updating all code which calls those methods.

Towards googleapis#952, removing connection from methods / constructors.
  • Loading branch information
dhermes committed Jul 13, 2015
1 parent c309d0c commit adb0bc1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 deletions.
20 changes: 10 additions & 10 deletions gcloud/storage/acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,16 @@ def reload(self, client=None):
for entry in found.get('items', ()):
self.add_entity(self.entity_from_dict(entry))

def save(self, acl=None, connection=None):
def save(self, acl=None, client=None):
"""Save this ACL for the current bucket.
:type acl: :class:`gcloud.storage.acl.ACL`, or a compatible list.
:param acl: The ACL object to save. If left blank, this will save
current entries.
: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.
"""
if acl is None:
acl = self
Expand All @@ -405,7 +405,7 @@ def save(self, acl=None, connection=None):

if save_to_backend:
path = self.save_path
connection = _require_connection(connection)
connection = self._client_or_connection(client)
result = connection.api_request(
method='PATCH',
path=path,
Expand All @@ -416,19 +416,19 @@ def save(self, acl=None, connection=None):
self.add_entity(self.entity_from_dict(entry))
self.loaded = True

def clear(self, connection=None):
def clear(self, client=None):
"""Remove all ACL entries.
Note that this won't actually remove *ALL* the rules, but it
will remove all the non-default rules. In short, you'll still
have access to a bucket that you created even after you clear
ACL rules with this method.
: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.
"""
self.save([], connection)
self.save([], client=client)


class BucketACL(ACL):
Expand Down
11 changes: 5 additions & 6 deletions gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,16 +531,15 @@ def upload_from_string(self, data, content_type='text/plain',
size=len(data), content_type=content_type,
client=client)

def make_public(self, connection=None):
def make_public(self, client=None):
"""Make this blob public giving all users read access.
: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 the ``connection`` stored on the blob's bucket.
"""
self.acl.all().grant_read()
self.acl.save(connection=connection)
self.acl.save(client=client)

cache_control = _scalar_property('cacheControl')
"""HTTP 'Cache-Control' header for this object.
Expand Down
6 changes: 3 additions & 3 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,14 +852,14 @@ def make_public(self, recursive=False, future=False, client=None):
connection = self._client_or_connection(client)

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

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

if recursive:
blobs = list(self.list_blobs(
Expand All @@ -877,4 +877,4 @@ def make_public(self, recursive=False, future=False, client=None):

for blob in blobs:
blob.acl.all().grant_read()
blob.acl.save(connection=connection)
blob.acl.save(client=client)
15 changes: 10 additions & 5 deletions gcloud/storage/test_acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,10 @@ def test_save_none_set_none_passed_w_implicit_connection(self):

def test_save_none_set_none_passed_w_explicit_connection(self):
connection = _Connection()
client = _Client(connection)
acl = self._makeOne()
acl.save_path = '/testing'
acl.save(connection=connection)
acl.save(client=client)
kw = connection._requested
self.assertEqual(len(kw), 0)

Expand All @@ -641,10 +642,11 @@ def test_save_existing_missing_none_passed_w_implicit_connection(self):

def test_save_existing_missing_none_passed_w_explicit_connection(self):
connection = _Connection({})
client = _Client(connection)
acl = self._makeOne()
acl.save_path = '/testing'
acl.loaded = True
acl.save(connection=connection)
acl.save(client=client)
self.assertEqual(list(acl), [])
kw = connection._requested
self.assertEqual(len(kw), 1)
Expand Down Expand Up @@ -676,11 +678,12 @@ def test_save_no_arg_w_explicit_connection(self):
ROLE = 'role'
AFTER = [{'entity': 'allUsers', 'role': ROLE}]
connection = _Connection({'acl': AFTER})
client = _Client(connection)
acl = self._makeOne()
acl.save_path = '/testing'
acl.loaded = True
acl.entity('allUsers').grant(ROLE)
acl.save(connection=connection)
acl.save(client=client)
self.assertEqual(list(acl), AFTER)
kw = connection._requested
self.assertEqual(len(kw), 1)
Expand Down Expand Up @@ -718,10 +721,11 @@ def test_save_w_arg_w_explicit_connection(self):
STICKY = {'entity': 'allUsers', 'role': ROLE2}
new_acl = [{'entity': 'allUsers', 'role': ROLE1}]
connection = _Connection({'acl': [STICKY] + new_acl})
client = _Client(connection)
acl = self._makeOne()
acl.save_path = '/testing'
acl.loaded = True
acl.save(new_acl, connection)
acl.save(new_acl, client=client)
entries = list(acl)
self.assertEqual(len(entries), 2)
self.assertTrue(STICKY in entries)
Expand Down Expand Up @@ -758,11 +762,12 @@ def test_clear_w_explicit_connection(self):
ROLE2 = 'role2'
STICKY = {'entity': 'allUsers', 'role': ROLE2}
connection = _Connection({'acl': [STICKY]})
client = _Client(connection)
acl = self._makeOne()
acl.save_path = '/testing'
acl.loaded = True
acl.entity('allUsers', ROLE1)
acl.clear(connection=connection)
acl.clear(client=client)
self.assertEqual(list(acl), [STICKY])
kw = connection._requested
self.assertEqual(len(kw), 1)
Expand Down
3 changes: 2 additions & 1 deletion gcloud/storage/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,10 +769,11 @@ def test_make_public_w_explicit_connection(self):
permissive = [{'entity': 'allUsers', 'role': _ACLEntity.READER_ROLE}]
after = {'acl': permissive}
connection = _Connection(after)
client = _Client(connection)
bucket = _Bucket()
blob = self._makeOne(BLOB_NAME, bucket=bucket)
blob.acl.loaded = True
blob.make_public(connection=connection)
blob.make_public(client=client)
self.assertEqual(list(blob.acl), permissive)
kw = connection._requested
self.assertEqual(len(kw), 1)
Expand Down
6 changes: 3 additions & 3 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,9 +975,9 @@ def all(self):
def grant_read(self):
self._granted = True

def save(self, connection=None):
def save(self, client=None):
_saved.append(
(self._bucket, self._name, self._granted, connection))
(self._bucket, self._name, self._granted, client))

class _Iterator(_BlobIterator):
def get_items_from_response(self, response):
Expand All @@ -997,7 +997,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, connection)])
self.assertEqual(_saved, [(bucket, BLOB_NAME, True, None)])
kw = connection._requested
self.assertEqual(len(kw), 2)
self.assertEqual(kw[0]['method'], 'PATCH')
Expand Down

0 comments on commit adb0bc1

Please sign in to comment.