Skip to content

Commit a681014

Browse files
committed
Merge pull request #586 from dhermes/storage-ditch-new-bucket
Ditch Connection.new_bucket method in storage
2 parents e80958a + 4fec0fb commit a681014

File tree

3 files changed

+24
-78
lines changed

3 files changed

+24
-78
lines changed

gcloud/storage/bucket.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,25 @@ def connection(self):
128128
"""
129129
return self._connection
130130

131+
@staticmethod
132+
def path_helper(bucket_name):
133+
"""Relative URL path for a bucket.
134+
135+
:type bucket_name: string
136+
:param bucket_name: The bucket name in the path.
137+
138+
:rtype: string
139+
:returns: The relative URL path for ``bucket_name``.
140+
"""
141+
return '/b/' + bucket_name
142+
131143
@property
132144
def path(self):
133145
"""The URL path to this bucket."""
134146
if not self.name:
135147
raise ValueError('Cannot determine path without bucket name.')
136148

137-
return '/b/' + self.name
149+
return self.path_helper(self.name)
138150

139151
def get_blob(self, blob):
140152
"""Get a blob object by name.

gcloud/storage/connection.py

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from gcloud.exceptions import NotFound
2323
from gcloud.storage.bucket import Bucket
2424
from gcloud.storage.iterator import Iterator
25-
import six
2625

2726

2827
class Connection(_Base):
@@ -296,7 +295,7 @@ def get_bucket(self, bucket_name):
296295
:returns: The bucket matching the name provided.
297296
:raises: :class:`gcloud.exceptions.NotFound`
298297
"""
299-
bucket = self.new_bucket(bucket_name)
298+
bucket = Bucket(connection=self, name=bucket_name)
300299
response = self.api_request(method='GET', path=bucket.path)
301300
return Bucket(properties=response, connection=self)
302301

@@ -326,7 +325,7 @@ def lookup(self, bucket_name):
326325
except NotFound:
327326
return None
328327

329-
def create_bucket(self, bucket):
328+
def create_bucket(self, bucket_name):
330329
"""Create a new bucket.
331330
332331
For example::
@@ -337,34 +336,27 @@ def create_bucket(self, bucket):
337336
>>> print bucket
338337
<Bucket: my-bucket>
339338
340-
:type bucket: string or :class:`gcloud.storage.bucket.Bucket`
341-
:param bucket: The bucket name (or bucket object) to create.
339+
:type bucket_name: string
340+
:param bucket_name: The bucket name to create.
342341
343342
:rtype: :class:`gcloud.storage.bucket.Bucket`
344343
:returns: The newly created bucket.
345344
:raises: :class:`gcloud.exceptions.Conflict` if
346345
there is a confict (bucket already exists, invalid name, etc.)
347346
"""
348-
bucket = self.new_bucket(bucket)
349347
response = self.api_request(method='POST', path='/b',
350-
data={'name': bucket.name})
348+
data={'name': bucket_name})
351349
return Bucket(properties=response, connection=self)
352350

353-
def delete_bucket(self, bucket):
351+
def delete_bucket(self, bucket_name):
354352
"""Delete a bucket.
355353
356-
You can use this method to delete a bucket by name, or to delete
357-
a bucket object::
354+
You can use this method to delete a bucket by name.
358355
359356
>>> from gcloud import storage
360357
>>> connection = storage.get_connection(project)
361358
>>> connection.delete_bucket('my-bucket')
362359
363-
You can also delete pass in the bucket object::
364-
365-
>>> bucket = connection.get_bucket('other-bucket')
366-
>>> connection.delete_bucket(bucket)
367-
368360
If the bucket doesn't exist, this will raise a
369361
:class:`gcloud.exceptions.NotFound`::
370362
@@ -383,36 +375,11 @@ def delete_bucket(self, bucket):
383375
>>> except Conflict:
384376
>>> print 'That bucket is not empty!'
385377
386-
:type bucket: string or :class:`gcloud.storage.bucket.Bucket`
387-
:param bucket: The bucket name (or bucket object) to delete.
388-
"""
389-
bucket = self.new_bucket(bucket)
390-
self.api_request(method='DELETE', path=bucket.path)
391-
392-
def new_bucket(self, bucket):
393-
"""Factory method for creating a new (unsaved) bucket object.
394-
395-
This method is really useful when you're not sure whether you
396-
have an actual :class:`gcloud.storage.bucket.Bucket` object or
397-
just a name of a bucket. It always returns the object::
398-
399-
>>> bucket = connection.new_bucket('bucket')
400-
>>> print bucket
401-
<Bucket: bucket>
402-
>>> bucket = connection.new_bucket(bucket)
403-
>>> print bucket
404-
<Bucket: bucket>
405-
406-
:type bucket: string or :class:`gcloud.storage.bucket.Bucket`
407-
:param bucket: A name of a bucket or an existing Bucket object.
378+
:type bucket_name: string
379+
:param bucket_name: The bucket name to delete.
408380
"""
409-
if isinstance(bucket, Bucket):
410-
return bucket
411-
412-
if isinstance(bucket, six.string_types):
413-
return Bucket(connection=self, name=bucket)
414-
415-
raise TypeError('Invalid bucket: %s' % bucket)
381+
bucket_path = Bucket.path_helper(bucket_name)
382+
self.api_request(method='DELETE', path=bucket_path)
416383

417384

418385
class _BucketIterator(Iterator):

gcloud/storage/test_connection.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,6 @@ def test_create_bucket_ok(self):
505505
def test_delete_bucket_defaults_miss(self):
506506
_deleted_blobs = []
507507

508-
class _Bucket(object):
509-
510-
def __init__(self, name):
511-
self._name = name
512-
self.path = '/b/' + name
513-
514508
PROJECT = 'project'
515509
BLOB_NAME = 'blob-name'
516510
conn = self._makeOne(PROJECT)
@@ -526,38 +520,11 @@ def __init__(self, name):
526520
'{}',
527521
)
528522

529-
def _new_bucket(name):
530-
return _Bucket(name)
531-
532-
conn.new_bucket = _new_bucket
533523
self.assertEqual(conn.delete_bucket(BLOB_NAME), None)
534524
self.assertEqual(_deleted_blobs, [])
535525
self.assertEqual(http._called_with['method'], 'DELETE')
536526
self.assertEqual(http._called_with['uri'], URI)
537527

538-
def test_new_bucket_w_existing(self):
539-
from gcloud.storage.bucket import Bucket
540-
PROJECT = 'project'
541-
BLOB_NAME = 'blob-name'
542-
conn = self._makeOne(PROJECT)
543-
existing = Bucket(self, BLOB_NAME)
544-
self.assertTrue(conn.new_bucket(existing) is existing)
545-
546-
def test_new_bucket_w_blob(self):
547-
from gcloud.storage.bucket import Bucket
548-
PROJECT = 'project'
549-
BLOB_NAME = 'blob-name'
550-
conn = self._makeOne(PROJECT)
551-
bucket = conn.new_bucket(BLOB_NAME)
552-
self.assertTrue(isinstance(bucket, Bucket))
553-
self.assertTrue(bucket.connection is conn)
554-
self.assertEqual(bucket.name, BLOB_NAME)
555-
556-
def test_new_bucket_w_invalid(self):
557-
PROJECT = 'project'
558-
conn = self._makeOne(PROJECT)
559-
self.assertRaises(TypeError, conn.new_bucket, object())
560-
561528

562529
class Test__BucketIterator(unittest2.TestCase):
563530

0 commit comments

Comments
 (0)