Skip to content

Commit

Permalink
Removing Connection.new_bucket.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Feb 3, 2015
1 parent e80958a commit 41223d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 76 deletions.
54 changes: 11 additions & 43 deletions gcloud/storage/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,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 +326,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 +337,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 +376,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 = '/b/' + 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

0 comments on commit 41223d0

Please sign in to comment.