From b91b2af678b4ef684fafe592346b9d054e16b618 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Sat, 14 Mar 2015 00:10:46 -0600 Subject: [PATCH] STORAGE: Removing project from Connection. Only needed for create_bucket() and get_all_buckets(). Fixes #726. --- docs/_components/storage-getting-started.rst | 4 +- gcloud/storage/__init__.py | 23 ++--- gcloud/storage/acl.py | 2 +- gcloud/storage/api.py | 28 ++++-- gcloud/storage/batch.py | 2 +- gcloud/storage/bucket.py | 14 +-- gcloud/storage/connection.py | 28 ++---- gcloud/storage/demo/__init__.py | 11 ++- gcloud/storage/demo/demo.py | 8 +- gcloud/storage/test___init__.py | 89 ++------------------ gcloud/storage/test_api.py | 63 +++++++------- gcloud/storage/test_batch.py | 2 - gcloud/storage/test_connection.py | 66 +++++---------- 13 files changed, 119 insertions(+), 221 deletions(-) diff --git a/docs/_components/storage-getting-started.rst b/docs/_components/storage-getting-started.rst index 0cf44bf793a0..ffc22489b874 100644 --- a/docs/_components/storage-getting-started.rst +++ b/docs/_components/storage-getting-started.rst @@ -38,7 +38,7 @@ The first step in accessing Cloud Storage is to create a connection to the service:: >>> from gcloud import storage - >>> connection = storage.get_connection(project_name) + >>> connection = storage.get_connection() We're going to use this :class:`connection ` object for the rest of this guide. @@ -56,7 +56,7 @@ bucket. Let's create a bucket: - >>> bucket = storage.create_bucket('test', connection=connection) + >>> bucket = storage.create_bucket('test', project_name, connection=connection) Traceback (most recent call last): File "", line 1, in File "gcloud/storage/connection.py", line 340, in create_bucket diff --git a/gcloud/storage/__init__.py b/gcloud/storage/__init__.py index ff30ba717dfe..1cb6af842a4e 100644 --- a/gcloud/storage/__init__.py +++ b/gcloud/storage/__init__.py @@ -107,16 +107,13 @@ def set_default_project(project=None): _implicit_environ._DEFAULTS.project = project -def set_default_connection(project=None, connection=None): +def set_default_connection(connection=None): """Set default connection either explicitly or implicitly as fall-back. - :type project: string - :param project: Optional. The name of the project to connect to. - :type connection: :class:`gcloud.storage.connection.Connection` :param connection: A connection provided to be the default. """ - connection = connection or get_connection(project) + connection = connection or get_connection() _implicit_environ._DEFAULTS.connection = connection @@ -134,35 +131,27 @@ def set_defaults(bucket=None, project=None, connection=None): :type connection: :class:`gcloud.storage.connection.Connection` :param connection: Optional. A connection provided to be the default. """ - # NOTE: `set_default_project` is called before `set_default_connection` - # since `set_default_connection` falls back to implicit project. set_default_project(project=project) - set_default_connection(project=project, connection=connection) + set_default_connection(connection=connection) # NOTE: `set_default_bucket` is called after `set_default_connection` # since `set_default_bucket` falls back to implicit connection. set_default_bucket(bucket=bucket) -def get_connection(project=None): +def get_connection(): """Shortcut method to establish a connection to Cloud Storage. Use this if you are going to access several buckets with the same set of credentials: >>> from gcloud import storage - >>> connection = storage.get_connection(project) + >>> connection = storage.get_connection() >>> bucket1 = storage.get_bucket('bucket1', connection=connection) >>> bucket2 = storage.get_bucket('bucket2', connection=connection) - :type project: string or ``NoneType`` - :param project: Optional. The name of the project to connect to. If not - included, falls back to default project. - :rtype: :class:`gcloud.storage.connection.Connection` :returns: A connection defined with the proper credentials. """ - if project is None: - project = get_default_project() implicit_credentials = credentials.get_credentials() scoped_credentials = implicit_credentials.create_scoped(SCOPE) - return Connection(project=project, credentials=scoped_credentials) + return Connection(credentials=scoped_credentials) diff --git a/gcloud/storage/acl.py b/gcloud/storage/acl.py index 656eb08ed433..48b971e6d8b7 100644 --- a/gcloud/storage/acl.py +++ b/gcloud/storage/acl.py @@ -19,7 +19,7 @@ :func:`gcloud.storage.bucket.Bucket.acl`:: >>> from gcloud import storage - >>> connection = storage.get_connection(project) + >>> connection = storage.get_connection() >>> bucket = storage.get_bucket(bucket_name, connection=connection) >>> acl = bucket.acl diff --git a/gcloud/storage/api.py b/gcloud/storage/api.py index 169efb541a92..268dc641b428 100644 --- a/gcloud/storage/api.py +++ b/gcloud/storage/api.py @@ -20,6 +20,7 @@ from gcloud.exceptions import NotFound from gcloud.storage._implicit_environ import get_default_connection +from gcloud.storage._implicit_environ import get_default_project from gcloud.storage.bucket import Bucket from gcloud.storage.iterator import Iterator @@ -59,7 +60,7 @@ def lookup_bucket(bucket_name, connection=None): return None -def get_all_buckets(connection=None): +def get_all_buckets(project=None, connection=None): """Get all buckets in the project. This will not populate the list of blobs available in each @@ -71,6 +72,10 @@ def get_all_buckets(connection=None): This implements "storage.buckets.list". + :type project: string + :param project: Optional. The project to use when listing all buckets. + 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. @@ -81,7 +86,11 @@ def get_all_buckets(connection=None): """ if connection is None: connection = get_default_connection() - return iter(_BucketIterator(connection=connection)) + if project is None: + project = get_default_project() + extra_params = {'project': project} + return iter(_BucketIterator(connection=connection, + extra_params=extra_params)) def get_bucket(bucket_name, connection=None): @@ -121,7 +130,7 @@ def get_bucket(bucket_name, connection=None): return Bucket(properties=response, connection=connection) -def create_bucket(bucket_name, connection=None): +def create_bucket(bucket_name, project=None, connection=None): """Create a new bucket. For example:: @@ -134,6 +143,10 @@ def create_bucket(bucket_name, connection=None): 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. + :type bucket_name: string :param bucket_name: The bucket name to create. @@ -149,8 +162,12 @@ def create_bucket(bucket_name, connection=None): """ 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}) return Bucket(properties=response, connection=connection) @@ -166,8 +183,9 @@ class _BucketIterator(Iterator): :param connection: The connection to use for querying the list of buckets. """ - def __init__(self, connection): - super(_BucketIterator, self).__init__(connection=connection, path='/b') + def __init__(self, connection, extra_params=None): + super(_BucketIterator, self).__init__(connection=connection, path='/b', + extra_params=extra_params) def get_items_from_response(self, response): """Factory method which yields :class:`.Bucket` items from a response. diff --git a/gcloud/storage/batch.py b/gcloud/storage/batch.py index 2713151308b1..3df45b96a0ad 100644 --- a/gcloud/storage/batch.py +++ b/gcloud/storage/batch.py @@ -83,7 +83,7 @@ def __init__(self, connection=None): if connection is None: connection = _implicit_environ.get_default_connection() - super(Batch, self).__init__(project=connection.project) + super(Batch, self).__init__() self._connection = connection self._requests = [] self._responses = [] diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index c14d8e1fae86..993918004ce6 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -169,7 +169,7 @@ def get_blob(self, blob_name): This will return None if the blob doesn't exist:: >>> from gcloud import storage - >>> connection = storage.get_connection(project) + >>> connection = storage.get_connection() >>> bucket = storage.get_bucket('my-bucket', connection=connection) >>> print bucket.get_blob('/path/to/blob.txt') @@ -290,7 +290,7 @@ def delete_blob(self, blob_name): >>> from gcloud.exceptions import NotFound >>> from gcloud import storage - >>> connection = storage.get_connection(project) + >>> connection = storage.get_connection() >>> bucket = storage.get_bucket('my-bucket', connection=connection) >>> print bucket.get_all_blobs() [] @@ -371,7 +371,7 @@ def upload_file(self, filename, blob_name=None): For example:: >>> from gcloud import storage - >>> connection = storage.get_connection(project) + >>> connection = storage.get_connection() >>> bucket = storage.get_bucket('my-bucket', connection=connection) >>> bucket.upload_file('~/my-file.txt', 'remote-text-file.txt') >>> print bucket.get_all_blobs() @@ -381,7 +381,7 @@ def upload_file(self, filename, blob_name=None): using the local filename (**not** the complete path):: >>> from gcloud import storage - >>> connection = storage.get_connection(project) + >>> connection = storage.get_connection() >>> bucket = storage.get_bucket('my-bucket', connection=connection) >>> bucket.upload_file('~/my-file.txt') >>> print bucket.get_all_blobs() @@ -413,7 +413,7 @@ def upload_file_object(self, file_obj, blob_name=None): For example:: >>> from gcloud import storage - >>> connection = storage.get_connection(project) + >>> connection = storage.get_connection() >>> bucket = storage.get_bucket('my-bucket', connection=connection) >>> bucket.upload_file(open('~/my-file.txt'), 'remote-text-file.txt') >>> print bucket.get_all_blobs() @@ -423,7 +423,7 @@ def upload_file_object(self, file_obj, blob_name=None): using the local filename (**not** the complete path):: >>> from gcloud import storage - >>> connection = storage.get_connection(project) + >>> connection = storage.get_connection() >>> bucket = storage.get_bucket('my-bucket', connection=connection) >>> bucket.upload_file(open('~/my-file.txt')) >>> print bucket.get_all_blobs() @@ -677,7 +677,7 @@ def configure_website(self, main_page_suffix=None, not_found_page=None): of an index page and a page to use when a blob isn't found:: >>> from gcloud import storage - >>> connection = storage.get_connection(project) + >>> connection = storage.get_connection() >>> bucket = storage.get_bucket(bucket_name, connection=connection) >>> bucket.configure_website('index.html', '404.html') diff --git a/gcloud/storage/connection.py b/gcloud/storage/connection.py index 036e0663bf0a..702bb8f26cee 100644 --- a/gcloud/storage/connection.py +++ b/gcloud/storage/connection.py @@ -30,11 +30,6 @@ class Connection(base_connection.Connection): :mod:`gcloud.storage.api` and :class:`gcloud.storage.bucket.Bucket` and :class:`gcloud.storage.blob.Blob`). - - See :class:`gcloud.connection.Connection` for a full list of - parameters. This subclass differs only in needing a project - name (which you specify when creating a project in the Cloud - Console). """ API_BASE_URL = base_connection.API_BASE_URL @@ -46,15 +41,8 @@ class Connection(base_connection.Connection): API_URL_TEMPLATE = '{api_base_url}/storage/{api_version}{path}' """A template for the URL of a particular API call.""" - def __init__(self, project, *args, **kwargs): - """:type project: string - - :param project: The project name to connect to. - """ - super(Connection, self).__init__(*args, **kwargs) - self.project = project - - def build_api_url(self, path, query_params=None, api_base_url=None, + @classmethod + def build_api_url(cls, path, query_params=None, api_base_url=None, api_version=None, upload=False): """Construct an API url given a few components, some optional. @@ -82,18 +70,18 @@ def build_api_url(self, path, query_params=None, api_base_url=None, :rtype: string :returns: The URL assembled from the pieces provided. """ - api_base_url = api_base_url or self.API_BASE_URL + api_base_url = api_base_url or cls.API_BASE_URL if upload: api_base_url += '/upload' - url = self.API_URL_TEMPLATE.format( - api_base_url=(api_base_url or self.API_BASE_URL), - api_version=(api_version or self.API_VERSION), + url = cls.API_URL_TEMPLATE.format( + api_base_url=(api_base_url or cls.API_BASE_URL), + api_version=(api_version or cls.API_VERSION), path=path) query_params = query_params or {} - query_params.update({'project': self.project}) - url += '?' + urlencode(query_params) + if query_params: + url += '?' + urlencode(query_params) return url diff --git a/gcloud/storage/demo/__init__.py b/gcloud/storage/demo/__init__.py index c007b779e26b..26e949ba8c45 100644 --- a/gcloud/storage/demo/__init__.py +++ b/gcloud/storage/demo/__init__.py @@ -15,10 +15,15 @@ import os from gcloud import storage -__all__ = ['get_connection', 'PROJECT_ID'] +__all__ = ['create_bucket', 'get_all_buckets', 'PROJECT_ID'] PROJECT_ID = os.getenv('GCLOUD_TESTS_PROJECT_ID') -def get_connection(): - return storage.get_connection(PROJECT_ID) +def get_all_buckets(connection): + return list(storage.get_all_buckets(PROJECT_ID, connection)) + + +def create_bucket(bucket_name, connection): + return storage.create_bucket(bucket_name, PROJECT_ID, + connection=connection) diff --git a/gcloud/storage/demo/demo.py b/gcloud/storage/demo/demo.py index de6795deca38..2923cfb1423e 100644 --- a/gcloud/storage/demo/demo.py +++ b/gcloud/storage/demo/demo.py @@ -9,19 +9,19 @@ from gcloud import storage from gcloud.storage import demo -connection = demo.get_connection() +connection = storage.get_connection() # OK, now let's look at all of the buckets... -print(list(storage.get_all_buckets(connection))) # This might take a second... +print(demo.get_all_buckets(connection)) # This might take a second... # Now let's create a new bucket... bucket_name = ("bucket-%s" % time.time()).replace(".", "") # Get rid of dots. print(bucket_name) -bucket = storage.create_bucket(bucket_name, connection=connection) +bucket = demo.create_bucket(bucket_name, connection) print(bucket) # Let's look at all of the buckets again... -print(list(storage.get_all_buckets(connection))) +print(demo.get_all_buckets(connection)) # How about we create a new blob inside this bucket. blob = storage.Blob("my-new-file.txt", bucket=bucket) diff --git a/gcloud/storage/test___init__.py b/gcloud/storage/test___init__.py index b6e8976b6d53..083291b0dd9d 100644 --- a/gcloud/storage/test___init__.py +++ b/gcloud/storage/test___init__.py @@ -26,30 +26,10 @@ def test_it(self): from gcloud.storage.connection import Connection from gcloud.test_credentials import _Client from gcloud._testing import _Monkey - PROJECT = 'project' client = _Client() with _Monkey(credentials, client=client): - found = self._callFUT(PROJECT) + found = self._callFUT() self.assertTrue(isinstance(found, Connection)) - self.assertEqual(found.project, PROJECT) - self.assertTrue(found._credentials is client._signed) - self.assertTrue(client._get_app_default_called) - - def test_default_project(self): - from gcloud import credentials - from gcloud.storage._testing import _monkey_defaults - from gcloud.storage.connection import Connection - from gcloud.test_credentials import _Client - from gcloud._testing import _Monkey - - PROJECT = 'project' - client = _Client() - with _Monkey(credentials, client=client): - with _monkey_defaults(project=PROJECT): - found = self._callFUT() - - self.assertTrue(isinstance(found, Connection)) - self.assertEqual(found.project, PROJECT) self.assertTrue(found._credentials is client._signed) self.assertTrue(client._get_app_default_called) @@ -232,9 +212,9 @@ def tearDown(self): from gcloud.storage._testing import _tear_down_defaults _tear_down_defaults(self) - def _callFUT(self, project=None, connection=None): + def _callFUT(self, connection=None): from gcloud.storage import set_default_connection - return set_default_connection(project=project, connection=connection) + return set_default_connection(connection=connection) def test_set_explicit(self): from gcloud.storage import _implicit_environ @@ -244,7 +224,7 @@ def test_set_explicit(self): self._callFUT(connection=fake_cnxn) self.assertEqual(_implicit_environ.get_default_connection(), fake_cnxn) - def test_set_implicit_no_project(self): + def test_set_implicit(self): from gcloud._testing import _Monkey from gcloud import storage from gcloud.storage import _implicit_environ @@ -264,60 +244,7 @@ def mock_get_connection(*args, **kwargs): self._callFUT() self.assertEqual(_implicit_environ.get_default_connection(), fake_cnxn) - self.assertEqual(_called_args, [(None,)]) - self.assertEqual(_called_kwargs, [{}]) - - def test_set_implicit_with_implicit_project(self): - from gcloud._testing import _Monkey - from gcloud.storage._testing import _monkey_defaults - from gcloud import storage - from gcloud.storage import _implicit_environ - - self.assertEqual(_implicit_environ.get_default_connection(), None) - - fake_cnxn = object() - _called_args = [] - _called_kwargs = [] - - def mock_get_connection(*args, **kwargs): - _called_args.append(args) - _called_kwargs.append(kwargs) - return fake_cnxn - - PROJECT = 'project' - - with _monkey_defaults(project=PROJECT): - with _Monkey(storage, get_connection=mock_get_connection): - self._callFUT() - - self.assertEqual(_implicit_environ.get_default_connection(), - fake_cnxn) - self.assertEqual(_called_args, [(None,)]) - self.assertEqual(_called_kwargs, [{}]) - - def test_set_implicit_with_explicit_project(self): - from gcloud._testing import _Monkey - from gcloud import storage - from gcloud.storage import _implicit_environ - - self.assertEqual(_implicit_environ.get_default_connection(), None) - - fake_cnxn = object() - _called_args = [] - _called_kwargs = [] - - def mock_get_connection(*args, **kwargs): - _called_args.append(args) - _called_kwargs.append(kwargs) - return fake_cnxn - - PROJECT = 'project' - - with _Monkey(storage, get_connection=mock_get_connection): - self._callFUT(PROJECT) - - self.assertEqual(_implicit_environ.get_default_connection(), fake_cnxn) - self.assertEqual(_called_args, [(PROJECT,)]) + self.assertEqual(_called_args, [()]) self.assertEqual(_called_kwargs, [{}]) @@ -348,8 +275,8 @@ def call_set_project(project=None): SET_CONNECTION_CALLED = [] - def call_set_connection(project=None, connection=None): - SET_CONNECTION_CALLED.append((project, connection)) + def call_set_connection(connection=None): + SET_CONNECTION_CALLED.append(connection) with _Monkey(storage, set_default_bucket=call_set_bucket, set_default_connection=call_set_connection, @@ -358,5 +285,5 @@ def call_set_connection(project=None, connection=None): connection=CONNECTION) self.assertEqual(SET_PROJECT_CALLED, [PROJECT]) - self.assertEqual(SET_CONNECTION_CALLED, [(PROJECT, CONNECTION)]) + self.assertEqual(SET_CONNECTION_CALLED, [CONNECTION]) self.assertEqual(SET_BUCKET_CALLED, [BUCKET]) diff --git a/gcloud/storage/test_api.py b/gcloud/storage/test_api.py index e09f0d51b1fe..03ba6c610e00 100644 --- a/gcloud/storage/test_api.py +++ b/gcloud/storage/test_api.py @@ -23,15 +23,14 @@ def _callFUT(self, bucket_name, connection=None): def test_miss(self): from gcloud.storage.connection import Connection - PROJECT = 'project' NONESUCH = 'nonesuch' - conn = Connection(PROJECT) + conn = Connection() URI = '/'.join([ conn.API_BASE_URL, 'storage', conn.API_VERSION, 'b', - 'nonesuch?project=%s' % PROJECT, + 'nonesuch', ]) http = conn._http = Http( {'status': '404', 'content-type': 'application/json'}, @@ -46,15 +45,14 @@ def _lookup_bucket_hit_helper(self, use_default=False): from gcloud.storage._testing import _monkey_defaults from gcloud.storage.bucket import Bucket from gcloud.storage.connection import Connection - PROJECT = 'project' BLOB_NAME = 'blob-name' - conn = Connection(PROJECT) + conn = Connection() URI = '/'.join([ conn.API_BASE_URL, 'storage', conn.API_VERSION, 'b', - '%s?project=%s' % (BLOB_NAME, PROJECT), + '%s' % (BLOB_NAME,), ]) http = conn._http = Http( {'status': '200', 'content-type': 'application/json'}, @@ -82,14 +80,14 @@ def test_use_default(self): class Test_get_all_buckets(unittest2.TestCase): - def _callFUT(self, connection=None): + def _callFUT(self, project=None, connection=None): from gcloud.storage.api import get_all_buckets - return get_all_buckets(connection=connection) + return get_all_buckets(project=project, connection=connection) def test_empty(self): from gcloud.storage.connection import Connection PROJECT = 'project' - conn = Connection(PROJECT) + conn = Connection() URI = '/'.join([ conn.API_BASE_URL, 'storage', @@ -100,22 +98,21 @@ def test_empty(self): {'status': '200', 'content-type': 'application/json'}, '{}', ) - buckets = list(self._callFUT(conn)) + buckets = list(self._callFUT(PROJECT, conn)) self.assertEqual(len(buckets), 0) self.assertEqual(http._called_with['method'], 'GET') self.assertEqual(http._called_with['uri'], URI) - def _get_all_buckets_non_empty_helper(self, use_default=False): + def _get_all_buckets_non_empty_helper(self, project, use_default=False): from gcloud.storage._testing import _monkey_defaults from gcloud.storage.connection import Connection - PROJECT = 'project' BUCKET_NAME = 'bucket-name' - conn = Connection(PROJECT) + conn = Connection() URI = '/'.join([ conn.API_BASE_URL, 'storage', conn.API_VERSION, - 'b?project=%s' % PROJECT, + 'b?project=%s' % project, ]) http = conn._http = Http( {'status': '200', 'content-type': 'application/json'}, @@ -123,10 +120,10 @@ def _get_all_buckets_non_empty_helper(self, use_default=False): ) if use_default: - with _monkey_defaults(connection=conn): + with _monkey_defaults(project=project, connection=conn): buckets = list(self._callFUT()) else: - buckets = list(self._callFUT(conn)) + buckets = list(self._callFUT(project, conn)) self.assertEqual(len(buckets), 1) self.assertEqual(buckets[0].name, BUCKET_NAME) @@ -134,10 +131,10 @@ def _get_all_buckets_non_empty_helper(self, use_default=False): self.assertEqual(http._called_with['uri'], URI) def test_non_empty(self): - self._get_all_buckets_non_empty_helper(use_default=False) + self._get_all_buckets_non_empty_helper('PROJECT', use_default=False) def test_non_use_default(self): - self._get_all_buckets_non_empty_helper(use_default=True) + self._get_all_buckets_non_empty_helper('PROJECT', use_default=True) class Test_get_bucket(unittest2.TestCase): @@ -149,15 +146,14 @@ def _callFUT(self, bucket_name, connection=None): def test_miss(self): from gcloud.exceptions import NotFound from gcloud.storage.connection import Connection - PROJECT = 'project' NONESUCH = 'nonesuch' - conn = Connection(PROJECT) + conn = Connection() URI = '/'.join([ conn.API_BASE_URL, 'storage', conn.API_VERSION, 'b', - 'nonesuch?project=%s' % PROJECT, + 'nonesuch', ]) http = conn._http = Http( {'status': '404', 'content-type': 'application/json'}, @@ -171,15 +167,14 @@ def _get_bucket_hit_helper(self, use_default=False): from gcloud.storage._testing import _monkey_defaults from gcloud.storage.bucket import Bucket from gcloud.storage.connection import Connection - PROJECT = 'project' BLOB_NAME = 'blob-name' - conn = Connection(PROJECT) + conn = Connection() URI = '/'.join([ conn.API_BASE_URL, 'storage', conn.API_VERSION, 'b', - '%s?project=%s' % (BLOB_NAME, PROJECT), + '%s' % (BLOB_NAME,), ]) http = conn._http = Http( {'status': '200', 'content-type': 'application/json'}, @@ -207,22 +202,22 @@ def test_hit_use_default(self): class Test_create_bucket(unittest2.TestCase): - def _callFUT(self, bucket_name, connection=None): + def _callFUT(self, bucket_name, project=None, connection=None): from gcloud.storage.api import create_bucket - return create_bucket(bucket_name, connection=connection) + return create_bucket(bucket_name, project=project, + connection=connection) - def _create_bucket_success_helper(self, use_default=False): + def _create_bucket_success_helper(self, project, use_default=False): from gcloud.storage._testing import _monkey_defaults from gcloud.storage.connection import Connection from gcloud.storage.bucket import Bucket - PROJECT = 'project' BLOB_NAME = 'blob-name' - conn = Connection(PROJECT) + conn = Connection() URI = '/'.join([ conn.API_BASE_URL, 'storage', conn.API_VERSION, - 'b?project=%s' % PROJECT, + 'b?project=%s' % project, ]) http = conn._http = Http( {'status': '200', 'content-type': 'application/json'}, @@ -230,10 +225,10 @@ def _create_bucket_success_helper(self, use_default=False): ) if use_default: - with _monkey_defaults(connection=conn): + with _monkey_defaults(project=project, connection=conn): bucket = self._callFUT(BLOB_NAME) else: - bucket = self._callFUT(BLOB_NAME, connection=conn) + bucket = self._callFUT(BLOB_NAME, project=project, connection=conn) self.assertTrue(isinstance(bucket, Bucket)) self.assertTrue(bucket.connection is conn) @@ -242,10 +237,10 @@ def _create_bucket_success_helper(self, use_default=False): self.assertEqual(http._called_with['uri'], URI) def test_success(self): - self._create_bucket_success_helper(use_default=False) + self._create_bucket_success_helper('PROJECT', use_default=False) def test_success_use_default(self): - self._create_bucket_success_helper(use_default=True) + self._create_bucket_success_helper('PROJECT', use_default=True) class Test__BucketIterator(unittest2.TestCase): diff --git a/gcloud/storage/test_batch.py b/gcloud/storage/test_batch.py index 700a0aec0f52..2f7569f45f83 100644 --- a/gcloud/storage/test_batch.py +++ b/gcloud/storage/test_batch.py @@ -88,7 +88,6 @@ def test_ctor_w_explicit_connection(self): connection = _Connection(http=http) batch = self._makeOne(connection) self.assertTrue(batch._connection is connection) - self.assertEqual(batch.project, connection.project) self.assertEqual(len(batch._requests), 0) self.assertEqual(len(batch._responses), 0) @@ -101,7 +100,6 @@ def test_ctor_w_implicit_connection(self): batch = self._makeOne() self.assertTrue(batch._connection is connection) - self.assertEqual(batch.project, connection.project) self.assertEqual(len(batch._requests), 0) self.assertEqual(len(batch._responses), 0) diff --git a/gcloud/storage/test_connection.py b/gcloud/storage/test_connection.py index fc5aa6615f52..69785870715a 100644 --- a/gcloud/storage/test_connection.py +++ b/gcloud/storage/test_connection.py @@ -25,33 +25,26 @@ def _makeOne(self, *args, **kw): return self._getTargetClass()(*args, **kw) def test_ctor_defaults(self): - PROJECT = 'project' - conn = self._makeOne(PROJECT) - self.assertEqual(conn.project, PROJECT) + conn = self._makeOne() self.assertEqual(conn.credentials, None) def test_ctor_explicit(self): - PROJECT = 'project' creds = object() - conn = self._makeOne(PROJECT, creds) - self.assertEqual(conn.project, PROJECT) + conn = self._makeOne(creds) self.assertTrue(conn.credentials is creds) def test_http_w_existing(self): - PROJECT = 'project' - conn = self._makeOne(PROJECT) + conn = self._makeOne() conn._http = http = object() self.assertTrue(conn.http is http) def test_http_wo_creds(self): import httplib2 - PROJECT = 'project' - conn = self._makeOne(PROJECT) + conn = self._makeOne() self.assertTrue(isinstance(conn.http, httplib2.Http)) def test_http_w_creds(self): import httplib2 - PROJECT = 'project' authorized = object() class Creds(object): @@ -59,50 +52,45 @@ def authorize(self, http): self._called_with = http return authorized creds = Creds() - conn = self._makeOne(PROJECT, creds) + conn = self._makeOne(creds) self.assertTrue(conn.http is authorized) self.assertTrue(isinstance(creds._called_with, httplib2.Http)) def test_build_api_url_no_extra_query_params(self): - PROJECT = 'project' - conn = self._makeOne(PROJECT) + conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'storage', conn.API_VERSION, - 'foo?project=%s' % PROJECT, + 'foo', ]) self.assertEqual(conn.build_api_url('/foo'), URI) def test_build_api_url_w_extra_query_params(self): from six.moves.urllib.parse import parse_qsl from six.moves.urllib.parse import urlsplit - PROJECT = 'project' - conn = self._makeOne(PROJECT) + conn = self._makeOne() uri = conn.build_api_url('/foo', {'bar': 'baz'}) scheme, netloc, path, qs, _ = urlsplit(uri) self.assertEqual('%s://%s' % (scheme, netloc), conn.API_BASE_URL) self.assertEqual(path, '/'.join(['', 'storage', conn.API_VERSION, 'foo'])) parms = dict(parse_qsl(qs)) - self.assertEqual(parms['project'], PROJECT) self.assertEqual(parms['bar'], 'baz') def test_build_api_url_w_upload(self): - PROJECT = 'project' - conn = self._makeOne(PROJECT) + conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'upload', 'storage', conn.API_VERSION, - 'foo?project=%s' % PROJECT, + 'foo', ]) self.assertEqual(conn.build_api_url('/foo', upload=True), URI) def test__make_request_no_data_no_content_type_no_headers(self): - PROJECT = 'project' - conn = self._makeOne(PROJECT) + conn = self._makeOne() URI = 'http://example.com/test' http = conn._http = Http( {'status': '200', 'content-type': 'text/plain'}, @@ -123,8 +111,7 @@ def test__make_request_no_data_no_content_type_no_headers(self): self.assertEqual(http._called_with['headers'], expected_headers) def test__make_request_w_data_no_extra_headers(self): - PROJECT = 'project' - conn = self._makeOne(PROJECT) + conn = self._makeOne() URI = 'http://example.com/test' http = conn._http = Http( {'status': '200', 'content-type': 'text/plain'}, @@ -143,8 +130,7 @@ def test__make_request_w_data_no_extra_headers(self): self.assertEqual(http._called_with['headers'], expected_headers) def test__make_request_w_extra_headers(self): - PROJECT = 'project' - conn = self._makeOne(PROJECT) + conn = self._makeOne() URI = 'http://example.com/test' http = conn._http = Http( {'status': '200', 'content-type': 'text/plain'}, @@ -163,13 +149,12 @@ def test__make_request_w_extra_headers(self): self.assertEqual(http._called_with['headers'], expected_headers) def test_api_request_defaults(self): - PROJECT = 'project' PATH = '/path/required' - conn = self._makeOne(PROJECT) + conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'storage', - '%s%s?project=%s' % (conn.API_VERSION, PATH, PROJECT), + '%s%s' % (conn.API_VERSION, PATH), ]) http = conn._http = Http( {'status': '200', 'content-type': 'application/json'}, @@ -187,8 +172,7 @@ def test_api_request_defaults(self): self.assertEqual(http._called_with['headers'], expected_headers) def test_api_request_w_non_json_response(self): - PROJECT = 'project' - conn = self._makeOne(PROJECT) + conn = self._makeOne() conn._http = Http( {'status': '200', 'content-type': 'text/plain'}, 'CONTENT', @@ -197,8 +181,7 @@ def test_api_request_w_non_json_response(self): self.assertRaises(TypeError, conn.api_request, 'GET', '/') def test_api_request_wo_json_expected(self): - PROJECT = 'project' - conn = self._makeOne(PROJECT) + conn = self._makeOne() conn._http = Http( {'status': '200', 'content-type': 'text/plain'}, 'CONTENT', @@ -209,8 +192,7 @@ def test_api_request_wo_json_expected(self): def test_api_request_w_query_params(self): from six.moves.urllib.parse import parse_qsl from six.moves.urllib.parse import urlsplit - PROJECT = 'project' - conn = self._makeOne(PROJECT) + conn = self._makeOne() http = conn._http = Http( {'status': '200', 'content-type': 'application/json'}, '{}', @@ -223,7 +205,6 @@ def test_api_request_w_query_params(self): self.assertEqual(path, '/'.join(['', 'storage', conn.API_VERSION, ''])) parms = dict(parse_qsl(qs)) - self.assertEqual(parms['project'], PROJECT) self.assertEqual(parms['foo'], 'bar') self.assertEqual(http._called_with['body'], None) expected_headers = { @@ -235,15 +216,14 @@ def test_api_request_w_query_params(self): def test_api_request_w_data(self): import json - PROJECT = 'project' DATA = {'foo': 'bar'} DATAJ = json.dumps(DATA) - conn = self._makeOne(PROJECT) + conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'storage', conn.API_VERSION, - '?project=%s' % PROJECT, + '', ]) http = conn._http = Http( {'status': '200', 'content-type': 'application/json'}, @@ -263,8 +243,7 @@ def test_api_request_w_data(self): def test_api_request_w_404(self): from gcloud.exceptions import NotFound - PROJECT = 'project' - conn = self._makeOne(PROJECT) + conn = self._makeOne() conn._http = Http( {'status': '404', 'content-type': 'text/plain'}, '{}' @@ -273,8 +252,7 @@ def test_api_request_w_404(self): def test_api_request_w_500(self): from gcloud.exceptions import InternalServerError - PROJECT = 'project' - conn = self._makeOne(PROJECT) + conn = self._makeOne() conn._http = Http( {'status': '500', 'content-type': 'text/plain'}, '{}',