Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions gcloud/storage/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,32 @@ def __init__(self, name=None):
self._properties = {}
self._changes = set()

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 properties from Cloud Storage.

:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: An explicit connection to use for the API request.
If not passed, use the connection assigned to
the object in its constructor.
: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)
# Pass only '?projection=noAcl' here because 'acl' and related
# are handled via custom endpoints.
query_params = {'projection': 'noAcl'}
Expand Down Expand Up @@ -90,17 +107,16 @@ def _set_properties(self, value):
# If the values are reset, the changes must as well.
self._changes = set()

def patch(self, connection=None):
def patch(self, client=None):
"""Sends all changed properties in a PATCH request.

Updates the ``_properties`` with the response from the backend.

:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: An explicit connection to use for the API request.
If not passed, use the connection assigned to
the object in its constructor.
: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)
# Pass '?projection=full' here because 'PATCH' documented not
# to work properly w/ 'noAcl'.
update_properties = dict((key, self._properties[key])
Expand Down
48 changes: 33 additions & 15 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 All @@ -368,16 +386,16 @@ def reload(self, connection=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 @@ -387,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 @@ -398,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
Loading