diff --git a/plugins/azure/resoto_plugin_azure/azure_client.py b/plugins/azure/resoto_plugin_azure/azure_client.py index 0c50f8f14b..a9b85e6a78 100644 --- a/plugins/azure/resoto_plugin_azure/azure_client.py +++ b/plugins/azure/resoto_plugin_azure/azure_client.py @@ -41,6 +41,10 @@ def list(self, spec: AzureApiSpec, **kwargs: Any) -> List[Json]: def for_location(self, location: str) -> AzureClient: pass + @abstractmethod + def delete(self, resource_id: str) -> bool: + pass + @staticmethod def __create_management_client( credential: AzureCredentials, subscription_id: str, resource_group: Optional[str] = None @@ -66,8 +70,19 @@ def list(self, spec: AzureApiSpec, **kwargs: Any) -> List[Json]: else: raise e - def delete(self, resource_id: str) -> None: - self.client.resources.delete_by_id(resource_id) + def delete(self, resource_id: str) -> bool: + try: + self._delete_by_id(resource_id, api_version="2021-04-01") + except HttpResponseError as e: + if e.error and e.error.code == "ResourceNotFoundError": + return False + else: + raise e + + return True + + def _delete_by_id(self, resource_id: str, api_version: str) -> None: + self.client.resources._delete_by_id_initial(resource_id=resource_id, api_version=api_version) # noinspection PyProtectedMember def _call(self, spec: AzureApiSpec, **kwargs: Any) -> List[Json]: diff --git a/plugins/azure/resoto_plugin_azure/resource/base.py b/plugins/azure/resoto_plugin_azure/resource/base.py index 8eb21a3bc1..3c2ca8813f 100644 --- a/plugins/azure/resoto_plugin_azure/resource/base.py +++ b/plugins/azure/resoto_plugin_azure/resource/base.py @@ -9,7 +9,7 @@ from azure.core.utils import CaseInsensitiveDict from resoto_plugin_azure.azure_client import AzureApiSpec, AzureClient -from resoto_plugin_azure.config import AzureCredentials +from resoto_plugin_azure.config import AzureCredentials, AzureAccountConfig from resotolib.baseresources import BaseResource, Cloud, EdgeType, BaseAccount, BaseRegion, ModelReference from resotolib.core.actions import CoreFeedback from resotolib.graph import Graph, EdgeKey @@ -18,6 +18,14 @@ from resotolib.types import Json log = logging.getLogger("resoto.plugins.azure") + + +def get_client(builder: GraphBuilder) -> AzureClient: + subscription_id = builder.subscription.subscription_id + credential = AzureAccountConfig().credentials() + return AzureClient.create(credential=credential, subscription_id=subscription_id) + + T = TypeVar("T") @@ -28,10 +36,14 @@ class AzureResource(BaseResource): # Which API to call and what to expect in the result. api_spec: ClassVar[Optional[AzureApiSpec]] = None - def delete(self, graph: Any) -> bool: - # TODO: implement me. - # get_client().delete(self.id) - return False + def delete(self, graph: GraphBuilder) -> bool: + """ + Deletes a resource by ID. + + Returns: + bool: True if the resource was successfully deleted; False otherwise. + """ + return get_client(graph).delete(self.id) def pre_process(self, graph_builder: GraphBuilder, source: Json) -> None: """ diff --git a/plugins/azure/test/conftest.py b/plugins/azure/test/conftest.py index bc97daa8ad..4841ad2545 100644 --- a/plugins/azure/test/conftest.py +++ b/plugins/azure/test/conftest.py @@ -36,6 +36,9 @@ def create(*args: Any, **kwargs: Any) -> StaticFileAzureClient: def for_location(self, location: str) -> AzureClient: return self + def delete(self, resource_id: str) -> bool: + return False + @fixture def config() -> AzureConfig: