Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/vcenter_vm_libraryitem_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: vcenter_vm_libraryitem_info
12 short_description: Returns the information about the library item associated with
13 the virtual machine.
14 description: Returns the information about the library item associated with the virtual
15 machine.
16 deprecated:
17 removed_in: 5.0.0
18 why: This module has been moved to the L(new vmware.vmware collection,https://forum.ansible.com/t/5880)
19 alternative: Use M(vmware.vmware.content_library_item_info) instead.
20 options:
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 vm:
67 description:
68 - Identifier of the virtual machine.
69 - The parameter must be the id of a resource returned by M(vmware.vmware_rest.vcenter_vm_info).
70 Required with I(state=['get'])
71 type: str
72 author:
73 - Ansible Cloud Team (@ansible-collections)
74 version_added: 0.1.0
75 requirements:
76 - vSphere 7.0.3 or greater
77 - python >= 3.6
78 - aiohttp
79 notes:
80 - Tested on vSphere 7.0.3
81 """
82
83 EXAMPLES = r"""
84 - name: Look up the VM called test_vm1 in the inventory
85 register: search_result
86 vmware.vmware_rest.vcenter_vm_info:
87 filter_names:
88 - test_vm1
89
90 - name: Collect information about a specific VM
91 vmware.vmware_rest.vcenter_vm_info:
92 vm: '{{ search_result.value[0].vm }}'
93 register: test_vm1_info
94
95 - name: Get the information from the library
96 vmware.vmware_rest.vcenter_vm_libraryitem_info:
97 vm: '{{ test_vm1_info.id }}'
98 register: _result
99 """
100 RETURN = r"""
101 # content generated by the update_return_section callback# task: Get the information from the library
102 value:
103 description: Get the information from the library
104 returned: On success
105 sample: {}
106 type: dict
107 """
108
109
110 # This structure describes the format of the data expected by the end-points
111 PAYLOAD_FORMAT = {
112 "get": {"query": {}, "body": {}, "path": {"vm": "vm"}}
113 } # pylint: disable=line-too-long
114
115 from ansible.module_utils.basic import env_fallback
116 import os
117
118 if os.getenv("VMWARE_ENABLE_TURBO", False):
119 try:
120 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
121 AnsibleTurboModule as AnsibleModule,
122 )
123
124 AnsibleModule.collection_name = "vmware.vmware_rest"
125 except ImportError:
126 from ansible.module_utils.basic import AnsibleModule
127 else:
128 from ansible.module_utils.basic import AnsibleModule
129
130 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
131 gen_args,
132 open_session,
133 session_timeout,
134 update_changed_flag,
135 )
136
137
138 def prepare_argument_spec():
139 argument_spec = {
140 "vcenter_hostname": dict(
141 type="str",
142 required=True,
143 fallback=(env_fallback, ["VMWARE_HOST"]),
144 ),
145 "vcenter_username": dict(
146 type="str",
147 required=True,
148 fallback=(env_fallback, ["VMWARE_USER"]),
149 ),
150 "vcenter_password": dict(
151 type="str",
152 required=True,
153 no_log=True,
154 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
155 ),
156 "vcenter_validate_certs": dict(
157 type="bool",
158 required=False,
159 default=True,
160 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
161 ),
162 "vcenter_rest_log_file": dict(
163 type="str",
164 required=False,
165 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
166 ),
167 "session_timeout": dict(
168 type="float",
169 required=False,
170 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
171 ),
172 }
173
174 argument_spec["vm"] = {"type": "str"}
175
176 return argument_spec
177
178
179 async def main():
180 required_if = list([])
181
182 module_args = prepare_argument_spec()
183 module = AnsibleModule(
184 argument_spec=module_args, required_if=required_if, supports_check_mode=True
185 )
186 if not module.params["vcenter_hostname"]:
187 module.fail_json("vcenter_hostname cannot be empty")
188 if not module.params["vcenter_username"]:
189 module.fail_json("vcenter_username cannot be empty")
190 if not module.params["vcenter_password"]:
191 module.fail_json("vcenter_password cannot be empty")
192 try:
193 session = await open_session(
194 vcenter_hostname=module.params["vcenter_hostname"],
195 vcenter_username=module.params["vcenter_username"],
196 vcenter_password=module.params["vcenter_password"],
197 validate_certs=module.params["vcenter_validate_certs"],
198 log_file=module.params["vcenter_rest_log_file"],
199 )
200 except Exception as err:
201 if hasattr(err, "get_message"):
202 module.fail_json(err.get_message())
203 else:
204 module.fail_json(str(err))
205 result = await entry_point(module, session)
206 module.exit_json(**result)
207
208
209 # template: info_no_list_module.j2
210 def build_url(params):
211 return ("https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/library-item").format(
212 **params
213 )
214
215
216 async def entry_point(module, session):
217 return await _info(module.params, session)
218
219
220 async def _info(params, session):
221 payload_format = list(PAYLOAD_FORMAT.values())[0]
222 _in_query_parameters = payload_format["query"].keys()
223 _url = ("https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/library-item").format(
224 **params
225 ) + gen_args(params, _in_query_parameters)
226 async with session.get(_url, **session_timeout(params)) as resp:
227 try:
228 if resp.headers["Content-Type"] == "application/json":
229 _json = await resp.json()
230 except KeyError:
231 _json = {}
232 return await update_changed_flag(_json, resp.status, "get")
233
234
235 if __name__ == "__main__":
236 import asyncio
237
238 current_loop = asyncio.new_event_loop()
239 try:
240 asyncio.set_event_loop(current_loop)
241 current_loop.run_until_complete(main())
242 finally:
243 current_loop.close()