Debian Codegraph @

ansible_collections/dellemc/openmanage/plugins/modules/ome_application_network_proxy.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 #
5 # Dell OpenManage Ansible Modules
6 # Version 10.0.0
7 # Copyright (C) 2020-2025 Dell Inc. or its subsidiaries. All Rights Reserved.
8
9 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
10 #
11
12
13 from __future__ import (absolute_import, division, print_function)
14
15 __metaclass__ = type
16
17 DOCUMENTATION = r'''
18 ---
19 module: ome_application_network_proxy
20 short_description: Updates the proxy configuration on OpenManage Enterprise
21 version_added: "2.1.0"
22 description: This module allows to configure a network proxy on OpenManage Enterprise.
23 extends_documentation_fragment:
24 - dellemc.openmanage.ome_auth_options
25 options:
26 enable_proxy:
27 description:
28 - Enables or disables the HTTP proxy configuration.
29 - If I(enable proxy) is false, then the HTTP proxy configuration is set to its default value.
30 required: true
31 type: bool
32 ip_address:
33 description:
34 - Proxy server address.
35 - This option is mandatory when I(enable_proxy) is true.
36 type: str
37 proxy_port:
38 description:
39 - Proxy server's port number.
40 - This option is mandatory when I(enable_proxy) is true.
41 type: int
42 enable_authentication:
43 description:
44 - Enable or disable proxy authentication.
45 - If I(enable_authentication) is true, I(proxy_username) and I(proxy_password) must be provided.
46 - If I(enable_authentication) is false, the proxy username and password are set to its default values.
47 type: bool
48 proxy_username:
49 description:
50 - Proxy server username.
51 - This option is mandatory when I(enable_authentication) is true.
52 type: str
53 proxy_password:
54 description:
55 - Proxy server password.
56 - This option is mandatory when I(enable_authentication) is true.
57 type: str
58 ignore_certificate_validation:
59 description:
60 - This option will ignore the integrated certificate checks like those used for the warranty and catalog updates.
61 - C(true) ignores the certificate validation.
62 - C(false) does not ignore the certificate validation.
63 type: bool
64 default: false
65 aliases: [ssl_check_disabled]
66 version_added: 9.5.0
67 proxy_exclusion_list:
68 description:
69 - The list of IPv4 addresses, IPv6 addresses or the domain names of the devices that can bypass the proxy server to directly access the appliance.
70 type: list
71 elements: str
72 version_added: 9.5.0
73 update_password:
74 description:
75 - This flag is used to update the I(proxy_password).
76 - This is applicable only when I(enable_authentication) is C(true).
77 - C(true) will update the I(proxy_password).
78 - C(false) will not update the I(proxy_password).
79 type: bool
80 default: false
81 version_added: 9.5.0
82 requirements:
83 - "python >= 3.9.6"
84 author:
85 - "Sajna Shetty(@Sajna-Shetty)"
86 - "Rajshekar P(@rajshekarp87)"
87 - "Sapana Gupta(@sapana05)"
88 notes:
89 - Run this module from a system that has direct access to Dell OpenManage Enterprise.
90 - This module supports C(check_mode).
91 - This module supports IPv4 and IPv6 addresses.
92 '''
93
94 EXAMPLES = r'''
95 ---
96 - name: Update proxy configuration and enable authentication
97 dellemc.openmanage.ome_application_network_proxy:
98 hostname: "192.168.0.1"
99 username: "username"
100 password: "password"
101 ca_path: "/path/to/ca_cert.pem"
102 enable_proxy: true
103 ip_address: "192.168.0.2"
104 proxy_port: 444
105 enable_authentication: true
106 proxy_username: "proxy_username"
107 proxy_password: "proxy_password"
108
109 - name: Reset proxy authentication
110 dellemc.openmanage.ome_application_network_proxy:
111 hostname: "192.168.0.1"
112 username: "username"
113 password: "password"
114 ca_path: "/path/to/ca_cert.pem"
115 enable_proxy: true
116 ip_address: "192.168.0.2"
117 proxy_port: 444
118 enable_authentication: false
119
120 - name: Reset proxy configuration
121 dellemc.openmanage.ome_application_network_proxy:
122 hostname: "192.168.0.1"
123 username: "username"
124 password: "password"
125 ca_path: "/path/to/ca_cert.pem"
126 enable_proxy: false
127
128 - name: Add IPv4, IPv6 and domain names of devices in proxy exclusion list
129 dellemc.openmanage.ome_application_network_proxy:
130 hostname: "192.168.0.1"
131 username: "username"
132 password: "password"
133 ca_path: "/path/to/ca_cert.pem"
134 enable_proxy: true
135 ip_address: "192.168.0.2"
136 proxy_port: 444
137 enable_authentication: false
138 proxy_exclusion_list:
139 - 192.168.1.0
140 - 191.187.2.0
141 - www.*.com
142 - 191.1.168.1/24
143
144 - name: Clear the proxy exclusion list
145 dellemc.openmanage.ome_application_network_proxy:
146 hostname: "192.168.0.1"
147 username: "username"
148 password: "password"
149 ca_path: "/path/to/ca_cert.pem"
150 enable_proxy: true
151 ip_address: "192.168.0.2"
152 proxy_port: 444
153 proxy_exclusion_list: []
154
155 - name: Ignore the certificate validation
156 dellemc.openmanage.ome_application_network_proxy:
157 hostname: "192.168.0.1"
158 username: "username"
159 password: "password"
160 ca_path: "/path/to/ca_cert.pem"
161 enable_proxy: true
162 ip_address: "192.168.0.2"
163 proxy_port: 444
164 ignore_certificate_validation: true
165 '''
166
167 RETURN = r'''
168 ---
169 msg:
170 type: str
171 description: Overall status of the network proxy configuration change.
172 returned: always
173 sample: "Successfully updated network proxy configuration."
174 proxy_configuration:
175 type: dict
176 description: Updated application network proxy configuration.
177 returned: On successful configuration of network proxy settings
178 sample: {
179 "EnableAuthentication": true,
180 "EnableProxy": true,
181 "IpAddress": "192.168.0.2",
182 "Password": null,
183 "PortNumber": 444,
184 "ProxyExclusionList": ["192.168.0.1", "www.*.com", "172.1.1.1/24"],
185 "SslCheckDisabled": false,
186 "Username": "root",
187 }
188 error_info:
189 description: Details of the HTTP error.
190 returned: On HTTP error
191 type: dict
192 sample: {
193 "error": {
194 "@Message.ExtendedInfo": [
195 {
196 "Message": "Unable to complete the request because the input value
197 for PortNumber is missing or an invalid value is entered.",
198 "MessageArgs": [
199 "PortNumber"
200 ],
201 "MessageId": "CGEN6002",
202 "RelatedProperties": [],
203 "Resolution": "Enter a valid value and retry the operation.",
204 "Severity": "Critical"
205 }
206 ],
207 "code": "Base.1.0.GeneralError",
208 "message": "A general error has occurred. See ExtendedInfo for more information."
209 }
210 }
211 '''
212
213
214 import json
215 from urllib.error import HTTPError, URLError
216 from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule
217 from ansible.module_utils.urls import ConnectionError, SSLValidationError
218 from ansible_collections.dellemc.openmanage.plugins.module_utils.utils import remove_key
219
220 PROXY_CONFIG = "ApplicationService/Network/ProxyConfiguration"
221 CHECK_MODE_CHANGE_FOUND_MSG = "Changes found to be applied."
222 CHECK_MODE_CHANGE_NOT_FOUND_MSG = "No Changes found to be applied."
223 NO_PROXY_CONFIGURATION = "Unable to configure the proxy because proxy configuration settings " \
224 "are not provided."
225 NO_CHANGE_IN_CONFIGURATION = "No changes made to proxy configuration as entered values are the " \
226 "same as current configuration values."
227 ODATA_REGEX = "(.*?)@odata"
228
229
230 def remove_unwanted_keys(key_list, payload):
231 """
232 Remove unwanted keys from the payload dictionary.
233
234 Args:
235 key_list (list): A list of keys to remove from the payload dictionary.
236 payload (dict): The payload dictionary from which unwanted keys will be removed.
237
238 Returns:
239 None
240
241 """
242 [payload.pop(key) for key in key_list if key in payload]
243
244
245 def validate_check_mode_for_network_proxy(payload_diff, module):
246 """
247 check mode support validation
248 :param payload_diff: payload difference
249 :param module: ansible module object
250 :return: None
251 """
252 if module.check_mode:
253 if payload_diff:
254 module.exit_json(msg=CHECK_MODE_CHANGE_FOUND_MSG, changed=True)
255 else:
256 module.exit_json(msg=CHECK_MODE_CHANGE_NOT_FOUND_MSG, changed=False)
257
258
259 def get_payload(module):
260 """
261 Generates a payload for the network proxy configuration.
262
263 Args:
264 module (object): The Ansible module object.
265
266 Returns:
267 dict: The payload for the network proxy configuration.
268 """
269 params = module.params
270 proxy_payload_map = {
271 "ip_address": "IpAddress",
272 "proxy_port": "PortNumber",
273 "enable_proxy": "EnableProxy",
274 "proxy_username": "Username",
275 "proxy_password": "Password",
276 "enable_authentication": "EnableAuthentication",
277 "ignore_certificate_validation": "SslCheckDisabled",
278 "proxy_exclusion_list": "ProxyExclusionList"
279 }
280 backup_params = params.copy()
281 remove_keys = ["hostname", "username", "password", "port", "ca_path", "validate_certs",
282 "timeout", "update_password"]
283 remove_unwanted_keys(remove_keys, backup_params)
284 payload = dict([(proxy_payload_map[key], val) for key, val in backup_params.items() if val
285 is not None])
286 if backup_params.get("proxy_exclusion_list") or backup_params.get("proxy_exclusion_list") == []:
287 temp_proxy_exclusion_list = backup_params.get("proxy_exclusion_list")
288 converted_proxy_exclusion_list = ";".join(temp_proxy_exclusion_list)
289 payload["ProxyExclusionList"] = converted_proxy_exclusion_list
290 return payload
291
292
293 def get_updated_payload(rest_obj, module, payload):
294 """
295 Generates a payload for the network proxy configuration.
296
297 Args:
298 rest_obj (object): The REST object.
299 module (object): The Ansible module object.
300 payload (dict): The payload for the network proxy configuration.
301
302 Returns:
303 dict: The updated payload for the network proxy configuration.
304 """
305 current_setting = {}
306 if not any(payload):
307 module.exit_json(msg=NO_PROXY_CONFIGURATION, failed=True)
308 else:
309 params = module.params
310 if params.get("update_password"):
311 remove_keys = ["@odata.context", "@odata.type", "@odata.id"]
312 else:
313 remove_keys = ["@odata.context", "@odata.type", "@odata.id", "Password"]
314 enable_authentication = params.get("enable_authentication")
315 if enable_authentication is False:
316 remove_keys.append("Username")
317 payload.pop('Username', None)
318 payload.pop('Password', None)
319 else:
320 if params.get("update_password"):
321 payload['Username'] = params.get("proxy_username")
322 payload['Password'] = params.get("proxy_password")
323 resp = rest_obj.invoke_request("GET", PROXY_CONFIG)
324 current_setting = resp.json_data
325 remove_unwanted_keys(remove_keys, current_setting)
326 diff = any(key in current_setting and val != current_setting[key] for key, val in
327 payload.items())
328 validate_check_mode_for_network_proxy(diff, module)
329 if not diff:
330 module.exit_json(msg=NO_CHANGE_IN_CONFIGURATION)
331 else:
332 current_setting.update(payload)
333 return current_setting
334
335
336 def main():
337 """
338 Main function that serves as the entry point for the module.
339
340 Returns:
341 None
342 """
343 specs = {
344 "ip_address": {"required": False, "type": "str"},
345 "proxy_port": {"required": False, "type": "int"},
346 "enable_proxy": {"required": True, "type": "bool"},
347 "proxy_username": {"required": False, "type": "str"},
348 "proxy_password": {"required": False, "type": "str", "no_log": True},
349 "enable_authentication": {"required": False, "type": "bool"},
350 "ignore_certificate_validation": {"required": False, "type": "bool", "default": False,
351 "aliases": ["ssl_check_disabled"]},
352 "proxy_exclusion_list": {"required": False, "type": "list", "elements": "str"},
353 "update_password": {"required": False, "type": "bool", "default": False},
354 }
355
356 module = OmeAnsibleModule(
357 argument_spec=specs,
358 required_if=[['enable_proxy', True, ['ip_address', 'proxy_port']],
359 ['enable_authentication', True, ['proxy_username', 'proxy_password']], ],
360 supports_check_mode=True
361 )
362 try:
363 with RestOME(module.params, req_session=True) as rest_obj:
364 payload = get_payload(module)
365 updated_payload = get_updated_payload(rest_obj, module, payload)
366 resp = rest_obj.invoke_request("PUT", PROXY_CONFIG, data=updated_payload)
367 response_data = resp.json_data
368 proxy_configuration_details = response_data.copy()
369 if not response_data.get("ProxyExclusionList"):
370 proxy_exclusion_list_list = []
371 else:
372 proxy_exclusion_list_str = response_data.get("ProxyExclusionList")
373 proxy_exclusion_list_list = proxy_exclusion_list_str.split(';')
374 proxy_configuration_details["ProxyExclusionList"] = proxy_exclusion_list_list
375 module.exit_json(msg="Successfully updated network proxy configuration.",
376 proxy_configuration=proxy_configuration_details,
377 changed=True)
378 except HTTPError as err:
379 filter_err = remove_key(json.load(err), regex_pattern=ODATA_REGEX)
380 module.exit_json(msg=str(err), error_info=filter_err, failed=True)
381 except URLError as err:
382 module.exit_json(msg=str(err), unreachable=True)
383 except (ValueError, TypeError, ConnectionError, SSLValidationError, OSError) as err:
384 module.exit_json(msg=str(err), failed=True)
385
386
387 if __name__ == "__main__":
388 main()