Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
aedbee9
feat: apply PEP 0810 lazy imports to google-cloud-compute
hebaalazzeh Jun 30, 2026
4b3ba9d
fix: remove python version guard for PEP 810 lazy imports
hebaalazzeh Jun 30, 2026
c60e977
feat: centralize service-agnostic boilerplate into api_core for GAPIC…
hebaalazzeh Jul 6, 2026
4463094
Update packages/google-api-core/google/api_core/gapic_v1/client_helpe…
hebaalazzeh Jul 6, 2026
4d9bea2
refactor: optimize regex and update type hints in client_helpers
hebaalazzeh Jul 6, 2026
54cf6a0
style: run black formatter on client_helpers.py
hebaalazzeh Jul 6, 2026
69260e5
fix: address lint and mypy issues in api_core and kms
hebaalazzeh Jul 6, 2026
46e0797
fix: address black formatting issue in client_helpers.py
hebaalazzeh Jul 6, 2026
a4513c4
fix: add underscore prefix to internal helpers in client_helpers.py
hebaalazzeh Jul 6, 2026
ad1c01f
test: add unit tests for gapic client_helpers
hebaalazzeh Jul 6, 2026
1456ead
chore: revert experimental kms validation changes
hebaalazzeh Jul 6, 2026
0ffcdd3
fix: resolve flake8 line length issues in tests
hebaalazzeh Jul 6, 2026
2168bc0
chore: revert experimental PEP 0810 lazy loading in compute
hebaalazzeh Jul 6, 2026
5abeb85
feat: centralize additional client helpers to api_core
hebaalazzeh Jul 6, 2026
09243e9
test: mock missing google-auth mtls attributes
hebaalazzeh Jul 6, 2026
be2b472
style: remove manual 79-char line breaks to satisfy black
hebaalazzeh Jul 6, 2026
d386dfe
refactor: remove underscore prefix from gapic client helpers
hebaalazzeh Jul 7, 2026
72d6561
fix: linting issues and add backward compatibility aliases for intern…
hebaalazzeh Jul 7, 2026
6a44282
fix: skip test_client_helpers in non-grpc environments
hebaalazzeh Jul 7, 2026
2353e82
fix: mock patch missing attribute in old google-auth versions
hebaalazzeh Jul 7, 2026
7208833
feat: split client_helpers into domain-specific modules
hebaalazzeh Jul 9, 2026
65861d9
chore: fix lint and formatting
hebaalazzeh Jul 10, 2026
17acb05
chore: revert accidental formatting and generated files to fix dedupl…
hebaalazzeh Jul 10, 2026
3e9393a
test: split test_client_helpers.py into domain-specific test files
hebaalazzeh Jul 10, 2026
1888b2e
fix: address reviewer comments on api-core
hebaalazzeh Jul 10, 2026
9a36216
test: restore grpc skip to test_method_helpers
hebaalazzeh Jul 13, 2026
f1ca030
fix: safely handle None in get_api_endpoint default_endpoint_template
hebaalazzeh Jul 13, 2026
68f5263
chore: revert unrelated bigframes changes
hebaalazzeh Jul 13, 2026
018d107
fix: make grpc-dependent imports conditional in gapic_v1/__init__.py …
hebaalazzeh Jul 13, 2026
f286631
chore: fix black formatting in gapic_v1/__init__.py
hebaalazzeh Jul 13, 2026
b830151
fix: add noqa flags to unused conditional imports and remove unused p…
hebaalazzeh Jul 13, 2026
a175e88
chore: trigger CI to retry flaky bigframes test
hebaalazzeh Jul 13, 2026
575244e
chore: revert unrelated changes to bigframes, bigquery, and pandas-gbq
hebaalazzeh Jul 13, 2026
a1341f0
fix: isolate unit tests from workstation mTLS environments and fix co…
hebaalazzeh Jul 13, 2026
dcce1e7
test: resolve timing flake in test_bidi.py by adding tolerance to wai…
hebaalazzeh Jul 13, 2026
e0f3f85
merge: merge origin/main into feat/gapic-centralization-api-core
hebaalazzeh Jul 13, 2026
b8a3b32
Update packages/google-api-core/tests/unit/gapic/test_routing.py
hebaalazzeh Jul 14, 2026
317c7ec
Update packages/google-api-core/google/api_core/gapic_v1/_method_help…
hebaalazzeh Jul 14, 2026
b4b9450
Update packages/google-api-core/google/api_core/gapic_v1/_routing.py
hebaalazzeh Jul 14, 2026
b4fe2d0
Update packages/google-api-core/tests/unit/gapic/test_client_cert.py
hebaalazzeh Jul 14, 2026
5471865
Update packages/google-api-core/tests/unit/gapic/test_config_helpers.py
hebaalazzeh Jul 14, 2026
ed6438e
Update packages/google-api-core/google/api_core/gapic_v1/_routing.py
hebaalazzeh Jul 14, 2026
393fd85
fix: address code review feedback for GAPIC centralization
hebaalazzeh Jul 14, 2026
2fae23a
fix: add grpc check back to test modules to prevent ImportError on no…
hebaalazzeh Jul 14, 2026
70445ef
style: run black formatter on gapic test files
hebaalazzeh Jul 14, 2026
2cdabf9
Merge branch 'main' into feat/gapic-centralization-api-core
hebaalazzeh Jul 15, 2026
6bf0e26
fix(gapic): resolve linting errors from redundant eager imports
hebaalazzeh Jul 15, 2026
6844bd2
fix: remove unnecessary grpc check in test_client_cert.py
hebaalazzeh Jul 15, 2026
94e51a1
chore: remove unused grpc import checks in test files
hebaalazzeh Jul 15, 2026
ac1c090
Merge branch 'main' into feat/gapic-centralization-api-core
hebaalazzeh Jul 15, 2026
79c74a4
refactor(api-core): rename gapic internal helper modules and function…
hebaalazzeh Jul 16, 2026
5889da8
style(api-core): run black formatter on gapic centralization files
hebaalazzeh Jul 16, 2026
4eb40c0
merge: merge origin/main into feat/gapic-centralization-api-core
hebaalazzeh Jul 16, 2026
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 @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from google.api_core.gapic_v1 import client_helpers
from google.api_core.gapic_v1 import client_info
from google.api_core.gapic_v1 import config
from google.api_core.gapic_v1 import config_async
Expand All @@ -20,10 +21,12 @@
from google.api_core.gapic_v1 import routing_header

__all__ = [
"client_helpers",
"client_info",
"config",
"config_async",
"method",
"method_async",
"routing_header",
]

128 changes: 128 additions & 0 deletions packages/google-api-core/google/api_core/gapic_v1/client_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# -*- coding: utf-8 -*-
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Helpers for GAPIC client initialization."""

import os
import re
from typing import Optional
Comment thread
hebaalazzeh marked this conversation as resolved.
Outdated

from google.auth.exceptions import MutualTLSChannelError # type: ignore
from google.auth.transport import mtls # type: ignore



Comment thread
hebaalazzeh marked this conversation as resolved.
Outdated
def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
"""Converts api endpoint to mTLS endpoint.

Convert "*.sandbox.googleapis.com" and "*.googleapis.com" to
"*.mtls.sandbox.googleapis.com" and "*.mtls.googleapis.com" respectively.
Args:
api_endpoint (Optional[str]): the api endpoint to convert.
Returns:
Optional[str]: converted mTLS api endpoint.
"""
if not api_endpoint:
return api_endpoint

mtls_endpoint_re = re.compile(
r"(?P<name>[^.]+)(?P<mtls>\.mtls)?(?P<sandbox>\.sandbox)?(?P<googledomain>\.googleapis\.com)?"
)

m = mtls_endpoint_re.match(api_endpoint)
Comment thread
hebaalazzeh marked this conversation as resolved.
Outdated
if m is None:
# Could not parse api_endpoint; return as-is.
return api_endpoint

name, mtls_group, sandbox, googledomain = m.groups()
if mtls_group or not googledomain:
return api_endpoint

if sandbox:
return api_endpoint.replace(
"sandbox.googleapis.com", "mtls.sandbox.googleapis.com"
)

return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com")


def use_client_cert_effective() -> bool:
"""Returns whether client certificate should be used for mTLS if the
google-auth version supports should_use_client_cert automatic mTLS enablement.

Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var.

Returns:
bool: whether client certificate should be used for mTLS
Raises:
ValueError: (If using a version of google-auth without should_use_client_cert and
GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.)
"""
# check if google-auth version supports should_use_client_cert for automatic mTLS enablement
if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER
return mtls.should_use_client_cert()
else: # pragma: NO COVER
# if unsupported, fallback to reading from env var
use_client_cert_str = os.getenv(
"GOOGLE_API_USE_CLIENT_CERTIFICATE", "false"
).lower()
if use_client_cert_str not in ("true", "false"):
raise ValueError(
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be"
" either `true` or `false`"
)
return use_client_cert_str == "true"


def get_api_endpoint(
api_override: Optional[str],
client_cert_source: Optional[bytes],
universe_domain: str,
use_mtls_endpoint: str,
default_universe: str,
default_mtls_endpoint: Optional[str],
default_endpoint_template: str,
) -> str:
"""Return the API endpoint used by the client.

Args:
api_override (Optional[str]): The API endpoint override.
client_cert_source (Optional[bytes]): The client certificate source.
universe_domain (str): The universe domain.
use_mtls_endpoint (str): How to use the mTLS endpoint.
default_universe (str): The default universe.
default_mtls_endpoint (Optional[str]): The default mTLS endpoint.
default_endpoint_template (str): The default endpoint template.

Returns:
str: The API endpoint to be used by the client.
"""
Comment thread
hebaalazzeh marked this conversation as resolved.
Outdated
if api_override is not None:
api_endpoint = api_override
elif use_mtls_endpoint == "always" or (
use_mtls_endpoint == "auto" and client_cert_source
):
if universe_domain != default_universe:
raise MutualTLSChannelError(
f"mTLS is not supported in any universe other than {default_universe}."
)
api_endpoint = default_mtls_endpoint
else:
api_endpoint = default_endpoint_template.format(
UNIVERSE_DOMAIN=universe_domain
)
return api_endpoint

132 changes: 132 additions & 0 deletions packages/google-cloud-compute/google/cloud/compute_v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,138 @@

from importlib import metadata

# PEP 0810: Global Lazy Imports Control
# This variable provides a migration path for library maintainers.
# Python 3.15+ natively intercepts and defers these imports.
# Older Python versions safely ignore this variable.
__lazy_modules__ = {
f"{__name__}.services.accelerator_types",
f"{__name__}.services.addresses",
f"{__name__}.services.advice",
f"{__name__}.services.autoscalers",
f"{__name__}.services.backend_buckets",
f"{__name__}.services.backend_services",
f"{__name__}.services.cross_site_networks",
f"{__name__}.services.disk_types",
f"{__name__}.services.disks",
f"{__name__}.services.external_vpn_gateways",
f"{__name__}.services.firewall_policies",
f"{__name__}.services.firewalls",
f"{__name__}.services.forwarding_rules",
f"{__name__}.services.future_reservations",
f"{__name__}.services.global_addresses",
f"{__name__}.services.global_forwarding_rules",
f"{__name__}.services.global_network_endpoint_groups",
f"{__name__}.services.global_operations",
f"{__name__}.services.global_organization_operations",
f"{__name__}.services.global_public_delegated_prefixes",
f"{__name__}.services.global_vm_extension_policies",
f"{__name__}.services.health_checks",
f"{__name__}.services.image_family_views",
f"{__name__}.services.images",
f"{__name__}.services.instance_group_manager_resize_requests",
f"{__name__}.services.instance_group_managers",
f"{__name__}.services.instance_groups",
f"{__name__}.services.instance_settings_service",
f"{__name__}.services.instance_templates",
f"{__name__}.services.instances",
f"{__name__}.services.instant_snapshot_groups",
f"{__name__}.services.instant_snapshots",
f"{__name__}.services.interconnect_attachment_groups",
f"{__name__}.services.interconnect_attachments",
f"{__name__}.services.interconnect_groups",
f"{__name__}.services.interconnect_locations",
f"{__name__}.services.interconnect_remote_locations",
f"{__name__}.services.interconnects",
f"{__name__}.services.license_codes",
f"{__name__}.services.licenses",
f"{__name__}.services.machine_images",
f"{__name__}.services.machine_types",
f"{__name__}.services.network_attachments",
f"{__name__}.services.network_edge_security_services",
f"{__name__}.services.network_endpoint_groups",
f"{__name__}.services.network_firewall_policies",
f"{__name__}.services.network_profiles",
f"{__name__}.services.networks",
f"{__name__}.services.node_groups",
f"{__name__}.services.node_templates",
f"{__name__}.services.node_types",
f"{__name__}.services.organization_security_policies",
f"{__name__}.services.packet_mirrorings",
f"{__name__}.services.preview_features",
f"{__name__}.services.projects",
f"{__name__}.services.public_advertised_prefixes",
f"{__name__}.services.public_delegated_prefixes",
f"{__name__}.services.region_autoscalers",
f"{__name__}.services.region_backend_buckets",
f"{__name__}.services.region_backend_services",
f"{__name__}.services.region_commitments",
f"{__name__}.services.region_composite_health_checks",
f"{__name__}.services.region_disk_types",
f"{__name__}.services.region_disks",
f"{__name__}.services.region_health_aggregation_policies",
f"{__name__}.services.region_health_check_services",
f"{__name__}.services.region_health_checks",
f"{__name__}.services.region_health_sources",
f"{__name__}.services.region_instance_group_manager_resize_requests",
f"{__name__}.services.region_instance_group_managers",
f"{__name__}.services.region_instance_groups",
f"{__name__}.services.region_instance_templates",
f"{__name__}.services.region_instances",
f"{__name__}.services.region_instant_snapshot_groups",
f"{__name__}.services.region_instant_snapshots",
f"{__name__}.services.region_network_endpoint_groups",
f"{__name__}.services.region_network_firewall_policies",
f"{__name__}.services.region_notification_endpoints",
f"{__name__}.services.region_operations",
f"{__name__}.services.region_security_policies",
f"{__name__}.services.region_snapshot_settings",
f"{__name__}.services.region_snapshots",
f"{__name__}.services.region_ssl_certificates",
f"{__name__}.services.region_ssl_policies",
f"{__name__}.services.region_target_http_proxies",
f"{__name__}.services.region_target_https_proxies",
f"{__name__}.services.region_target_tcp_proxies",
f"{__name__}.services.region_url_maps",
f"{__name__}.services.region_zones",
f"{__name__}.services.regions",
f"{__name__}.services.reservation_blocks",
f"{__name__}.services.reservation_slots",
f"{__name__}.services.reservation_sub_blocks",
f"{__name__}.services.reservations",
f"{__name__}.services.resource_policies",
f"{__name__}.services.rollout_plans",
f"{__name__}.services.rollouts",
f"{__name__}.services.routers",
f"{__name__}.services.routes",
f"{__name__}.services.security_policies",
f"{__name__}.services.service_attachments",
f"{__name__}.services.snapshot_settings_service",
f"{__name__}.services.snapshots",
f"{__name__}.services.ssl_certificates",
f"{__name__}.services.ssl_policies",
f"{__name__}.services.storage_pool_types",
f"{__name__}.services.storage_pools",
f"{__name__}.services.subnetworks",
f"{__name__}.services.target_grpc_proxies",
f"{__name__}.services.target_http_proxies",
f"{__name__}.services.target_https_proxies",
f"{__name__}.services.target_instances",
f"{__name__}.services.target_pools",
f"{__name__}.services.target_ssl_proxies",
f"{__name__}.services.target_tcp_proxies",
f"{__name__}.services.target_vpn_gateways",
f"{__name__}.services.url_maps",
f"{__name__}.services.vpn_gateways",
f"{__name__}.services.vpn_tunnels",
f"{__name__}.services.wire_groups",
f"{__name__}.services.zone_operations",
f"{__name__}.services.zone_vm_extension_policies",
f"{__name__}.services.zones",
f"{__name__}.types.compute",
}


from .services.accelerator_types import AcceleratorTypesClient
from .services.addresses import AddressesClient
from .services.advice import AdviceClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,28 +145,7 @@ def _get_default_mtls_endpoint(api_endpoint) -> Optional[str]:
Returns:
Optional[str]: converted mTLS api endpoint.
"""
if not api_endpoint:
return api_endpoint

mtls_endpoint_re = re.compile(
r"(?P<name>[^.]+)(?P<mtls>\.mtls)?(?P<sandbox>\.sandbox)?(?P<googledomain>\.googleapis\.com)?"
)

m = mtls_endpoint_re.match(api_endpoint)
if m is None:
# Could not parse api_endpoint; return as-is.
return api_endpoint

name, mtls, sandbox, googledomain = m.groups()
if mtls or not googledomain:
return api_endpoint

if sandbox:
return api_endpoint.replace(
"sandbox.googleapis.com", "mtls.sandbox.googleapis.com"
)

return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com")
return gapic_v1.client_helpers.get_default_mtls_endpoint(api_endpoint)

# Note: DEFAULT_ENDPOINT is deprecated. Use _DEFAULT_ENDPOINT_TEMPLATE instead.
DEFAULT_ENDPOINT = "cloudkms.googleapis.com"
Expand All @@ -190,20 +169,10 @@ def _use_client_cert_effective():
ValueError: (If using a version of google-auth without should_use_client_cert and
GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.)
"""
# check if google-auth version supports should_use_client_cert for automatic mTLS enablement
if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER
return mtls.should_use_client_cert()
else: # pragma: NO COVER
# if unsupported, fallback to reading from env var
use_client_cert_str = os.getenv(
"GOOGLE_API_USE_CLIENT_CERTIFICATE", "false"
).lower()
if use_client_cert_str not in ("true", "false"):
raise ValueError(
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be"
" either `true` or `false`"
)
return use_client_cert_str == "true"
return gapic_v1.client_helpers.use_client_cert_effective()




@classmethod
def from_service_account_info(cls, info: dict, *args, **kwargs):
Expand Down Expand Up @@ -600,22 +569,18 @@ def _get_api_endpoint(
Returns:
str: The API endpoint to be used by the client.
"""
if api_override is not None:
api_endpoint = api_override
elif use_mtls_endpoint == "always" or (
use_mtls_endpoint == "auto" and client_cert_source
):
_default_universe = KeyManagementServiceClient._DEFAULT_UNIVERSE
if universe_domain != _default_universe:
raise MutualTLSChannelError(
f"mTLS is not supported in any universe other than {_default_universe}."
)
api_endpoint = KeyManagementServiceClient.DEFAULT_MTLS_ENDPOINT
else:
api_endpoint = KeyManagementServiceClient._DEFAULT_ENDPOINT_TEMPLATE.format(
UNIVERSE_DOMAIN=universe_domain
)
return api_endpoint
return gapic_v1.client_helpers.get_api_endpoint(
api_override,
client_cert_source,
universe_domain,
use_mtls_endpoint,
KeyManagementServiceClient._DEFAULT_UNIVERSE,
KeyManagementServiceClient.DEFAULT_MTLS_ENDPOINT,
KeyManagementServiceClient._DEFAULT_ENDPOINT_TEMPLATE,
)




@staticmethod
def _get_universe_domain(
Expand Down
Loading