diff --git a/gcloud/storage/api.py b/gcloud/storage/api.py index 0fada1334322..edd31abe81d0 100644 --- a/gcloud/storage/api.py +++ b/gcloud/storage/api.py @@ -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. @@ -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 diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index 71468e80ac27..8a769c022ea0 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -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 @@ -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.""" diff --git a/gcloud/storage/test_bucket.py b/gcloud/storage/test_bucket.py index 9a9ea2ed3979..10ca83b590c1 100644 --- a/gcloud/storage/test_bucket.py +++ b/gcloud/storage/test_bucket.py @@ -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()