Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/vcenter_vm_hardware_floppy_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_hardware_floppy_info
12 short_description: Returns information about a virtual floppy drive.
13 description: Returns information about a virtual floppy drive.
14 options:
15 floppy:
16 description:
17 - Virtual floppy drive identifier.
18 - The parameter must be the id of a resource returned by M(vmware.vmware_rest.vcenter_vm_hardware_floppy).
19 Required with I(state=['get'])
20 type: str
21 label:
22 description:
23 - The name of the item
24 type: str
25 session_timeout:
26 description:
27 - 'Timeout settings for client session. '
28 - 'The maximal number of seconds for the whole operation including connection
29 establishment, request sending and response. '
30 - The default value is 300s.
31 type: float
32 version_added: 2.1.0
33 vcenter_hostname:
34 description:
35 - The hostname or IP address of the vSphere vCenter
36 - If the value is not specified in the task, the value of environment variable
37 C(VMWARE_HOST) will be used instead.
38 required: true
39 type: str
40 vcenter_password:
41 description:
42 - The vSphere vCenter password
43 - If the value is not specified in the task, the value of environment variable
44 C(VMWARE_PASSWORD) will be used instead.
45 required: true
46 type: str
47 vcenter_rest_log_file:
48 description:
49 - 'You can use this optional parameter to set the location of a log file. '
50 - 'This file will be used to record the HTTP REST interaction. '
51 - 'The file will be stored on the host that runs the module. '
52 - 'If the value is not specified in the task, the value of '
53 - environment variable C(VMWARE_REST_LOG_FILE) will be used instead.
54 type: str
55 vcenter_username:
56 description:
57 - The vSphere vCenter username
58 - If the value is not specified in the task, the value of environment variable
59 C(VMWARE_USER) will be used instead.
60 required: true
61 type: str
62 vcenter_validate_certs:
63 default: true
64 description:
65 - Allows connection when SSL certificates are not valid. Set to C(false) when
66 certificates are not trusted.
67 - If the value is not specified in the task, the value of environment variable
68 C(VMWARE_VALIDATE_CERTS) will be used instead.
69 type: bool
70 vm:
71 description:
72 - Virtual machine identifier.
73 - The parameter must be the id of a resource returned by M(vmware.vmware_rest.vcenter_vm_info).
74 This parameter is mandatory.
75 required: true
76 type: str
77 author:
78 - Ansible Cloud Team (@ansible-collections)
79 version_added: 0.1.0
80 requirements:
81 - vSphere 7.0.3 or greater
82 - python >= 3.6
83 - aiohttp
84 notes:
85 - Tested on vSphere 7.0.3
86 """
87
88 EXAMPLES = r"""
89 - name: Look up the VM called test_vm1 in the inventory
90 register: search_result
91 vmware.vmware_rest.vcenter_vm_info:
92 filter_names:
93 - test_vm1
94
95 - name: Collect information about a specific VM
96 vmware.vmware_rest.vcenter_vm_info:
97 vm: '{{ search_result.value[0].vm }}'
98 register: test_vm1_info
99
100 - name: List the floppy disk drives
101 vmware.vmware_rest.vcenter_vm_hardware_floppy_info:
102 vm: '{{ test_vm1_info.id }}'
103 register: _result
104 """
105 RETURN = r"""
106 # content generated by the update_return_section callback# task: List the floppy disk drives
107 value:
108 description: List the floppy disk drives
109 returned: On success
110 sample: []
111 type: list
112 """
113
114
115 # This structure describes the format of the data expected by the end-points
116 PAYLOAD_FORMAT = {
117 "get": {"query": {}, "body": {}, "path": {"floppy": "floppy", "vm": "vm"}},
118 "list": {"query": {}, "body": {}, "path": {"vm": "vm"}},
119 } # pylint: disable=line-too-long
120
121 from ansible.module_utils.basic import env_fallback
122 import os
123
124 if os.getenv("VMWARE_ENABLE_TURBO", False):
125 try:
126 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
127 AnsibleTurboModule as AnsibleModule,
128 )
129
130 AnsibleModule.collection_name = "vmware.vmware_rest"
131 except ImportError:
132 from ansible.module_utils.basic import AnsibleModule
133 else:
134 from ansible.module_utils.basic import AnsibleModule
135
136 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
137 build_full_device_list,
138 exists,
139 gen_args,
140 open_session,
141 session_timeout,
142 update_changed_flag,
143 )
144
145
146 def prepare_argument_spec():
147 argument_spec = {
148 "vcenter_hostname": dict(
149 type="str",
150 required=True,
151 fallback=(env_fallback, ["VMWARE_HOST"]),
152 ),
153 "vcenter_username": dict(
154 type="str",
155 required=True,
156 fallback=(env_fallback, ["VMWARE_USER"]),
157 ),
158 "vcenter_password": dict(
159 type="str",
160 required=True,
161 no_log=True,
162 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
163 ),
164 "vcenter_validate_certs": dict(
165 type="bool",
166 required=False,
167 default=True,
168 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
169 ),
170 "vcenter_rest_log_file": dict(
171 type="str",
172 required=False,
173 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
174 ),
175 "session_timeout": dict(
176 type="float",
177 required=False,
178 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
179 ),
180 }
181
182 argument_spec["floppy"] = {"type": "str"}
183 argument_spec["label"] = {"type": "str"}
184 argument_spec["vm"] = {"required": True, "type": "str"}
185
186 return argument_spec
187
188
189 async def main():
190 required_if = list([])
191
192 module_args = prepare_argument_spec()
193 module = AnsibleModule(
194 argument_spec=module_args, required_if=required_if, supports_check_mode=True
195 )
196 if not module.params["vcenter_hostname"]:
197 module.fail_json("vcenter_hostname cannot be empty")
198 if not module.params["vcenter_username"]:
199 module.fail_json("vcenter_username cannot be empty")
200 if not module.params["vcenter_password"]:
201 module.fail_json("vcenter_password cannot be empty")
202 try:
203 session = await open_session(
204 vcenter_hostname=module.params["vcenter_hostname"],
205 vcenter_username=module.params["vcenter_username"],
206 vcenter_password=module.params["vcenter_password"],
207 validate_certs=module.params["vcenter_validate_certs"],
208 log_file=module.params["vcenter_rest_log_file"],
209 )
210 except Exception as err:
211 if hasattr(err, "get_message"):
212 module.fail_json(err.get_message())
213 else:
214 module.fail_json(str(err))
215 result = await entry_point(module, session)
216 module.exit_json(**result)
217
218
219 # template: info_list_and_get_module.j2
220 def build_url(params):
221 import yarl
222
223 if params.get("floppy"):
224 _in_query_parameters = PAYLOAD_FORMAT["get"]["query"].keys()
225 return yarl.URL(
226 (
227 "https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/hardware/floppy/"
228 ).format(**params)
229 + params["floppy"]
230 + gen_args(params, _in_query_parameters),
231 encoded=True,
232 )
233 _in_query_parameters = PAYLOAD_FORMAT["list"]["query"].keys()
234 return yarl.URL(
235 ("https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/hardware/floppy").format(
236 **params
237 )
238 + gen_args(params, _in_query_parameters),
239 encoded=True,
240 )
241
242
243 async def entry_point(module, session):
244 url = build_url(module.params)
245 async with session.get(url, **session_timeout(module.params)) as resp:
246 _json = await resp.json()
247
248 if "value" not in _json: # 7.0.2+
249 _json = {"value": _json}
250
251 if module.params.get("floppy"):
252 _json["id"] = module.params.get("floppy")
253 elif module.params.get("label"): # TODO extend the list of filter
254 _json = await exists(module.params, session, str(url))
255 elif (
256 isinstance(_json["value"], list)
257 and len(_json["value"]) > 0
258 and isinstance(_json["value"][0], str)
259 ):
260 # this is a list of id, we fetch the details
261 full_device_list = await build_full_device_list(session, str(url), _json)
262 _json = {"value": [i["value"] for i in full_device_list]}
263
264 return await update_changed_flag(_json, resp.status, "get")
265
266
267 if __name__ == "__main__":
268 import asyncio
269
270 current_loop = asyncio.new_event_loop()
271 try:
272 asyncio.set_event_loop(current_loop)
273 current_loop.run_until_complete(main())
274 finally:
275 current_loop.close()