Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/content_locallibrary_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright: (c) 2021, Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # template: header.j2
6 # This module is autogenerated using the ansible.content_builder.
7 # See: https://github.com/ansible-community/ansible.content_builder
8
9
10 DOCUMENTATION = r"""
11 module: content_locallibrary_info
12 short_description: Returns a given local library.
13 description: Returns a given local library.
14 options:
15 library_id:
16 description:
17 - Identifier of the local library to return.
18 - The parameter must be the id of a resource returned by M(vmware.vmware_rest.content_library_info).
19 Required with I(state=['get'])
20 type: str
21 session_timeout:
22 description:
23 - 'Timeout settings for client session. '
24 - 'The maximal number of seconds for the whole operation including connection
25 establishment, request sending and response. '
26 - The default value is 300s.
27 type: float
28 version_added: 2.1.0
29 vcenter_hostname:
30 description:
31 - The hostname or IP address of the vSphere vCenter
32 - If the value is not specified in the task, the value of environment variable
33 C(VMWARE_HOST) will be used instead.
34 required: true
35 type: str
36 vcenter_password:
37 description:
38 - The vSphere vCenter password
39 - If the value is not specified in the task, the value of environment variable
40 C(VMWARE_PASSWORD) will be used instead.
41 required: true
42 type: str
43 vcenter_rest_log_file:
44 description:
45 - 'You can use this optional parameter to set the location of a log file. '
46 - 'This file will be used to record the HTTP REST interaction. '
47 - 'The file will be stored on the host that runs the module. '
48 - 'If the value is not specified in the task, the value of '
49 - environment variable C(VMWARE_REST_LOG_FILE) will be used instead.
50 type: str
51 vcenter_username:
52 description:
53 - The vSphere vCenter username
54 - If the value is not specified in the task, the value of environment variable
55 C(VMWARE_USER) will be used instead.
56 required: true
57 type: str
58 vcenter_validate_certs:
59 default: true
60 description:
61 - Allows connection when SSL certificates are not valid. Set to C(false) when
62 certificates are not trusted.
63 - If the value is not specified in the task, the value of environment variable
64 C(VMWARE_VALIDATE_CERTS) will be used instead.
65 type: bool
66 author:
67 - Ansible Cloud Team (@ansible-collections)
68 version_added: 2.0.0
69 requirements:
70 - vSphere 7.0.3 or greater
71 - python >= 3.6
72 - aiohttp
73 notes:
74 - Tested on vSphere 7.0.3
75 """
76
77 EXAMPLES = r"""
78 - name: List Local Content Library
79 vmware.vmware_rest.content_locallibrary_info:
80 register: my_content_library
81
82 - name: List all Local Content Library
83 vmware.vmware_rest.content_locallibrary_info:
84 register: all_content_libraries
85
86 - name: Create a new local content library
87 vmware.vmware_rest.content_locallibrary:
88 name: local_library_001
89 description: automated
90 publish_info:
91 published: true
92 authentication_method: NONE
93 storage_backings:
94 - datastore_id: "{{ lookup('vmware.vmware_rest.datastore_moid', '/my_dc/datastore/rw_datastore') }}"
95 type: DATASTORE
96 state: present
97 register: ds_lib
98
99 - name: Retrieve the local content library information based upon id check mode
100 vmware.vmware_rest.content_locallibrary_info:
101 library_id: '{{ ds_lib.id }}'
102 register: result
103 check_mode: true
104 """
105 RETURN = r"""
106 # content generated by the update_return_section callback# task: List all Local Content Library
107 value:
108 description: List all Local Content Library
109 returned: On success
110 sample:
111 - creation_time: '2022-11-23T20:02:22.940Z'
112 description: automated
113 id: a66d5c73-57f8-4a3a-9361-292a55f68516
114 last_modified_time: '2022-11-23T20:02:22.940Z'
115 name: my_library_on_nfs
116 publish_info:
117 authentication_method: NONE
118 persist_json_enabled: 0
119 publish_url: https://vcenter.test:443/cls/vcsp/lib/a66d5c73-57f8-4a3a-9361-292a55f68516/lib.json
120 published: 1
121 user_name: vcsp
122 server_guid: 52fb0b5e-ffc3-465b-bf4f-e4e6d5423cf5
123 storage_backings:
124 - storage_uri: nfs://datastore.test/srv/share/content-library
125 type: OTHER
126 type: LOCAL
127 version: '2'
128 - creation_time: '2022-11-23T20:02:25.134Z'
129 description: automated
130 id: 3393956a-43ed-4e7f-bd96-eb39bd604445
131 last_modified_time: '2022-11-23T20:02:25.134Z'
132 name: my_library_on_nfs_0
133 publish_info:
134 authentication_method: NONE
135 persist_json_enabled: 0
136 publish_url: https://vcenter.test:443/cls/vcsp/lib/3393956a-43ed-4e7f-bd96-eb39bd604445/lib.json
137 published: 1
138 user_name: vcsp
139 server_guid: 52fb0b5e-ffc3-465b-bf4f-e4e6d5423cf5
140 storage_backings:
141 - storage_uri: nfs://datastore.test/srv/share/content-library
142 type: OTHER
143 type: LOCAL
144 version: '2'
145 - creation_time: '2022-11-23T20:02:26.342Z'
146 description: automated
147 id: 87f66f86-c046-45a7-9563-d59ea220babf
148 last_modified_time: '2022-11-23T20:02:26.342Z'
149 name: my_library_on_nfs_1
150 publish_info:
151 authentication_method: NONE
152 persist_json_enabled: 0
153 publish_url: https://vcenter.test:443/cls/vcsp/lib/87f66f86-c046-45a7-9563-d59ea220babf/lib.json
154 published: 1
155 user_name: vcsp
156 server_guid: 52fb0b5e-ffc3-465b-bf4f-e4e6d5423cf5
157 storage_backings:
158 - storage_uri: nfs://datastore.test/srv/share/content-library
159 type: OTHER
160 type: LOCAL
161 version: '2'
162 - creation_time: '2022-11-23T20:02:27.504Z'
163 description: automated
164 id: f6c590c4-ae6d-4ad0-9362-378196e98a3c
165 last_modified_time: '2022-11-23T20:02:27.504Z'
166 name: my_library_on_nfs_2
167 publish_info:
168 authentication_method: NONE
169 persist_json_enabled: 0
170 publish_url: https://vcenter.test:443/cls/vcsp/lib/f6c590c4-ae6d-4ad0-9362-378196e98a3c/lib.json
171 published: 1
172 user_name: vcsp
173 server_guid: 52fb0b5e-ffc3-465b-bf4f-e4e6d5423cf5
174 storage_backings:
175 - storage_uri: nfs://datastore.test/srv/share/content-library
176 type: OTHER
177 type: LOCAL
178 version: '2'
179 - creation_time: '2022-11-23T20:02:28.821Z'
180 description: automated
181 id: e8917499-2a4c-4b70-b39b-ae0caaef89c3
182 last_modified_time: '2022-11-23T20:02:28.821Z'
183 name: my_library_on_nfs_3
184 publish_info:
185 authentication_method: NONE
186 persist_json_enabled: 0
187 publish_url: https://vcenter.test:443/cls/vcsp/lib/e8917499-2a4c-4b70-b39b-ae0caaef89c3/lib.json
188 published: 1
189 user_name: vcsp
190 server_guid: 52fb0b5e-ffc3-465b-bf4f-e4e6d5423cf5
191 storage_backings:
192 - storage_uri: nfs://datastore.test/srv/share/content-library
193 type: OTHER
194 type: LOCAL
195 version: '2'
196 - creation_time: '2022-11-23T20:02:30.085Z'
197 description: automated
198 id: 630ebdfe-8913-45d3-aaa8-9c2fdecbb76b
199 last_modified_time: '2022-11-23T20:02:30.085Z'
200 name: my_library_on_nfs_4
201 publish_info:
202 authentication_method: NONE
203 persist_json_enabled: 0
204 publish_url: https://vcenter.test:443/cls/vcsp/lib/630ebdfe-8913-45d3-aaa8-9c2fdecbb76b/lib.json
205 published: 1
206 user_name: vcsp
207 server_guid: 52fb0b5e-ffc3-465b-bf4f-e4e6d5423cf5
208 storage_backings:
209 - storage_uri: nfs://datastore.test/srv/share/content-library
210 type: OTHER
211 type: LOCAL
212 version: '2'
213 - creation_time: '2022-11-23T20:02:31.482Z'
214 description: automated
215 id: a046e2e5-bd55-4d04-9443-750a2ab35a6d
216 last_modified_time: '2022-11-23T20:02:31.482Z'
217 name: my_library_on_nfs_5
218 publish_info:
219 authentication_method: NONE
220 persist_json_enabled: 0
221 publish_url: https://vcenter.test:443/cls/vcsp/lib/a046e2e5-bd55-4d04-9443-750a2ab35a6d/lib.json
222 published: 1
223 user_name: vcsp
224 server_guid: 52fb0b5e-ffc3-465b-bf4f-e4e6d5423cf5
225 storage_backings:
226 - storage_uri: nfs://datastore.test/srv/share/content-library
227 type: OTHER
228 type: LOCAL
229 version: '2'
230 - creation_time: '2022-11-23T20:02:32.846Z'
231 description: automated
232 id: b94383b1-7877-4dbd-8c33-51abc39451ff
233 last_modified_time: '2022-11-23T20:02:32.846Z'
234 name: my_library_on_nfs_6
235 publish_info:
236 authentication_method: NONE
237 persist_json_enabled: 0
238 publish_url: https://vcenter.test:443/cls/vcsp/lib/b94383b1-7877-4dbd-8c33-51abc39451ff/lib.json
239 published: 1
240 user_name: vcsp
241 server_guid: 52fb0b5e-ffc3-465b-bf4f-e4e6d5423cf5
242 storage_backings:
243 - storage_uri: nfs://datastore.test/srv/share/content-library
244 type: OTHER
245 type: LOCAL
246 version: '2'
247 - creation_time: '2022-11-23T20:02:34.218Z'
248 description: automated
249 id: 8e3efb68-cb84-4ce0-a65a-6c94cc6e6e00
250 last_modified_time: '2022-11-23T20:02:34.218Z'
251 name: my_library_on_nfs_7
252 publish_info:
253 authentication_method: NONE
254 persist_json_enabled: 0
255 publish_url: https://vcenter.test:443/cls/vcsp/lib/8e3efb68-cb84-4ce0-a65a-6c94cc6e6e00/lib.json
256 published: 1
257 user_name: vcsp
258 server_guid: 52fb0b5e-ffc3-465b-bf4f-e4e6d5423cf5
259 storage_backings:
260 - storage_uri: nfs://datastore.test/srv/share/content-library
261 type: OTHER
262 type: LOCAL
263 version: '2'
264 - creation_time: '2022-11-23T20:02:35.922Z'
265 description: automated
266 id: 0b12c0b3-6c6d-448d-9033-e054a70183e7
267 last_modified_time: '2022-11-23T20:02:35.922Z'
268 name: my_library_on_nfs_8
269 publish_info:
270 authentication_method: NONE
271 persist_json_enabled: 0
272 publish_url: https://vcenter.test:443/cls/vcsp/lib/0b12c0b3-6c6d-448d-9033-e054a70183e7/lib.json
273 published: 1
274 user_name: vcsp
275 server_guid: 52fb0b5e-ffc3-465b-bf4f-e4e6d5423cf5
276 storage_backings:
277 - storage_uri: nfs://datastore.test/srv/share/content-library
278 type: OTHER
279 type: LOCAL
280 version: '2'
281 - creation_time: '2022-11-23T20:02:37.796Z'
282 description: automated
283 id: 46454797-bbe0-415a-9fe9-3cf2f74a14db
284 last_modified_time: '2022-11-23T20:02:37.796Z'
285 name: my_library_on_nfs_9
286 publish_info:
287 authentication_method: NONE
288 persist_json_enabled: 0
289 publish_url: https://vcenter.test:443/cls/vcsp/lib/46454797-bbe0-415a-9fe9-3cf2f74a14db/lib.json
290 published: 1
291 user_name: vcsp
292 server_guid: 52fb0b5e-ffc3-465b-bf4f-e4e6d5423cf5
293 storage_backings:
294 - storage_uri: nfs://datastore.test/srv/share/content-library
295 type: OTHER
296 type: LOCAL
297 version: '2'
298 - creation_time: '2022-11-23T20:02:38.976Z'
299 description: automated
300 id: 209926aa-e3fe-46a5-95f2-501e82a5139b
301 last_modified_time: '2022-11-23T20:02:38.976Z'
302 name: my_library_on_nfs_10
303 publish_info:
304 authentication_method: NONE
305 persist_json_enabled: 0
306 publish_url: https://vcenter.test:443/cls/vcsp/lib/209926aa-e3fe-46a5-95f2-501e82a5139b/lib.json
307 published: 1
308 user_name: vcsp
309 server_guid: 52fb0b5e-ffc3-465b-bf4f-e4e6d5423cf5
310 storage_backings:
311 - storage_uri: nfs://datastore.test/srv/share/content-library
312 type: OTHER
313 type: LOCAL
314 version: '2'
315 type: list
316 """
317
318
319 # This structure describes the format of the data expected by the end-points
320 PAYLOAD_FORMAT = {
321 "get": {"query": {}, "body": {}, "path": {"library_id": "library_id"}},
322 "list": {"query": {}, "body": {}, "path": {}},
323 } # pylint: disable=line-too-long
324
325 from ansible.module_utils.basic import env_fallback
326 import os
327
328 if os.getenv("VMWARE_ENABLE_TURBO", False):
329 try:
330 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
331 AnsibleTurboModule as AnsibleModule,
332 )
333
334 AnsibleModule.collection_name = "vmware.vmware_rest"
335 except ImportError:
336 from ansible.module_utils.basic import AnsibleModule
337 else:
338 from ansible.module_utils.basic import AnsibleModule
339
340 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
341 build_full_device_list,
342 exists,
343 gen_args,
344 open_session,
345 session_timeout,
346 update_changed_flag,
347 )
348
349
350 def prepare_argument_spec():
351 argument_spec = {
352 "vcenter_hostname": dict(
353 type="str",
354 required=True,
355 fallback=(env_fallback, ["VMWARE_HOST"]),
356 ),
357 "vcenter_username": dict(
358 type="str",
359 required=True,
360 fallback=(env_fallback, ["VMWARE_USER"]),
361 ),
362 "vcenter_password": dict(
363 type="str",
364 required=True,
365 no_log=True,
366 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
367 ),
368 "vcenter_validate_certs": dict(
369 type="bool",
370 required=False,
371 default=True,
372 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
373 ),
374 "vcenter_rest_log_file": dict(
375 type="str",
376 required=False,
377 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
378 ),
379 "session_timeout": dict(
380 type="float",
381 required=False,
382 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
383 ),
384 }
385
386 argument_spec["library_id"] = {"type": "str"}
387
388 return argument_spec
389
390
391 async def main():
392 required_if = list([])
393
394 module_args = prepare_argument_spec()
395 module = AnsibleModule(
396 argument_spec=module_args, required_if=required_if, supports_check_mode=True
397 )
398 if not module.params["vcenter_hostname"]:
399 module.fail_json("vcenter_hostname cannot be empty")
400 if not module.params["vcenter_username"]:
401 module.fail_json("vcenter_username cannot be empty")
402 if not module.params["vcenter_password"]:
403 module.fail_json("vcenter_password cannot be empty")
404 try:
405 session = await open_session(
406 vcenter_hostname=module.params["vcenter_hostname"],
407 vcenter_username=module.params["vcenter_username"],
408 vcenter_password=module.params["vcenter_password"],
409 validate_certs=module.params["vcenter_validate_certs"],
410 log_file=module.params["vcenter_rest_log_file"],
411 )
412 except Exception as err:
413 if hasattr(err, "get_message"):
414 module.fail_json(err.get_message())
415 else:
416 module.fail_json(str(err))
417 result = await entry_point(module, session)
418 module.exit_json(**result)
419
420
421 # template: info_list_and_get_module.j2
422 def build_url(params):
423 import yarl
424
425 if params.get("library_id"):
426 _in_query_parameters = PAYLOAD_FORMAT["get"]["query"].keys()
427 return yarl.URL(
428 ("https://{vcenter_hostname}" "/api/content/local-library/").format(
429 **params
430 )
431 + params["library_id"]
432 + gen_args(params, _in_query_parameters),
433 encoded=True,
434 )
435 _in_query_parameters = PAYLOAD_FORMAT["list"]["query"].keys()
436 return yarl.URL(
437 ("https://{vcenter_hostname}" "/api/content/local-library").format(**params)
438 + gen_args(params, _in_query_parameters),
439 encoded=True,
440 )
441
442
443 async def entry_point(module, session):
444 url = build_url(module.params)
445 async with session.get(url, **session_timeout(module.params)) as resp:
446 _json = await resp.json()
447
448 if "value" not in _json: # 7.0.2+
449 _json = {"value": _json}
450
451 if module.params.get("library_id"):
452 _json["id"] = module.params.get("library_id")
453 elif module.params.get("label"): # TODO extend the list of filter
454 _json = await exists(module.params, session, str(url))
455 elif (
456 isinstance(_json["value"], list)
457 and len(_json["value"]) > 0
458 and isinstance(_json["value"][0], str)
459 ):
460 # this is a list of id, we fetch the details
461 full_device_list = await build_full_device_list(session, str(url), _json)
462 _json = {"value": [i["value"] for i in full_device_list]}
463
464 return await update_changed_flag(_json, resp.status, "get")
465
466
467 if __name__ == "__main__":
468 import asyncio
469
470 current_loop = asyncio.new_event_loop()
471 try:
472 asyncio.set_event_loop(current_loop)
473 current_loop.run_until_complete(main())
474 finally:
475 current_loop.close()