|
| 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()) |
0 commit comments