Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/appliance_shutdown_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: appliance_shutdown_info
12 short_description: Get details about the pending shutdown action.
13 description: Get details about the pending shutdown action.
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 author:
61 - Ansible Cloud Team (@ansible-collections)
62 version_added: 2.0.0
63 requirements:
64 - vSphere 7.0.3 or greater
65 - python >= 3.6
66 - aiohttp
67 notes:
68 - Tested on vSphere 7.0.3
69 """
70
71 EXAMPLES = r"""
72 - name: Ensure there is no ongoing shutdown
73 vmware.vmware_rest.appliance_shutdown_info:
74 register: result
75
76 - name: Get the ongoing shutdown
77 vmware.vmware_rest.appliance_shutdown_info:
78 register: result
79 """
80 RETURN = r"""
81 # content generated by the update_return_section callback# task: Get the ongoing shutdown
82 value:
83 description: Get the ongoing shutdown
84 returned: On success
85 sample:
86 action: reboot
87 reason: this is an example
88 shutdown_time: '2022-11-24T06:12:16.000Z'
89 type: dict
90 """
91
92
93 # This structure describes the format of the data expected by the end-points
94 PAYLOAD_FORMAT = {
95 "get": {"query": {}, "body": {}, "path": {}}
96 } # pylint: disable=line-too-long
97
98 from ansible.module_utils.basic import env_fallback
99 import os
100
101 if os.getenv("VMWARE_ENABLE_TURBO", False):
102 try:
103 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
104 AnsibleTurboModule as AnsibleModule,
105 )
106
107 AnsibleModule.collection_name = "vmware.vmware_rest"
108 except ImportError:
109 from ansible.module_utils.basic import AnsibleModule
110 else:
111 from ansible.module_utils.basic import AnsibleModule
112
113 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
114 gen_args,
115 open_session,
116 session_timeout,
117 update_changed_flag,
118 )
119
120
121 def prepare_argument_spec():
122 argument_spec = {
123 "vcenter_hostname": dict(
124 type="str",
125 required=True,
126 fallback=(env_fallback, ["VMWARE_HOST"]),
127 ),
128 "vcenter_username": dict(
129 type="str",
130 required=True,
131 fallback=(env_fallback, ["VMWARE_USER"]),
132 ),
133 "vcenter_password": dict(
134 type="str",
135 required=True,
136 no_log=True,
137 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
138 ),
139 "vcenter_validate_certs": dict(
140 type="bool",
141 required=False,
142 default=True,
143 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
144 ),
145 "vcenter_rest_log_file": dict(
146 type="str",
147 required=False,
148 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
149 ),
150 "session_timeout": dict(
151 type="float",
152 required=False,
153 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
154 ),
155 }
156
157 return argument_spec
158
159
160 async def main():
161 required_if = list([])
162
163 module_args = prepare_argument_spec()
164 module = AnsibleModule(
165 argument_spec=module_args, required_if=required_if, supports_check_mode=True
166 )
167 if not module.params["vcenter_hostname"]:
168 module.fail_json("vcenter_hostname cannot be empty")
169 if not module.params["vcenter_username"]:
170 module.fail_json("vcenter_username cannot be empty")
171 if not module.params["vcenter_password"]:
172 module.fail_json("vcenter_password cannot be empty")
173 try:
174 session = await open_session(
175 vcenter_hostname=module.params["vcenter_hostname"],
176 vcenter_username=module.params["vcenter_username"],
177 vcenter_password=module.params["vcenter_password"],
178 validate_certs=module.params["vcenter_validate_certs"],
179 log_file=module.params["vcenter_rest_log_file"],
180 )
181 except Exception as err:
182 if hasattr(err, "get_message"):
183 module.fail_json(err.get_message())
184 else:
185 module.fail_json(str(err))
186 result = await entry_point(module, session)
187 module.exit_json(**result)
188
189
190 # template: info_no_list_module.j2
191 def build_url(params):
192 return ("https://{vcenter_hostname}" "/api/appliance/shutdown").format(**params)
193
194
195 async def entry_point(module, session):
196 return await _info(module.params, session)
197
198
199 async def _info(params, session):
200 payload_format = list(PAYLOAD_FORMAT.values())[0]
201 _in_query_parameters = payload_format["query"].keys()
202 _url = ("https://{vcenter_hostname}" "/api/appliance/shutdown").format(
203 **params
204 ) + gen_args(params, _in_query_parameters)
205 async with session.get(_url, **session_timeout(params)) as resp:
206 try:
207 if resp.headers["Content-Type"] == "application/json":
208 _json = await resp.json()
209 except KeyError:
210 _json = {}
211 return await update_changed_flag(_json, resp.status, "get")
212
213
214 if __name__ == "__main__":
215 import asyncio
216
217 current_loop = asyncio.new_event_loop()
218 try:
219 asyncio.set_event_loop(current_loop)
220 current_loop.run_until_complete(main())
221 finally:
222 current_loop.close()