Skip to content
Open
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 @@ -14,6 +14,7 @@

"""An async client for interacting with Google Cloud Storage using the gRPC API."""

import google.auth
import grpc
from google.auth import credentials as auth_credentials

Expand All @@ -26,9 +27,9 @@
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,
_validate_universe_domain,
)


Expand Down Expand Up @@ -118,6 +119,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 +134,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 +181,39 @@ 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"
)
if credentials is None:
credentials, _ = google.auth.default(
default_scopes=transport_cls.AUTH_SCOPES
)
_validate_universe_domain(self._universe_domain, credentials)

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
133 changes: 101 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,16 @@

import os

from google.api_core import universe
from google.auth import credentials as auth_credentials
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 +64,66 @@ 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,
)


def _validate_universe_domain(universe_domain: str, credentials) -> None:
"""Raises UniverseMismatchError unless the credentials belong to ``universe_domain``.

Credentials that do not expose a universe domain are treated as
``googleapis.com``, so they are rejected in any other universe.
"""
if credentials is not None and not isinstance(
credentials, auth_credentials.AnonymousCredentials
):
universe.compare_domains(universe_domain, credentials)


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

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

use_dp_interconnect = _resolve_direct_path_interconnect(
attempt_direct_path_xds_over_interconnect
effective_credentials = (
credentials
if credentials is not None
else getattr(self, "_credentials", None)
)

self._universe_domain = _resolve_universe_domain(client_options)
self._grpc_client = self._create_gapic_client(
credentials=credentials,
credentials=effective_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 +253,34 @@ 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,
)
_validate_universe_domain(self._universe_domain, credentials)

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