Debian Codegraph @

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