Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright: (c) 2022, Patrice Le Guyader
4 # heavily inspired by the work of @LKaemmerling
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7
8 from __future__ import annotations
9
10 DOCUMENTATION = """
11 ---
12 module: iso_info
13
14 short_description: Gather infos about the Hetzner Cloud ISO list.
15
16 description:
17 - Gather infos about the Hetzner Cloud ISO list.
18
19 author:
20 - Patrice Le Guyader (@patlegu)
21 - Lukas Kaemmerling (@LKaemmerling)
22
23 options:
24 id:
25 description:
26 - The ID of the ISO image you want to get.
27 - The module will fail if the provided ID is invalid.
28 type: int
29 name:
30 description:
31 - The name of the ISO you want to get.
32 type: str
33 architecture:
34 description:
35 - Filter ISOs with compatible architecture.
36 type: str
37 choices: [x86, arm]
38 include_architecture_wildcard:
39 description:
40 - Include ISOs with wildcard architecture (architecture is null).
41 - Works only if architecture filter is specified.
42 type: bool
43
44 extends_documentation_fragment:
45 - hetzner.hcloud.hcloud
46 """
47
48 EXAMPLES = """
49 - name: Gather hcloud ISO type infos
50 hetzner.hcloud.iso_info:
51 register: output
52
53 - name: Print the gathered infos
54 debug:
55 var: output.hcloud_iso_info
56 """
57
58 RETURN = """
59 hcloud_iso_info:
60 description: The ISO type infos as list
61 returned: always
62 type: list
63 contains:
64 id:
65 description: ID of the ISO
66 returned: always
67 type: int
68 sample: 22110
69 name:
70 description: Unique identifier of the ISO. Only set for public ISOs
71 returned: always
72 type: str
73 sample: debian-12.0.0-amd64-netinst.iso
74 description:
75 description: Description of the ISO
76 returned: always
77 type: str
78 sample: Debian 12.0 (amd64/netinstall)
79 architecture:
80 description: >
81 Type of cpu architecture this ISO is compatible with.
82 None indicates no restriction on the architecture (wildcard).
83 returned: when supported
84 type: str
85 sample: x86
86 type:
87 description: Type of the ISO, can be one of `public`, `private`.
88 returned: always
89 type: str
90 sample: public
91 deprecated:
92 description: >
93 ISO 8601 timestamp of deprecation, None if ISO is still available.
94 After the deprecation time it will no longer be possible to attach the
95 ISO to servers. This field is deprecated. Use `deprecation` instead.
96 returned: always
97 type: str
98 sample: "2024-12-01T00:00:00+00:00"
99 deprecation:
100 description: >
101 Describes if, when & how the resources was deprecated. If this field is
102 set to None the resource is not deprecated. If it has a value, it is
103 considered deprecated.
104 returned: if the resource is deprecated
105 type: dict
106 contains:
107 announced:
108 description: Date of when the deprecation was announced.
109 returned: always
110 type: str
111 sample: "2021-11-01T00:00:00+00:00"
112 unavailable_after:
113 description: >
114 After the time in this field, the resource will not be available
115 from the general listing endpoint of the resource type, and it can
116 not be used in new resources. For example, if this is an image,
117 you can not create new servers with this image after the mentioned
118 date.
119 returned: always
120 type: str
121 sample: "2021-12-01T00:00:00+00:00"
122 """
123
124 from ansible.module_utils.basic import AnsibleModule
125
126 from ..module_utils._base import AnsibleHCloud
127 from ..module_utils._vendor.hcloud import HCloudException
128 from ..module_utils._vendor.hcloud.isos import BoundIso
129
130
131 class AnsibleHCloudIsoInfo(AnsibleHCloud):
132 represent = "hcloud_iso_info"
133
134 hcloud_iso_info: list[BoundIso] | None = None
135
136 def _prepare_result(self):
137 tmp = []
138
139 for iso_info in self.hcloud_iso_info:
140 if iso_info is None:
141 continue
142
143 tmp.append(
144 {
145 "id": iso_info.id,
146 "name": iso_info.name,
147 "description": iso_info.description,
148 "type": iso_info.type,
149 "architecture": iso_info.architecture,
150 "deprecated": (
151 iso_info.deprecation.unavailable_after.isoformat() if iso_info.deprecation is not None else None
152 ),
153 "deprecation": (
154 {
155 "announced": iso_info.deprecation.announced.isoformat(),
156 "unavailable_after": iso_info.deprecation.unavailable_after.isoformat(),
157 }
158 if iso_info.deprecation is not None
159 else None
160 ),
161 }
162 )
163
164 return tmp
165
166 def get_iso_infos(self):
167 try:
168 if self.module.params.get("id") is not None:
169 self.hcloud_iso_info = [self.client.isos.get_by_id(self.module.params.get("id"))]
170 elif self.module.params.get("name") is not None:
171 self.hcloud_iso_info = [self.client.isos.get_by_name(self.module.params.get("name"))]
172 else:
173 self.hcloud_iso_info = self.client.isos.get_all(
174 architecture=self.module.params.get("architecture"),
175 include_architecture_wildcard=self.module.params.get("include_wildcard_architecture"),
176 )
177
178 except HCloudException as exception:
179 self.fail_json_hcloud(exception)
180
181 @classmethod
182 def define_module(cls):
183 return AnsibleModule(
184 argument_spec=dict(
185 id={"type": "int"},
186 name={"type": "str"},
187 architecture={"type": "str", "choices": ["x86", "arm"]},
188 include_architecture_wildcard={"type": "bool"},
189 **super().base_module_arguments(),
190 ),
191 supports_check_mode=True,
192 )
193
194
195 def main():
196 module = AnsibleHCloudIsoInfo.define_module()
197 hcloud = AnsibleHCloudIsoInfo(module)
198 hcloud.get_iso_infos()
199 result = hcloud.get_result()
200 ansible_info = {"hcloud_iso_info": result["hcloud_iso_info"]}
201 module.exit_json(**ansible_info)
202
203
204 if __name__ == "__main__":
205 main()