-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathclient.py
More file actions
1127 lines (943 loc) · 37.7 KB
/
client.py
File metadata and controls
1127 lines (943 loc) · 37.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
from typing import TYPE_CHECKING, Any, NamedTuple
from ..actions import ActionsPageResult, BoundAction, ResourceActionsClient
from ..core import BoundModelBase, Meta, ResourceClientBase
from ..locations import BoundLocation, Location
from ..storage_box_types import BoundStorageBoxType, StorageBoxType
from .domain import (
CreateStorageBoxResponse,
CreateStorageBoxSnapshotResponse,
CreateStorageBoxSubaccountResponse,
DeleteStorageBoxResponse,
DeleteStorageBoxSnapshotResponse,
DeleteStorageBoxSubaccountResponse,
StorageBox,
StorageBoxAccessSettings,
StorageBoxFoldersResponse,
StorageBoxSnapshot,
StorageBoxSnapshotPlan,
StorageBoxSnapshotStats,
StorageBoxStats,
StorageBoxSubaccount,
StorageBoxSubaccountAccessSettings,
)
if TYPE_CHECKING:
from .._client import Client
class BoundStorageBox(BoundModelBase, StorageBox):
_client: StorageBoxesClient
model = StorageBox
def __init__(
self,
client: StorageBoxesClient,
data: dict[str, Any],
complete: bool = True,
):
raw = data.get("storage_box_type")
if raw is not None:
data["storage_box_type"] = BoundStorageBoxType(
client._parent.storage_box_types, raw
)
raw = data.get("location")
if raw is not None:
data["location"] = BoundLocation(client._parent.locations, raw)
raw = data.get("snapshot_plan")
if raw is not None:
data["snapshot_plan"] = StorageBoxSnapshotPlan.from_dict(raw)
raw = data.get("access_settings")
if raw is not None:
data["access_settings"] = StorageBoxAccessSettings.from_dict(raw)
raw = data.get("stats")
if raw is not None:
data["stats"] = StorageBoxStats.from_dict(raw)
super().__init__(client, data, complete)
def get_actions_list(
self,
*,
status: list[str] | None = None,
sort: list[str] | None = None,
page: int | None = None,
per_page: int | None = None,
) -> ActionsPageResult:
"""
Returns all Actions for the Storage Box for a specific page.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-list-actions
:param status: Filter the actions by status. The response will only contain actions matching the specified statuses.
:param sort: Sort resources by field and direction.
:param page: Page number to return.
:param per_page: Maximum number of entries returned per page.
"""
return self._client.get_actions_list(
self,
status=status,
sort=sort,
page=page,
per_page=per_page,
)
def get_actions(
self,
*,
status: list[str] | None = None,
sort: list[str] | None = None,
) -> list[BoundAction]:
"""
Returns all Actions for the Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-list-actions
:param status: Filter the actions by status. The response will only contain actions matching the specified statuses.
:param sort: Sort resources by field and direction.
"""
return self._client.get_actions(
self,
status=status,
sort=sort,
)
# TODO: implement bound methods
class BoundStorageBoxSnapshot(BoundModelBase, StorageBoxSnapshot):
_client: StorageBoxesClient
model = StorageBoxSnapshot
def __init__(
self,
client: StorageBoxesClient,
data: dict[str, Any],
complete: bool = True,
):
raw = data.get("storage_box")
if raw is not None:
data["storage_box"] = BoundStorageBox(
client, data={"id": raw}, complete=False
)
raw = data.get("stats")
if raw is not None:
data["stats"] = StorageBoxSnapshotStats.from_dict(raw)
super().__init__(client, data, complete)
def _get_self(self) -> BoundStorageBoxSnapshot:
return self._client.get_snapshot_by_id(
self.data_model.storage_box,
self.data_model.id,
)
# TODO: implement bound methods
class BoundStorageBoxSubaccount(BoundModelBase, StorageBoxSubaccount):
_client: StorageBoxesClient
model = StorageBoxSubaccount
def __init__(
self,
client: StorageBoxesClient,
data: dict[str, Any],
complete: bool = True,
):
raw = data.get("storage_box")
if raw is not None:
data["storage_box"] = BoundStorageBox(
client, data={"id": raw}, complete=False
)
raw = data.get("access_settings")
if raw is not None:
data["access_settings"] = StorageBoxSubaccountAccessSettings.from_dict(raw)
super().__init__(client, data, complete)
def _get_self(self) -> BoundStorageBoxSubaccount:
return self._client.get_subaccount_by_id(
self.data_model.storage_box,
self.data_model.id,
)
# TODO: implement bound methods
class StorageBoxesPageResult(NamedTuple):
storage_boxes: list[BoundStorageBox]
meta: Meta
class StorageBoxSnapshotsPageResult(NamedTuple):
snapshots: list[BoundStorageBoxSnapshot]
meta: Meta
class StorageBoxSubaccountsPageResult(NamedTuple):
subaccounts: list[BoundStorageBoxSubaccount]
meta: Meta
class StorageBoxesClient(ResourceClientBase):
"""
A client for the Storage Boxes API.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes.
"""
_base_url = "/storage_boxes"
actions: ResourceActionsClient
"""Storage Boxes scoped actions client
:type: :class:`ResourceActionsClient <hcloud.actions.client.ResourceActionsClient>`
"""
def __init__(self, client: Client):
super().__init__(client)
self._client = client._client_hetzner
self.actions = ResourceActionsClient(self, self._base_url)
def get_by_id(self, id: int) -> BoundStorageBox:
"""
Returns a specific Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-get-a-storage-box
:param id: ID of the Storage Box.
"""
response = self._client.request(
method="GET",
url=f"{self._base_url}/{id}",
)
return BoundStorageBox(self, response["storage_box"])
def get_by_name(self, name: str) -> BoundStorageBox | None:
"""
Returns a specific Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-list-storage-boxes
:param name: Name of the Storage Box.
"""
return self._get_first_by(self.get_list, name=name)
def get_list(
self,
name: str | None = None,
label_selector: str | None = None,
page: int | None = None,
per_page: int | None = None,
) -> StorageBoxesPageResult:
"""
Returns a list of Storage Boxes for a specific page.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-list-storage-boxes
:param name: Name of the Storage Box.
:param label_selector: Filter resources by labels. The response will only contain resources matching the label selector.
:param page: Page number to return.
:param per_page: Maximum number of entries returned per page.
"""
params: dict[str, Any] = {}
if name is not None:
params["name"] = name
if label_selector is not None:
params["label_selector"] = label_selector
if page is not None:
params["page"] = page
if per_page is not None:
params["per_page"] = per_page
response = self._client.request(
method="GET",
url=f"{self._base_url}",
params=params,
)
return StorageBoxesPageResult(
storage_boxes=[BoundStorageBox(self, o) for o in response["storage_boxes"]],
meta=Meta.parse_meta(response),
)
def get_all(
self,
name: str | None = None,
label_selector: str | None = None,
) -> list[BoundStorageBox]:
"""
Returns all Storage Boxes.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-list-storage-boxes
:param name: Name of the Storage Box.
:param label_selector: Filter resources by labels. The response will only contain resources matching the label selector.
"""
return self._iter_pages(
self.get_list,
name=name,
label_selector=label_selector,
)
def create(
self,
*,
name: str,
password: str,
location: BoundLocation | Location,
storage_box_type: BoundStorageBoxType | StorageBoxType,
ssh_keys: list[str] | None = None,
access_settings: StorageBoxAccessSettings | None = None,
labels: dict[str, str] | None = None,
) -> CreateStorageBoxResponse:
"""
Creates a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-create-a-storage-box
:param name: Name of the Storage Box.
:param password: Password of the Storage Box.
:param location: Location of the Storage Box.
:param storage_box_type: Type of the Storage Box.
:param ssh_keys: SSH public keys of the Storage Box.
:param access_settings: Access settings of the Storage Box.
:param labels: User-defined labels (key/value pairs) for the Storage Box.
"""
data: dict[str, Any] = {
"name": name,
"password": password,
"location": location.id_or_name,
"storage_box_type": storage_box_type.id_or_name,
}
if ssh_keys is not None:
data["ssh_keys"] = ssh_keys
if access_settings is not None:
data["access_settings"] = access_settings.to_payload()
if labels is not None:
data["labels"] = labels
response = self._client.request(
method="POST",
url="/storage_boxes",
json=data,
)
return CreateStorageBoxResponse(
storage_box=BoundStorageBox(self, response["storage_box"]),
action=BoundAction(self._parent.actions, response["action"]),
)
def update(
self,
storage_box: BoundStorageBox | StorageBox,
*,
name: str | None = None,
labels: dict[str, str] | None = None,
) -> BoundStorageBox:
"""
Updates a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-update-a-storage-box
:param storage_box: Storage Box to update.
:param name: Name of the Storage Box.
:param labels: User-defined labels (key/value pairs) for the Storage Box.
"""
data: dict[str, Any] = {}
if name is not None:
data["name"] = name
if labels is not None:
data["labels"] = labels
response = self._client.request(
method="PUT",
url=f"{self._base_url}/{storage_box.id}",
json=data,
)
return BoundStorageBox(self, response["storage_box"])
def delete(
self,
storage_box: BoundStorageBox | StorageBox,
) -> DeleteStorageBoxResponse:
"""
Deletes a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-delete-a-storage-box
:param storage_box: Storage Box to delete.
"""
response = self._client.request(
method="DELETE",
url=f"{self._base_url}/{storage_box.id}",
)
return DeleteStorageBoxResponse(
action=BoundAction(self._parent.actions, response["action"])
)
def get_folders(
self,
storage_box: BoundStorageBox | StorageBox,
path: str | None = None,
) -> StorageBoxFoldersResponse:
"""
Lists the (sub)folders contained in a Storage Box.
Files are not part of the response.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-list-folders-of-a-storage-box
:param storage_box: Storage Box to list the folders from.
:param path: Relative path to list the folders from.
"""
params: dict[str, Any] = {}
if path is not None:
params["path"] = path
response = self._client.request(
method="GET",
url=f"{self._base_url}/{storage_box.id}/folders",
params=params,
)
return StorageBoxFoldersResponse(folders=response["folders"])
def get_actions_list(
self,
storage_box: StorageBox | BoundStorageBox,
*,
status: list[str] | None = None,
sort: list[str] | None = None,
page: int | None = None,
per_page: int | None = None,
) -> ActionsPageResult:
"""
Returns all Actions for a Storage Box for a specific page.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-list-actions-for-a-storage-box
:param storage_box: Storage Box to fetch the Actions from.
:param status: Filter the actions by status. The response will only contain actions matching the specified statuses.
:param sort: Sort resources by field and direction.
:param page: Page number to return.
:param per_page: Maximum number of entries returned per page.
"""
params: dict[str, Any] = {}
if status is not None:
params["status"] = status
if sort is not None:
params["sort"] = sort
if page is not None:
params["page"] = page
if per_page is not None:
params["per_page"] = per_page
response = self._client.request(
method="GET",
url=f"/storage_boxes/{storage_box.id}/actions",
params=params,
)
return ActionsPageResult(
actions=[BoundAction(self._parent.actions, o) for o in response["actions"]],
meta=Meta.parse_meta(response),
)
def get_actions(
self,
storage_box: StorageBox | BoundStorageBox,
*,
status: list[str] | None = None,
sort: list[str] | None = None,
) -> list[BoundAction]:
"""
Returns all Actions for a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-list-actions-for-a-storage-box
:param storage_box: Storage Box to fetch the Actions from.
:param status: Filter the actions by status. The response will only contain actions matching the specified statuses.
:param sort: Sort resources by field and direction.
"""
return self._iter_pages(
self.get_actions_list,
storage_box,
status=status,
sort=sort,
)
def change_protection(
self,
storage_box: StorageBox | BoundStorageBox,
*,
delete: bool | None = None,
) -> BoundAction:
"""
Changes the protection of a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-change-protection
:param storage_box: Storage Box to update.
:param delete: Prevents the Storage Box from being deleted.
"""
data: dict[str, Any] = {}
if delete is not None:
data["delete"] = delete
response = self._client.request(
method="POST",
url=f"{self._base_url}/{storage_box.id}/actions/change_protection",
json=data,
)
return BoundAction(self._parent.actions, response["action"])
def change_type(
self,
storage_box: StorageBox | BoundStorageBox,
storage_box_type: StorageBoxType | BoundStorageBoxType,
) -> BoundAction:
"""
Changes the type of a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-change-type
:param storage_box: Storage Box to update.
:param storage_box_type: Storage Box Type to change to.
"""
data: dict[str, Any] = {
"storage_box_type": storage_box_type.id_or_name,
}
response = self._client.request(
method="POST",
url=f"{self._base_url}/{storage_box.id}/actions/change_type",
json=data,
)
return BoundAction(self._parent.actions, response["action"])
def reset_password(
self,
storage_box: StorageBox | BoundStorageBox,
*,
password: str,
) -> BoundAction:
"""
Reset the password of a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-reset-password
:param storage_box: Storage Box to update.
:param password: New password.
"""
data: dict[str, Any] = {
"password": password,
}
response = self._client.request(
method="POST",
url=f"{self._base_url}/{storage_box.id}/actions/reset_password",
json=data,
)
return BoundAction(self._parent.actions, response["action"])
def update_access_settings(
self,
storage_box: StorageBox | BoundStorageBox,
access_settings: StorageBoxAccessSettings,
) -> BoundAction:
"""
Update the access settings of a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-update-access-settings
:param storage_box: Storage Box to update.
:param access_settings: New access settings for the Storage Box.
"""
data: dict[str, Any] = access_settings.to_payload()
response = self._client.request(
method="POST",
url=f"{self._base_url}/{storage_box.id}/actions/update_access_settings",
json=data,
)
return BoundAction(self._parent.actions, response["action"])
def rollback_snapshot(
self,
storage_box: StorageBox | BoundStorageBox,
snapshot: StorageBoxSnapshot, # TODO: Add BoundStorageBoxSnapshot
) -> BoundAction:
"""
Rollback the Storage Box to the given snapshot.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-rollback-snapshot
:param storage_box: Storage Box to update.
:param snapshot: Snapshot to rollback to.
"""
data: dict[str, Any] = {
"snapshot": snapshot.id_or_name,
}
response = self._client.request(
method="POST",
url=f"{self._base_url}/{storage_box.id}/actions/rollback_snapshot",
json=data,
)
return BoundAction(self._parent.actions, response["action"])
def disable_snapshot_plan(
self,
storage_box: StorageBox | BoundStorageBox,
) -> BoundAction:
"""
Disable the snapshot plan a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-disable-snapshot-plan
:param storage_box: Storage Box to update.
"""
response = self._client.request(
method="POST",
url=f"{self._base_url}/{storage_box.id}/actions/disable_snapshot_plan",
)
return BoundAction(self._parent.actions, response["action"])
def enable_snapshot_plan(
self,
storage_box: StorageBox | BoundStorageBox,
snapshot_plan: StorageBoxSnapshotPlan,
) -> BoundAction:
"""
Enable the snapshot plan a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-actions-enable-snapshot-plan
:param storage_box: Storage Box to update.
:param snapshot_plan: Snapshot Plan to enable.
"""
data: dict[str, Any] = snapshot_plan.to_payload()
response = self._client.request(
method="POST",
url=f"{self._base_url}/{storage_box.id}/actions/enable_snapshot_plan",
json=data,
)
return BoundAction(self._parent.actions, response["action"])
# Snapshots
###########################################################################
def get_snapshot_by_id(
self,
storage_box: StorageBox | BoundStorageBox,
id: int,
) -> BoundStorageBoxSnapshot:
"""
Returns a single Snapshot from a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-snapshots-get-a-snapshot
:param storage_box: Storage Box to get the Snapshot from.
:param id: ID of the Snapshot.
"""
response = self._client.request(
method="GET",
url=f"{self._base_url}/{storage_box.id}/snapshots/{id}",
)
return BoundStorageBoxSnapshot(self, response["snapshot"])
def get_snapshot_by_name(
self,
storage_box: StorageBox | BoundStorageBox,
name: str,
) -> BoundStorageBoxSnapshot:
"""
Returns a single Snapshot from a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-snapshots-list-snapshots
:param storage_box: Storage Box to get the Snapshot from.
:param name: Name of the Snapshot.
"""
return self._get_first_by(self.get_snapshot_list, storage_box, name=name)
def get_snapshot_list(
self,
storage_box: StorageBox | BoundStorageBox,
*,
name: str | None = None,
is_automatic: bool | None = None,
label_selector: str | None = None,
sort: list[str] | None = None,
) -> StorageBoxSnapshotsPageResult:
"""
Returns all Snapshots for a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-snapshots-list-snapshots
:param storage_box: Storage Box to get the Snapshots from.
:param name: Filter resources by their name. The response will only contain the resources matching exactly the specified name.
:param is_automatic: Filter wether the snapshot was made by a Snapshot Plan.
:param label_selector: Filter resources by labels. The response will only contain resources matching the label selector.
:param sort: Sort resources by field and direction.
"""
params: dict[str, Any] = {}
if name is not None:
params["name"] = name
if is_automatic is not None:
params["is_automatic"] = is_automatic
if label_selector is not None:
params["label_selector"] = label_selector
if sort is not None:
params["sort"] = sort
response = self._client.request(
method="GET",
url=f"{self._base_url}/{storage_box.id}/snapshots",
params=params,
)
return StorageBoxSnapshotsPageResult(
snapshots=[
BoundStorageBoxSnapshot(self, item) for item in response["snapshots"]
],
meta=Meta.parse_meta(response),
)
def get_snapshot_all(
self,
storage_box: StorageBox | BoundStorageBox,
*,
name: str | None = None,
is_automatic: bool | None = None,
label_selector: str | None = None,
sort: list[str] | None = None,
) -> list[BoundStorageBoxSnapshot]:
"""
Returns all Snapshots for a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-snapshots-list-snapshots
:param storage_box: Storage Box to get the Snapshots from.
:param name: Filter resources by their name. The response will only contain the resources matching exactly the specified name.
:param is_automatic: Filter wether the snapshot was made by a Snapshot Plan.
:param label_selector: Filter resources by labels. The response will only contain resources matching the label selector.
:param sort: Sort resources by field and direction.
"""
# The endpoint does not have pagination, forward to the list method.
result, _ = self.get_snapshot_list(
storage_box,
name=name,
is_automatic=is_automatic,
label_selector=label_selector,
sort=sort,
)
return result
def create_snapshot(
self,
storage_box: StorageBox | BoundStorageBox,
*,
description: str | None = None,
labels: dict[str, str] | None = None,
) -> CreateStorageBoxSnapshotResponse:
"""
Creates a Snapshot of the Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-snapshots-create-a-snapshot
:param storage_box: Storage Box to create a Snapshot from.
:param description: Description of the Snapshot.
:param labels: User-defined labels (key/value pairs) for the Resource.
"""
data: dict[str, Any] = {}
if description is not None:
data["description"] = description
if labels is not None:
data["labels"] = labels
response = self._client.request(
method="POST",
url=f"{self._base_url}/{storage_box.id}/snapshots",
json=data,
)
return CreateStorageBoxSnapshotResponse(
snapshot=BoundStorageBoxSnapshot(
self,
response["snapshot"],
# API only returns a partial object.
complete=False,
),
action=BoundAction(self._parent.actions, response["action"]),
)
def update_snapshot(
self,
snapshot: StorageBoxSnapshot | BoundStorageBoxSnapshot,
*,
description: str | None = None,
labels: dict[str, str] | None = None,
) -> BoundStorageBoxSnapshot:
"""
Updates a Storage Box Snapshot.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-snapshots-update-a-snapshot
:param snapshot: Storage Box Snapshot to update.
:param description: Description of the Snapshot.
:param labels: User-defined labels (key/value pairs) for the Resource.
"""
if snapshot.storage_box is None:
raise ValueError("snapshot storage_box property is none")
data: dict[str, Any] = {}
if description is not None:
data["description"] = description
if labels is not None:
data["labels"] = labels
response = self._client.request(
method="PUT",
url=f"{self._base_url}/{snapshot.storage_box.id}/snapshots/{snapshot.id}",
json=data,
)
return BoundStorageBoxSnapshot(self, response["snapshot"])
def delete_snapshot(
self,
snapshot: StorageBoxSnapshot | BoundStorageBoxSnapshot,
) -> DeleteStorageBoxSnapshotResponse:
"""
Deletes a Storage Box Snapshot.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-snapshots-delete-a-snapshot
:param snapshot: Storage Box Snapshot to delete.
"""
if snapshot.storage_box is None:
raise ValueError("snapshot storage_box property is none")
response = self._client.request(
method="DELETE",
url=f"{self._base_url}/{snapshot.storage_box.id}/snapshots/{snapshot.id}",
)
return DeleteStorageBoxSnapshotResponse(
action=BoundAction(self._parent.actions, response["action"]),
)
# Subaccounts
###########################################################################
def get_subaccount_by_id(
self,
storage_box: StorageBox | BoundStorageBox,
id: int,
) -> BoundStorageBoxSubaccount:
"""
Returns a single Subaccount from a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-subaccounts-get-a-subaccount
:param storage_box: Storage Box to get the Subaccount from.
:param id: ID of the Subaccount.
"""
response = self._client.request(
method="GET",
url=f"{self._base_url}/{storage_box.id}/subaccounts/{id}",
)
return BoundStorageBoxSubaccount(self, response["subaccount"])
def get_subaccount_by_username(
self,
storage_box: StorageBox | BoundStorageBox,
username: str,
) -> BoundStorageBoxSubaccount:
"""
Returns a single Subaccount from a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-subaccounts-list-subaccounts
:param storage_box: Storage Box to get the Subaccount from.
:param username: User name of the Subaccount.
"""
return self._get_first_by(
self.get_subaccount_list,
storage_box,
username=username,
)
def get_subaccount_list(
self,
storage_box: StorageBox | BoundStorageBox,
*,
username: str | None = None,
label_selector: str | None = None,
sort: list[str] | None = None,
) -> StorageBoxSubaccountsPageResult:
"""
Returns all Subaccounts for a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-subaccounts-list-subaccounts
:param storage_box: Storage Box to get the Subaccount from.
:param username: Filter resources by their username. The response will only contain the resources matching exactly the specified username.
:param label_selector: Filter resources by labels. The response will only contain resources matching the label selector.
:param sort: Sort resources by field and direction.
"""
params: dict[str, Any] = {}
if username is not None:
params["username"] = username
if label_selector is not None:
params["label_selector"] = label_selector
if sort is not None:
params["sort"] = sort
response = self._client.request(
method="GET",
url=f"{self._base_url}/{storage_box.id}/subaccounts",
params=params,
)
return StorageBoxSubaccountsPageResult(
subaccounts=[
BoundStorageBoxSubaccount(self, item)
for item in response["subaccounts"]
],
meta=Meta.parse_meta(response),
)
def get_subaccount_all(
self,
storage_box: StorageBox | BoundStorageBox,
*,
username: str | None = None,
label_selector: str | None = None,
sort: list[str] | None = None,
) -> list[BoundStorageBoxSubaccount]:
"""
Returns all Subaccounts for a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-subaccounts-list-subaccounts
:param storage_box: Storage Box to get the Subaccount from.
:param username: Filter resources by their username. The response will only contain the resources matching exactly the specified username.
:param label_selector: Filter resources by labels. The response will only contain resources matching the label selector.
:param sort: Sort resources by field and direction.
"""
# The endpoint does not have pagination, forward to the list method.
result, _ = self.get_subaccount_list(
storage_box,
username=username,
label_selector=label_selector,
sort=sort,
)
return result
def create_subaccount(
self,
storage_box: StorageBox | BoundStorageBox,
*,
home_directory: str,
password: str,
access_settings: StorageBoxSubaccountAccessSettings | None = None,
description: str | None = None,
labels: dict[str, str] | None = None,
) -> CreateStorageBoxSubaccountResponse:
"""
Creates a Subaccount for the Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-box-subaccounts-create-a-subaccount
:param storage_box: Storage Box to create a Subaccount for.
:param home_directory: Home directory of the Subaccount.
:param password: Password of the Subaccount.
:param access_settings: Access Settings of the Subaccount.
:param description: Description of the Subaccount.
:param labels: User-defined labels (key/value pairs) for the Resource.
"""
data: dict[str, Any] = {
"home_directory": home_directory,
"password": password,
}
if access_settings is not None:
data["access_settings"] = access_settings.to_payload()
if description is not None:
data["description"] = description
if labels is not None:
data["labels"] = labels
response = self._client.request(
method="POST",
url=f"{self._base_url}/{storage_box.id}/subaccounts",
json=data,
)
return CreateStorageBoxSubaccountResponse(
subaccount=BoundStorageBoxSubaccount(
self,
response["subaccount"],
# API only returns a partial object.
complete=False,
),
action=BoundAction(self._parent.actions, response["action"]),
)
def update_subaccount(
self,
subaccount: StorageBoxSubaccount | BoundStorageBoxSubaccount,
*,
description: str | None = None,