Debian Codegraph @

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