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

Ditch Connection.new_bucket method in storage #586

Merged
merged 2 commits into from
Feb 3, 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
14 changes: 13 additions & 1 deletion gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,25 @@ def connection(self):
"""
return self._connection

@staticmethod
def path_helper(bucket_name):
"""Relative URL path for a bucket.
:type bucket_name: string
:param bucket_name: The bucket name in the path.
:rtype: string
:returns: The relative URL path for ``bucket_name``.
"""
return '/b/' + bucket_name

@property
def path(self):
"""The URL path to this bucket."""
if not self.name:
raise ValueError('Cannot determine path without bucket name.')

return '/b/' + self.name
return self.path_helper(self.name)

def get_blob(self, blob):
"""Get a blob object by name.
Expand Down
55 changes: 11 additions & 44 deletions gcloud/storage/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from gcloud.exceptions import NotFound
from gcloud.storage.bucket import Bucket
from gcloud.storage.iterator import Iterator
import six


class Connection(_Base):
Expand Down Expand Up @@ -296,7 +295,7 @@ def get_bucket(self, bucket_name):
:returns: The bucket matching the name provided.
:raises: :class:`gcloud.exceptions.NotFound`
"""
bucket = self.new_bucket(bucket_name)
bucket = Bucket(connection=self, name=bucket_name)
response = self.api_request(method='GET', path=bucket.path)
return Bucket(properties=response, connection=self)

Expand Down Expand Up @@ -326,7 +325,7 @@ def lookup(self, bucket_name):
except NotFound:
return None

def create_bucket(self, bucket):
def create_bucket(self, bucket_name):
"""Create a new bucket.

For example::
Expand All @@ -337,34 +336,27 @@ def create_bucket(self, bucket):
>>> print bucket
<Bucket: my-bucket>

:type bucket: string or :class:`gcloud.storage.bucket.Bucket`
:param bucket: The bucket name (or bucket object) to create.
:type bucket_name: string
:param bucket_name: The bucket name to create.

:rtype: :class:`gcloud.storage.bucket.Bucket`
:returns: The newly created bucket.
:raises: :class:`gcloud.exceptions.Conflict` if
there is a confict (bucket already exists, invalid name, etc.)
"""
bucket = self.new_bucket(bucket)
response = self.api_request(method='POST', path='/b',
data={'name': bucket.name})
data={'name': bucket_name})
return Bucket(properties=response, connection=self)

def delete_bucket(self, bucket):
def delete_bucket(self, bucket_name):
"""Delete a bucket.

You can use this method to delete a bucket by name, or to delete
a bucket object::
You can use this method to delete a bucket by name.

>>> from gcloud import storage
>>> connection = storage.get_connection(project)
>>> connection.delete_bucket('my-bucket')

You can also delete pass in the bucket object::

>>> bucket = connection.get_bucket('other-bucket')
>>> connection.delete_bucket(bucket)

If the bucket doesn't exist, this will raise a
:class:`gcloud.exceptions.NotFound`::

Expand All @@ -383,36 +375,11 @@ def delete_bucket(self, bucket):
>>> except Conflict:
>>> print 'That bucket is not empty!'

:type bucket: string or :class:`gcloud.storage.bucket.Bucket`
:param bucket: The bucket name (or bucket object) to delete.
"""
bucket = self.new_bucket(bucket)
self.api_request(method='DELETE', path=bucket.path)

def new_bucket(self, bucket):
"""Factory method for creating a new (unsaved) bucket object.

This method is really useful when you're not sure whether you
have an actual :class:`gcloud.storage.bucket.Bucket` object or
just a name of a bucket. It always returns the object::

>>> bucket = connection.new_bucket('bucket')
>>> print bucket
<Bucket: bucket>
>>> bucket = connection.new_bucket(bucket)
>>> print bucket
<Bucket: bucket>

:type bucket: string or :class:`gcloud.storage.bucket.Bucket`
:param bucket: A name of a bucket or an existing Bucket object.
:type bucket_name: string
:param bucket_name: The bucket name to delete.
"""
if isinstance(bucket, Bucket):
return bucket

if isinstance(bucket, six.string_types):
return Bucket(connection=self, name=bucket)

raise TypeError('Invalid bucket: %s' % bucket)
bucket_path = Bucket.path_helper(bucket_name)
self.api_request(method='DELETE', path=bucket_path)


class _BucketIterator(Iterator):
Expand Down
33 changes: 0 additions & 33 deletions gcloud/storage/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,12 +505,6 @@ def test_create_bucket_ok(self):
def test_delete_bucket_defaults_miss(self):
_deleted_blobs = []

class _Bucket(object):

def __init__(self, name):
self._name = name
self.path = '/b/' + name

PROJECT = 'project'
BLOB_NAME = 'blob-name'
conn = self._makeOne(PROJECT)
Expand All @@ -526,38 +520,11 @@ def __init__(self, name):
'{}',
)

def _new_bucket(name):
return _Bucket(name)

conn.new_bucket = _new_bucket
self.assertEqual(conn.delete_bucket(BLOB_NAME), None)
self.assertEqual(_deleted_blobs, [])
self.assertEqual(http._called_with['method'], 'DELETE')
self.assertEqual(http._called_with['uri'], URI)

def test_new_bucket_w_existing(self):
from gcloud.storage.bucket import Bucket
PROJECT = 'project'
BLOB_NAME = 'blob-name'
conn = self._makeOne(PROJECT)
existing = Bucket(self, BLOB_NAME)
self.assertTrue(conn.new_bucket(existing) is existing)

def test_new_bucket_w_blob(self):
from gcloud.storage.bucket import Bucket
PROJECT = 'project'
BLOB_NAME = 'blob-name'
conn = self._makeOne(PROJECT)
bucket = conn.new_bucket(BLOB_NAME)
self.assertTrue(isinstance(bucket, Bucket))
self.assertTrue(bucket.connection is conn)
self.assertEqual(bucket.name, BLOB_NAME)

def test_new_bucket_w_invalid(self):
PROJECT = 'project'
conn = self._makeOne(PROJECT)
self.assertRaises(TypeError, conn.new_bucket, object())


class Test__BucketIterator(unittest2.TestCase):

Expand Down