diff --git a/gcloud/connection.py b/gcloud/connection.py index c56a27ff8b8f..5ede41322086 100644 --- a/gcloud/connection.py +++ b/gcloud/connection.py @@ -20,6 +20,7 @@ import httplib2 +from gcloud.credentials import get_credentials from gcloud.exceptions import make_exception @@ -297,3 +298,20 @@ def api_request(self, method, path, query_params=None, return json.loads(content) return content + + +def get_scoped_connection(klass, scopes): + """Create a scoped connection to GCloud. + + :type klass: subclass of :class:`gcloud.connection.Connection` + :param klass: the specific ``Connection`` class to instantiate. + + :type scopes: list of URLs + :param scopes: the effective service auth scopes for the connection. + + :rtype: instance of ``klass`` + :returns: A connection defined with the proper credentials. + """ + implicit_credentials = get_credentials() + scoped_credentials = implicit_credentials.create_scoped(scopes) + return klass(credentials=scoped_credentials) diff --git a/gcloud/datastore/_implicit_environ.py b/gcloud/datastore/_implicit_environ.py index f48619e2dc81..0d18b7540bf0 100644 --- a/gcloud/datastore/_implicit_environ.py +++ b/gcloud/datastore/_implicit_environ.py @@ -23,7 +23,7 @@ from gcloud._helpers import _app_engine_id from gcloud._helpers import _compute_engine_id from gcloud._helpers import _lazy_property_deco -from gcloud import credentials +from gcloud.connection import get_scoped_connection from gcloud.datastore.connection import Connection @@ -126,9 +126,7 @@ def get_connection(): :rtype: :class:`gcloud.datastore.connection.Connection` :returns: A connection defined with the proper credentials. """ - implicit_credentials = credentials.get_credentials() - scoped_credentials = implicit_credentials.create_scoped(SCOPE) - return Connection(credentials=scoped_credentials) + return get_scoped_connection(Connection, SCOPE) def set_default_connection(connection=None): diff --git a/gcloud/datastore/test__implicit_environ.py b/gcloud/datastore/test__implicit_environ.py index 4c41d8eb9d0c..9f2d7018041e 100644 --- a/gcloud/datastore/test__implicit_environ.py +++ b/gcloud/datastore/test__implicit_environ.py @@ -305,6 +305,7 @@ def _callFUT(self): def test_it(self): from gcloud import credentials + from gcloud.datastore._implicit_environ import SCOPE from gcloud.datastore.connection import Connection from gcloud.test_credentials import _Client from gcloud._testing import _Monkey @@ -314,6 +315,7 @@ def test_it(self): found = self._callFUT() self.assertTrue(isinstance(found, Connection)) self.assertTrue(found._credentials is client._signed) + self.assertEqual(found._credentials._scopes, SCOPE) self.assertTrue(client._get_app_default_called) diff --git a/gcloud/storage/__init__.py b/gcloud/storage/__init__.py index 1040466863e2..200d863ea544 100644 --- a/gcloud/storage/__init__.py +++ b/gcloud/storage/__init__.py @@ -43,6 +43,7 @@ from gcloud import credentials from gcloud._helpers import get_default_project from gcloud._helpers import set_default_project +from gcloud.connection import get_scoped_connection from gcloud.storage import _implicit_environ from gcloud.storage._implicit_environ import get_default_bucket from gcloud.storage._implicit_environ import get_default_connection @@ -133,6 +134,4 @@ def get_connection(): :rtype: :class:`gcloud.storage.connection.Connection` :returns: A connection defined with the proper credentials. """ - implicit_credentials = credentials.get_credentials() - scoped_credentials = implicit_credentials.create_scoped(SCOPE) - return Connection(credentials=scoped_credentials) + return get_scoped_connection(Connection, SCOPE) diff --git a/gcloud/storage/test___init__.py b/gcloud/storage/test___init__.py index d4a1e3dfca8e..86127b6b5117 100644 --- a/gcloud/storage/test___init__.py +++ b/gcloud/storage/test___init__.py @@ -23,6 +23,7 @@ def _callFUT(self, *args, **kw): def test_it(self): from gcloud import credentials + from gcloud.storage import SCOPE from gcloud.storage.connection import Connection from gcloud.test_credentials import _Client from gcloud._testing import _Monkey @@ -31,6 +32,7 @@ def test_it(self): found = self._callFUT() self.assertTrue(isinstance(found, Connection)) self.assertTrue(found._credentials is client._signed) + self.assertEqual(found._credentials._scopes, SCOPE) self.assertTrue(client._get_app_default_called) diff --git a/gcloud/test_connection.py b/gcloud/test_connection.py index 2cdda517eca8..8b6261ba3725 100644 --- a/gcloud/test_connection.py +++ b/gcloud/test_connection.py @@ -347,3 +347,31 @@ def __init__(self, headers, content): def request(self, **kw): self._called_with = kw return self._response, self._content + + +class Test_get_scoped_connection(unittest2.TestCase): + + def _callFUT(self, klass, scopes): + from gcloud.connection import get_scoped_connection + return get_scoped_connection(klass, scopes) + + def test_it(self): + from gcloud import credentials + from gcloud.test_credentials import _Client + from gcloud._testing import _Monkey + + class _Connection(object): + def __init__(self, credentials): + self._credentials = credentials + + SCOPES = ('https://www.googleapis.com/auth/example', + 'https://www.googleapis.com/auth/userinfo.email') + + client = _Client() + with _Monkey(credentials, client=client): + found = self._callFUT(_Connection, SCOPES) + + self.assertTrue(isinstance(found, _Connection)) + self.assertTrue(found._credentials is client._signed) + self.assertEqual(found._credentials._scopes, SCOPES) + self.assertTrue(client._get_app_default_called) diff --git a/tox.ini b/tox.ini index 5d767ed9b5b9..b4c53ad25234 100644 --- a/tox.ini +++ b/tox.ini @@ -10,7 +10,7 @@ commands = deps = nose unittest2 - protobuf==3.0.0-alpha-1 + protobuf>=3.0.0-alpha-1 [testenv:cover] basepython = @@ -20,7 +20,7 @@ commands = deps = nose unittest2 - protobuf==3.0.0-alpha-1 + protobuf>=3.0.0-alpha-1 coverage nosexcover @@ -56,7 +56,7 @@ deps = pep8 pylint unittest2 - protobuf==3.0.0-alpha-1 + protobuf>=3.0.0-alpha-1 [testenv:regression] basepython = @@ -65,7 +65,7 @@ commands = {toxinidir}/scripts/run_regression.sh deps = unittest2 - protobuf==3.0.0-alpha-1 + protobuf>=3.0.0-alpha-1 [testenv:regression3] basepython = @@ -77,4 +77,4 @@ deps = # Use a development checkout of oauth2client until a release is made # which fixes https://github.com/google/oauth2client/issues/125 -egit+https://github.com/google/oauth2client.git#egg=oauth2client - protobuf==3.0.0-alpha-1 + protobuf>=3.0.0-alpha-1