FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Adding Bucket.create() method. by dhermes · Pull Request #780 · googleapis/google-cloud-python · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
17 changes: 5 additions & 12 deletions gcloud/storage/api.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL