From a14f18a4059eac61764ab295eac9da10dabf765b Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Tue, 28 Apr 2015 10:48:47 -0700 Subject: [PATCH] Making Bucket.(exists|create) have an optional connection. Towards #728. --- gcloud/storage/api.py | 2 +- gcloud/storage/bucket.py | 23 ++++++++++++++++++----- gcloud/storage/test_bucket.py | 16 ++++++++-------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/gcloud/storage/api.py b/gcloud/storage/api.py index 1d5a047e073d..0e73ab9a75d3 100644 --- a/gcloud/storage/api.py +++ b/gcloud/storage/api.py @@ -196,7 +196,7 @@ def create_bucket(bucket_name, project=None, connection=None): """ connection = _require_connection(connection) bucket = Bucket(bucket_name, connection=connection) - bucket.create(project) + bucket.create(project, connection=connection) return bucket diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index d45383b19ffc..955d2e4adcd1 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -43,6 +43,7 @@ from gcloud._helpers import get_default_project from gcloud.exceptions import NotFound from gcloud.storage._helpers import _PropertyMixin +from gcloud.storage._helpers import _require_connection from gcloud.storage._helpers import _scalar_property from gcloud.storage.acl import BucketACL from gcloud.storage.acl import DefaultObjectACL @@ -114,23 +115,29 @@ def __contains__(self, blob_name): blob = Blob(blob_name, bucket=self) return blob.exists() - def exists(self): + def exists(self, connection=None): """Determines whether or not this bucket exists. + :type connection: :class:`gcloud.storage.connection.Connection` or + ``NoneType`` + :param connection: Optional. The connection to use when sending + requests. If not provided, falls back to default. + :rtype: boolean :returns: True if the bucket exists in Cloud Storage. """ + connection = _require_connection(connection) try: # We only need the status code (200 or not) so we seek to # minimize the returned payload. query_params = {'fields': 'name'} - self.connection.api_request(method='GET', path=self.path, - query_params=query_params) + connection.api_request(method='GET', path=self.path, + query_params=query_params) return True except NotFound: return False - def create(self, project=None): + def create(self, project=None, connection=None): """Creates current bucket. If the bucket already exists, will raise @@ -142,11 +149,17 @@ def create(self, project=None): :param project: Optional. The project to use when creating bucket. If not provided, falls back to default. + :type connection: :class:`gcloud.storage.connection.Connection` or + ``NoneType`` + :param connection: Optional. The connection to use when sending + requests. 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. """ + connection = _require_connection(connection) if project is None: project = get_default_project() if project is None: @@ -154,7 +167,7 @@ def create(self, project=None): 'from environment.') query_params = {'project': project} - api_response = self.connection.api_request( + api_response = connection.api_request( method='POST', path='/b', query_params=query_params, data={'name': self.name}) self._set_properties(api_response) diff --git a/gcloud/storage/test_bucket.py b/gcloud/storage/test_bucket.py index c9ae0e56b320..349c6e606f0e 100644 --- a/gcloud/storage/test_bucket.py +++ b/gcloud/storage/test_bucket.py @@ -154,8 +154,8 @@ def api_request(cls, *args, **kwargs): raise NotFound(args) BUCKET_NAME = 'bucket-name' - bucket = self._makeOne(BUCKET_NAME, connection=_FakeConnection) - self.assertFalse(bucket.exists()) + bucket = self._makeOne(BUCKET_NAME) + self.assertFalse(bucket.exists(connection=_FakeConnection)) expected_called_kwargs = { 'method': 'GET', 'path': bucket.path, @@ -178,8 +178,8 @@ def api_request(cls, *args, **kwargs): return object() BUCKET_NAME = 'bucket-name' - bucket = self._makeOne(BUCKET_NAME, connection=_FakeConnection) - self.assertTrue(bucket.exists()) + bucket = self._makeOne(BUCKET_NAME) + self.assertTrue(bucket.exists(connection=_FakeConnection)) expected_called_kwargs = { 'method': 'GET', 'path': bucket.path, @@ -202,8 +202,8 @@ def test_create_hit_explicit_project(self): DATA = {'name': BUCKET_NAME} connection = _Connection(DATA) PROJECT = 'PROJECT' - bucket = self._makeOne(BUCKET_NAME, connection=connection) - bucket.create(PROJECT) + bucket = self._makeOne(BUCKET_NAME) + bucket.create(PROJECT, connection=connection) kw, = connection._requested self.assertEqual(kw['method'], 'POST') @@ -217,9 +217,9 @@ def test_create_hit_implicit_project(self): DATA = {'name': BUCKET_NAME} connection = _Connection(DATA) PROJECT = 'PROJECT' - bucket = self._makeOne(BUCKET_NAME, connection=connection) + bucket = self._makeOne(BUCKET_NAME) with _monkey_defaults(project=PROJECT): - bucket.create() + bucket.create(connection=connection) kw, = connection._requested self.assertEqual(kw['method'], 'POST')