Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/appliance_health_mem_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_health_mem_info
12 short_description: Get memory health.
13 description: Get memory health.
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: Get the system mem status
73 vmware.vmware_rest.appliance_health_mem_info:
74 register: result
75 """
76 RETURN = r"""
77
78 # content generated by the update_return_section callback# task: Get the system mem status
79 value:
80 description: Get the system mem status
81 returned: On success
82 sample: green
83 type: str
84 """
85
86
87 # This structure describes the format of the data expected by the end-points
88 PAYLOAD_FORMAT = {
89 "get": {"query": {}, "body": {}, "path": {}}
90 } # pylint: disable=line-too-long
91
92 from ansible.module_utils.basic import env_fallback
93 import os
94
95 if os.getenv("VMWARE_ENABLE_TURBO", False):
96 try:
97 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
98 AnsibleTurboModule as AnsibleModule,
99 )
100
101 AnsibleModule.collection_name = "vmware.vmware_rest"
102 except ImportError:
103 from ansible.module_utils.basic import AnsibleModule
104 else:
105 from ansible.module_utils.basic import AnsibleModule
106
107 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
108 gen_args,
109 open_session,
110 session_timeout,
111 update_changed_flag,
112 )
113
114
115 def prepare_argument_spec():
116 argument_spec = {
117 "vcenter_hostname": dict(
118 type="str",
119 required=True,
120 fallback=(env_fallback, ["VMWARE_HOST"]),
121 ),
122 "vcenter_username": dict(
123 type="str",
124 required=True,
125 fallback=(env_fallback, ["VMWARE_USER"]),
126 ),
127 "vcenter_password": dict(
128 type="str",
129 required=True,
130 no_log=True,
131 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
132 ),
133 "vcenter_validate_certs": dict(
134 type="bool",
135 required=False,
136 default=True,
137 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
138 ),
139 "vcenter_rest_log_file": dict(
140 type="str",
141 required=False,
142 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
143 ),
144 "session_timeout": dict(
145 type="float",
146 required=False,
147 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
148 ),
149 }
150
151 return argument_spec
152
153
154 async def main():
155 required_if = list([])
156
157 module_args = prepare_argument_spec()
158 module = AnsibleModule(
159 argument_spec=module_args, required_if=required_if, supports_check_mode=True
160 )
161 if not module.params["vcenter_hostname"]:
162 module.fail_json("vcenter_hostname cannot be empty")
163 if not module.params["vcenter_username"]:
164 module.fail_json("vcenter_username cannot be empty")
165 if not module.params["vcenter_password"]:
166 module.fail_json("vcenter_password cannot be empty")
167 try:
168 session = await open_session(
169 vcenter_hostname=module.params["vcenter_hostname"],
170 vcenter_username=module.params["vcenter_username"],
171 vcenter_password=module.params["vcenter_password"],
172 validate_certs=module.params["vcenter_validate_certs"],
173 log_file=module.params["vcenter_rest_log_file"],
174 )
175 except Exception as err:
176 if hasattr(err, "get_message"):
177 module.fail_json(err.get_message())
178 else:
179 module.fail_json(str(err))
180 result = await entry_point(module, session)
181 module.exit_json(**result)
182
183
184 # template: info_no_list_module.j2
185 def build_url(params):
186 return ("https://{vcenter_hostname}" "/api/appliance/health/mem").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 = ("https://{vcenter_hostname}" "/api/appliance/health/mem").format(
197 **params
198 ) + 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()