Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass 'user_project' if set for Bucket API requests #3492

Merged
merged 10 commits into from
Jun 13, 2017
Next Next commit
Block 'Bucket.create' if 'user_project' set.
This API does not accept that parameter.
  • Loading branch information
tseaver committed Jun 13, 2017
commit 25fb746258d87bbf7712d494131ffc437df1a8d6
3 changes: 3 additions & 0 deletions storage/google/cloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ def create(self, client=None):
:param client: Optional. The client to use. If not passed, falls back
to the ``client`` stored on the current bucket.
"""
if self.user_project is not None:
raise ValueError("Cannot create bucket with 'user_project' set.")

client = self._require_client(client)
query_params = {'project': client.project}
properties = {key: self._properties[key] for key in self._changes}
Expand Down
25 changes: 20 additions & 5 deletions storage/tests/unit/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ def _get_target_class():
from google.cloud.storage.bucket import Bucket
return Bucket

def _make_one(self, client=None, name=None, properties=None):
def _make_one(
self, client=None, name=None, properties=None, user_project=None):
if client is None:
connection = _Connection()
client = _Client(connection)
bucket = self._get_target_class()(client, name=name)
if user_project is None:
bucket = self._get_target_class()(client, name=name)
else:
bucket = self._get_target_class()(
client, name=name, user_project=user_project)
bucket._properties = properties or {}
return bucket

Expand All @@ -63,8 +68,7 @@ def test_ctor_w_user_project(self):
USER_PROJECT = 'user-project-123'
connection = _Connection()
client = _Client(connection)
klass = self._get_target_class()
bucket = klass(client, name=NAME, user_project=USER_PROJECT)
bucket = self._make_one(client, name=NAME, user_project=USER_PROJECT)
self.assertEqual(bucket.name, NAME)
self.assertEqual(bucket._properties, {})
self.assertEqual(bucket.user_project, USER_PROJECT)
Expand Down Expand Up @@ -163,11 +167,22 @@ def api_request(cls, *args, **kwargs):
expected_cw = [((), expected_called_kwargs)]
self.assertEqual(_FakeConnection._called_with, expected_cw)

def test_create_w_user_project(self):
PROJECT = 'PROJECT'
BUCKET_NAME = 'bucket-name'
USER_PROJECT = 'user-project-123'
connection = _Connection()
client = _Client(connection, project=PROJECT)
bucket = self._make_one(client, BUCKET_NAME, user_project=USER_PROJECT)

with self.assertRaises(ValueError):
bucket.create()

def test_create_hit(self):
PROJECT = 'PROJECT'
BUCKET_NAME = 'bucket-name'
DATA = {'name': BUCKET_NAME}
connection = _Connection(DATA)
PROJECT = 'PROJECT'
client = _Client(connection, project=PROJECT)
bucket = self._make_one(client=client, name=BUCKET_NAME)
bucket.create()
Expand Down