This is an automated email from the ASF dual-hosted git repository.

rahulvats pushed a commit to branch py-client-sync
in repository https://gitbox.apache.org/repos/asf/airflow.git

commit 52dac018db07377d71855cbd0bfa0ba67efbb83f
Author: SameerMesiah97 <[email protected]>
AuthorDate: Mon Mar 23 21:47:13 2026 +0000

    Replace the scan of container groups in a resource group with a direct in 
AzureContainerInstanceHook (#63567)
    
    container_groups.get(resource_group, name) call. This avoids listing and
    pagination when checking whether a container group exists.
    
    Update tests to mock container_groups.get and cover success, not found, and 
error cases.
    
    Co-authored-by: Sameer Mesiah <[email protected]>
---
 .../microsoft/azure/hooks/container_instance.py    | 10 +++---
 .../azure/hooks/test_container_instance.py         | 40 +++++++++++-----------
 2 files changed, 26 insertions(+), 24 deletions(-)

diff --git 
a/providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/container_instance.py
 
b/providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/container_instance.py
index 08aa6251f2e..bfb7940ec21 100644
--- 
a/providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/container_instance.py
+++ 
b/providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/container_instance.py
@@ -21,6 +21,7 @@ from functools import cached_property
 from typing import TYPE_CHECKING, Any, cast
 
 from azure.common.client_factory import get_client_from_auth_file, 
get_client_from_json_dict
+from azure.core.exceptions import ResourceNotFoundError
 from azure.identity import ClientSecretCredential, DefaultAzureCredential
 from azure.mgmt.containerinstance import ContainerInstanceManagementClient
 
@@ -154,10 +155,11 @@ class AzureContainerInstanceHook(AzureBaseHook):
         :param resource_group: the name of the resource group
         :param name: the name of the container group
         """
-        for container in 
self.connection.container_groups.list_by_resource_group(resource_group):
-            if container.name == name:
-                return True
-        return False
+        try:
+            self.connection.container_groups.get(resource_group, name)
+            return True
+        except ResourceNotFoundError:
+            return False
 
     def test_connection(self):
         """Test a configured Azure Container Instance connection."""
diff --git 
a/providers/microsoft/azure/tests/unit/microsoft/azure/hooks/test_container_instance.py
 
b/providers/microsoft/azure/tests/unit/microsoft/azure/hooks/test_container_instance.py
index 38b2c743d57..850409dcc3f 100644
--- 
a/providers/microsoft/azure/tests/unit/microsoft/azure/hooks/test_container_instance.py
+++ 
b/providers/microsoft/azure/tests/unit/microsoft/azure/hooks/test_container_instance.py
@@ -20,9 +20,8 @@ from __future__ import annotations
 from unittest.mock import MagicMock, patch
 
 import pytest
+from azure.core.exceptions import ResourceNotFoundError
 from azure.mgmt.containerinstance.models import (
-    Container,
-    ContainerGroup,
     Logs,
     ResourceRequests,
     ResourceRequirements,
@@ -96,25 +95,26 @@ class TestAzureContainerInstanceHook:
         self.hook.delete("resource_group", "aci-test")
         delete_mock.assert_called_once_with("resource_group", "aci-test")
 
-    
@patch("azure.mgmt.containerinstance.operations.ContainerGroupsOperations.list_by_resource_group")
-    def test_exists_with_existing(self, list_mock):
-        list_mock.return_value = [
-            ContainerGroup(
-                os_type="Linux",
-                containers=[Container(name="test1", image="hello-world", 
resources=self.resources)],
-            )
-        ]
-        assert not self.hook.exists("test", "test1")
-
-    
@patch("azure.mgmt.containerinstance.operations.ContainerGroupsOperations.list_by_resource_group")
-    def test_exists_with_not_existing(self, list_mock):
-        list_mock.return_value = [
-            ContainerGroup(
-                os_type="Linux",
-                containers=[Container(name="test1", image="hello-world", 
resources=self.resources)],
-            )
-        ]
+    
@patch("azure.mgmt.containerinstance.operations.ContainerGroupsOperations.get")
+    def test_exists_with_existing(self, get_mock):
+        get_mock.return_value = MagicMock()
+
+        assert self.hook.exists("test", "test1")
+        get_mock.assert_called_once_with("test", "test1")
+
+    
@patch("azure.mgmt.containerinstance.operations.ContainerGroupsOperations.get")
+    def test_exists_with_not_existing(self, get_mock):
+        get_mock.side_effect = ResourceNotFoundError("not found")
+
         assert not self.hook.exists("test", "not found")
+        get_mock.assert_called_once_with("test", "not found")
+
+    
@patch("azure.mgmt.containerinstance.operations.ContainerGroupsOperations.get")
+    def test_exists_unexpected_exception(self, get_mock):
+        get_mock.side_effect = RuntimeError("Unexpected Exception")
+
+        with pytest.raises(RuntimeError):
+            self.hook.exists("test", "test")
 
     
@patch("azure.mgmt.containerinstance.operations.ContainerGroupsOperations.list")
     def test_connection_success(self, mock_container_groups_list):

Reply via email to