Skip to content

Commit dda6735

Browse files
committed
STORAGE: Removing Connection.delete_bucket.
It was a simple one-liner only being used in one place.
1 parent 341b479 commit dda6735

File tree

6 files changed

+37
-94
lines changed

6 files changed

+37
-94
lines changed

docs/_components/storage-getting-started.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,17 @@ bucket itself as an iterator::
171171
Deleting a bucket
172172
-----------------
173173

174-
You can delete a bucket using the :func:`delete_bucket
175-
<gcloud.storage.connection.Connection.delete_bucket>` method::
174+
You can delete a bucket using the
175+
:meth:`delete <gcloud.storage.bucket.Bucket.delete>` method::
176176

177-
>>> connection.delete_bucket('my-bucket')
177+
>>> bucket.delete()
178178

179179
Remember, the bucket you're deleting needs to be empty, otherwise you'll
180-
get an error.
180+
get an error (409 conflict).
181181

182182
If you have a full bucket, you can delete it this way::
183183

184-
>>> bucket = connection.delete_bucket('my-bucket', force=True)
184+
>>> bucket.delete(force=True)
185185

186186
Listing available buckets
187187
-------------------------

gcloud/storage/bucket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def delete(self, force=False):
322322
# Ignore 404 errors on delete.
323323
self.delete_blobs(blobs, on_error=lambda blob: None)
324324

325-
self.connection.delete_bucket(self.name)
325+
self.connection.api_request(method='DELETE', path=self.path)
326326

327327
def delete_blob(self, blob):
328328
"""Deletes a blob from the current bucket.

gcloud/storage/connection.py

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ class Connection(base_connection.Connection):
3131
:class:`gcloud.storage.bucket.Bucket` and
3232
:class:`gcloud.storage.blob.Blob`).
3333
34-
Methods for getting, creating and deleting individual buckets as well
35-
as listing buckets associated with a project are defined here. This
36-
corresponds to the "storage.buckets" resource in the API.
37-
3834
See :class:`gcloud.connection.Connection` for a full list of
3935
parameters. This subclass differs only in needing a project
4036
name (which you specify when creating a project in the Cloud
@@ -46,12 +42,6 @@ class Connection(base_connection.Connection):
4642
>>> from gcloud import storage
4743
>>> connection = storage.get_connection(project)
4844
>>> bucket = connection.create_bucket('my-bucket-name')
49-
50-
You can then delete this bucket::
51-
52-
>>> bucket.delete()
53-
>>> # or
54-
>>> connection.delete_bucket(bucket.name)
5545
"""
5646

5747
API_BASE_URL = base_connection.API_BASE_URL
@@ -280,38 +270,3 @@ def create_bucket(self, bucket_name):
280270
response = self.api_request(method='POST', path='/b',
281271
data={'name': bucket_name})
282272
return Bucket(properties=response, connection=self)
283-
284-
def delete_bucket(self, bucket_name):
285-
"""Delete a bucket.
286-
287-
You can use this method to delete a bucket by name.
288-
289-
>>> from gcloud import storage
290-
>>> connection = storage.get_connection(project)
291-
>>> connection.delete_bucket('my-bucket')
292-
293-
If the bucket doesn't exist, this will raise a
294-
:class:`gcloud.exceptions.NotFound`::
295-
296-
>>> from gcloud.exceptions import NotFound
297-
>>> try:
298-
>>> connection.delete_bucket('my-bucket')
299-
>>> except NotFound:
300-
>>> print 'That bucket does not exist!'
301-
302-
If the bucket still has objects in it, this will raise a
303-
:class:`gcloud.exceptions.Conflict`::
304-
305-
>>> from gcloud.exceptions import Conflict
306-
>>> try:
307-
>>> connection.delete_bucket('my-bucket')
308-
>>> except Conflict:
309-
>>> print 'That bucket is not empty!'
310-
311-
This implements "storage.buckets.delete".
312-
313-
:type bucket_name: string
314-
:param bucket_name: The bucket name to delete.
315-
"""
316-
bucket_path = Bucket.path_helper(bucket_name)
317-
self.api_request(method='DELETE', path=bucket_path)

gcloud/storage/test_batch.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,6 @@ def api_request(self, method, path, query_params=None,
386386
def create_bucket(self, name): # pragma: NO COVER
387387
pass
388388

389-
def delete_bucket(self, name): # pragma: NO COVER
390-
pass
391-
392389

393390
class _Response(dict):
394391

gcloud/storage/test_bucket.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -331,16 +331,18 @@ def test_delete_default_miss(self):
331331
connection = _Connection()
332332
bucket = self._makeOne(connection, NAME)
333333
self.assertRaises(NotFound, bucket.delete)
334-
self.assertEqual(connection._deleted, [NAME])
334+
expected_cw = [{'method': 'DELETE', 'path': bucket.path}]
335+
self.assertEqual(connection._deleted_buckets, expected_cw)
335336

336337
def test_delete_explicit_hit(self):
337338
NAME = 'name'
338339
GET_BLOBS_RESP = {'items': []}
339340
connection = _Connection(GET_BLOBS_RESP)
340-
connection._delete_ok = True
341+
connection._delete_bucket = True
341342
bucket = self._makeOne(connection, NAME)
342343
self.assertEqual(bucket.delete(force=True), None)
343-
self.assertEqual(connection._deleted, [NAME])
344+
expected_cw = [{'method': 'DELETE', 'path': bucket.path}]
345+
self.assertEqual(connection._deleted_buckets, expected_cw)
344346

345347
def test_delete_explicit_force_delete_blobs(self):
346348
NAME = 'name'
@@ -355,21 +357,23 @@ def test_delete_explicit_force_delete_blobs(self):
355357
DELETE_BLOB1_RESP = DELETE_BLOB2_RESP = {}
356358
connection = _Connection(GET_BLOBS_RESP, DELETE_BLOB1_RESP,
357359
DELETE_BLOB2_RESP)
358-
connection._delete_ok = True
360+
connection._delete_bucket = True
359361
bucket = self._makeOne(connection, NAME)
360362
self.assertEqual(bucket.delete(force=True), None)
361-
self.assertEqual(connection._deleted, [NAME])
363+
expected_cw = [{'method': 'DELETE', 'path': bucket.path}]
364+
self.assertEqual(connection._deleted_buckets, expected_cw)
362365

363366
def test_delete_explicit_force_miss_blobs(self):
364367
NAME = 'name'
365368
BLOB_NAME = 'blob-name1'
366369
GET_BLOBS_RESP = {'items': [{'name': BLOB_NAME}]}
367370
# Note the connection does not have a response for the blob.
368371
connection = _Connection(GET_BLOBS_RESP)
369-
connection._delete_ok = True
372+
connection._delete_bucket = True
370373
bucket = self._makeOne(connection, NAME)
371374
self.assertEqual(bucket.delete(force=True), None)
372-
self.assertEqual(connection._deleted, [NAME])
375+
expected_cw = [{'method': 'DELETE', 'path': bucket.path}]
376+
self.assertEqual(connection._deleted_buckets, expected_cw)
373377

374378
def test_delete_explicit_too_many(self):
375379
NAME = 'name'
@@ -382,12 +386,13 @@ def test_delete_explicit_too_many(self):
382386
],
383387
}
384388
connection = _Connection(GET_BLOBS_RESP)
385-
connection._delete_ok = True
389+
connection._delete_bucket = True
386390
bucket = self._makeOne(connection, NAME)
387391

388392
# Make the Bucket refuse to delete with 2 objects.
389393
bucket._MAX_OBJECTS_FOR_BUCKET_DELETE = 1
390394
self.assertRaises(ValueError, bucket.delete, force=True)
395+
self.assertEqual(connection._deleted_buckets, [])
391396

392397
def test_delete_blob_miss(self):
393398
from gcloud.exceptions import NotFound
@@ -1079,31 +1084,40 @@ def get_items_from_response(self, response):
10791084

10801085

10811086
class _Connection(object):
1082-
_delete_ok = False
1087+
_delete_bucket = False
10831088

10841089
def __init__(self, *responses):
10851090
self._responses = responses
10861091
self._requested = []
1087-
self._deleted = []
1092+
self._deleted_buckets = []
1093+
1094+
@staticmethod
1095+
def _is_bucket_path(path):
1096+
if not path.startswith('/b/'): # pragma: NO COVER
1097+
return False
1098+
# Now just ensure the path only has /b/ and one more segment.
1099+
return path.count('/') == 2
10881100

10891101
def api_request(self, **kw):
10901102
from gcloud.exceptions import NotFound
10911103
self._requested.append(kw)
10921104

1105+
method = kw.get('method')
1106+
path = kw.get('path', '')
1107+
if method == 'DELETE' and self._is_bucket_path(path):
1108+
self._deleted_buckets.append(kw)
1109+
if self._delete_bucket:
1110+
return
1111+
else:
1112+
raise NotFound('miss')
1113+
10931114
try:
10941115
response, self._responses = self._responses[0], self._responses[1:]
10951116
except:
10961117
raise NotFound('miss')
10971118
else:
10981119
return response
10991120

1100-
def delete_bucket(self, bucket):
1101-
from gcloud.exceptions import NotFound
1102-
self._deleted.append(bucket)
1103-
if not self._delete_ok:
1104-
raise NotFound('miss')
1105-
return True
1106-
11071121

11081122
class _Bucket(object):
11091123
path = '/b/name'

gcloud/storage/test_connection.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -303,29 +303,6 @@ def test_create_bucket_ok(self):
303303
self.assertEqual(http._called_with['method'], 'POST')
304304
self.assertEqual(http._called_with['uri'], URI)
305305

306-
def test_delete_bucket_defaults_miss(self):
307-
_deleted_blobs = []
308-
309-
PROJECT = 'project'
310-
BLOB_NAME = 'blob-name'
311-
conn = self._makeOne(PROJECT)
312-
URI = '/'.join([
313-
conn.API_BASE_URL,
314-
'storage',
315-
conn.API_VERSION,
316-
'b',
317-
'%s?project=%s' % (BLOB_NAME, PROJECT),
318-
])
319-
http = conn._http = Http(
320-
{'status': '200', 'content-type': 'application/json'},
321-
'{}',
322-
)
323-
324-
self.assertEqual(conn.delete_bucket(BLOB_NAME), None)
325-
self.assertEqual(_deleted_blobs, [])
326-
self.assertEqual(http._called_with['method'], 'DELETE')
327-
self.assertEqual(http._called_with['uri'], URI)
328-
329306

330307
class Http(object):
331308

0 commit comments

Comments
 (0)