Skip to content

Commit 450d72e

Browse files
jrmccluskeygemini-code-assist[bot]
authored andcommitted
Fix parsing of dataflow api endpoint URLs to remove trailing slash (apache#38847)
* Fix parsing of dataflow api endpoint URLs to remove trailing slash * Apply suggestions from code review Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * gemini existance suggestion --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 58a030e commit 450d72e

3 files changed

Lines changed: 45 additions & 7 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"comment": "Modify this file in a trivial way to cause this test suite to run",
3-
"modification": 3
3+
"modification": 4
44
}

sdks/python/apache_beam/runners/dataflow/internal/apiclient.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,14 @@ def __init__(self, options, root_staging_location=None):
519519
client_options = None
520520
transport = None
521521
if self.google_cloud_options.dataflow_endpoint:
522-
endpoint = self.google_cloud_options.dataflow_endpoint
523-
if 'localhost' in endpoint or 'sandbox' in endpoint:
524-
transport = 'rest'
525-
else:
526-
endpoint = re.sub('^https?://', '', endpoint)
527-
client_options = client_options_lib.ClientOptions(api_endpoint=endpoint)
522+
endpoint = self.google_cloud_options.dataflow_endpoint.strip()
523+
if endpoint:
524+
if 'localhost' in endpoint or 'sandbox' in endpoint:
525+
transport = 'rest'
526+
else:
527+
endpoint = re.sub('^https?://', '', endpoint)
528+
endpoint = endpoint.rstrip('/')
529+
client_options = client_options_lib.ClientOptions(api_endpoint=endpoint)
528530

529531
self._jobs_client = dataflow.JobsV1Beta3Client(
530532
credentials=gapic_credentials,

sdks/python/apache_beam/runners/dataflow/internal/apiclient_test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,42 @@ def test_template_file_generation_with_upload_graph(self):
12981298
self.assertFalse(template_obj.get('steps'))
12991299
self.assertTrue(template_obj['stepsLocation'])
13001300

1301+
def test_dataflow_endpoint_clean(self):
1302+
endpoints_and_expectations = [
1303+
# (input_endpoint, expected_endpoint, expected_transport)
1304+
('https://dataflow.googleapis.com/', 'dataflow.googleapis.com', None),
1305+
('https://dataflow.googleapis.com ', 'dataflow.googleapis.com', None),
1306+
('dataflow.googleapis.com/', 'dataflow.googleapis.com', None),
1307+
('http://localhost:8080/', 'http://localhost:8080', 'rest'),
1308+
('localhost:8080/', 'localhost:8080', 'rest'),
1309+
]
1310+
1311+
for input_ep, expected_ep, expected_transport in endpoints_and_expectations:
1312+
pipeline_options = PipelineOptions([
1313+
'--project',
1314+
'test-project',
1315+
'--temp_location',
1316+
'gs://test-location/temp',
1317+
'--dataflow_endpoint',
1318+
input_ep,
1319+
'--no_auth',
1320+
])
1321+
with mock.patch('apache_beam.runners.dataflow.internal.apiclient.dataflow'
1322+
'.JobsV1Beta3Client') as mock_jobs:
1323+
with mock.patch(
1324+
'apache_beam.runners.dataflow.internal.apiclient.dataflow'
1325+
'.MessagesV1Beta3Client'):
1326+
with mock.patch(
1327+
'apache_beam.runners.dataflow.internal.apiclient.dataflow'
1328+
'.MetricsV1Beta3Client'):
1329+
apiclient.DataflowApplicationClient(pipeline_options)
1330+
mock_jobs.assert_called_once()
1331+
called_kwargs = mock_jobs.call_args.kwargs
1332+
client_opts = called_kwargs.get('client_options')
1333+
self.assertIsNotNone(client_opts)
1334+
self.assertEqual(client_opts.api_endpoint, expected_ep)
1335+
self.assertEqual(called_kwargs.get('transport'), expected_transport)
1336+
13011337
def test_stage_resources(self):
13021338
pipeline_options = PipelineOptions([
13031339
'--temp_location',

0 commit comments

Comments
 (0)