Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
from google.cloud.storage._helpers import create_trace_span_helper
from google.cloud.storage.asyncio import _utils
from google.cloud.storage.grpc_client import (
_DEFAULT_HOST,
_resolve_direct_path_interconnect,
_rewrite_host_for_interconnect,
_resolve_grpc_config,
_resolve_universe_domain,
)


Expand Down Expand Up @@ -118,6 +117,7 @@ def __init__(
raise ValueError(
"Either client_options or `client_option.api_endpoint` is None. Please provide api_endpoint when `AnonymousCredentials` is used "
)
self._universe_domain = _resolve_universe_domain(client_options)
self._grpc_client = self._create_anonymous_client(
client_options, credentials
)
Expand All @@ -132,18 +132,20 @@ def __init__(
if agent_version not in client_info.user_agent:
client_info.user_agent += f" {agent_version} "

use_dp_interconnect = _resolve_direct_path_interconnect(
attempt_direct_path_xds_over_interconnect
)

self._universe_domain = _resolve_universe_domain(client_options)
self._grpc_client = self._create_async_grpc_client(
credentials=credentials,
client_info=client_info,
client_options=client_options,
attempt_direct_path=attempt_direct_path,
attempt_direct_path_xds_over_interconnect=use_dp_interconnect,
attempt_direct_path_xds_over_interconnect=attempt_direct_path_xds_over_interconnect,
)

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

@property
def metrics_enabled(self) -> bool:
"""(Experimental) Returns ``True`` if OpenTelemetry metrics recording is active for this client."""
Expand Down Expand Up @@ -177,40 +179,34 @@ def _create_async_grpc_client(
attempt_direct_path=True,
attempt_direct_path_xds_over_interconnect=False,
):
(
self._universe_domain,
host,
quota_project_id,
attempt_direct_path,
use_dp_interconnect,
) = _resolve_grpc_config(
client_options,
attempt_direct_path,
attempt_direct_path_xds_over_interconnect,
)
transport_cls = storage_v2.StorageAsyncClient.get_transport_class(
"grpc_asyncio"
)

primary_user_agent = client_info.to_user_agent()

host = _DEFAULT_HOST
quota_project_id = None
if isinstance(client_options, dict):
host = client_options.get("api_endpoint") or _DEFAULT_HOST
quota_project_id = client_options.get("quota_project_id")
elif client_options:
host = getattr(client_options, "api_endpoint", None) or _DEFAULT_HOST
quota_project_id = getattr(client_options, "quota_project_id", None)

if attempt_direct_path_xds_over_interconnect:
host = _rewrite_host_for_interconnect(host)
channel = transport_cls.create_channel(
host=host,
quota_project_id=quota_project_id,
attempt_direct_path=True,
attempt_direct_path_xds_over_interconnect=True,
credentials=credentials,
options=(("grpc.primary_user_agent", primary_user_agent),),
)
else:
channel = transport_cls.create_channel(
host=host,
quota_project_id=quota_project_id,
attempt_direct_path=attempt_direct_path,
credentials=credentials,
options=(("grpc.primary_user_agent", primary_user_agent),),
)
transport = transport_cls(channel=channel)
channel_kwargs = {
"host": host,
"quota_project_id": quota_project_id,
"attempt_direct_path": attempt_direct_path,
"credentials": credentials,
"options": (("grpc.primary_user_agent", primary_user_agent),),
}
if use_dp_interconnect:
channel_kwargs["attempt_direct_path_xds_over_interconnect"] = True

channel = transport_cls.create_channel(**channel_kwargs)
transport = transport_cls(host=host, channel=channel)

return storage_v2.StorageAsyncClient(
transport=transport,
Expand Down
114 changes: 82 additions & 32 deletions packages/google-cloud-storage/google/cloud/storage/grpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

import os

from google.api_core import universe
from google.cloud.client import ClientWithProject

from google.cloud import _storage_v2 as storage_v2
from google.cloud._storage_v2._compat import get_universe_domain
from google.cloud.storage import _opentelemetry_metrics

_marker = object()
_DEFAULT_UNIVERSE_DOMAIN = universe.DEFAULT_UNIVERSE
_DEFAULT_HOST = "storage.googleapis.com"
_DEFAULT_HOST_DIRECT_PATH = "storage-direct.googleapis.com"
_DIRECT_PATH_INTERCONNECT_ENV = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT"
Expand Down Expand Up @@ -60,6 +63,54 @@ def _rewrite_host_for_interconnect(
return host


def _get_option(client_options, name):
"""Reads an option from a dict, a ``ClientOptions`` instance, or ``None``."""
if isinstance(client_options, dict):
return client_options.get(name)
return getattr(client_options, name, None)


def _resolve_universe_domain(client_options) -> str:
"""Resolves the universe domain with the helper the generated client uses."""
return get_universe_domain(
_get_option(client_options, "universe_domain"),
os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN"),
default_universe=_DEFAULT_UNIVERSE_DOMAIN,
)


def _resolve_grpc_config(
client_options,
attempt_direct_path,
attempt_direct_path_xds_over_interconnect: bool,
):
"""Resolves universe domain, target host, quota project, and DirectPath flags.

DirectPath (including DirectPath over Interconnect) is only attempted in the
default universe.
"""
universe_domain = _resolve_universe_domain(client_options)
use_dp_interconnect = _resolve_direct_path_interconnect(
attempt_direct_path_xds_over_interconnect
)
if universe_domain.lower() != _DEFAULT_UNIVERSE_DOMAIN.lower():
attempt_direct_path = False
use_dp_interconnect = False

host = _get_option(client_options, "api_endpoint") or f"storage.{universe_domain}"
if use_dp_interconnect:
host = _rewrite_host_for_interconnect(host)
attempt_direct_path = True

return (
universe_domain,
host,
_get_option(client_options, "quota_project_id"),
attempt_direct_path,
use_dp_interconnect,
)


class GrpcClient(ClientWithProject):
"""A client for interacting with Google Cloud Storage using the gRPC API.

Expand Down Expand Up @@ -151,18 +202,20 @@ def __init__(
elif api_key:
client_options.api_key = api_key

use_dp_interconnect = _resolve_direct_path_interconnect(
attempt_direct_path_xds_over_interconnect
)

self._universe_domain = _resolve_universe_domain(client_options)
self._grpc_client = self._create_gapic_client(
credentials=credentials,
client_info=client_info,
client_options=client_options,
attempt_direct_path=attempt_direct_path,
attempt_direct_path_xds_over_interconnect=use_dp_interconnect,
attempt_direct_path_xds_over_interconnect=attempt_direct_path_xds_over_interconnect,
)

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

@property
def metrics_enabled(self) -> bool:
"""(Experimental) Returns ``True`` if OpenTelemetry metrics recording is active for this client."""
Expand All @@ -182,36 +235,33 @@ def _create_gapic_client(
attempt_direct_path_xds_over_interconnect=False,
):
"""Creates and configures the low-level GAPIC `storage_v2` client."""
transport_cls = storage_v2.StorageClient.get_transport_class("grpc")

if attempt_direct_path_xds_over_interconnect:
host = _DEFAULT_HOST
quota_project_id = None
if isinstance(client_options, dict):
host = client_options.get("api_endpoint") or _DEFAULT_HOST
quota_project_id = client_options.get("quota_project_id")
elif client_options is not None:
host = getattr(client_options, "api_endpoint", None) or _DEFAULT_HOST
quota_project_id = getattr(client_options, "quota_project_id", None)
host = _rewrite_host_for_interconnect(host)
channel_kwargs = {
"host": host,
"credentials": credentials,
"attempt_direct_path": True,
"attempt_direct_path_xds_over_interconnect": True,
}
if quota_project_id is not None:
channel_kwargs["quota_project_id"] = quota_project_id
channel = transport_cls.create_channel(**channel_kwargs)
else:
channel = transport_cls.create_channel(
attempt_direct_path=attempt_direct_path
)
(
self._universe_domain,
host,
quota_project_id,
attempt_direct_path,
use_dp_interconnect,
) = _resolve_grpc_config(
client_options,
attempt_direct_path,
attempt_direct_path_xds_over_interconnect,
)

transport = transport_cls(credentials=credentials, channel=channel)
transport_cls = storage_v2.StorageClient.get_transport_class("grpc")
channel_kwargs = {
"host": host,
"credentials": credentials,
"attempt_direct_path": attempt_direct_path,
}
if use_dp_interconnect:
channel_kwargs["attempt_direct_path_xds_over_interconnect"] = True
if quota_project_id is not None:
channel_kwargs["quota_project_id"] = quota_project_id

channel = transport_cls.create_channel(**channel_kwargs)
transport = transport_cls(host=host, channel=channel)

return storage_v2.StorageClient(
credentials=credentials,
transport=transport,
client_info=client_info,
client_options=client_options,
Expand Down
Loading
Loading