Visitar URL original
chore(generator): move `read_environment_variables` to `_compat.py.j2… · googleapis/google-cloud-python@2059bdf · GitHub
Skip to content

Commit 2059bdf

Browse files
authored
chore(generator): move read_environment_variables to _compat.py.j2 (#17997)
We move `_read_environment_variables` and its associated unit tests into `_compat.py.j2` as `read_environment_variables`. This reduces code duplication in packages that have multiple services. In the future, once `should_use_mtls_endpoint` is added to `google-auth`, we can defer parsing `GOOGLE_API_USE_MTLS_ENDPOINT` upstream. Towards: #17883
1 parent 453a77d commit 2059bdf

31 files changed

Lines changed: 371 additions & 306 deletions

File tree

‎packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,34 @@ except ImportError: # pragma: NO COVER
5050
)
5151
return use_client_cert == "true"
5252

53+
54+
{# TODO(https://github.com/googleapis/google-cloud-python/issues/17883):
55+
Defer to google.auth.transport.mtls.should_use_mtls_endpoint (available in google-auth >= 2.56.0)
56+
to parse GOOGLE_API_USE_MTLS_ENDPOINT when available in minimum supported google-auth. #}
57+
def read_environment_variables():
58+
"""Returns the environment variables used by the client.
59+
60+
Returns:
61+
Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE,
62+
GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
63+
64+
Raises:
65+
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
66+
any of ["true", "false"].
67+
google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
68+
is not any of ["auto", "never", "always"].
69+
"""
70+
use_client_cert = should_use_client_cert()
71+
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
72+
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
73+
if use_mtls_endpoint not in ("auto", "never", "always"):
74+
raise MutualTLSChannelError(
75+
"Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`,"
76+
" `auto` or `always`"
77+
)
78+
return use_client_cert, use_mtls_endpoint, universe_domain_env
79+
80+
5381
DEFAULT_UNIVERSE = "googleapis.com"
5482

5583

‎packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ from google.api_core import exceptions as core_exceptions
3030
from google.api_core import extended_operation
3131
{% endif %}
3232
from google.api_core import gapic_v1
33-
from {{package_path}}._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert
33+
from {{package_path}}._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert, read_environment_variables
3434
{% if has_auto_populated_fields %}
3535
from {{package_path}}._compat import setup_request_id
3636
{% endif %}
@@ -293,26 +293,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
293293

294294
return api_endpoint, client_cert_source
295295

296-
@staticmethod
297-
def _read_environment_variables():
298-
"""Returns the environment variables used by the client.
299-
300-
Returns:
301-
Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE,
302-
GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
303296

304-
Raises:
305-
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
306-
any of ["true", "false"].
307-
google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
308-
is not any of ["auto", "never", "always"].
309-
"""
310-
use_client_cert = should_use_client_cert()
311-
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
312-
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
313-
if use_mtls_endpoint not in ("auto", "never", "always"):
314-
raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`")
315-
return use_client_cert, use_mtls_endpoint, universe_domain_env
316297

317298
@staticmethod
318299
def _get_client_cert_source(provided_cert_source, use_cert_flag):
@@ -459,7 +440,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
459440

460441
universe_domain_opt = getattr(self._client_options, 'universe_domain', None)
461442

462-
self._use_client_cert, self._use_mtls_endpoint, self._universe_domain_env = {{ service.client_name }}._read_environment_variables()
443+
self._use_client_cert, self._use_mtls_endpoint, self._universe_domain_env = read_environment_variables()
463444
self._client_cert_source = {{ service.client_name }}._get_client_cert_source(self._client_options.client_cert_source, self._use_client_cert)
464445
self._universe_domain = get_universe_domain(universe_domain_opt, self._universe_domain_env)
465446
self._api_endpoint: str = "" # updated below, depending on `transport`

‎packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import google.auth.transport.mtls
2323

2424
{% set package_path = api.naming.module_namespace|join('.') + "." + api.naming.versioned_module_name %}
2525
from {{package_path}}._compat import transcode_request
26-
from {{package_path}}._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert
26+
from {{package_path}}._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert, read_environment_variables
2727
{% if has_auto_populated_fields %}
2828
from {{package_path}}._compat import setup_request_id
2929
{% endif %}
@@ -519,4 +519,16 @@ def test_transcode_request_proto_plus_wrapper():
519519
transcoded, _, _ = transcode_request(http_options, mock_proto_plus)
520520
assert transcoded["uri"] == "/v1/test/proto-plus-field"
521521

522+
523+
def test_read_environment_variables():
524+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true", "GOOGLE_API_USE_MTLS_ENDPOINT": "always", "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"}):
525+
use_cert, mtls_endpoint, universe_domain = read_environment_variables()
526+
assert use_cert is True
527+
assert mtls_endpoint == "always"
528+
assert universe_domain == "foo.com"
529+
530+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}):
531+
with pytest.raises(MutualTLSChannelError):
532+
read_environment_variables()
533+
522534
{% endblock %}

‎packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ def should_use_client_cert():
4242
)
4343
return use_client_cert == "true"
4444

45+
46+
def read_environment_variables():
47+
"""Returns the environment variables used by the client.
48+
49+
Returns:
50+
Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE,
51+
GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
52+
53+
Raises:
54+
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
55+
any of ["true", "false"].
56+
google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
57+
is not any of ["auto", "never", "always"].
58+
"""
59+
use_client_cert = should_use_client_cert()
60+
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
61+
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
62+
if use_mtls_endpoint not in ("auto", "never", "always"):
63+
raise MutualTLSChannelError(
64+
"Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`,"
65+
" `auto` or `always`"
66+
)
67+
return use_client_cert, use_mtls_endpoint, universe_domain_env
68+
69+
4570
DEFAULT_UNIVERSE = "googleapis.com"
4671

4772

‎packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py‎

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from google.api_core import client_options as client_options_lib
2828
from google.api_core import exceptions as core_exceptions
2929
from google.api_core import gapic_v1
30-
from google.cloud.asset_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert
30+
from google.cloud.asset_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert, read_environment_variables
3131
from google.api_core import retry as retries
3232
from google.auth import credentials as ga_credentials # type: ignore
3333
from google.auth.transport import mtls # type: ignore
@@ -348,27 +348,6 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio
348348

349349
return api_endpoint, client_cert_source
350350

351-
@staticmethod
352-
def _read_environment_variables():
353-
"""Returns the environment variables used by the client.
354-
355-
Returns:
356-
Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE,
357-
GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
358-
359-
Raises:
360-
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
361-
any of ["true", "false"].
362-
google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
363-
is not any of ["auto", "never", "always"].
364-
"""
365-
use_client_cert = should_use_client_cert()
366-
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
367-
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
368-
if use_mtls_endpoint not in ("auto", "never", "always"):
369-
raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`")
370-
return use_client_cert, use_mtls_endpoint, universe_domain_env
371-
372351
@staticmethod
373352
def _get_client_cert_source(provided_cert_source, use_cert_flag):
374353
"""Return the client cert source to be used by the client.
@@ -510,7 +489,7 @@ def __init__(self, *,
510489

511490
universe_domain_opt = getattr(self._client_options, 'universe_domain', None)
512491

513-
self._use_client_cert, self._use_mtls_endpoint, self._universe_domain_env = AssetServiceClient._read_environment_variables()
492+
self._use_client_cert, self._use_mtls_endpoint, self._universe_domain_env = read_environment_variables()
514493
self._client_cert_source = AssetServiceClient._get_client_cert_source(self._client_options.client_cert_source, self._use_client_cert)
515494
self._universe_domain = get_universe_domain(universe_domain_opt, self._universe_domain_env)
516495
self._api_endpoint: str = "" # updated below, depending on `transport`

‎packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import google.auth.transport.mtls
2525

2626
from google.cloud.asset_v1._compat import transcode_request
27-
from google.cloud.asset_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert
27+
from google.cloud.asset_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert, read_environment_variables
2828

2929
from google.auth.exceptions import MutualTLSChannelError
3030
from google.api_core.universe import EmptyUniverseError
@@ -411,3 +411,15 @@ def test_transcode_request_proto_plus_wrapper():
411411

412412
transcoded, _, _ = transcode_request(http_options, mock_proto_plus)
413413
assert transcoded["uri"] == "/v1/test/proto-plus-field"
414+
415+
416+
def test_read_environment_variables():
417+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true", "GOOGLE_API_USE_MTLS_ENDPOINT": "always", "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"}):
418+
use_cert, mtls_endpoint, universe_domain = read_environment_variables()
419+
assert use_cert is True
420+
assert mtls_endpoint == "always"
421+
assert universe_domain == "foo.com"
422+
423+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}):
424+
with pytest.raises(MutualTLSChannelError):
425+
read_environment_variables()

‎packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ def should_use_client_cert():
4242
)
4343
return use_client_cert == "true"
4444

45+
46+
def read_environment_variables():
47+
"""Returns the environment variables used by the client.
48+
49+
Returns:
50+
Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE,
51+
GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
52+
53+
Raises:
54+
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
55+
any of ["true", "false"].
56+
google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
57+
is not any of ["auto", "never", "always"].
58+
"""
59+
use_client_cert = should_use_client_cert()
60+
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
61+
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
62+
if use_mtls_endpoint not in ("auto", "never", "always"):
63+
raise MutualTLSChannelError(
64+
"Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`,"
65+
" `auto` or `always`"
66+
)
67+
return use_client_cert, use_mtls_endpoint, universe_domain_env
68+
69+
4570
DEFAULT_UNIVERSE = "googleapis.com"
4671

4772

‎packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py‎

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from google.api_core import client_options as client_options_lib
2828
from google.api_core import exceptions as core_exceptions
2929
from google.api_core import gapic_v1
30-
from google.iam.credentials_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert
30+
from google.iam.credentials_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert, read_environment_variables
3131
from google.api_core import retry as retries
3232
from google.auth import credentials as ga_credentials # type: ignore
3333
from google.auth.transport import mtls # type: ignore
@@ -285,27 +285,6 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio
285285

286286
return api_endpoint, client_cert_source
287287

288-
@staticmethod
289-
def _read_environment_variables():
290-
"""Returns the environment variables used by the client.
291-
292-
Returns:
293-
Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE,
294-
GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
295-
296-
Raises:
297-
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
298-
any of ["true", "false"].
299-
google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
300-
is not any of ["auto", "never", "always"].
301-
"""
302-
use_client_cert = should_use_client_cert()
303-
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
304-
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
305-
if use_mtls_endpoint not in ("auto", "never", "always"):
306-
raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`")
307-
return use_client_cert, use_mtls_endpoint, universe_domain_env
308-
309288
@staticmethod
310289
def _get_client_cert_source(provided_cert_source, use_cert_flag):
311290
"""Return the client cert source to be used by the client.
@@ -447,7 +426,7 @@ def __init__(self, *,
447426

448427
universe_domain_opt = getattr(self._client_options, 'universe_domain', None)
449428

450-
self._use_client_cert, self._use_mtls_endpoint, self._universe_domain_env = IAMCredentialsClient._read_environment_variables()
429+
self._use_client_cert, self._use_mtls_endpoint, self._universe_domain_env = read_environment_variables()
451430
self._client_cert_source = IAMCredentialsClient._get_client_cert_source(self._client_options.client_cert_source, self._use_client_cert)
452431
self._universe_domain = get_universe_domain(universe_domain_opt, self._universe_domain_env)
453432
self._api_endpoint: str = "" # updated below, depending on `transport`

‎packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import google.auth.transport.mtls
2525

2626
from google.iam.credentials_v1._compat import transcode_request
27-
from google.iam.credentials_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert
27+
from google.iam.credentials_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert, read_environment_variables
2828

2929
from google.auth.exceptions import MutualTLSChannelError
3030
from google.api_core.universe import EmptyUniverseError
@@ -411,3 +411,15 @@ def test_transcode_request_proto_plus_wrapper():
411411

412412
transcoded, _, _ = transcode_request(http_options, mock_proto_plus)
413413
assert transcoded["uri"] == "/v1/test/proto-plus-field"
414+
415+
416+
def test_read_environment_variables():
417+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true", "GOOGLE_API_USE_MTLS_ENDPOINT": "always", "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"}):
418+
use_cert, mtls_endpoint, universe_domain = read_environment_variables()
419+
assert use_cert is True
420+
assert mtls_endpoint == "always"
421+
assert universe_domain == "foo.com"
422+
423+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}):
424+
with pytest.raises(MutualTLSChannelError):
425+
read_environment_variables()

‎packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ def should_use_client_cert():
4242
)
4343
return use_client_cert == "true"
4444

45+
46+
def read_environment_variables():
47+
"""Returns the environment variables used by the client.
48+
49+
Returns:
50+
Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE,
51+
GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
52+
53+
Raises:
54+
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
55+
any of ["true", "false"].
56+
google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
57+
is not any of ["auto", "never", "always"].
58+
"""
59+
use_client_cert = should_use_client_cert()
60+
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
61+
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
62+
if use_mtls_endpoint not in ("auto", "never", "always"):
63+
raise MutualTLSChannelError(
64+
"Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`,"
65+
" `auto` or `always`"
66+
)
67+
return use_client_cert, use_mtls_endpoint, universe_domain_env
68+
69+
4570
DEFAULT_UNIVERSE = "googleapis.com"
4671

4772

0 commit comments

Comments
 (0)