Skip to content

Commit

Permalink
Making Bucket.(exists|create) have an optional connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Apr 28, 2015
1 parent 286207b commit a14f18a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gcloud/storage/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
23 changes: 18 additions & 5 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -142,19 +149,25 @@ 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:
raise EnvironmentError('Project could not be inferred '
'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)
Expand Down
16 changes: 8 additions & 8 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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')
Expand All @@ -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')
Expand Down

0 comments on commit a14f18a

Please sign in to comment.