From b54e9aadbcb3617fc256ff9575338dbae6881c60 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Mon, 30 Mar 2015 13:43:50 -0700 Subject: [PATCH] Adding Bucket.create() method. --- gcloud/storage/api.py | 17 +++++----------- gcloud/storage/bucket.py | 29 +++++++++++++++++++++++++++ gcloud/storage/test_bucket.py | 37 +++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 12 deletions(-) diff --git a/gcloud/storage/api.py b/gcloud/storage/api.py index e44a0d89778e9..5677300178185 100644 --- a/gcloud/storage/api.py +++ b/gcloud/storage/api.py @@ -143,6 +143,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. @@ -157,21 +160,11 @@ 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.) """ if connection is None: connection = get_default_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 71468e80ac27f..8a769c022ea07 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 9a9ea2ed3979b..10ca83b590c15 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()