Skip to content

Commit 9ff2e33

Browse files
authored
Merge branch 'dev' into optional-nodepools
2 parents 966859c + 6ed9f7d commit 9ff2e33

File tree

13 files changed

+424
-0
lines changed

13 files changed

+424
-0
lines changed

docs/linode_api4/linode_client.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ Includes methods for interacting with our Longview service.
125125
:members:
126126
:special-members:
127127

128+
LockGroup
129+
^^^^^^^^^^^^^
130+
131+
Includes methods for interacting with our Lock service.
132+
133+
.. autoclass:: linode_api4.linode_client.LockGroup
134+
:members:
135+
:special-members:
136+
128137
NetworkingGroup
129138
^^^^^^^^^^^^^^^
130139

linode_api4/groups/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .linode import *
1010
from .lke import *
1111
from .lke_tier import *
12+
from .lock import *
1213
from .longview import *
1314
from .maintenance import *
1415
from .monitor import *

linode_api4/groups/lock.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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)

linode_api4/linode_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
ImageGroup,
1919
LinodeGroup,
2020
LKEGroup,
21+
LockGroup,
2122
LongviewGroup,
2223
MaintenanceGroup,
2324
MetricsGroup,
@@ -454,6 +455,9 @@ def __init__(
454455

455456
self.monitor = MonitorGroup(self)
456457

458+
#: Access methods related to Resource Locks - See :any:`LockGroup` for more information.
459+
self.locks = LockGroup(self)
460+
457461
super().__init__(
458462
token=token,
459463
base_url=base_url,

linode_api4/objects/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
from .placement import *
2525
from .monitor import *
2626
from .monitor_api import *
27+
from .lock import *

linode_api4/objects/linode.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ class Instance(Base):
803803
"maintenance_policy": Property(
804804
mutable=True
805805
), # Note: This field is only available when using v4beta.
806+
"locks": Property(unordered=True),
806807
}
807808

808809
@property

linode_api4/objects/lock.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from dataclasses import dataclass
2+
3+
from linode_api4.objects.base import Base, Property
4+
from linode_api4.objects.serializable import JSONObject, StrEnum
5+
6+
__all__ = ["LockType", "LockEntity", "Lock"]
7+
8+
9+
class LockType(StrEnum):
10+
"""
11+
LockType defines valid values for resource lock types.
12+
13+
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-lock
14+
"""
15+
16+
cannot_delete = "cannot_delete"
17+
cannot_delete_with_subresources = "cannot_delete_with_subresources"
18+
19+
20+
@dataclass
21+
class LockEntity(JSONObject):
22+
"""
23+
Represents the entity that is locked.
24+
25+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-lock
26+
"""
27+
28+
id: int = 0
29+
type: str = ""
30+
label: str = ""
31+
url: str = ""
32+
33+
34+
class Lock(Base):
35+
"""
36+
A resource lock that prevents deletion or modification of a resource.
37+
38+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-lock
39+
"""
40+
41+
api_endpoint = "/locks/{id}"
42+
43+
properties = {
44+
"id": Property(identifier=True),
45+
"lock_type": Property(),
46+
"entity": Property(json_object=LockEntity),
47+
}

test/fixtures/locks.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"data": [
3+
{
4+
"id": 1,
5+
"lock_type": "cannot_delete",
6+
"entity": {
7+
"id": 123,
8+
"type": "linode",
9+
"label": "test-linode",
10+
"url": "/v4/linode/instances/123"
11+
}
12+
},
13+
{
14+
"id": 2,
15+
"lock_type": "cannot_delete_with_subresources",
16+
"entity": {
17+
"id": 456,
18+
"type": "linode",
19+
"label": "another-linode",
20+
"url": "/v4/linode/instances/456"
21+
}
22+
}
23+
],
24+
"page": 1,
25+
"pages": 1,
26+
"results": 2
27+
}

test/fixtures/locks_1.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"id": 1,
3+
"lock_type": "cannot_delete",
4+
"entity": {
5+
"id": 123,
6+
"type": "linode",
7+
"label": "test-linode",
8+
"url": "/v4/linode/instances/123"
9+
}
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This file is intentionally left empty to make the directory a Python package.

0 commit comments

Comments
 (0)