|
| 1 | +from typing import Union |
| 2 | + |
| 3 | +from linode_api4.errors import UnexpectedResponseError |
| 4 | +from linode_api4.groups import Group |
| 5 | +from linode_api4.objects import Lock, LockType |
| 6 | + |
| 7 | +__all__ = ["LockGroup"] |
| 8 | + |
| 9 | + |
| 10 | +class LockGroup(Group): |
| 11 | + """ |
| 12 | + Encapsulates methods for interacting with Resource Locks. |
| 13 | +
|
| 14 | + Resource locks prevent deletion or modification of resources. |
| 15 | + Currently, only Linode instances can be locked. |
| 16 | + """ |
| 17 | + |
| 18 | + def __call__(self, *filters): |
| 19 | + """ |
| 20 | + Returns a list of all Resource Locks on the account. |
| 21 | +
|
| 22 | + This is intended to be called off of the :any:`LinodeClient` |
| 23 | + class, like this:: |
| 24 | +
|
| 25 | + locks = client.locks() |
| 26 | +
|
| 27 | + API Documentation: TBD |
| 28 | +
|
| 29 | + :param filters: Any number of filters to apply to this query. |
| 30 | + See :doc:`Filtering Collections</linode_api4/objects/filtering>` |
| 31 | + for more details on filtering. |
| 32 | +
|
| 33 | + :returns: A list of Resource Locks on the account. |
| 34 | + :rtype: PaginatedList of Lock |
| 35 | + """ |
| 36 | + return self.client._get_and_filter(Lock, *filters) |
| 37 | + |
| 38 | + def create( |
| 39 | + self, |
| 40 | + entity_type: str, |
| 41 | + entity_id: Union[int, str], |
| 42 | + lock_type: Union[LockType, str], |
| 43 | + ) -> Lock: |
| 44 | + """ |
| 45 | + Creates a new Resource Lock for the specified entity. |
| 46 | +
|
| 47 | + API Documentation: TBD |
| 48 | +
|
| 49 | + :param entity_type: The type of entity to lock (e.g., "linode"). |
| 50 | + :type entity_type: str |
| 51 | + :param entity_id: The ID of the entity to lock. |
| 52 | + :type entity_id: int | str |
| 53 | + :param lock_type: The type of lock to create. Defaults to "cannot_delete". |
| 54 | + :type lock_type: LockType | str |
| 55 | +
|
| 56 | + :returns: The newly created Resource Lock. |
| 57 | + :rtype: Lock |
| 58 | + """ |
| 59 | + params = { |
| 60 | + "entity_type": entity_type, |
| 61 | + "entity_id": entity_id, |
| 62 | + "lock_type": lock_type, |
| 63 | + } |
| 64 | + |
| 65 | + result = self.client.post("/locks", data=params) |
| 66 | + |
| 67 | + if "id" not in result: |
| 68 | + raise UnexpectedResponseError( |
| 69 | + "Unexpected response when creating lock!", json=result |
| 70 | + ) |
| 71 | + |
| 72 | + return Lock(self.client, result["id"], result) |
0 commit comments