diff --git a/core/google/cloud/_helpers.py b/core/google/cloud/_helpers.py index b9dc5a83b1be..f4f6b43b1ce2 100644 --- a/core/google/cloud/_helpers.py +++ b/core/google/cloud/_helpers.py @@ -620,8 +620,8 @@ def __call__(self, unused_context, callback): callback(headers, None) -def make_secure_stub(credentials, user_agent, stub_class, host): - """Makes a secure stub for an RPC service. +def make_secure_channel(credentials, user_agent, host): + """Makes a secure channel for an RPC service. Uses / depends on gRPC. @@ -630,16 +630,13 @@ def make_secure_stub(credentials, user_agent, stub_class, host): access tokens. :type user_agent: str - :param user_agent: (Optional) The user agent to be used with API requests. - - :type stub_class: type - :param stub_class: A gRPC stub type for a given service. + :param user_agent: The user agent to be used with API requests. :type host: str :param host: The host for the service. - :rtype: object, instance of ``stub_class`` - :returns: The stub object used to make gRPC requests to a given API. + :rtype: :class:`grpc._channel.Channel` + :returns: gRPC secure channel with credentials attached. """ # ssl_channel_credentials() loads root certificates from # `grpc/_adapter/credentials/roots.pem`. @@ -653,8 +650,32 @@ def make_secure_stub(credentials, user_agent, stub_class, host): channel_args = ( ('grpc.primary_user_agent', user_agent), ) - channel = grpc.secure_channel(target, channel_creds, - options=channel_args) + return grpc.secure_channel(target, channel_creds, + options=channel_args) + + +def make_secure_stub(credentials, user_agent, stub_class, host): + """Makes a secure stub for an RPC service. + + Uses / depends on gRPC. + + :type credentials: :class:`oauth2client.client.OAuth2Credentials` + :param credentials: The OAuth2 Credentials to use for creating + access tokens. + + :type user_agent: str + :param user_agent: The user agent to be used with API requests. + + :type stub_class: type + :param stub_class: A gRPC stub type for a given service. + + :type host: str + :param host: The host for the service. + + :rtype: object, instance of ``stub_class`` + :returns: The stub object used to make gRPC requests to a given API. + """ + channel = make_secure_channel(credentials, user_agent, host) return stub_class(channel) diff --git a/core/unit_tests/test__helpers.py b/core/unit_tests/test__helpers.py index b1863b8ce5e0..779f9a88abbe 100644 --- a/core/unit_tests/test__helpers.py +++ b/core/unit_tests/test__helpers.py @@ -917,20 +917,17 @@ def callback(*args): self.assertEqual(len(credentials._tokens), 1) -class Test_make_secure_stub(unittest.TestCase): +class Test_make_secure_channel(unittest.TestCase): def _callFUT(self, *args, **kwargs): - from google.cloud._helpers import make_secure_stub - return make_secure_stub(*args, **kwargs) + from google.cloud._helpers import make_secure_channel + return make_secure_channel(*args, **kwargs) def test_it(self): from six.moves import http_client from google.cloud._testing import _Monkey from google.cloud import _helpers as MUT - mock_result = object() - stub_inputs = [] - SSL_CREDS = object() METADATA_CREDS = object() COMPOSITE_CREDS = object() @@ -961,11 +958,6 @@ def secure_channel(self, *args, **kwargs): return CHANNEL grpc_mod = _GRPCModule() - - def mock_stub_class(channel): - stub_inputs.append(channel) - return mock_result - metadata_plugin = object() plugin_args = [] @@ -978,11 +970,9 @@ def mock_plugin(*args): user_agent = 'USER_AGENT' with _Monkey(MUT, grpc=grpc_mod, MetadataPlugin=mock_plugin): - result = self._callFUT(credentials, user_agent, - mock_stub_class, host) + result = self._callFUT(credentials, user_agent, host) - self.assertIs(result, mock_result) - self.assertEqual(stub_inputs, [CHANNEL]) + self.assertIs(result, CHANNEL) self.assertEqual(plugin_args, [(credentials,)]) self.assertEqual(grpc_mod.ssl_channel_credentials_args, ()) self.assertEqual(grpc_mod.metadata_call_credentials_args, @@ -999,6 +989,42 @@ def mock_plugin(*args): (secure_args, secure_kwargs)) +class Test_make_secure_stub(unittest.TestCase): + + def _callFUT(self, *args, **kwargs): + from google.cloud._helpers import make_secure_stub + return make_secure_stub(*args, **kwargs) + + def test_it(self): + from google.cloud._testing import _Monkey + from google.cloud import _helpers as MUT + + result = object() + channel_obj = object() + channels = [] + channel_args = [] + + def stub_class(channel): + channels.append(channel) + return result + + def mock_channel(*args): + channel_args.append(args) + return channel_obj + + credentials = object() + user_agent = 'you-sir-age-int' + host = 'localhost' + with _Monkey(MUT, make_secure_channel=mock_channel): + stub = self._callFUT(credentials, user_agent, + stub_class, host) + + self.assertIs(stub, result) + self.assertEqual(channels, [channel_obj]) + self.assertEqual(channel_args, + [(credentials, user_agent, host)]) + + class Test_make_insecure_stub(unittest.TestCase): def _callFUT(self, *args, **kwargs):