Skip to content

Commit

Permalink
Merge pull request #780 from dhermes/add-bucket-create
Browse files Browse the repository at this point in the history
Adding Bucket.create() method.
  • Loading branch information
dhermes committed Mar 30, 2015
2 parents 37fb0ec + e4d84a4 commit 65341ae
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
17 changes: 5 additions & 12 deletions gcloud/storage/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ def create_bucket(bucket_name, project=None, connection=None):
This implements "storage.buckets.insert".
If the bucket already exists, will raise
:class:`gcloud.exceptions.Conflict`.
:type project: string
:param project: Optional. The project to use when creating bucket.
If not provided, falls back to default.
Expand All @@ -153,20 +156,10 @@ def create_bucket(bucket_name, project=None, connection=None):
: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.)
"""
connection = _require_connection(connection)
if project is None:
project = get_default_project()

query_params = {'project': project}
response = connection.api_request(method='POST', path='/b',
query_params=query_params,
data={'name': bucket_name})
name = response.get('name')
bucket = Bucket(name, connection=connection)
bucket._properties = response
bucket = Bucket(bucket_name, connection=connection)
bucket.create(project)
return bucket


Expand Down
29 changes: 29 additions & 0 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import os
import six

from gcloud._helpers import get_default_project
from gcloud.exceptions import NotFound
from gcloud.storage._helpers import _PropertyMixin
from gcloud.storage._helpers import _scalar_property
Expand Down Expand Up @@ -125,6 +126,34 @@ def exists(self):
except NotFound:
return False

def create(self, project=None):
"""Creates current bucket.
If the bucket already exists, will raise
:class:`gcloud.exceptions.Conflict`.
This implements "storage.buckets.insert".
:type project: string
:param project: Optional. The project to use when creating bucket.
If not provided, falls back to default.
:rtype: :class:`gcloud.storage.bucket.Bucket`
:returns: The newly created bucket.
:raises: :class:`EnvironmentError` if the project is not given and
can't be inferred.
"""
if project is None:
project = get_default_project()
if project is None:
raise EnvironmentError('Project could not be inferred '
'from environment.')

query_params = {'project': project}
self._properties = self.connection.api_request(
method='POST', path='/b', query_params=query_params,
data={'name': self.name})

@property
def acl(self):
"""Create our ACL on demand."""
Expand Down
37 changes: 37 additions & 0 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,43 @@ def api_request(cls, *args, **kwargs):
expected_cw = [((), expected_called_kwargs)]
self.assertEqual(_FakeConnection._called_with, expected_cw)

def test_create_no_project(self):
from gcloud._testing import _monkey_defaults
BUCKET_NAME = 'bucket-name'
bucket = self._makeOne(BUCKET_NAME)
with _monkey_defaults(project=None):
self.assertRaises(EnvironmentError, bucket.create)

def test_create_hit_explicit_project(self):
BUCKET_NAME = 'bucket-name'
DATA = {'name': BUCKET_NAME}
connection = _Connection(DATA)
PROJECT = 'PROJECT'
bucket = self._makeOne(BUCKET_NAME, connection=connection)
bucket.create(PROJECT)

kw, = connection._requested
self.assertEqual(kw['method'], 'POST')
self.assertEqual(kw['path'], '/b')
self.assertEqual(kw['query_params'], {'project': PROJECT})
self.assertEqual(kw['data'], DATA)

def test_create_hit_implicit_project(self):
from gcloud._testing import _monkey_defaults
BUCKET_NAME = 'bucket-name'
DATA = {'name': BUCKET_NAME}
connection = _Connection(DATA)
PROJECT = 'PROJECT'
bucket = self._makeOne(BUCKET_NAME, connection=connection)
with _monkey_defaults(project=PROJECT):
bucket.create()

kw, = connection._requested
self.assertEqual(kw['method'], 'POST')
self.assertEqual(kw['path'], '/b')
self.assertEqual(kw['query_params'], {'project': PROJECT})
self.assertEqual(kw['data'], DATA)

def test_acl_property(self):
from gcloud.storage.acl import BucketACL
bucket = self._makeOne()
Expand Down

0 comments on commit 65341ae

Please sign in to comment.