Debian Codegraph @

ansible_collections/community/general/plugins/modules/one_image_info.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2018, Milan Ilic <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 # Make coding more python3-ish
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: one_image_info
11 short_description: Gather information on OpenNebula images
12 description:
13 - Gather information on OpenNebula images.
14 requirements:
15 - pyone
16 extends_documentation_fragment:
17 - community.general._opennebula
18 - community.general._attributes
19 - community.general._attributes.info_module
20 options:
21 ids:
22 description:
23 - A list of images IDs whose facts you want to gather.
24 - Module can use integers too.
25 aliases: ['id']
26 type: list
27 elements: str
28 name:
29 description:
30 - A O(name) of the image whose facts is gathered.
31 - If the O(name) begins with V(~) the O(name) is used as regex pattern, which restricts the list of images (whose facts
32 is returned) whose names match specified regex.
33 - Also, if the O(name) begins with V(~*) case-insensitive matching is performed.
34 - See examples for more details.
35 type: str
36 author:
37 - "Milan Ilic (@ilicmilan)"
38 - "Jan Meerkamp (@meerkampdvv)"
39 """
40
41 EXAMPLES = r"""
42 - name: Gather facts about all images
43 community.general.one_image_info:
44 register: result
45
46 - name: Print all images facts
47 ansible.builtin.debug:
48 msg: result
49
50 - name: Gather facts about an image using ID
51 community.general.one_image_info:
52 ids: 123
53
54 - name: Gather facts about an image using list of ID
55 community.general.one_image_info:
56 ids:
57 - 123
58 - 456
59 - 789
60 - 0
61
62 - name: Gather facts about an image using the name
63 community.general.one_image_info:
64 name: 'foo-image'
65 register: foo_image
66
67 - name: Gather facts about all IMAGEs whose name matches regex 'app-image-.*'
68 community.general.one_image_info:
69 name: '~app-image-.*'
70 register: app_images
71
72 - name: Gather facts about all IMAGEs whose name matches regex 'foo-image-.*' ignoring cases
73 community.general.one_image_info:
74 name: '~*foo-image-.*'
75 register: foo_images
76 """
77
78 RETURN = r"""
79 images:
80 description: A list of images info.
81 type: list
82 elements: dict
83 returned: success
84 contains:
85 id:
86 description: The image's ID.
87 type: int
88 sample: 153
89 name:
90 description: The image's name.
91 type: str
92 sample: app1
93 group_id:
94 description: The image's group ID.
95 type: int
96 sample: 1
97 group_name:
98 description: The image's group name.
99 type: str
100 sample: one-users
101 owner_id:
102 description: The image's owner ID.
103 type: int
104 sample: 143
105 owner_name:
106 description: The image's owner name.
107 type: str
108 sample: ansible-test
109 state:
110 description: The image's state.
111 type: str
112 sample: READY
113 used:
114 description: The image's usage status.
115 type: bool
116 sample: true
117 running_vms:
118 description: The image's count of running vms that use this image.
119 type: int
120 sample: 7
121 permissions:
122 description: The image's permissions.
123 type: dict
124 version_added: 9.5.0
125 contains:
126 owner_u:
127 description: The image's owner USAGE permissions.
128 type: str
129 sample: 1
130 owner_m:
131 description: The image's owner MANAGE permissions.
132 type: str
133 sample: 0
134 owner_a:
135 description: The image's owner ADMIN permissions.
136 type: str
137 sample: 0
138 group_u:
139 description: The image's group USAGE permissions.
140 type: str
141 sample: 0
142 group_m:
143 description: The image's group MANAGE permissions.
144 type: str
145 sample: 0
146 group_a:
147 description: The image's group ADMIN permissions.
148 type: str
149 sample: 0
150 other_u:
151 description: The image's other users USAGE permissions.
152 type: str
153 sample: 0
154 other_m:
155 description: The image's other users MANAGE permissions.
156 type: str
157 sample: 0
158 other_a:
159 description: The image's other users ADMIN permissions.
160 type: str
161 sample: 0
162 sample:
163 owner_u: 1
164 owner_m: 0
165 owner_a: 0
166 group_u: 0
167 group_m: 0
168 group_a: 0
169 other_u: 0
170 other_m: 0
171 other_a: 0
172 type:
173 description: The image's type.
174 type: int
175 sample: 0
176 version_added: 9.5.0
177 disk_type:
178 description: The image's format type.
179 type: int
180 sample: 0
181 version_added: 9.5.0
182 persistent:
183 description: The image's persistence status (1 means true, 0 means false).
184 type: int
185 sample: 1
186 version_added: 9.5.0
187 source:
188 description: The image's source.
189 type: str
190 sample: /var/lib/one//datastores/100/somerandomstringxd
191 version_added: 9.5.0
192 path:
193 description: The image's filesystem path.
194 type: str
195 sample: /var/tmp/hello.qcow2
196 version_added: 9.5.0
197 fstype:
198 description: The image's filesystem type.
199 type: str
200 sample: ext4
201 version_added: 9.5.0
202 size:
203 description: The image's size in MegaBytes.
204 type: int
205 sample: 10000
206 version_added: 9.5.0
207 cloning_ops:
208 description: The image's cloning operations per second.
209 type: int
210 sample: 0
211 version_added: 9.5.0
212 cloning_id:
213 description: The image's cloning ID.
214 type: int
215 sample: -1
216 version_added: 9.5.0
217 target_snapshot:
218 description: The image's target snapshot.
219 type: int
220 sample: 1
221 version_added: 9.5.0
222 datastore_id:
223 description: The image's datastore ID.
224 type: int
225 sample: 100
226 version_added: 9.5.0
227 datastore:
228 description: The image's datastore name.
229 type: int
230 sample: image_datastore
231 version_added: 9.5.0
232 vms:
233 description: The image's list of VM ID's.
234 type: list
235 elements: int
236 version_added: 9.5.0
237 sample:
238 - 1
239 - 2
240 - 3
241 clones:
242 description: The image's list of clones ID's.
243 type: list
244 elements: int
245 version_added: 9.5.0
246 sample:
247 - 1
248 - 2
249 - 3
250 app_clones:
251 description: The image's list of app_clones ID's.
252 type: list
253 elements: int
254 version_added: 9.5.0
255 sample:
256 - 1
257 - 2
258 - 3
259 snapshots:
260 description: The image's list of snapshots.
261 type: list
262 version_added: 9.5.0
263 sample:
264 - date: 123123
265 parent: 1
266 size: 10228
267 allow_orphans: 1
268 children: 0
269 active: 1
270 name: SampleName
271 """
272
273 import re
274
275 from ansible_collections.community.general.plugins.module_utils._opennebula import OpenNebulaModule
276
277 IMAGE_STATES = [
278 "INIT",
279 "READY",
280 "USED",
281 "DISABLED",
282 "LOCKED",
283 "ERROR",
284 "CLONE",
285 "DELETE",
286 "USED_PERS",
287 "LOCKED_USED",
288 "LOCKED_USED_PERS",
289 ]
290
291
292 class ImageInfoModule(OpenNebulaModule):
293 def __init__(self):
294 argument_spec = dict(
295 ids=dict(type="list", aliases=["id"], elements="str"),
296 name=dict(type="str"),
297 )
298 mutually_exclusive = [
299 ["ids", "name"],
300 ]
301
302 OpenNebulaModule.__init__(self, argument_spec, supports_check_mode=True, mutually_exclusive=mutually_exclusive)
303
304 def run(self, one, module, result):
305 params = module.params
306 ids = params.get("ids")
307 name = params.get("name")
308
309 if ids:
310 images = self.get_images_by_ids(ids)
311 elif name:
312 images = self.get_images_by_name(name)
313 else:
314 images = self.get_all_images().IMAGE
315
316 self.result = {"images": [self.get_image_info(image) for image in images]}
317
318 self.exit()
319
320 def get_all_images(self):
321 pool = self.one.imagepool.info(-2, -1, -1, -1)
322 # Filter -2 means fetch all images user can Use
323
324 return pool
325
326 def get_images_by_ids(self, ids):
327 images = []
328 pool = self.get_all_images()
329
330 for image in pool.IMAGE:
331 if str(image.ID) in ids:
332 images.append(image)
333 ids.remove(str(image.ID))
334 if len(ids) == 0:
335 break
336
337 if len(ids) > 0:
338 self.module.fail_json(msg=f"There is no IMAGE(s) with id(s)={', '.join(str(image_id) for image_id in ids)}")
339
340 return images
341
342 def get_images_by_name(self, name_pattern):
343 images = []
344 pattern = None
345
346 pool = self.get_all_images()
347
348 if name_pattern.startswith("~"):
349 if name_pattern[1] == "*":
350 pattern = re.compile(name_pattern[2:], re.IGNORECASE)
351 else:
352 pattern = re.compile(name_pattern[1:])
353
354 for image in pool.IMAGE:
355 if pattern is not None:
356 if pattern.match(image.NAME):
357 images.append(image)
358 elif name_pattern == image.NAME:
359 images.append(image)
360 break
361
362 # if the specific name is indicated
363 if pattern is None and len(images) == 0:
364 self.module.fail_json(msg=f"There is no IMAGE with name={name_pattern}")
365
366 return images
367
368
369 def main():
370 ImageInfoModule().run_module()
371
372
373 if __name__ == "__main__":
374 main()