Skip to content

Commit 8d6f1aa

Browse files
authored
Support num_retries field in env var for GCP connection (#8700)
1 parent 41b4c27 commit 8d6f1aa

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

airflow/providers/google/common/hooks/base_google.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,19 @@ def num_retries(self) -> int:
250250
:return: the number of times each API request should be retried
251251
:rtype: int
252252
"""
253-
return self._get_field('num_retries') or 5
253+
field_value = self._get_field('num_retries', default=5)
254+
if field_value is None:
255+
return 5
256+
if isinstance(field_value, str) and field_value.strip() == '':
257+
return 5
258+
try:
259+
return int(field_value)
260+
except ValueError:
261+
raise AirflowException(
262+
f"The num_retries field should be a integer. "
263+
f"Current value: \"{field_value}\" (type: {type(field_value)}). "
264+
f"Please check the connection configuration."
265+
)
254266

255267
@property
256268
def client_info(self) -> ClientInfo:

tests/providers/google/common/hooks/test_base_google.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,3 +724,49 @@ def test_provide_authorized_gcloud_via_gcloud_application_default(
724724
],
725725
any_order=False
726726
)
727+
728+
729+
class TestNumRetry(unittest.TestCase):
730+
731+
def test_should_return_int_when_set_int_via_connection(self):
732+
instance = hook.GoogleBaseHook(gcp_conn_id="google_cloud_default")
733+
instance.extras = {
734+
'extra__google_cloud_platform__num_retries': 10,
735+
}
736+
737+
self.assertIsInstance(instance.num_retries, int)
738+
self.assertEqual(10, instance.num_retries)
739+
740+
@mock.patch.dict(
741+
'os.environ',
742+
AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT=(
743+
'google-cloud-platform://?extra__google_cloud_platform__num_retries=5'
744+
)
745+
)
746+
def test_should_return_int_when_set_via_env_var(self):
747+
instance = hook.GoogleBaseHook(gcp_conn_id="google_cloud_default")
748+
self.assertIsInstance(instance.num_retries, int)
749+
750+
@mock.patch.dict(
751+
'os.environ',
752+
AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT=(
753+
'google-cloud-platform://?extra__google_cloud_platform__num_retries=cat'
754+
)
755+
)
756+
def test_should_raise_when_invalid_value_via_env_var(self):
757+
instance = hook.GoogleBaseHook(gcp_conn_id="google_cloud_default")
758+
with self.assertRaisesRegex(
759+
AirflowException, re.escape("The num_retries field should be a integer.")
760+
):
761+
self.assertIsInstance(instance.num_retries, int)
762+
763+
@mock.patch.dict(
764+
'os.environ',
765+
AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT=(
766+
'google-cloud-platform://?extra__google_cloud_platform__num_retries='
767+
)
768+
)
769+
def test_should_fallback_when_empty_string_in_env_var(self):
770+
instance = hook.GoogleBaseHook(gcp_conn_id="google_cloud_default")
771+
self.assertIsInstance(instance.num_retries, int)
772+
self.assertEqual(5, instance.num_retries)

0 commit comments

Comments
 (0)