Debian Codegraph @

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