Skip to content
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
20 changes: 15 additions & 5 deletions packages/google-cloud-logging/google/cloud/logging_v2/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import functools

from google.api_core import page_iterator
from google.api_core.universe import determine_domain

from google.cloud import _http
from google.cloud.logging_v2 import __version__
Expand All @@ -27,20 +28,29 @@

class Connection(_http.JSONConnection):
DEFAULT_API_ENDPOINT = "https://logging.googleapis.com"
DEFAULT_API_ENDPOINT_TEMPLATE = "https://logging.{universe_domain}"

def __init__(self, client, *, client_info=None, api_endpoint=DEFAULT_API_ENDPOINT):
def __init__(self, client, *, client_info=None, api_endpoint=None):
"""A connection to Google Cloud Logging via the JSON REST API.

Args:
client (google.cloud.logging_v2.cliet.Client):
client (google.cloud.logging_v2.client.Client):
The client that owns the current connection.
client_info (Optional[google.api_core.client_info.ClientInfo]):
Instance used to generate user agent.
client_options (Optional[google.api_core.client_options.ClientOptions]):
Client options used to set user options
on the client. API Endpoint should be set through client_options.
api_endpoint (Optional[str]):
The API endpoint to use. If not passed, derived from the
client's ``universe_domain`` (defaulting to
``"https://logging.googleapis.com"``).
"""
super(Connection, self).__init__(client, client_info)
if api_endpoint is None:
universe_domain = determine_domain(
getattr(client, "universe_domain", None), None
)
api_endpoint = self.DEFAULT_API_ENDPOINT_TEMPLATE.format(
universe_domain=universe_domain
)
self.API_BASE_URL = api_endpoint
self._client_info.gapic_version = __version__
self._client_info.client_library_version = __version__
Expand Down
28 changes: 20 additions & 8 deletions packages/google-cloud-logging/google/cloud/logging_v2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import sys

import google.api_core.client_options
import google.api_core.universe
from google.cloud.client import ClientWithProject
from google.cloud.environment_vars import DISABLE_GRPC

Expand Down Expand Up @@ -125,15 +126,17 @@ def __init__(
client_options=client_options,
)

if isinstance(client_options, dict):
client_options = google.api_core.client_options.from_dict(client_options)

self._universe_domain = google.api_core.universe.determine_domain(
getattr(client_options, "universe_domain", None),
os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN"),
)

kw_args = {"client_info": client_info}
if client_options:
if isinstance(client_options, dict):
client_options = google.api_core.client_options.from_dict(
client_options
)
if client_options.api_endpoint:
api_endpoint = client_options.api_endpoint
kw_args["api_endpoint"] = api_endpoint
if client_options and client_options.api_endpoint:
kw_args["api_endpoint"] = client_options.api_endpoint

self._connection = Connection(self, **kw_args)
if client_info is None:
Expand All @@ -150,6 +153,15 @@ def __init__(

self._handlers = set()

@property
def universe_domain(self) -> str:
"""Return the universe domain used by the client instance.

Returns:
str: The universe domain used by the client instance.
"""
return self._universe_domain

@property
def logging_api(self):
"""Helper for logging-related API calls.
Expand Down
91 changes: 91 additions & 0 deletions packages/google-cloud-logging/tests/unit/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,53 @@ def test_default_url(self):
client = object()
conn = self._make_one(client)
self.assertIs(conn._client, client)
self.assertEqual(conn.API_BASE_URL, conn.DEFAULT_API_ENDPOINT)

def test_none_universe_domain_falls_back_to_default(self):
client = mock.Mock(universe_domain=None, spec=["universe_domain"])
conn = self._make_one(client)
self.assertEqual(conn.API_BASE_URL, conn.DEFAULT_API_ENDPOINT)

def test_empty_universe_domain_raises(self):
from google.api_core.universe import EmptyUniverseError

client = mock.Mock(universe_domain="", spec=["universe_domain"])
with self.assertRaises(EmptyUniverseError):
self._make_one(client)

def test_universe_domain_url(self):
client = mock.Mock(universe_domain="example.com", spec=["universe_domain"])
conn = self._make_one(client)
self.assertEqual(conn.API_BASE_URL, "https://logging.example.com")
self.assertEqual(
conn.build_api_url("/entries:write"),
"https://logging.example.com/v2/entries:write?prettyPrint=false",
)

def test_custom_endpoint_overrides_universe_domain(self):
custom_endpoint = "https://custom.logging.endpoint"
client = mock.Mock(universe_domain="example.com", spec=["universe_domain"])
conn = self._make_one(client, api_endpoint=custom_endpoint)
self.assertEqual(conn.API_BASE_URL, custom_endpoint)
self.assertEqual(
conn.build_api_url("/entries:write"),
"https://custom.logging.endpoint/v2/entries:write?prettyPrint=false",
)

def test_mtls_does_not_override_custom_universe_domain(self):
http = mock.Mock(is_mtls=True, spec=["is_mtls"])
client = mock.Mock(
universe_domain="example.com",
_http=http,
spec=["universe_domain", "_http"],
)
conn = self._make_one(client)
self.assertFalse(conn.ALLOW_AUTO_SWITCH_TO_MTLS_URL)
self.assertIsNone(conn.API_BASE_MTLS_URL)
self.assertEqual(
conn.build_api_url("/entries:write"),
"https://logging.example.com/v2/entries:write?prettyPrint=false",
)

def test_build_api_url_w_custom_endpoint(self):
from urllib.parse import parse_qsl, urlsplit
Expand Down Expand Up @@ -350,6 +397,50 @@ def test_logger_delete(self):
self.assertEqual(conn._called_with["method"], "DELETE")
self.assertEqual(conn._called_with["path"], path)

def test_background_thread_transport_batch_commit_universe_domain(self):
import json
import logging
import os

import requests

from google.cloud.logging import Client
from google.cloud.logging.handlers.transports import BackgroundThreadTransport

http = mock.create_autospec(requests.Session, instance=True)
response = requests.Response()
response.status_code = 200
response._content = b"{}"
http.request.return_value = response

with mock.patch.dict(
os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "example.com"}
):
client = Client(
project=self.PROJECT,
_http=http,
_use_grpc=False,
)

self.assertIsInstance(client.logging_api, self._get_target_class())
transport = BackgroundThreadTransport(client, self.LOGGER_NAME)
record = logging.LogRecord(
"mylogger", logging.INFO, __file__, 1, "hello world", None, None
)
transport.send(record, "hello world")
transport.flush()
transport.close()

http.request.assert_called_once()
call_kwargs = http.request.call_args.kwargs
self.assertEqual(call_kwargs["method"], "POST")
self.assertEqual(
call_kwargs["url"],
"https://logging.example.com/v2/entries:write?prettyPrint=false",
)
payload = json.loads(call_kwargs["data"])
self.assertEqual(payload["entries"][0]["textPayload"], "hello world")


class Test_SinksAPI(unittest.TestCase):
PROJECT = "project"
Expand Down
126 changes: 126 additions & 0 deletions packages/google-cloud-logging/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,132 @@ def test_ctor_w_client_options_dict(self):
client._connection.API_BASE_URL, "https://foo-logging.googleapis.com"
)

def test_ctor_default_universe_domain(self):
creds = _make_credentials()
client = self._make_one(
project=self.PROJECT, credentials=creds, _use_grpc=False
)
self.assertEqual(client.universe_domain, "googleapis.com")
self.assertEqual(
client._connection.API_BASE_URL, "https://logging.googleapis.com"
)

def test_ctor_w_universe_domain_dict_http(self):
creds = _make_credentials()
client = self._make_one(
project=self.PROJECT,
credentials=creds,
client_options={"universe_domain": "example.com"},
_use_grpc=False,
)
self.assertEqual(client.universe_domain, "example.com")
self.assertEqual(client._connection.API_BASE_URL, "https://logging.example.com")

def test_ctor_w_universe_domain_client_options_object_http(self):
from google.api_core.client_options import ClientOptions

creds = _make_credentials()
client_options = ClientOptions(universe_domain="example.com")
client = self._make_one(
project=self.PROJECT,
credentials=creds,
client_options=client_options,
_use_grpc=False,
)
self.assertEqual(client.universe_domain, "example.com")
self.assertEqual(client._connection.API_BASE_URL, "https://logging.example.com")

def test_ctor_w_universe_domain_env_var_http(self):
import os

creds = _make_credentials()
with mock.patch.dict(
os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "example.com"}
):
client = self._make_one(
project=self.PROJECT, credentials=creds, _use_grpc=False
)
self.assertEqual(client.universe_domain, "example.com")
self.assertEqual(client._connection.API_BASE_URL, "https://logging.example.com")

def test_ctor_universe_domain_precedence(self):
import os

from google.api_core.client_options import ClientOptions

creds = _make_credentials()
with mock.patch.dict(
os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "env-universe.example.com"}
):
# client_options.universe_domain overrides GOOGLE_CLOUD_UNIVERSE_DOMAIN
client = self._make_one(
project=self.PROJECT,
credentials=creds,
client_options=ClientOptions(universe_domain="example.com"),
_use_grpc=False,
)
self.assertEqual(client.universe_domain, "example.com")
self.assertEqual(
client._connection.API_BASE_URL, "https://logging.example.com"
)

# client_options.api_endpoint overrides universe_domain and env var
client_custom_ep = self._make_one(
project=self.PROJECT,
credentials=creds,
client_options={
"api_endpoint": "https://custom-logging.example.com",
"universe_domain": "example.com",
},
_use_grpc=False,
)
self.assertEqual(client_custom_ep.universe_domain, "example.com")
self.assertEqual(
client_custom_ep._connection.API_BASE_URL,
"https://custom-logging.example.com",
)

# client_options.api_endpoint also overrides GOOGLE_CLOUD_UNIVERSE_DOMAIN alone
with mock.patch.dict(
os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "example.com"}
):
client_ep_over_env = self._make_one(
project=self.PROJECT,
credentials=creds,
client_options={"api_endpoint": "https://custom-logging.example.com"},
_use_grpc=False,
)
self.assertEqual(client_ep_over_env.universe_domain, "example.com")
self.assertEqual(
client_ep_over_env._connection.API_BASE_URL,
"https://custom-logging.example.com",
)

def test_ctor_empty_universe_domain_raises(self):
import os

from google.api_core.client_options import ClientOptions
from google.api_core.universe import EmptyUniverseError

creds = _make_credentials()
with self.assertRaises(EmptyUniverseError):
self._make_one(
project=self.PROJECT,
credentials=creds,
client_options={"universe_domain": ""},
)

with self.assertRaises(EmptyUniverseError):
self._make_one(
project=self.PROJECT,
credentials=creds,
client_options=ClientOptions(universe_domain=""),
)

with mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": ""}):
with self.assertRaises(EmptyUniverseError):
self._make_one(project=self.PROJECT, credentials=creds)

def test_logging_api_wo_gapic(self):
from google.cloud.logging_v2._http import _LoggingAPI

Expand Down
Loading