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

STORAGE: Adding default project in get_connection. #699

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions gcloud/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ def set_default_connection(project=None, connection=None):
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: A connection provided to be the default.
"""
if project is None:
project = get_default_project()

connection = connection or get_connection(project)
_implicit_environ._DEFAULTS.connection = connection

Expand Down Expand Up @@ -141,7 +138,7 @@ def set_defaults(bucket=None, project=None, connection=None):
set_default_bucket(bucket=bucket)


def get_connection(project):
def get_connection(project=None):
"""Shortcut method to establish a connection to Cloud Storage.
Use this if you are going to access several buckets with the same
Expand All @@ -152,12 +149,15 @@ def get_connection(project):
>>> bucket1 = connection.get_bucket('bucket1')
>>> bucket2 = connection.get_bucket('bucket2')
:type project: string
:param project: The name of the project to connect to.
: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)
Expand Down
20 changes: 19 additions & 1 deletion gcloud/storage/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ def test_it(self):
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)


class Test_get_bucket(unittest2.TestCase):

Expand Down Expand Up @@ -309,7 +327,7 @@ def mock_get_connection(*args, **kwargs):

self.assertEqual(_implicit_environ.get_default_connection(),
fake_cnxn)
self.assertEqual(_called_args, [(PROJECT,)])
self.assertEqual(_called_args, [(None,)])
self.assertEqual(_called_kwargs, [{}])

def test_set_implicit_with_explicit_project(self):
Expand Down