Skip to content

Commit f21cacf

Browse files
committed
STORAGE: Making Connection.create_bucket a standalone method.
1 parent 66dc298 commit f21cacf

File tree

10 files changed

+86
-65
lines changed

10 files changed

+86
-65
lines changed

docs/_components/storage-getting-started.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bucket.
5656

5757
Let's create a bucket:
5858

59-
>>> bucket = connection.create_bucket('test')
59+
>>> bucket = storage.create_bucket('test', connection=connection)
6060
Traceback (most recent call last):
6161
File "<stdin>", line 1, in <module>
6262
File "gcloud/storage/connection.py", line 340, in create_bucket

docs/_components/storage-quickstart.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ you can create buckets and blobs::
5858
>>> from gcloud import storage
5959
>>> storage.get_all_buckets(connection)
6060
[<Bucket: ...>, ...]
61-
>>> bucket = connection.create_bucket('my-new-bucket')
61+
>>> bucket = storage.create_bucket('my-new-bucket', connection=connection)
6262
>>> print bucket
6363
<Bucket: my-new-bucket>
6464
>>> blob = bucket.new_blob('my-test-file.txt')

gcloud/storage/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from gcloud.storage._implicit_environ import get_default_bucket
4646
from gcloud.storage._implicit_environ import get_default_connection
4747
from gcloud.storage._implicit_environ import get_default_project
48+
from gcloud.storage.api import create_bucket
4849
from gcloud.storage.api import get_all_buckets
4950
from gcloud.storage.api import get_bucket
5051
from gcloud.storage.api import lookup_bucket

gcloud/storage/api.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,40 @@ def get_bucket(bucket_name, connection=None):
121121
return Bucket(properties=response, connection=connection)
122122

123123

124+
def create_bucket(bucket_name, connection=None):
125+
"""Create a new bucket.
126+
127+
For example::
128+
129+
>>> from gcloud import storage
130+
>>> storage.set_defaults()
131+
>>> bucket = storage.create_bucket('my-bucket')
132+
>>> print bucket
133+
<Bucket: my-bucket>
134+
135+
This implements "storage.buckets.insert".
136+
137+
:type bucket_name: string
138+
:param bucket_name: The bucket name to create.
139+
140+
:type connection: :class:`gcloud.storage.connection.Connection` or
141+
``NoneType``
142+
:param connection: Optional. The connection to use when sending requests.
143+
If not provided, falls back to default.
144+
145+
:rtype: :class:`gcloud.storage.bucket.Bucket`
146+
:returns: The newly created bucket.
147+
:raises: :class:`gcloud.exceptions.Conflict` if
148+
there is a confict (bucket already exists, invalid name, etc.)
149+
"""
150+
if connection is None:
151+
connection = get_default_connection()
152+
153+
response = connection.api_request(method='POST', path='/b',
154+
data={'name': bucket_name})
155+
return Bucket(properties=response, connection=connection)
156+
157+
124158
class _BucketIterator(Iterator):
125159
"""An iterator listing all buckets.
126160

gcloud/storage/connection.py

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,21 @@
2020

2121
from gcloud import connection as base_connection
2222
from gcloud.exceptions import make_exception
23-
from gcloud.storage.bucket import Bucket
2423

2524

2625
class Connection(base_connection.Connection):
2726
"""A connection to Google Cloud Storage via the JSON REST API.
2827
2928
This defines :meth:`Connection.api_request` for making a generic JSON
30-
API request and most API requests are created elsewhere (e.g. in
29+
API request and API requests are created elsewhere (e.g. in
30+
:mod:`gcloud.storage.api` and
3131
:class:`gcloud.storage.bucket.Bucket` and
3232
:class:`gcloud.storage.blob.Blob`).
3333
3434
See :class:`gcloud.connection.Connection` for a full list of
3535
parameters. This subclass differs only in needing a project
3636
name (which you specify when creating a project in the Cloud
3737
Console).
38-
39-
A typical use of this is to operate on
40-
:class:`gcloud.storage.bucket.Bucket` objects::
41-
42-
>>> from gcloud import storage
43-
>>> connection = storage.get_connection(project)
44-
>>> bucket = connection.create_bucket('my-bucket-name')
4538
"""
4639

4740
API_BASE_URL = base_connection.API_BASE_URL
@@ -245,28 +238,3 @@ def api_request(self, method, path, query_params=None,
245238
return json.loads(content)
246239

247240
return content
248-
249-
def create_bucket(self, bucket_name):
250-
"""Create a new bucket.
251-
252-
For example::
253-
254-
>>> from gcloud import storage
255-
>>> connection = storage.get_connection(project)
256-
>>> bucket = connection.create_bucket('my-bucket')
257-
>>> print bucket
258-
<Bucket: my-bucket>
259-
260-
This implements "storage.buckets.insert".
261-
262-
:type bucket_name: string
263-
:param bucket_name: The bucket name to create.
264-
265-
:rtype: :class:`gcloud.storage.bucket.Bucket`
266-
:returns: The newly created bucket.
267-
:raises: :class:`gcloud.exceptions.Conflict` if
268-
there is a confict (bucket already exists, invalid name, etc.)
269-
"""
270-
response = self.api_request(method='POST', path='/b',
271-
data={'name': bucket_name})
272-
return Bucket(properties=response, connection=self)

gcloud/storage/demo/demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# Now let's create a new bucket...
1818
bucket_name = ("bucket-%s" % time.time()).replace(".", "") # Get rid of dots.
1919
print(bucket_name)
20-
bucket = connection.create_bucket(bucket_name)
20+
bucket = storage.create_bucket(bucket_name, connection=connection)
2121
print(bucket)
2222

2323
# Let's look at all of the buckets again...

gcloud/storage/test_api.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,49 @@ def test_hit_use_default(self):
205205
self._get_bucket_hit_helper(use_default=True)
206206

207207

208+
class Test_create_bucket(unittest2.TestCase):
209+
210+
def _callFUT(self, bucket_name, connection=None):
211+
from gcloud.storage.api import create_bucket
212+
return create_bucket(bucket_name, connection=connection)
213+
214+
def _create_bucket_success_helper(self, use_default=False):
215+
from gcloud.storage._testing import _monkey_defaults
216+
from gcloud.storage.connection import Connection
217+
from gcloud.storage.bucket import Bucket
218+
PROJECT = 'project'
219+
BLOB_NAME = 'blob-name'
220+
conn = Connection(PROJECT)
221+
URI = '/'.join([
222+
conn.API_BASE_URL,
223+
'storage',
224+
conn.API_VERSION,
225+
'b?project=%s' % PROJECT,
226+
])
227+
http = conn._http = Http(
228+
{'status': '200', 'content-type': 'application/json'},
229+
'{"name": "%s"}' % BLOB_NAME,
230+
)
231+
232+
if use_default:
233+
with _monkey_defaults(connection=conn):
234+
bucket = self._callFUT(BLOB_NAME)
235+
else:
236+
bucket = self._callFUT(BLOB_NAME, connection=conn)
237+
238+
self.assertTrue(isinstance(bucket, Bucket))
239+
self.assertTrue(bucket.connection is conn)
240+
self.assertEqual(bucket.name, BLOB_NAME)
241+
self.assertEqual(http._called_with['method'], 'POST')
242+
self.assertEqual(http._called_with['uri'], URI)
243+
244+
def test_success(self):
245+
self._create_bucket_success_helper(use_default=False)
246+
247+
def test_success_use_default(self):
248+
self._create_bucket_success_helper(use_default=True)
249+
250+
208251
class Test__BucketIterator(unittest2.TestCase):
209252

210253
def _getTargetClass(self):

gcloud/storage/test_batch.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,6 @@ def api_request(self, method, path, query_params=None,
383383
expect_json=True): # pragma: NO COVER
384384
pass
385385

386-
def create_bucket(self, name): # pragma: NO COVER
387-
pass
388-
389386

390387
class _Response(dict):
391388

gcloud/storage/test_connection.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -281,28 +281,6 @@ def test_api_request_w_500(self):
281281
)
282282
self.assertRaises(InternalServerError, conn.api_request, 'GET', '/')
283283

284-
def test_create_bucket_ok(self):
285-
from gcloud.storage.bucket import Bucket
286-
PROJECT = 'project'
287-
BLOB_NAME = 'blob-name'
288-
conn = self._makeOne(PROJECT)
289-
URI = '/'.join([
290-
conn.API_BASE_URL,
291-
'storage',
292-
conn.API_VERSION,
293-
'b?project=%s' % PROJECT,
294-
])
295-
http = conn._http = Http(
296-
{'status': '200', 'content-type': 'application/json'},
297-
'{"name": "%s"}' % BLOB_NAME,
298-
)
299-
bucket = conn.create_bucket(BLOB_NAME)
300-
self.assertTrue(isinstance(bucket, Bucket))
301-
self.assertTrue(bucket.connection is conn)
302-
self.assertEqual(bucket.name, BLOB_NAME)
303-
self.assertEqual(http._called_with['method'], 'POST')
304-
self.assertEqual(http._called_with['uri'], URI)
305-
306284

307285
class Http(object):
308286

regression/storage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def setUpModule():
3838
bucket_name = 'new%d' % (1000 * time.time(),)
3939
# In the **very** rare case the bucket name is reserved, this
4040
# fails with a ConnectionError.
41-
SHARED_BUCKETS['test_bucket'] = CONNECTION.create_bucket(bucket_name)
41+
SHARED_BUCKETS['test_bucket'] = storage.create_bucket(bucket_name)
4242

4343

4444
def tearDownModule():
@@ -60,7 +60,7 @@ def test_create_bucket(self):
6060
new_bucket_name = 'a-new-bucket'
6161
self.assertRaises(exceptions.NotFound,
6262
storage.get_bucket, new_bucket_name)
63-
created = CONNECTION.create_bucket(new_bucket_name)
63+
created = storage.create_bucket(new_bucket_name)
6464
self.case_buckets_to_delete.append(new_bucket_name)
6565
self.assertEqual(created.name, new_bucket_name)
6666

@@ -72,7 +72,7 @@ def test_get_buckets(self):
7272
]
7373
created_buckets = []
7474
for bucket_name in buckets_to_create:
75-
bucket = CONNECTION.create_bucket(bucket_name)
75+
bucket = storage.create_bucket(bucket_name)
7676
self.case_buckets_to_delete.append(bucket_name)
7777

7878
# Retrieve the buckets.

0 commit comments

Comments
 (0)