Skip to content

Commit 59571dc

Browse files
committed
Add ObjectDeleteMixin to delete ApiObjects
Add `transip.mixins.ObjectDeleteMixin` to allow `transip.base.ApiObjects` instances to call `self.delete()`. Signed-off-by: Roald Nefs <info@roaldnefs.com>
1 parent 98ec7fe commit 59571dc

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

tests/services/test_ssh_keys.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ def test_delete(self) -> None:
6161
except Exception as exc:
6262
assert False, f"'transip.TransIP.ssh_keys.delete' raised an exception {exc}"
6363

64+
@responses.activate
65+
def test_delete_object(self) -> None:
66+
ssh_key_id: int = 123
67+
ssh_key: Type[SshKey] = self.client.ssh_keys.get(ssh_key_id)
68+
69+
try:
70+
ssh_key.delete() # type: ignore
71+
except Exception as exc:
72+
assert False, f"'transip.v6.objects.SshKey.delete' raised an exception {exc}"
73+
6474
@responses.activate
6575
def test_create(self) -> None:
6676
ssh_key_data: Dict[str, str] = {

transip/mixins.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from typing import Optional, List, Type, Dict, Any, Tuple, Union
2121

2222
from transip import TransIP
23-
from transip.base import ApiObject
23+
from transip.base import ApiObject, ApiService
2424

2525

2626
# Typing alias for the _create_attrs attribute in the CreateMixin
@@ -66,6 +66,16 @@ def delete(self, id: str, **kwargs) -> None:
6666
self.client.delete(f"{self.path}/{id}")
6767

6868

69+
class ObjectDeleteMixin:
70+
"""Delete a single ApiObject."""
71+
72+
service: ApiService
73+
74+
def delete(self) -> None:
75+
if self.get_id(): # type: ignore
76+
self.service.delete(self.get_id()) # type: ignore
77+
78+
6979
class ListMixin:
7080
"""Retrieve a list of ApiObjects.
7181

transip/v6/objects.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from transip.base import ApiService, ApiObject
2323
from transip.mixins import (
2424
GetMixin, DeleteMixin, ListMixin, CreateMixin, UpdateMixin,
25+
ObjectDeleteMixin,
2526
CreateAttrsTuple, UpdateAttrsTuple
2627
)
2728

@@ -39,7 +40,7 @@ class AvailabilityZoneService(ListMixin, ApiService):
3940
_resp_list_attr: str = "availabilityZones"
4041

4142

42-
class SshKey(ApiObject):
43+
class SshKey(ObjectDeleteMixin, ApiObject):
4344

4445
_id_attr: str = "id"
4546

0 commit comments

Comments
 (0)