Skip to content

Commit 9f3d082

Browse files
committed
fix(functions): Support EXT_INSTANCE_ID in current scope and improve fallback warning suggestion
Updated _resolve_resource to support resolving current scope using EXT_INSTANCE_ID (checking it first before falling back to FIREBASE_KIT_INSTANCE_ID) for completeness and consistency. Updated _log_fallback_warning message to suggest the more idiomatic functions.FunctionScope.
1 parent cf3590f commit 9f3d082

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

firebase_admin/functions.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,12 @@ def _resolve_resource(self, scope: FunctionScope) -> Tuple[Resource, Optional[st
248248
extension_or_kit_id = None
249249

250250
if scope.type == 'current':
251+
ext_instance_id = os.environ.get('EXT_INSTANCE_ID')
251252
kit_instance_id = os.environ.get('FIREBASE_KIT_INSTANCE_ID')
252-
if kit_instance_id:
253+
if ext_instance_id:
254+
resource_id = f'ext-{ext_instance_id}-{resource_id}'
255+
extension_or_kit_id = ext_instance_id
256+
elif kit_instance_id:
253257
resource_id = f'kit-{kit_instance_id}-{resource_id}'
254258
extension_or_kit_id = kit_instance_id
255259
elif scope.type == 'global':
@@ -278,7 +282,7 @@ def _log_fallback_warning(self, function_name: str, instance: str) -> None:
278282
f"Targeting kit {instance} with the legacy extensions API, "
279283
"which has performance implications. Please change the call "
280284
f"task_queue('{function_name}', '{instance}') to "
281-
f"task_queue('{function_name}', FunctionScope('kit', '{instance}'))",
285+
f"task_queue('{function_name}', functions.FunctionScope('kit', '{instance}'))",
282286
UserWarning,
283287
stacklevel=3
284288
)

tests/test_functions.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,21 @@ def test_enqueue_current_scope_default(self):
290290
assert recorder[0].url == expected_url
291291

292292
def test_enqueue_current_scope_env_vars_ext(self, monkeypatch):
293-
# Python SDK does not handle EXT_INSTANCE_ID since no extensions use Python
294293
monkeypatch.setenv('EXT_INSTANCE_ID', 'my-ext')
295-
_, recorder = self._instrument_functions_service()
294+
expected_queue = 'ext-my-ext-test-function-name'
295+
response_payload = json.dumps({
296+
'name': (
297+
f'projects/test-project/locations/us-central1/'
298+
f'queues/{expected_queue}/tasks/test-task-id'
299+
)
300+
})
301+
_, recorder = self._instrument_functions_service(payload=response_payload)
296302
queue = functions.task_queue('test-function-name')
297303
queue.enqueue(_DEFAULT_DATA)
298304
assert len(recorder) == 1
299305
expected_url = (
300306
_CLOUD_TASKS_URL + 'projects/test-project/locations/us-central1/'
301-
'queues/test-function-name/tasks'
307+
f'queues/{expected_queue}/tasks'
302308
)
303309
assert recorder[0].url == expected_url
304310

@@ -321,6 +327,26 @@ def test_enqueue_current_scope_env_vars_kit(self, monkeypatch):
321327
)
322328
assert recorder[0].url == expected_url
323329

330+
def test_enqueue_current_scope_env_vars_precedence(self, monkeypatch):
331+
monkeypatch.setenv('EXT_INSTANCE_ID', 'my-ext')
332+
monkeypatch.setenv('FIREBASE_KIT_INSTANCE_ID', 'my-kit')
333+
expected_queue = 'ext-my-ext-test-function-name'
334+
response_payload = json.dumps({
335+
'name': (
336+
f'projects/test-project/locations/us-central1/'
337+
f'queues/{expected_queue}/tasks/test-task-id'
338+
)
339+
})
340+
_, recorder = self._instrument_functions_service(payload=response_payload)
341+
queue = functions.task_queue('test-function-name')
342+
queue.enqueue(_DEFAULT_DATA)
343+
assert len(recorder) == 1
344+
expected_url = (
345+
_CLOUD_TASKS_URL + 'projects/test-project/locations/us-central1/'
346+
f'queues/{expected_queue}/tasks'
347+
)
348+
assert recorder[0].url == expected_url
349+
324350
def test_enqueue_global_scope(self):
325351
_, recorder = self._instrument_functions_service()
326352
queue = functions.task_queue(
@@ -392,8 +418,8 @@ def test_enqueue_fallback_flow(self):
392418
task_id = queue.enqueue(_DEFAULT_DATA)
393419
assert task_id == 'task-123'
394420
assert queue._scope.type == 'extension_or_kit'
395-
assert len(record) == 1
396421
assert "Targeting kit my-instance with the legacy extensions API" in str(record[0].message)
422+
assert "functions.FunctionScope('kit', 'my-instance')" in str(record[0].message)
397423
assert len(recorder) == 2
398424
assert 'ext-my-instance-test-function-name' in recorder[0].url
399425
assert 'kit-my-instance-test-function-name' in recorder[1].url
@@ -415,6 +441,7 @@ def test_delete_fallback_flow(self):
415441
assert queue._scope.type == 'extension_or_kit'
416442
assert len(record) == 1
417443
assert "Targeting kit my-instance with the legacy extensions API" in str(record[0].message)
444+
assert "functions.FunctionScope('kit', 'my-instance')" in str(record[0].message)
418445
assert len(recorder) == 2
419446
assert 'ext-my-instance-test-function-name/tasks/task-123' in recorder[0].url
420447
assert 'kit-my-instance-test-function-name/tasks/task-123' in recorder[1].url

0 commit comments

Comments
 (0)