Skip to content

Commit 714adc0

Browse files
authored
feat(instance): add server action utils (#75)
1 parent 47ca0b0 commit 714adc0

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

.github/workflows/checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
- name: Install dependencies and library
5757
run: poetry install
5858
- name: Check typing
59-
run: poetry run mypy --install-types --non-interactive --strict $(echo "${{ matrix.lib }}" | tr "-" "_")
59+
run: poetry run mypy --install-types --non-interactive --strict --no-warn-unused-ignores $(echo "${{ matrix.lib }}" | tr "-" "_")
6060

6161
security:
6262
runs-on: ubuntu-latest

scaleway/scaleway/instance/v1/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This file was automatically generated. DO NOT EDIT.
22
# If you have any remark or suggestion do not hesitate to open an issue.
3+
from typing import TYPE_CHECKING
34
from .types import Arch
45
from .types import BootType
56
from .types import ImageState
@@ -115,6 +116,11 @@
115116
from .content import VOLUME_TRANSIENT_STATUSES
116117
from .api import InstanceV1API
117118

119+
try:
120+
from .api_utils import InstanceV1APIUtils as InstanceV1API # type: ignore[attr-defined]
121+
except ImportError:
122+
pass
123+
118124
__all__ = [
119125
"Arch",
120126
"BootType",
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from typing import Dict, Optional
2+
3+
from scaleway_core.bridge import Zone
4+
from scaleway_core.utils import validate_path_param
5+
6+
from .api import InstanceV1API
7+
from .marshalling import unmarshal_ServerActionResponse
8+
from .marshalling_utils import custom_marshal_ServerActionRequest
9+
from .types import (
10+
ServerAction,
11+
ServerActionRequest,
12+
ServerActionRequestVolumeBackupTemplate,
13+
ServerActionResponse,
14+
)
15+
16+
17+
class InstanceV1APIUtils(InstanceV1API):
18+
def server_action(
19+
self,
20+
*,
21+
server_id: str,
22+
action: ServerAction,
23+
zone: Optional[Zone] = None,
24+
name: Optional[str] = None,
25+
volumes: Optional[Dict[str, ServerActionRequestVolumeBackupTemplate]] = None,
26+
) -> ServerActionResponse:
27+
"""
28+
Perform power related actions on a server. Be wary that when terminating a server, all the attached volumes (local *and* block storage) are deleted. So, if you want to keep your local volumes, you must use the `archive` action instead of `terminate`. And if you want to keep block-storage volumes, **you must** detach it beforehand you issue the `terminate` call. For more information, read the [Volumes](#volumes-7e8a39) documentation.
29+
:param zone: Zone to target. If none is passed will use default zone from the config
30+
:param server_id: UUID of the server
31+
:param action: The action to perform on the server
32+
:param name: The name of the backup you want to create.
33+
This field should only be specified when performing a backup action.
34+
35+
:param volumes: For each volume UUID, the snapshot parameters of the volume.
36+
This field should only be specified when performing a backup action.
37+
38+
:return: :class:`ServerActionResponse <ServerActionResponse>`
39+
40+
Usage:
41+
::
42+
43+
result = api.server_action(
44+
server_id="example",
45+
action=poweron,
46+
)
47+
"""
48+
49+
param_zone = validate_path_param("zone", zone or self.client.default_zone)
50+
param_server_id = validate_path_param("server_id", server_id)
51+
52+
res = self._request(
53+
"POST",
54+
f"/instance/v1/zones/{param_zone}/servers/{param_server_id}/action",
55+
body=custom_marshal_ServerActionRequest(
56+
ServerActionRequest(
57+
server_id=server_id,
58+
action=action,
59+
zone=zone,
60+
name=name,
61+
volumes=volumes,
62+
),
63+
self.client,
64+
),
65+
)
66+
67+
self._throw_on_error(res)
68+
return unmarshal_ServerActionResponse(res.json())
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import Any, Dict
2+
3+
from scaleway_core.profile import ProfileDefaults
4+
5+
from .marshalling import marshal_ServerActionRequestVolumeBackupTemplate
6+
from .types import ServerAction, ServerActionRequest
7+
8+
9+
def custom_marshal_ServerActionRequest(
10+
request: ServerActionRequest,
11+
defaults: ProfileDefaults,
12+
) -> Dict[str, Any]:
13+
output: Dict[str, Any] = {
14+
"action": ServerAction(request.action),
15+
}
16+
17+
if request.name is not None:
18+
output["name"] = request.name
19+
20+
if request.volumes is not None:
21+
output["volumes"] = {
22+
k: marshal_ServerActionRequestVolumeBackupTemplate(v, defaults)
23+
for k, v in request.volumes.items()
24+
}
25+
26+
return output

0 commit comments

Comments
 (0)