Debian Codegraph @

ansible_collections/hetzner/hcloud/plugins/module_utils/_storage_box.py

raw
1 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
2 # Do not use this from other collections or standalone plugins/modules!
3
4 from __future__ import annotations
5
6 from ._client import client_resource_not_found
7 from ._vendor.hcloud.storage_boxes import (
8 BoundStorageBox,
9 StorageBoxesClient,
10 )
11
12
13 def get(client: StorageBoxesClient, param: str | int) -> BoundStorageBox:
14 """
15 Get a Bound Storage Box either by ID or name.
16
17 If the given parameter is an ID, return a partial Bound Storage Box to reduce the amount
18 of API requests.
19 """
20 try:
21 return BoundStorageBox(
22 client,
23 data={"id": int(param)},
24 complete=False,
25 )
26 except ValueError: # param is not an id
27 result = client.get_by_name(param)
28 if result is None:
29 # pylint: disable=raise-missing-from
30 raise client_resource_not_found("storage box", param)
31 return result
32
33
34 def prepare_result(o: BoundStorageBox):
35 return {
36 "id": o.id,
37 "name": o.name,
38 "storage_box_type": o.storage_box_type.name,
39 "location": o.location.name,
40 "labels": o.labels,
41 "delete_protection": o.protection["delete"],
42 "access_settings": {
43 "reachable_externally": o.access_settings.reachable_externally,
44 "samba_enabled": o.access_settings.samba_enabled,
45 "ssh_enabled": o.access_settings.ssh_enabled,
46 "webdav_enabled": o.access_settings.webdav_enabled,
47 "zfs_enabled": o.access_settings.zfs_enabled,
48 },
49 "username": o.username,
50 "server": o.server,
51 "system": o.system,
52 "status": o.status,
53 "stats": {
54 "size": o.stats.size,
55 "size_data": o.stats.size_data,
56 "size_snapshots": o.stats.size_snapshots,
57 },
58 "snapshot_plan": (
59 None
60 if o.snapshot_plan is None
61 else {
62 "max_snapshots": o.snapshot_plan.max_snapshots,
63 "hour": o.snapshot_plan.hour,
64 "minute": o.snapshot_plan.minute,
65 "day_of_week": o.snapshot_plan.day_of_week,
66 "day_of_month": o.snapshot_plan.day_of_month,
67 }
68 ),
69 }