Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/vcenter_vm_hardware_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_info
12 short_description: Returns the virtual hardware settings of a virtual machine.
13 description: Returns the virtual hardware 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 the hardware information
85 vmware.vmware_rest.vcenter_vm_hardware_info:
86 vm: '{{ search_result.value[0].vm }}'
87 register: my_vm1_hardware_info
88 """
89 RETURN = r"""
90 # content generated by the update_return_section callback# task: Collect the hardware information
91 value:
92 description: Collect the hardware information
93 returned: On success
94 sample:
95 upgrade_policy: NEVER
96 upgrade_status: NONE
97 version: VMX_10
98 type: dict
99 """
100
101
102 # This structure describes the format of the data expected by the end-points
103 PAYLOAD_FORMAT = {
104 "get": {"query": {}, "body": {}, "path": {"vm": "vm"}}
105 } # pylint: disable=line-too-long
106
107 from ansible.module_utils.basic import env_fallback
108 import os
109
110 if os.getenv("VMWARE_ENABLE_TURBO", False):
111 try:
112 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
113 AnsibleTurboModule as AnsibleModule,
114 )
115
116 AnsibleModule.collection_name = "vmware.vmware_rest"
117 except ImportError:
118 from ansible.module_utils.basic import AnsibleModule
119 else:
120 from ansible.module_utils.basic import AnsibleModule
121
122 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
123 gen_args,
124 open_session,
125 session_timeout,
126 update_changed_flag,
127 )
128
129
130 def prepare_argument_spec():
131 argument_spec = {
132 "vcenter_hostname": dict(
133 type="str",
134 required=True,
135 fallback=(env_fallback, ["VMWARE_HOST"]),
136 ),
137 "vcenter_username": dict(
138 type="str",
139 required=True,
140 fallback=(env_fallback, ["VMWARE_USER"]),
141 ),
142 "vcenter_password": dict(
143 type="str",
144 required=True,
145 no_log=True,
146 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
147 ),
148 "vcenter_validate_certs": dict(
149 type="bool",
150 required=False,
151 default=True,
152 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
153 ),
154 "vcenter_rest_log_file": dict(
155 type="str",
156 required=False,
157 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
158 ),
159 "session_timeout": dict(
160 type="float",
161 required=False,
162 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
163 ),
164 }
165
166 argument_spec["vm"] = {"type": "str"}
167
168 return argument_spec
169
170
171 async def main():
172 required_if = list([])
173
174 module_args = prepare_argument_spec()
175 module = AnsibleModule(
176 argument_spec=module_args, required_if=required_if, supports_check_mode=True
177 )
178 if not module.params["vcenter_hostname"]:
179 module.fail_json("vcenter_hostname cannot be empty")
180 if not module.params["vcenter_username"]:
181 module.fail_json("vcenter_username cannot be empty")
182 if not module.params["vcenter_password"]:
183 module.fail_json("vcenter_password cannot be empty")
184 try:
185 session = await open_session(
186 vcenter_hostname=module.params["vcenter_hostname"],
187 vcenter_username=module.params["vcenter_username"],
188 vcenter_password=module.params["vcenter_password"],
189 validate_certs=module.params["vcenter_validate_certs"],
190 log_file=module.params["vcenter_rest_log_file"],
191 )
192 except Exception as err:
193 if hasattr(err, "get_message"):
194 module.fail_json(err.get_message())
195 else:
196 module.fail_json(str(err))
197 result = await entry_point(module, session)
198 module.exit_json(**result)
199
200
201 # template: info_no_list_module.j2
202 def build_url(params):
203 return ("https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/hardware").format(
204 **params
205 )
206
207
208 async def entry_point(module, session):
209 return await _info(module.params, session)
210
211
212 async def _info(params, session):
213 payload_format = list(PAYLOAD_FORMAT.values())[0]
214 _in_query_parameters = payload_format["query"].keys()
215 _url = ("https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/hardware").format(
216 **params
217 ) + gen_args(params, _in_query_parameters)
218 async with session.get(_url, **session_timeout(params)) as resp:
219 try:
220 if resp.headers["Content-Type"] == "application/json":
221 _json = await resp.json()
222 except KeyError:
223 _json = {}
224 return await update_changed_flag(_json, resp.status, "get")
225
226
227 if __name__ == "__main__":
228 import asyncio
229
230 current_loop = asyncio.new_event_loop()
231 try:
232 asyncio.set_event_loop(current_loop)
233 current_loop.run_until_complete(main())
234 finally:
235 current_loop.close()