Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/appliance_networking_noproxy.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_noproxy
12 short_description: Sets servers for which no proxy configuration should be applied
13 description: Sets servers for which no proxy configuration should be applied. This
14 operation sets environment variables. In order for this operation to take effect,
15 a logout from appliance or a service restart is required. If IPv4 is enabled,
16 "127.0.0.1" and "localhost" will always bypass the proxy (even if they are not
17 explicitly configured).
18 deprecated:
19 removed_in: 5.0.0
20 why: This module has been moved to the L(new vmware.vmware collection,https://forum.ansible.com/t/5880)
21 alternative: Use M(vmware.vmware.vcsa_settings) instead.
22 options:
23 servers:
24 description:
25 - List of strings representing servers to bypass proxy. A server can be a
26 FQDN, IP address, FQDN:port or IP:port combinations. This parameter is
27 mandatory.
28 elements: str
29 required: true
30 type: list
31 session_timeout:
32 description:
33 - 'Timeout settings for client session. '
34 - 'The maximal number of seconds for the whole operation including connection
35 establishment, request sending and response. '
36 - The default value is 300s.
37 type: float
38 version_added: 2.1.0
39 state:
40 choices:
41 - set
42 default: set
43 description: []
44 type: str
45 vcenter_hostname:
46 description:
47 - The hostname or IP address of the vSphere vCenter
48 - If the value is not specified in the task, the value of environment variable
49 C(VMWARE_HOST) will be used instead.
50 required: true
51 type: str
52 vcenter_password:
53 description:
54 - The vSphere vCenter password
55 - If the value is not specified in the task, the value of environment variable
56 C(VMWARE_PASSWORD) will be used instead.
57 required: true
58 type: str
59 vcenter_rest_log_file:
60 description:
61 - 'You can use this optional parameter to set the location of a log file. '
62 - 'This file will be used to record the HTTP REST interaction. '
63 - 'The file will be stored on the host that runs the module. '
64 - 'If the value is not specified in the task, the value of '
65 - environment variable C(VMWARE_REST_LOG_FILE) will be used instead.
66 type: str
67 vcenter_username:
68 description:
69 - The vSphere vCenter username
70 - If the value is not specified in the task, the value of environment variable
71 C(VMWARE_USER) will be used instead.
72 required: true
73 type: str
74 vcenter_validate_certs:
75 default: true
76 description:
77 - Allows connection when SSL certificates are not valid. Set to C(false) when
78 certificates are not trusted.
79 - If the value is not specified in the task, the value of environment variable
80 C(VMWARE_VALIDATE_CERTS) will be used instead.
81 type: bool
82 author:
83 - Ansible Cloud Team (@ansible-collections)
84 version_added: 2.0.0
85 requirements:
86 - vSphere 7.0.3 or greater
87 - python >= 3.6
88 - aiohttp
89 notes:
90 - Tested on vSphere 7.0.3
91 """
92
93 EXAMPLES = r"""
94 - name: Set HTTP noproxy configuration
95 vmware.vmware_rest.appliance_networking_noproxy:
96 servers:
97 - redhat.com
98 - ansible.com
99 register: result
100
101 - name: Remove the noproxy entries
102 vmware.vmware_rest.appliance_networking_noproxy:
103 servers: []
104 register: result
105 """
106 RETURN = r"""
107 # content generated by the update_return_section callback# task: Remove the noproxy entries
108 value:
109 description: Remove the noproxy entries
110 returned: On success
111 sample: {}
112 type: dict
113 """
114
115
116 # This structure describes the format of the data expected by the end-points
117 PAYLOAD_FORMAT = {
118 "set": {"query": {}, "body": {"servers": "servers"}, "path": {}}
119 } # pylint: disable=line-too-long
120
121 from ansible.module_utils.basic import env_fallback
122 import os
123
124 if os.getenv("VMWARE_ENABLE_TURBO", False):
125 try:
126 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
127 AnsibleTurboModule as AnsibleModule,
128 )
129
130 AnsibleModule.collection_name = "vmware.vmware_rest"
131 except ImportError:
132 from ansible.module_utils.basic import AnsibleModule
133 else:
134 from ansible.module_utils.basic import AnsibleModule
135
136 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
137 exists,
138 gen_args,
139 get_subdevice_type,
140 open_session,
141 prepare_payload,
142 session_timeout,
143 update_changed_flag,
144 )
145
146
147 def prepare_argument_spec():
148 argument_spec = {
149 "vcenter_hostname": dict(
150 type="str",
151 required=True,
152 fallback=(env_fallback, ["VMWARE_HOST"]),
153 ),
154 "vcenter_username": dict(
155 type="str",
156 required=True,
157 fallback=(env_fallback, ["VMWARE_USER"]),
158 ),
159 "vcenter_password": dict(
160 type="str",
161 required=True,
162 no_log=True,
163 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
164 ),
165 "vcenter_validate_certs": dict(
166 type="bool",
167 required=False,
168 default=True,
169 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
170 ),
171 "vcenter_rest_log_file": dict(
172 type="str",
173 required=False,
174 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
175 ),
176 "session_timeout": dict(
177 type="float",
178 required=False,
179 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
180 ),
181 }
182
183 argument_spec["servers"] = {"required": True, "type": "list", "elements": "str"}
184 argument_spec["state"] = {"type": "str", "choices": ["set"], "default": "set"}
185
186 return argument_spec
187
188
189 async def main():
190 required_if = list([])
191
192 module_args = prepare_argument_spec()
193 module = AnsibleModule(
194 argument_spec=module_args, required_if=required_if, supports_check_mode=False
195 )
196 if not module.params["vcenter_hostname"]:
197 module.fail_json("vcenter_hostname cannot be empty")
198 if not module.params["vcenter_username"]:
199 module.fail_json("vcenter_username cannot be empty")
200 if not module.params["vcenter_password"]:
201 module.fail_json("vcenter_password cannot be empty")
202 try:
203 session = await open_session(
204 vcenter_hostname=module.params["vcenter_hostname"],
205 vcenter_username=module.params["vcenter_username"],
206 vcenter_password=module.params["vcenter_password"],
207 validate_certs=module.params["vcenter_validate_certs"],
208 log_file=module.params["vcenter_rest_log_file"],
209 )
210 except Exception as err:
211 if hasattr(err, "get_message"):
212 module.fail_json(err.get_message())
213 else:
214 module.fail_json(str(err))
215 result = await entry_point(module, session)
216 module.exit_json(**result)
217
218
219 # template: default_module.j2
220 def build_url(params):
221 return ("https://{vcenter_hostname}" "/api/appliance/networking/noproxy").format(
222 **params
223 )
224
225
226 async def entry_point(module, session):
227
228 if module.params["state"] == "present":
229 if "_create" in globals():
230 operation = "create"
231 else:
232 operation = "update"
233 elif module.params["state"] == "absent":
234 operation = "delete"
235 else:
236 operation = module.params["state"]
237
238 func = globals()["_" + operation]
239
240 return await func(module.params, session)
241
242
243 async def _set(params, session):
244 _in_query_parameters = PAYLOAD_FORMAT["set"]["query"].keys()
245 payload = prepare_payload(params, PAYLOAD_FORMAT["set"])
246 subdevice_type = get_subdevice_type("/api/appliance/networking/noproxy")
247 if subdevice_type and not params[subdevice_type]:
248 _json = await exists(params, session, build_url(params))
249 if _json:
250 params[subdevice_type] = _json["id"]
251 _url = ("https://{vcenter_hostname}" "/api/appliance/networking/noproxy").format(
252 **params
253 ) + gen_args(params, _in_query_parameters)
254 async with session.get(_url, json=payload, **session_timeout(params)) as resp:
255 before = await resp.json()
256
257 async with session.put(_url, json=payload, **session_timeout(params)) as resp:
258 try:
259 if resp.headers["Content-Type"] == "application/json":
260 _json = await resp.json()
261 except KeyError:
262 _json = {}
263 if "value" not in _json: # 7.0.2
264 _json = {"value": _json}
265
266 # The PUT answer does not let us know if the resource has actually been
267 # modified
268 if resp.status < 300:
269 async with session.get(
270 _url, json=payload, **session_timeout(params)
271 ) as resp_get:
272 after = await resp_get.json()
273 if before == after:
274 return await update_changed_flag(after, resp_get.status, "get")
275 return await update_changed_flag(_json, resp.status, "set")
276
277
278 if __name__ == "__main__":
279 import asyncio
280
281 current_loop = asyncio.new_event_loop()
282 try:
283 asyncio.set_event_loop(current_loop)
284 current_loop.run_until_complete(main())
285 finally:
286 current_loop.close()