Skip to content

Commit 19bdb13

Browse files
committed
Merge pull request #699 from dhermes/storage-default-project-in-connection
STORAGE: Adding default project in get_connection.
2 parents fb35d7a + da6ad6c commit 19bdb13

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

gcloud/storage/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ def set_default_connection(project=None, connection=None):
111111
:type connection: :class:`gcloud.storage.connection.Connection`
112112
:param connection: A connection provided to be the default.
113113
"""
114-
if project is None:
115-
project = get_default_project()
116-
117114
connection = connection or get_connection(project)
118115
_implicit_environ._DEFAULTS.connection = connection
119116

@@ -141,7 +138,7 @@ def set_defaults(bucket=None, project=None, connection=None):
141138
set_default_bucket(bucket=bucket)
142139

143140

144-
def get_connection(project):
141+
def get_connection(project=None):
145142
"""Shortcut method to establish a connection to Cloud Storage.
146143
147144
Use this if you are going to access several buckets with the same
@@ -152,12 +149,15 @@ def get_connection(project):
152149
>>> bucket1 = connection.get_bucket('bucket1')
153150
>>> bucket2 = connection.get_bucket('bucket2')
154151
155-
:type project: string
156-
:param project: The name of the project to connect to.
152+
:type project: string or ``NoneType``
153+
:param project: Optional. The name of the project to connect to. If not
154+
included, falls back to default project.
157155
158156
:rtype: :class:`gcloud.storage.connection.Connection`
159157
:returns: A connection defined with the proper credentials.
160158
"""
159+
if project is None:
160+
project = get_default_project()
161161
implicit_credentials = credentials.get_credentials()
162162
scoped_credentials = implicit_credentials.create_scoped(SCOPE)
163163
return Connection(project=project, credentials=scoped_credentials)

gcloud/storage/test___init__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ def test_it(self):
3535
self.assertTrue(found._credentials is client._signed)
3636
self.assertTrue(client._get_app_default_called)
3737

38+
def test_default_project(self):
39+
from gcloud import credentials
40+
from gcloud.storage._testing import _monkey_defaults
41+
from gcloud.storage.connection import Connection
42+
from gcloud.test_credentials import _Client
43+
from gcloud._testing import _Monkey
44+
45+
PROJECT = 'project'
46+
client = _Client()
47+
with _Monkey(credentials, client=client):
48+
with _monkey_defaults(project=PROJECT):
49+
found = self._callFUT()
50+
51+
self.assertTrue(isinstance(found, Connection))
52+
self.assertEqual(found.project, PROJECT)
53+
self.assertTrue(found._credentials is client._signed)
54+
self.assertTrue(client._get_app_default_called)
55+
3856

3957
class Test_get_bucket(unittest2.TestCase):
4058

@@ -309,7 +327,7 @@ def mock_get_connection(*args, **kwargs):
309327

310328
self.assertEqual(_implicit_environ.get_default_connection(),
311329
fake_cnxn)
312-
self.assertEqual(_called_args, [(PROJECT,)])
330+
self.assertEqual(_called_args, [(None,)])
313331
self.assertEqual(_called_kwargs, [{}])
314332

315333
def test_set_implicit_with_explicit_project(self):

0 commit comments

Comments
 (0)