Skip to content

Commit

Permalink
Making a gRPC channel with credentials from Client's.
Browse files Browse the repository at this point in the history
This over-rides the default behavior within GAX, which uses
application default credentials.
  • Loading branch information
dhermes committed Nov 4, 2016
1 parent b42fa03 commit 8c7faf2
Show file tree
Hide file tree
Showing 8 changed files with 371 additions and 101 deletions.
56 changes: 56 additions & 0 deletions logging/google/cloud/logging/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

import functools

from google.cloud.gapic.logging.v2.config_service_v2_api import (
ConfigServiceV2Api)
from google.cloud.gapic.logging.v2.logging_service_v2_api import (
LoggingServiceV2Api)
from google.cloud.gapic.logging.v2.metrics_service_v2_api import (
MetricsServiceV2Api)
from google.gax import CallOptions
from google.gax import INITIAL_PAGE
from google.gax.errors import GaxError
Expand All @@ -28,6 +34,8 @@
from grpc import StatusCode

from google.cloud._helpers import _datetime_to_rfc3339
from google.cloud._helpers import make_secure_channel
from google.cloud.connection import DEFAULT_USER_AGENT
from google.cloud.exceptions import Conflict
from google.cloud.exceptions import NotFound
from google.cloud.iterator import GAXIterator
Expand Down Expand Up @@ -511,3 +519,51 @@ def _item_to_metric(iterator, log_metric_pb):
"""
resource = MessageToDict(log_metric_pb)
return Metric.from_api_repr(resource, iterator.client)


def make_gax_logging_api(client):
"""Create an instance of the GAX Logging API.
:type client: :class:`~google.cloud.logging.client.Client`
:param client: The client that holds configuration details.
:rtype: :class:`_LoggingAPI`
:returns: A metrics API instance with the proper credentials.
"""
channel = make_secure_channel(
client.connection.credentials, DEFAULT_USER_AGENT,
LoggingServiceV2Api.SERVICE_ADDRESS)
generated = LoggingServiceV2Api(channel=channel)
return _LoggingAPI(generated, client)


def make_gax_metrics_api(client):
"""Create an instance of the GAX Metrics API.
:type client: :class:`~google.cloud.logging.client.Client`
:param client: The client that holds configuration details.
:rtype: :class:`_MetricsAPI`
:returns: A metrics API instance with the proper credentials.
"""
channel = make_secure_channel(
client.connection.credentials, DEFAULT_USER_AGENT,
MetricsServiceV2Api.SERVICE_ADDRESS)
generated = MetricsServiceV2Api(channel=channel)
return _MetricsAPI(generated, client)


def make_gax_sinks_api(client):
"""Create an instance of the GAX Sinks API.
:type client: :class:`~google.cloud.logging.client.Client`
:param client: The client that holds configuration details.
:rtype: :class:`_SinksAPI`
:returns: A metrics API instance with the proper credentials.
"""
channel = make_secure_channel(
client.connection.credentials, DEFAULT_USER_AGENT,
ConfigServiceV2Api.SERVICE_ADDRESS)
generated = ConfigServiceV2Api(channel=channel)
return _SinksAPI(generated, client)
27 changes: 9 additions & 18 deletions logging/google/cloud/logging/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,14 @@
import os

try:
from google.cloud.gapic.logging.v2.config_service_v2_api import (
ConfigServiceV2Api as GeneratedSinksAPI)
from google.cloud.gapic.logging.v2.logging_service_v2_api import (
LoggingServiceV2Api as GeneratedLoggingAPI)
from google.cloud.gapic.logging.v2.metrics_service_v2_api import (
MetricsServiceV2Api as GeneratedMetricsAPI)
from google.cloud.logging._gax import _LoggingAPI as GAXLoggingAPI
from google.cloud.logging._gax import _MetricsAPI as GAXMetricsAPI
from google.cloud.logging._gax import _SinksAPI as GAXSinksAPI
from google.cloud.logging._gax import make_gax_logging_api
from google.cloud.logging._gax import make_gax_metrics_api
from google.cloud.logging._gax import make_gax_sinks_api
except ImportError: # pragma: NO COVER
_HAVE_GAX = False
GeneratedLoggingAPI = GAXLoggingAPI = None
GeneratedMetricsAPI = GAXMetricsAPI = None
GeneratedSinksAPI = GAXSinksAPI = None
make_gax_logging_api = None
make_gax_metrics_api = None
make_gax_sinks_api = None
else:
_HAVE_GAX = True

Expand Down Expand Up @@ -97,8 +91,7 @@ def logging_api(self):
"""
if self._logging_api is None:
if self._use_gax:
generated = GeneratedLoggingAPI()
self._logging_api = GAXLoggingAPI(generated, self)
self._logging_api = make_gax_logging_api(self)
else:
self._logging_api = JSONLoggingAPI(self)
return self._logging_api
Expand All @@ -112,8 +105,7 @@ def sinks_api(self):
"""
if self._sinks_api is None:
if self._use_gax:
generated = GeneratedSinksAPI()
self._sinks_api = GAXSinksAPI(generated, self)
self._sinks_api = make_gax_sinks_api(self)
else:
self._sinks_api = JSONSinksAPI(self)
return self._sinks_api
Expand All @@ -127,8 +119,7 @@ def metrics_api(self):
"""
if self._metrics_api is None:
if self._use_gax:
generated = GeneratedMetricsAPI()
self._metrics_api = GAXMetricsAPI(generated, self)
self._metrics_api = make_gax_metrics_api(self)
else:
self._metrics_api = JSONMetricsAPI(self)
return self._metrics_api
Expand Down
138 changes: 138 additions & 0 deletions logging/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,132 @@ def test_metric_delete_hit(self):
self.assertIsNone(options)


@unittest.skipUnless(_HAVE_GAX, 'No gax-python')
class Test_make_gax_logging_api(unittest.TestCase):

def _call_fut(self, client):
from google.cloud.logging._gax import make_gax_logging_api
return make_gax_logging_api(client)

def test_it(self):
from google.cloud._testing import _Monkey
from google.cloud.logging import _gax as MUT

creds = object()
client = _Client(creds)
channels = []
channel_args = []
channel_obj = object()
generated = object()

def make_channel(*args):
channel_args.append(args)
return channel_obj

def generated_api(channel=None):
channels.append(channel)
return generated

host = 'foo.apis.invalid'
generated_api.SERVICE_ADDRESS = host

with _Monkey(MUT, LoggingServiceV2Api=generated_api,
make_secure_channel=make_channel):
logging_api = self._call_fut(client)

self.assertEqual(channels, [channel_obj])
self.assertEqual(channel_args,
[(creds, MUT.DEFAULT_USER_AGENT, host)])

self.assertIsInstance(logging_api, MUT._LoggingAPI)
self.assertIs(logging_api._gax_api, generated)
self.assertIs(logging_api._client, client)


@unittest.skipUnless(_HAVE_GAX, 'No gax-python')
class Test_make_gax_metrics_api(unittest.TestCase):

def _call_fut(self, client):
from google.cloud.logging._gax import make_gax_metrics_api
return make_gax_metrics_api(client)

def test_it(self):
from google.cloud._testing import _Monkey
from google.cloud.logging import _gax as MUT

creds = object()
client = _Client(creds)
channels = []
channel_args = []
channel_obj = object()
generated = object()

def make_channel(*args):
channel_args.append(args)
return channel_obj

def generated_api(channel=None):
channels.append(channel)
return generated

host = 'foo.apis.invalid'
generated_api.SERVICE_ADDRESS = host

with _Monkey(MUT, MetricsServiceV2Api=generated_api,
make_secure_channel=make_channel):
metrics_api = self._call_fut(client)

self.assertEqual(channels, [channel_obj])
self.assertEqual(channel_args,
[(creds, MUT.DEFAULT_USER_AGENT, host)])

self.assertIsInstance(metrics_api, MUT._MetricsAPI)
self.assertIs(metrics_api._gax_api, generated)
self.assertIs(metrics_api._client, client)


@unittest.skipUnless(_HAVE_GAX, 'No gax-python')
class Test_make_gax_sinks_api(unittest.TestCase):

def _call_fut(self, client):
from google.cloud.logging._gax import make_gax_sinks_api
return make_gax_sinks_api(client)

def test_it(self):
from google.cloud._testing import _Monkey
from google.cloud.logging import _gax as MUT

creds = object()
client = _Client(creds)
channels = []
channel_args = []
channel_obj = object()
generated = object()

def make_channel(*args):
channel_args.append(args)
return channel_obj

def generated_api(channel=None):
channels.append(channel)
return generated

host = 'foo.apis.invalid'
generated_api.SERVICE_ADDRESS = host

with _Monkey(MUT, ConfigServiceV2Api=generated_api,
make_secure_channel=make_channel):
sinks_api = self._call_fut(client)

self.assertEqual(channels, [channel_obj])
self.assertEqual(channel_args,
[(creds, MUT.DEFAULT_USER_AGENT, host)])

self.assertIsInstance(sinks_api, MUT._SinksAPI)
self.assertIs(sinks_api._gax_api, generated)
self.assertIs(sinks_api._client, client)


class _GAXLoggingAPI(_GAXBaseAPI):

_delete_not_found = False
Expand Down Expand Up @@ -1172,3 +1298,15 @@ def delete_log_metric(self, metric_name, options=None):
raise GaxError('error')
if self._log_metric_not_found:
raise GaxError('notfound', self._make_grpc_not_found())


class _Connection(object):

def __init__(self, credentials):
self.credentials = credentials


class _Client(object):

def __init__(self, credentials):
self.connection = _Connection(credentials)
Loading

0 comments on commit 8c7faf2

Please sign in to comment.