Debian Codegraph @

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