-
Notifications
You must be signed in to change notification settings - Fork 78
Add support for resource lock #624
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
Draft
zliang-akamai
wants to merge
3
commits into
linode:dev
Choose a base branch
from
zliang-akamai:zhiwei/resource-lock
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+596
−0
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,3 +24,4 @@ | |
| from .placement import * | ||
| from .monitor import * | ||
| from .monitor_api import * | ||
| from .lock import * | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| from dataclasses import dataclass | ||
|
|
||
| from linode_api4.objects.base import Base, Property | ||
| from linode_api4.objects.serializable import JSONObject, StrEnum | ||
|
|
||
|
|
||
| class LockType(StrEnum): | ||
| """ | ||
| LockType defines valid values for resource lock types. | ||
|
|
||
| API Documentation: https://techdocs.akamai.com/linode-api/reference/post-lock | ||
| """ | ||
|
|
||
| cannot_delete = "cannot_delete" | ||
| cannot_delete_with_subresources = "cannot_delete_with_subresources" | ||
|
|
||
|
|
||
| @dataclass | ||
| class LockEntity(JSONObject): | ||
| """ | ||
| Represents the entity that is locked. | ||
|
|
||
| API Documentation: https://techdocs.akamai.com/linode-api/reference/get-lock | ||
| """ | ||
|
|
||
| id: int = 0 | ||
| type: str = "" | ||
| label: str = "" | ||
| url: str = "" | ||
|
|
||
|
|
||
| class Lock(Base): | ||
| """ | ||
| A resource lock that prevents deletion or modification of a resource. | ||
|
|
||
| API Documentation: https://techdocs.akamai.com/linode-api/reference/get-lock | ||
| """ | ||
|
|
||
| api_endpoint = "/locks/{id}" | ||
|
|
||
| properties = { | ||
| "id": Property(identifier=True), | ||
| "lock_type": Property(), | ||
| "entity": Property(json_object=LockEntity), | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "data": [ | ||
| { | ||
| "id": 1, | ||
| "lock_type": "cannot_delete", | ||
| "entity": { | ||
| "id": 123, | ||
| "type": "linode", | ||
| "label": "test-linode", | ||
| "url": "/v4/linode/instances/123" | ||
| } | ||
| }, | ||
| { | ||
| "id": 2, | ||
| "lock_type": "cannot_delete_with_subresources", | ||
| "entity": { | ||
| "id": 456, | ||
| "type": "linode", | ||
| "label": "another-linode", | ||
| "url": "/v4/linode/instances/456" | ||
| } | ||
| } | ||
| ], | ||
| "page": 1, | ||
| "pages": 1, | ||
| "results": 2 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "id": 1, | ||
| "lock_type": "cannot_delete", | ||
| "entity": { | ||
| "id": 123, | ||
| "type": "linode", | ||
| "label": "test-linode", | ||
| "url": "/v4/linode/instances/123" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| from test.unit.base import ClientBaseCase | ||
|
|
||
| from linode_api4.objects.lock import Lock, LockEntity, LockType | ||
|
|
||
|
|
||
| class LockTest(ClientBaseCase): | ||
| """ | ||
| Tests methods of the Lock class | ||
| """ | ||
|
|
||
| def test_get_lock(self): | ||
| """ | ||
| Tests that a lock is loaded correctly by ID | ||
| """ | ||
| lock = Lock(self.client, 1) | ||
|
|
||
| self.assertEqual(lock.id, 1) | ||
| self.assertEqual(lock.lock_type, "cannot_delete") | ||
| self.assertIsInstance(lock.entity, LockEntity) | ||
| self.assertEqual(lock.entity.id, 123) | ||
| self.assertEqual(lock.entity.type, "linode") | ||
| self.assertEqual(lock.entity.label, "test-linode") | ||
| self.assertEqual(lock.entity.url, "/v4/linode/instances/123") | ||
|
|
||
| def test_get_locks(self): | ||
| """ | ||
| Tests that locks can be retrieved | ||
| """ | ||
| locks = self.client.account.locks() | ||
|
|
||
| self.assertEqual(len(locks), 2) | ||
| self.assertEqual(locks[0].id, 1) | ||
| self.assertEqual(locks[0].lock_type, "cannot_delete") | ||
| self.assertEqual(locks[1].id, 2) | ||
| self.assertEqual(locks[1].lock_type, "cannot_delete_with_subresources") | ||
|
|
||
| def test_create_lock(self): | ||
| """ | ||
| Tests that a lock can be created | ||
| """ | ||
| with self.mock_post("/locks/1") as m: | ||
| lock = self.client.account.lock_create( | ||
| "linode", 123, LockType.cannot_delete | ||
| ) | ||
|
|
||
| self.assertEqual(m.call_url, "/locks") | ||
| self.assertEqual(m.call_data["entity_type"], "linode") | ||
| self.assertEqual(m.call_data["entity_id"], 123) | ||
| self.assertEqual(m.call_data["lock_type"], "cannot_delete") | ||
|
|
||
| self.assertEqual(lock.id, 1) | ||
| self.assertEqual(lock.lock_type, "cannot_delete") | ||
|
|
||
| def test_delete_lock(self): | ||
| """ | ||
| Tests that a lock can be deleted | ||
| """ | ||
| lock = Lock(self.client, 1) | ||
|
|
||
| with self.mock_delete() as m: | ||
| lock.delete() | ||
|
|
||
| self.assertEqual(m.call_url, "/locks/1") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.