Debian Codegraph @

ansible_collections/hetzner/hcloud/plugins/modules/volume_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright: (c) 2019, Hetzner Cloud GmbH <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6
7 from __future__ import annotations
8
9 DOCUMENTATION = """
10 ---
11 module: volume_info
12
13 short_description: Gather infos about your Hetzner Cloud Volumes.
14
15 description:
16 - Gather infos about your Hetzner Cloud Volumes.
17
18 author:
19 - Lukas Kaemmerling (@LKaemmerling)
20
21 options:
22 id:
23 description:
24 - The ID of the Volume you want to get.
25 - The module will fail if the provided ID is invalid.
26 type: int
27 name:
28 description:
29 - The name of the Volume you want to get.
30 type: str
31 label_selector:
32 description:
33 - The label selector for the Volume you want to get.
34 type: str
35 extends_documentation_fragment:
36 - hetzner.hcloud.hcloud
37
38 """
39
40 EXAMPLES = """
41 - name: Gather hcloud Volume infos
42 hetzner.hcloud.volume_info:
43 register: output
44 - name: Print the gathered infos
45 debug:
46 var: output.hcloud_volume_info
47 """
48
49 RETURN = """
50 hcloud_volume_info:
51 description: The Volume infos as list
52 returned: always
53 type: list
54 contains:
55 id:
56 description: Numeric identifier of the Volume
57 returned: always
58 type: int
59 sample: 1937415
60 name:
61 description: Name of the Volume
62 returned: always
63 type: str
64 sample: my-volume
65 size:
66 description: Size of the Volume
67 returned: always
68 type: str
69 sample: 10
70 linux_device:
71 description: Path to the device that contains the Volume.
72 returned: always
73 type: str
74 sample: /dev/disk/by-id/scsi-0HC_Volume_12345
75 version_added: "0.1.0"
76 location:
77 description: Name of the location where the Volume resides in
78 returned: always
79 type: str
80 sample: fsn1
81 server:
82 description: Name of the server where the Volume is attached to
83 returned: always
84 type: str
85 sample: my-server
86 delete_protection:
87 description: True if the Volume is protected for deletion
88 returned: always
89 type: bool
90 version_added: "0.1.0"
91 labels:
92 description: User-defined labels (key-value pairs)
93 returned: always
94 type: dict
95 """
96
97 from ansible.module_utils.basic import AnsibleModule
98
99 from ..module_utils._base import AnsibleHCloud
100 from ..module_utils._vendor.hcloud import HCloudException
101 from ..module_utils._vendor.hcloud.volumes import BoundVolume
102
103
104 class AnsibleHCloudVolumeInfo(AnsibleHCloud):
105 represent = "hcloud_volume_info"
106
107 hcloud_volume_info: list[BoundVolume] | None = None
108
109 def _prepare_result(self):
110 tmp = []
111
112 for volume in self.hcloud_volume_info:
113 if volume is None:
114 continue
115
116 tmp.append(
117 {
118 "id": volume.id,
119 "name": volume.name,
120 "size": volume.size,
121 "location": volume.location.name,
122 "labels": volume.labels,
123 "server": volume.server.name if volume.server is not None else None,
124 "linux_device": volume.linux_device,
125 "delete_protection": volume.protection["delete"],
126 }
127 )
128
129 return tmp
130
131 def get_volumes(self):
132 try:
133 if self.module.params.get("id") is not None:
134 self.hcloud_volume_info = [self.client.volumes.get_by_id(self.module.params.get("id"))]
135 elif self.module.params.get("name") is not None:
136 self.hcloud_volume_info = [self.client.volumes.get_by_name(self.module.params.get("name"))]
137 elif self.module.params.get("label_selector") is not None:
138 self.hcloud_volume_info = self.client.volumes.get_all(
139 label_selector=self.module.params.get("label_selector")
140 )
141 else:
142 self.hcloud_volume_info = self.client.volumes.get_all()
143
144 except HCloudException as exception:
145 self.fail_json_hcloud(exception)
146
147 @classmethod
148 def define_module(cls):
149 return AnsibleModule(
150 argument_spec=dict(
151 id={"type": "int"},
152 name={"type": "str"},
153 label_selector={"type": "str"},
154 **super().base_module_arguments(),
155 ),
156 supports_check_mode=True,
157 )
158
159
160 def main():
161 module = AnsibleHCloudVolumeInfo.define_module()
162 hcloud = AnsibleHCloudVolumeInfo(module)
163
164 hcloud.get_volumes()
165 result = hcloud.get_result()
166
167 ansible_info = {"hcloud_volume_info": result["hcloud_volume_info"]}
168 module.exit_json(**ansible_info)
169
170
171 if __name__ == "__main__":
172 main()