Skip to content

File tree

4 files changed

+491
-10
lines changed

4 files changed

+491
-10
lines changed

hcloud/storage_boxes/client.py

Lines changed: 279 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import TYPE_CHECKING, Any, NamedTuple
44

5-
from ..actions import BoundAction
5+
from ..actions import ActionsPageResult, BoundAction, ResourceActionsClient
66
from ..core import BoundModelBase, Meta, ResourceClientBase
77
from ..locations import BoundLocation, Location
88
from ..storage_box_types import BoundStorageBoxType, StorageBoxType
@@ -12,6 +12,7 @@
1212
StorageBox,
1313
StorageBoxAccessSettings,
1414
StorageBoxFoldersResponse,
15+
StorageBoxSnapshot,
1516
StorageBoxSnapshotPlan,
1617
StorageBoxStats,
1718
)
@@ -55,6 +56,52 @@ def __init__(
5556

5657
super().__init__(client, data, complete)
5758

59+
def get_actions_list(
60+
self,
61+
*,
62+
status: list[str] | None = None,
63+
sort: list[str] | None = None,
64+
page: int | None = None,
65+
per_page: int | None = None,
66+
) -> ActionsPageResult:
67+
"""
68+
Returns all Actions for the Storage Box for a specific page.
69+
70+
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-list-actions
71+
72+
:param status: Filter the actions by status. The response will only contain actions matching the specified statuses.
73+
:param sort: Sort resources by field and direction.
74+
:param page: Page number to return.
75+
:param per_page: Maximum number of entries returned per page.
76+
"""
77+
return self._client.get_actions_list(
78+
self,
79+
status=status,
80+
sort=sort,
81+
page=page,
82+
per_page=per_page,
83+
)
84+
85+
def get_actions(
86+
self,
87+
*,
88+
status: list[str] | None = None,
89+
sort: list[str] | None = None,
90+
) -> list[BoundAction]:
91+
"""
92+
Returns all Actions for the Storage Box.
93+
94+
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-list-actions
95+
96+
:param status: Filter the actions by status. The response will only contain actions matching the specified statuses.
97+
:param sort: Sort resources by field and direction.
98+
"""
99+
return self._client.get_actions(
100+
self,
101+
status=status,
102+
sort=sort,
103+
)
104+
58105
# TODO: implement bound methods
59106

60107

@@ -72,9 +119,16 @@ class StorageBoxesClient(ResourceClientBase):
72119

73120
_base_url = "/storage_boxes"
74121

122+
actions: ResourceActionsClient
123+
"""Storage Boxes scoped actions client
124+
125+
:type: :class:`ResourceActionsClient <hcloud.actions.client.ResourceActionsClient>`
126+
"""
127+
75128
def __init__(self, client: Client):
76129
super().__init__(client)
77130
self._client = client._client_hetzner
131+
self.actions = ResourceActionsClient(self, self._base_url)
78132

79133
def get_by_id(self, id: int) -> BoundStorageBox:
80134
"""
@@ -241,7 +295,7 @@ def delete(
241295
"""
242296
Deletes a Storage Box.
243297
244-
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-delete-storage-box
298+
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-delete-a-storage-box
245299
246300
:param storage_box: Storage Box to delete.
247301
"""
@@ -264,7 +318,7 @@ def get_folders(
264318
265319
Files are not part of the response.
266320
267-
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-list-content-of-storage-box
321+
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-list-folders-of-a-storage-box
268322
269323
:param storage_box: Storage Box to list the folders from.
270324
:param path: Relative path to list the folders from.
@@ -280,3 +334,225 @@ def get_folders(
280334
)
281335

282336
return StorageBoxFoldersResponse(folders=response["folders"])
337+
338+
def get_actions_list(
339+
self,
340+
storage_box: StorageBox | BoundStorageBox,
341+
*,
342+
status: list[str] | None = None,
343+
sort: list[str] | None = None,
344+
page: int | None = None,
345+
per_page: int | None = None,
346+
) -> ActionsPageResult:
347+
"""
348+
Returns all Actions for a Storage Box for a specific page.
349+
350+
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-list-actions-for-a-storage-box
351+
352+
:param storage_box: Storage Box to fetch the Actions from.
353+
:param status: Filter the actions by status. The response will only contain actions matching the specified statuses.
354+
:param sort: Sort resources by field and direction.
355+
:param page: Page number to return.
356+
:param per_page: Maximum number of entries returned per page.
357+
"""
358+
params: dict[str, Any] = {}
359+
if status is not None:
360+
params["status"] = status
361+
if sort is not None:
362+
params["sort"] = sort
363+
if page is not None:
364+
params["page"] = page
365+
if per_page is not None:
366+
params["per_page"] = per_page
367+
368+
response = self._client.request(
369+
method="GET",
370+
url=f"/storage_boxes/{storage_box.id}/actions",
371+
params=params,
372+
)
373+
return ActionsPageResult(
374+
actions=[BoundAction(self._parent.actions, o) for o in response["actions"]],
375+
meta=Meta.parse_meta(response),
376+
)
377+
378+
def get_actions(
379+
self,
380+
storage_box: StorageBox | BoundStorageBox,
381+
*,
382+
status: list[str] | None = None,
383+
sort: list[str] | None = None,
384+
) -> list[BoundAction]:
385+
"""
386+
Returns all Actions for a Storage Box.
387+
388+
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-list-actions-for-a-storage-box
389+
390+
:param storage_box: Storage Box to fetch the Actions from.
391+
:param status: Filter the actions by status. The response will only contain actions matching the specified statuses.
392+
:param sort: Sort resources by field and direction.
393+
"""
394+
return self._iter_pages(
395+
self.get_actions_list,
396+
storage_box,
397+
status=status,
398+
sort=sort,
399+
)
400+
401+
def change_protection(
402+
self,
403+
storage_box: StorageBox | BoundStorageBox,
404+
*,
405+
delete: bool | None = None,
406+
) -> BoundAction:
407+
"""
408+
Changes the protection of a Storage Box.
409+
410+
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-change-protection
411+
412+
:param storage_box: Storage Box to update.
413+
:param delete: Prevents the Storage Box from being deleted.
414+
"""
415+
data: dict[str, Any] = {}
416+
if delete is not None:
417+
data["delete"] = delete
418+
419+
response = self._client.request(
420+
method="POST",
421+
url=f"{self._base_url}/{storage_box.id}/actions/change_protection",
422+
json=data,
423+
)
424+
return BoundAction(self._parent.actions, response["action"])
425+
426+
def change_type(
427+
self,
428+
storage_box: StorageBox | BoundStorageBox,
429+
storage_box_type: StorageBoxType | BoundStorageBoxType,
430+
) -> BoundAction:
431+
"""
432+
Changes the type of a Storage Box.
433+
434+
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-change-type
435+
436+
:param storage_box: Storage Box to update.
437+
:param storage_box_type: Storage Box Type to change to.
438+
"""
439+
data: dict[str, Any] = {
440+
"storage_box_type": storage_box_type.id_or_name,
441+
}
442+
443+
response = self._client.request(
444+
method="POST",
445+
url=f"{self._base_url}/{storage_box.id}/actions/change_type",
446+
json=data,
447+
)
448+
return BoundAction(self._parent.actions, response["action"])
449+
450+
def reset_password(
451+
self,
452+
storage_box: StorageBox | BoundStorageBox,
453+
*,
454+
password: str,
455+
) -> BoundAction:
456+
"""
457+
Reset the password of a Storage Box.
458+
459+
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-reset-password
460+
461+
:param storage_box: Storage Box to update.
462+
:param password: New password.
463+
"""
464+
data: dict[str, Any] = {
465+
"password": password,
466+
}
467+
468+
response = self._client.request(
469+
method="POST",
470+
url=f"{self._base_url}/{storage_box.id}/actions/reset_password",
471+
json=data,
472+
)
473+
return BoundAction(self._parent.actions, response["action"])
474+
475+
def update_access_settings(
476+
self,
477+
storage_box: StorageBox | BoundStorageBox,
478+
access_settings: StorageBoxAccessSettings,
479+
) -> BoundAction:
480+
"""
481+
Reset the password of a Storage Box.
482+
483+
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-update-access-settings
484+
485+
:param storage_box: Storage Box to update.
486+
:param access_settings: New access settings for the Storage Box.
487+
"""
488+
data: dict[str, Any] = access_settings.to_payload()
489+
490+
response = self._client.request(
491+
method="POST",
492+
url=f"{self._base_url}/{storage_box.id}/actions/update_access_settings",
493+
json=data,
494+
)
495+
return BoundAction(self._parent.actions, response["action"])
496+
497+
def rollback_snapshot(
498+
self,
499+
storage_box: StorageBox | BoundStorageBox,
500+
snapshot: StorageBoxSnapshot, # TODO: Add BoundStorageBoxSnapshot
501+
) -> BoundAction:
502+
"""
503+
Rollback the Storage Box to the given snapshot.
504+
505+
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-rollback-snapshot
506+
507+
:param storage_box: Storage Box to update.
508+
:param snapshot: Snapshot to rollback to.
509+
"""
510+
data: dict[str, Any] = {
511+
"snapshot": snapshot.id_or_name,
512+
}
513+
514+
response = self._client.request(
515+
method="POST",
516+
url=f"{self._base_url}/{storage_box.id}/actions/rollback_snapshot",
517+
json=data,
518+
)
519+
return BoundAction(self._parent.actions, response["action"])
520+
521+
def disable_snapshot_plan(
522+
self,
523+
storage_box: StorageBox | BoundStorageBox,
524+
) -> BoundAction:
525+
"""
526+
Disable the snapshot plan a Storage Box.
527+
528+
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-disable-snapshot-plan
529+
530+
:param storage_box: Storage Box to update.
531+
"""
532+
response = self._client.request(
533+
method="POST",
534+
url=f"{self._base_url}/{storage_box.id}/actions/disable_snapshot_plan",
535+
)
536+
return BoundAction(self._parent.actions, response["action"])
537+
538+
def enable_snapshot_plan(
539+
self,
540+
storage_box: StorageBox | BoundStorageBox,
541+
snapshot_plan: StorageBoxSnapshotPlan,
542+
) -> BoundAction:
543+
"""
544+
Enable the snapshot plan a Storage Box.
545+
546+
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-enable-snapshot-plan
547+
548+
:param storage_box: Storage Box to update.
549+
:param snapshot_plan: Snapshot Plan to enable.
550+
"""
551+
data: dict[str, Any] = snapshot_plan.to_payload()
552+
553+
response = self._client.request(
554+
method="POST",
555+
url=f"{self._base_url}/{storage_box.id}/actions/enable_snapshot_plan",
556+
json=data,
557+
)
558+
return BoundAction(self._parent.actions, response["action"])

0 commit comments

Comments
 (0)