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 32b7853 commit 2538110
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 @@ -379,7 +379,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 @@ -409,7 +409,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 @@ -420,34 +420,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 @@ -466,36 +459,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)

def generate_signed_url(self, resource, expiration,
method='GET', content_md5=None,
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())

def test_generate_signed_url_w_expiration_int(self):
import base64
import urlparse
Expand Down

0 comments on commit 2538110

Please sign in to comment.