Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/appliance_networking_proxy_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_networking_proxy_info
12 short_description: Gets the proxy configuration for a specific protocol.
13 description: Gets the proxy configuration for a specific protocol.
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.appliance_info) instead.
18 options:
19 protocol:
20 description:
21 - The protocol whose proxy configuration is requested. Required with I(state=['get'])
22 type: str
23 session_timeout:
24 description:
25 - 'Timeout settings for client session. '
26 - 'The maximal number of seconds for the whole operation including connection
27 establishment, request sending and response. '
28 - The default value is 300s.
29 type: float
30 version_added: 2.1.0
31 vcenter_hostname:
32 description:
33 - The hostname or IP address of the vSphere vCenter
34 - If the value is not specified in the task, the value of environment variable
35 C(VMWARE_HOST) will be used instead.
36 required: true
37 type: str
38 vcenter_password:
39 description:
40 - The vSphere vCenter password
41 - If the value is not specified in the task, the value of environment variable
42 C(VMWARE_PASSWORD) will be used instead.
43 required: true
44 type: str
45 vcenter_rest_log_file:
46 description:
47 - 'You can use this optional parameter to set the location of a log file. '
48 - 'This file will be used to record the HTTP REST interaction. '
49 - 'The file will be stored on the host that runs the module. '
50 - 'If the value is not specified in the task, the value of '
51 - environment variable C(VMWARE_REST_LOG_FILE) will be used instead.
52 type: str
53 vcenter_username:
54 description:
55 - The vSphere vCenter username
56 - If the value is not specified in the task, the value of environment variable
57 C(VMWARE_USER) will be used instead.
58 required: true
59 type: str
60 vcenter_validate_certs:
61 default: true
62 description:
63 - Allows connection when SSL certificates are not valid. Set to C(false) when
64 certificates are not trusted.
65 - If the value is not specified in the task, the value of environment variable
66 C(VMWARE_VALIDATE_CERTS) will be used instead.
67 type: bool
68 author:
69 - Ansible Cloud Team (@ansible-collections)
70 version_added: 2.0.0
71 requirements:
72 - vSphere 7.0.3 or greater
73 - python >= 3.6
74 - aiohttp
75 notes:
76 - Tested on vSphere 7.0.3
77 """
78
79 EXAMPLES = r"""
80 - name: Get the HTTP proxy configuration
81 vmware.vmware_rest.appliance_networking_proxy_info:
82 register: result
83 """
84 RETURN = r"""
85 # content generated by the update_return_section callback# task: Get the HTTP proxy configuration
86 value:
87 description: Get the HTTP proxy configuration
88 returned: On success
89 sample:
90 ftp:
91 enabled: 0
92 port: -1
93 server: ''
94 http:
95 enabled: 0
96 port: -1
97 server: ''
98 https:
99 enabled: 0
100 port: -1
101 server: ''
102 type: dict
103 """
104
105
106 # This structure describes the format of the data expected by the end-points
107 PAYLOAD_FORMAT = {
108 "get": {"query": {}, "body": {}, "path": {"protocol": "protocol"}},
109 "list": {"query": {}, "body": {}, "path": {}},
110 } # pylint: disable=line-too-long
111
112 from ansible.module_utils.basic import env_fallback
113 import os
114
115 if os.getenv("VMWARE_ENABLE_TURBO", False):
116 try:
117 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
118 AnsibleTurboModule as AnsibleModule,
119 )
120
121 AnsibleModule.collection_name = "vmware.vmware_rest"
122 except ImportError:
123 from ansible.module_utils.basic import AnsibleModule
124 else:
125 from ansible.module_utils.basic import AnsibleModule
126
127 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
128 build_full_device_list,
129 exists,
130 gen_args,
131 open_session,
132 session_timeout,
133 update_changed_flag,
134 )
135
136
137 def prepare_argument_spec():
138 argument_spec = {
139 "vcenter_hostname": dict(
140 type="str",
141 required=True,
142 fallback=(env_fallback, ["VMWARE_HOST"]),
143 ),
144 "vcenter_username": dict(
145 type="str",
146 required=True,
147 fallback=(env_fallback, ["VMWARE_USER"]),
148 ),
149 "vcenter_password": dict(
150 type="str",
151 required=True,
152 no_log=True,
153 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
154 ),
155 "vcenter_validate_certs": dict(
156 type="bool",
157 required=False,
158 default=True,
159 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
160 ),
161 "vcenter_rest_log_file": dict(
162 type="str",
163 required=False,
164 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
165 ),
166 "session_timeout": dict(
167 type="float",
168 required=False,
169 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
170 ),
171 }
172
173 argument_spec["protocol"] = {"type": "str"}
174
175 return argument_spec
176
177
178 async def main():
179 required_if = list([])
180
181 module_args = prepare_argument_spec()
182 module = AnsibleModule(
183 argument_spec=module_args, required_if=required_if, supports_check_mode=True
184 )
185 if not module.params["vcenter_hostname"]:
186 module.fail_json("vcenter_hostname cannot be empty")
187 if not module.params["vcenter_username"]:
188 module.fail_json("vcenter_username cannot be empty")
189 if not module.params["vcenter_password"]:
190 module.fail_json("vcenter_password cannot be empty")
191 try:
192 session = await open_session(
193 vcenter_hostname=module.params["vcenter_hostname"],
194 vcenter_username=module.params["vcenter_username"],
195 vcenter_password=module.params["vcenter_password"],
196 validate_certs=module.params["vcenter_validate_certs"],
197 log_file=module.params["vcenter_rest_log_file"],
198 )
199 except Exception as err:
200 if hasattr(err, "get_message"):
201 module.fail_json(err.get_message())
202 else:
203 module.fail_json(str(err))
204 result = await entry_point(module, session)
205 module.exit_json(**result)
206
207
208 # template: info_list_and_get_module.j2
209 def build_url(params):
210 import yarl
211
212 if params.get("protocol"):
213 _in_query_parameters = PAYLOAD_FORMAT["get"]["query"].keys()
214 return yarl.URL(
215 ("https://{vcenter_hostname}" "/api/appliance/networking/proxy/").format(
216 **params
217 )
218 + params["protocol"]
219 + gen_args(params, _in_query_parameters),
220 encoded=True,
221 )
222 _in_query_parameters = PAYLOAD_FORMAT["list"]["query"].keys()
223 return yarl.URL(
224 ("https://{vcenter_hostname}" "/api/appliance/networking/proxy").format(
225 **params
226 )
227 + gen_args(params, _in_query_parameters),
228 encoded=True,
229 )
230
231
232 async def entry_point(module, session):
233 url = build_url(module.params)
234 async with session.get(url, **session_timeout(module.params)) as resp:
235 _json = await resp.json()
236
237 if "value" not in _json: # 7.0.2+
238 _json = {"value": _json}
239
240 if module.params.get("protocol"):
241 _json["id"] = module.params.get("protocol")
242 elif module.params.get("label"): # TODO extend the list of filter
243 _json = await exists(module.params, session, str(url))
244 elif (
245 isinstance(_json["value"], list)
246 and len(_json["value"]) > 0
247 and isinstance(_json["value"][0], str)
248 ):
249 # this is a list of id, we fetch the details
250 full_device_list = await build_full_device_list(session, str(url), _json)
251 _json = {"value": [i["value"] for i in full_device_list]}
252
253 return await update_changed_flag(_json, resp.status, "get")
254
255
256 if __name__ == "__main__":
257 import asyncio
258
259 current_loop = asyncio.new_event_loop()
260 try:
261 asyncio.set_event_loop(current_loop)
262 current_loop.run_until_complete(main())
263 finally:
264 current_loop.close()