Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#825: Allow passing explicit connection to 'Blob.{rename,delete}'. #854

Merged
merged 1 commit into from
May 4, 2015
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
21 changes: 16 additions & 5 deletions gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def exists(self, connection=None):
except NotFound:
return False

def rename(self, new_name):
def rename(self, new_name, connection=None):
"""Renames this blob using copy and delete operations.

Effectively, copies blob to the same bucket with a new name, then
Expand All @@ -246,23 +246,34 @@ def rename(self, new_name):
:type new_name: string
:param new_name: The new name for this blob.

: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:`Blob`
:returns: The newly-copied blob.
"""
new_blob = self.bucket.copy_blob(self, self.bucket, new_name)
self.delete()
new_blob = self.bucket.copy_blob(self, self.bucket, new_name,
connection=connection)
self.delete(connection=connection)
return new_blob

def delete(self):
def delete(self, connection=None):
"""Deletes a blob from Cloud Storage.

: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:`Blob`
:returns: The blob that was just deleted.
:raises: :class:`gcloud.exceptions.NotFound`
(propagated from
:meth:`gcloud.storage.bucket.Bucket.delete_blob`).
"""
return self.bucket.delete_blob(self.name)
return self.bucket.delete_blob(self.name, connection=connection)

def download_to_file(self, file_obj, connection=None):
"""Download the contents of this blob into a file-like object.
Expand Down
43 changes: 38 additions & 5 deletions gcloud/storage/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,46 @@ def test_rename(self):
self.assertEqual(blob.name, BLOB_NAME)
self.assertEqual(new_blob.name, NEW_NAME)
self.assertFalse(BLOB_NAME in bucket._blobs)
self.assertTrue(BLOB_NAME in bucket._deleted)
self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)])
self.assertTrue(NEW_NAME in bucket._blobs)

def test_rename_w_explicit_connection(self):
BLOB_NAME = 'blob-name'
NEW_NAME = 'new-name'
connection = _Connection()
bucket = _Bucket(None)
blob = self._makeOne(BLOB_NAME, bucket=bucket)
bucket._blobs[BLOB_NAME] = 1
new_blob = blob.rename(NEW_NAME, connection=connection)
self.assertEqual(blob.name, BLOB_NAME)
self.assertEqual(new_blob.name, NEW_NAME)
self.assertFalse(BLOB_NAME in bucket._blobs)
self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)])
self.assertTrue(NEW_NAME in bucket._blobs)

def test_delete(self):
from six.moves.http_client import NOT_FOUND
BLOB_NAME = 'blob-name'
not_found_response = {'status': NOT_FOUND}
connection = _Connection(not_found_response)
bucket = _Bucket(None)
bucket = _Bucket(connection)
blob = self._makeOne(BLOB_NAME, bucket=bucket)
bucket._blobs[BLOB_NAME] = 1
blob.delete()
self.assertFalse(blob.exists(connection=connection))
self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)])

def test_delete_w_explicit_connection(self):
from six.moves.http_client import NOT_FOUND
BLOB_NAME = 'blob-name'
not_found_response = {'status': NOT_FOUND}
connection = _Connection(not_found_response)
bucket = _Bucket(None)
blob = self._makeOne(BLOB_NAME, bucket=bucket)
bucket._blobs[BLOB_NAME] = 1
blob.delete(connection=connection)
self.assertFalse(blob.exists(connection=connection))
self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)])

def _download_to_file_helper(self, chunk_size=None):
from six.moves.http_client import OK
Expand Down Expand Up @@ -1111,15 +1138,21 @@ class _Bucket(object):
def __init__(self, connection):
self.connection = connection
self._blobs = {}
self._copied = []
self._deleted = []

def copy_blob(self, blob, destination_bucket, new_name):
def copy_blob(self, blob, destination_bucket, new_name, connection=None):
if connection is None:
connection = self.connection
self._copied.append((blob, destination_bucket, new_name, connection))
destination_bucket._blobs[new_name] = self._blobs[blob.name]
return blob.__class__(new_name, bucket=destination_bucket)

def delete_blob(self, blob_name):
def delete_blob(self, blob_name, connection=None):
if connection is None:
connection = self.connection
del self._blobs[blob_name]
self._deleted.append(blob_name)
self._deleted.append((blob_name, connection))


class _Signer(object):
Expand Down