Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement delete of a resource #1800

Merged
merged 7 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions plugins/azure/resoto_plugin_azure/azure_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -66,8 +70,16 @@ 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.client.resources.begin_delete_by_id(resource_id=resource_id, api_version="2021-04-01")
except HttpResponseError as e:
if e.error and e.error.code == "ResourceNotFoundError":
return False # Resource not found to delete
else:
raise e

return True

# noinspection PyProtectedMember
def _call(self, spec: AzureApiSpec, **kwargs: Any) -> List[Json]:
Expand Down
28 changes: 22 additions & 6 deletions plugins/azure/resoto_plugin_azure/resource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@
import logging
from concurrent.futures import Future
from threading import Lock
from typing import Any, ClassVar, Dict, Optional, TypeVar, List, Type, Callable
from typing import Any, ClassVar, Dict, Optional, TypeVar, List, Type, Callable, cast

from attr import define, field
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
from resotolib.json_bender import Bender, bend, S, ForallBend, Bend
from resotolib.threading import ExecutorQueue
from resotolib.types import Json
from resotolib.config import current_config

log = logging.getLogger("resoto.plugins.azure")


def get_client(subscription_id: str) -> AzureClient:
config = current_config()
azure_config = cast(AzureAccountConfig, config.azure)
return AzureClient.create(credential=azure_config.credentials(), subscription_id=subscription_id)


T = TypeVar("T")


Expand All @@ -28,10 +37,17 @@ 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: Graph) -> bool:
"""
Deletes a resource by ID.

Returns:
bool: True if the resource was successfully deleted; False otherwise.
"""
# Extracts {subscriptionId} value from a resource_id
# e.g /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/...
subscription_id = self.id.split("/")[2]
aquamatthias marked this conversation as resolved.
Show resolved Hide resolved
return get_client(subscription_id).delete(self.id)

def pre_process(self, graph_builder: GraphBuilder, source: Json) -> None:
"""
Expand Down
3 changes: 3 additions & 0 deletions plugins/azure/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading