Debian Codegraph @

ansible_collections/fortinet/fortios/plugins/modules/fortios_dpdk_global.py

raw symbols SCIP
1 #!/usr/bin/python
2 from __future__ import absolute_import, division, print_function
3
4 # Copyright: (c) 2022 Fortinet
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 __metaclass__ = type
13
14 ANSIBLE_METADATA = {
15 "status": ["preview"],
16 "supported_by": "community",
17 "metadata_version": "1.1",
18 }
19
20 DOCUMENTATION = """
21 ---
22 module: fortios_dpdk_global
23 short_description: Configure global DPDK options in Fortinet's FortiOS and FortiGate.
24 description:
25 - This module is able to configure a FortiGate or FortiOS (FOS) device by allowing the
26 user to set and modify dpdk feature and global category.
27 Examples include all parameters and values need to be adjusted to datasources before usage.
28 Tested with FOS v6.0.0
29 version_added: "2.0.0"
30 author:
31 - Link Zheng (@chillancezen)
32 - Jie Xue (@JieX19)
33 - Hongbin Lu (@fgtdev-hblu)
34 - Frank Shen (@frankshen01)
35 - Miguel Angel Munoz (@mamunozgonzalez)
36 - Nicolas Thomas (@thomnico)
37 notes:
38 - Legacy fortiosapi has been deprecated, httpapi is the preferred way to run playbooks
39
40 - The module supports check_mode.
41
42 requirements:
43 - ansible>=2.16
44 options:
45 access_token:
46 description:
47 - Token-based authentication.
48 Generated from GUI of Fortigate.
49 type: str
50 required: false
51 enable_log:
52 description:
53 - Enable/Disable logging for task.
54 type: bool
55 required: false
56 default: false
57 vdom:
58 description:
59 - Virtual domain, among those defined previously. A vdom is a
60 virtual instance of the FortiGate that can be configured and
61 used as a different unit.
62 type: str
63 default: root
64 member_path:
65 type: str
66 description:
67 - Member attribute path to operate on.
68 - Delimited by a slash character if there are more than one attribute.
69 - Parameter marked with member_path is legitimate for doing member operation.
70 member_state:
71 type: str
72 description:
73 - Add or delete a member under specified attribute path.
74 - When member_state is specified, the state option is ignored.
75 choices:
76 - 'present'
77 - 'absent'
78
79 dpdk_global:
80 description:
81 - Configure global DPDK options.
82 default: null
83 type: dict
84 suboptions:
85 elasticbuffer:
86 description:
87 - Enable/disable elasticbuffer support for all DPDK ports.
88 type: str
89 choices:
90 - 'disable'
91 - 'enable'
92 hugepage_percentage:
93 description:
94 - Percentage of main memory allocated to hugepages, which are available for DPDK operation.
95 type: int
96 interface:
97 description:
98 - Physical interfaces that enable DPDK.
99 type: list
100 elements: dict
101 suboptions:
102 interface_name:
103 description:
104 - Physical interface name. Source system.interface.name.
105 required: true
106 type: str
107 ipsec_offload:
108 description:
109 - Enable/disable DPDK IPsec phase 2 offloading.
110 type: str
111 choices:
112 - 'disable'
113 - 'enable'
114 mbufpool_percentage:
115 description:
116 - Percentage of main memory allocated to DPDK packet buffer.
117 type: int
118 multiqueue:
119 description:
120 - Enable/disable multi-queue RX/TX support for all DPDK ports.
121 type: str
122 choices:
123 - 'disable'
124 - 'enable'
125 per_session_accounting:
126 description:
127 - Enable/disable per-session accounting.
128 type: str
129 choices:
130 - 'disable'
131 - 'traffic-log-only'
132 - 'enable'
133 protects:
134 description:
135 - Special arguments for device
136 type: str
137 session_table_percentage:
138 description:
139 - Percentage of main memory allocated to DPDK session table.
140 type: int
141 sleep_on_idle:
142 description:
143 - Enable/disable sleep-on-idle support for all FDH engines.
144 type: str
145 choices:
146 - 'disable'
147 - 'enable'
148 status:
149 description:
150 - Enable/disable DPDK operation for the entire system.
151 type: str
152 choices:
153 - 'disable'
154 - 'enable'
155 """
156
157 EXAMPLES = """
158 - name: Configure global DPDK options.
159 fortinet.fortios.fortios_dpdk_global:
160 vdom: "{{ vdom }}"
161 dpdk_global:
162 elasticbuffer: "disable"
163 hugepage_percentage: "30"
164 interface:
165 -
166 interface_name: "<your_own_value> (source system.interface.name)"
167 ipsec_offload: "disable"
168 mbufpool_percentage: "25"
169 multiqueue: "disable"
170 per_session_accounting: "disable"
171 protects: "<your_own_value>"
172 session_table_percentage: "5"
173 sleep_on_idle: "disable"
174 status: "disable"
175 """
176
177 RETURN = """
178 build:
179 description: Build number of the fortigate image
180 returned: always
181 type: str
182 sample: '1547'
183 http_method:
184 description: Last method used to provision the content into FortiGate
185 returned: always
186 type: str
187 sample: 'PUT'
188 http_status:
189 description: Last result given by FortiGate on last operation applied
190 returned: always
191 type: str
192 sample: "200"
193 mkey:
194 description: Master key (id) used in the last call to FortiGate
195 returned: success
196 type: str
197 sample: "id"
198 name:
199 description: Name of the table used to fulfill the request
200 returned: always
201 type: str
202 sample: "urlfilter"
203 path:
204 description: Path of the table used to fulfill the request
205 returned: always
206 type: str
207 sample: "webfilter"
208 revision:
209 description: Internal revision number
210 returned: always
211 type: str
212 sample: "17.0.2.10658"
213 serial:
214 description: Serial number of the unit
215 returned: always
216 type: str
217 sample: "FGVMEVYYQT3AB5352"
218 status:
219 description: Indication of the operation's result
220 returned: always
221 type: str
222 sample: "success"
223 vdom:
224 description: Virtual domain used
225 returned: always
226 type: str
227 sample: "root"
228 version:
229 description: Version of the FortiGate
230 returned: always
231 type: str
232 sample: "v5.6.3"
233 """
234 from ansible.module_utils.basic import AnsibleModule
235 from ansible.module_utils.connection import Connection
236 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
237 FortiOSHandler,
238 )
239 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
240 check_legacy_fortiosapi,
241 )
242 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
243 schema_to_module_spec,
244 )
245 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
246 check_schema_versioning,
247 )
248 from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import (
249 FAIL_SOCKET_MSG,
250 )
251 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.data_post_processor import (
252 remove_invalid_fields,
253 )
254 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
255 is_same_comparison,
256 )
257 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
258 serialize,
259 )
260 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
261 find_current_values,
262 )
263 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
264 unify_data_format,
265 )
266
267
268 def filter_dpdk_global_data(json):
269 option_list = [
270 "elasticbuffer",
271 "hugepage_percentage",
272 "interface",
273 "ipsec_offload",
274 "mbufpool_percentage",
275 "multiqueue",
276 "per_session_accounting",
277 "protects",
278 "session_table_percentage",
279 "sleep_on_idle",
280 "status",
281 ]
282
283 json = remove_invalid_fields(json)
284 dictionary = {}
285
286 for attribute in option_list:
287 if attribute in json and json[attribute] is not None:
288 dictionary[attribute] = json[attribute]
289
290 return dictionary
291
292
293 def underscore_to_hyphen(data):
294 new_data = None
295 if isinstance(data, list):
296 new_data = []
297 for i, elem in enumerate(data):
298 new_data.append(underscore_to_hyphen(elem))
299 elif isinstance(data, dict):
300 new_data = {}
301 for k, v in data.items():
302 new_data[k.replace("_", "-")] = underscore_to_hyphen(v)
303 else:
304 return data
305 return new_data
306
307
308 def dpdk_global(data, fos, check_mode=False):
309
310 state = None
311 vdom = data["vdom"]
312 parameters = None
313 state = data.get("state", None)
314 dpdk_global_data = data["dpdk_global"]
315
316 filtered_data = filter_dpdk_global_data(dpdk_global_data)
317 converted_data = underscore_to_hyphen(filtered_data)
318
319 # check_mode starts from here
320 if check_mode:
321 diff = {
322 "before": "",
323 "after": filtered_data,
324 }
325 mkeyname = fos.get_mkeyname(None, None)
326 mkey = fos.get_mkey("dpdk", "global", filtered_data, vdom=vdom)
327 current_data = fos.get(
328 "dpdk", "global", vdom=vdom, mkey=mkey, parameters=parameters
329 )
330 is_existed = (
331 current_data
332 and current_data.get("http_status") == 200
333 and (
334 mkeyname
335 and isinstance(current_data.get("results"), list)
336 and len(current_data["results"]) > 0
337 or not mkeyname
338 and current_data["results"] # global object response
339 )
340 )
341
342 # 2. if it exists and the state is 'present' then compare current settings with desired
343 if state == "present" or state is True or state is None:
344 # for non global modules, mkeyname must exist and it's a new module when mkey is None
345 if mkeyname is not None and mkey is None:
346 return False, True, filtered_data, diff
347
348 # if mkey exists then compare each other
349 # record exits and they're matched or not
350 copied_filtered_data = filtered_data.copy()
351 copied_filtered_data.pop(mkeyname, None)
352 unified_filtered_data = unify_data_format(copied_filtered_data)
353
354 current_data_results = current_data.get("results", {})
355 current_config = (
356 current_data_results[0]
357 if mkeyname
358 and isinstance(current_data_results, list)
359 and len(current_data_results) > 0
360 else current_data_results
361 )
362 if is_existed:
363 unified_current_values = find_current_values(
364 unified_filtered_data,
365 unify_data_format(current_config),
366 )
367
368 is_same = is_same_comparison(
369 serialize(unified_current_values), serialize(unified_filtered_data)
370 )
371
372 return (
373 False,
374 not is_same,
375 filtered_data,
376 {"before": unified_current_values, "after": unified_filtered_data},
377 )
378
379 # record does not exist
380 return False, True, filtered_data, diff
381
382 if state == "absent":
383 if mkey is None:
384 return (
385 False,
386 False,
387 filtered_data,
388 {"before": current_data["results"][0], "after": ""},
389 )
390
391 if is_existed:
392 return (
393 False,
394 True,
395 filtered_data,
396 {"before": current_data["results"][0], "after": ""},
397 )
398 return False, False, filtered_data, {}
399
400 return True, False, {"reason: ": "Must provide state parameter"}, {}
401 # pass post processed data to member operations
402 # no need to do underscore_to_hyphen since do_member_operation handles it by itself
403 data_copy = data.copy()
404 data_copy["dpdk_global"] = filtered_data
405 fos.do_member_operation(
406 "dpdk",
407 "global",
408 data_copy,
409 )
410
411 return fos.set(
412 "dpdk", "global", data=converted_data, vdom=vdom, parameters=parameters
413 )
414
415
416 def is_successful_status(resp):
417 return (
418 "status" in resp
419 and resp["status"] == "success"
420 or "http_status" in resp
421 and resp["http_status"] == 200
422 or "http_method" in resp
423 and resp["http_method"] == "DELETE"
424 and resp["http_status"] == 404
425 )
426
427
428 def fortios_dpdk(data, fos, check_mode):
429
430 if data["dpdk_global"]:
431 resp = dpdk_global(data, fos, check_mode)
432 else:
433 fos._module.fail_json(msg="missing task body: %s" % ("dpdk_global"))
434 if isinstance(resp, tuple) and len(resp) == 4:
435 return resp
436 return (
437 not is_successful_status(resp),
438 is_successful_status(resp)
439 and (resp["revision_changed"] if "revision_changed" in resp else True),
440 resp,
441 {},
442 )
443
444
445 versioned_schema = {
446 "v_range": [["v7.0.0", "v7.0.12"], ["v7.2.1", "v7.2.2"], ["v7.4.0", "v7.6.1"]],
447 "type": "dict",
448 "children": {
449 "status": {
450 "v_range": [
451 ["v7.0.0", "v7.0.12"],
452 ["v7.2.1", "v7.2.2"],
453 ["v7.4.0", "v7.6.1"],
454 ],
455 "type": "string",
456 "options": [{"value": "disable"}, {"value": "enable"}],
457 },
458 "interface": {
459 "type": "list",
460 "elements": "dict",
461 "children": {
462 "interface_name": {
463 "v_range": [
464 ["v7.0.0", "v7.0.12"],
465 ["v7.2.1", "v7.2.2"],
466 ["v7.4.0", "v7.6.1"],
467 ],
468 "type": "string",
469 "required": True,
470 }
471 },
472 "v_range": [
473 ["v7.0.0", "v7.0.12"],
474 ["v7.2.1", "v7.2.2"],
475 ["v7.4.0", "v7.6.1"],
476 ],
477 },
478 "multiqueue": {
479 "v_range": [
480 ["v7.0.0", "v7.0.12"],
481 ["v7.2.1", "v7.2.2"],
482 ["v7.4.0", "v7.6.1"],
483 ],
484 "type": "string",
485 "options": [{"value": "disable"}, {"value": "enable"}],
486 },
487 "sleep_on_idle": {
488 "v_range": [
489 ["v7.0.0", "v7.0.12"],
490 ["v7.2.1", "v7.2.2"],
491 ["v7.4.0", "v7.6.1"],
492 ],
493 "type": "string",
494 "options": [{"value": "disable"}, {"value": "enable"}],
495 },
496 "elasticbuffer": {
497 "v_range": [
498 ["v7.0.0", "v7.0.12"],
499 ["v7.2.1", "v7.2.2"],
500 ["v7.4.0", "v7.6.1"],
501 ],
502 "type": "string",
503 "options": [{"value": "disable"}, {"value": "enable"}],
504 },
505 "protects": {"v_range": [["v7.4.2", "v7.6.1"]], "type": "string"},
506 "per_session_accounting": {
507 "v_range": [
508 ["v7.0.0", "v7.0.12"],
509 ["v7.2.1", "v7.2.2"],
510 ["v7.4.0", "v7.6.1"],
511 ],
512 "type": "string",
513 "options": [
514 {"value": "disable"},
515 {"value": "traffic-log-only"},
516 {"value": "enable"},
517 ],
518 },
519 "ipsec_offload": {
520 "v_range": [
521 ["v7.0.6", "v7.0.12"],
522 ["v7.2.1", "v7.2.2"],
523 ["v7.4.0", "v7.6.1"],
524 ],
525 "type": "string",
526 "options": [{"value": "disable"}, {"value": "enable"}],
527 },
528 "hugepage_percentage": {
529 "v_range": [
530 ["v7.0.0", "v7.0.12"],
531 ["v7.2.1", "v7.2.2"],
532 ["v7.4.0", "v7.6.1"],
533 ],
534 "type": "integer",
535 },
536 "mbufpool_percentage": {
537 "v_range": [
538 ["v7.0.0", "v7.0.12"],
539 ["v7.2.1", "v7.2.2"],
540 ["v7.4.0", "v7.6.1"],
541 ],
542 "type": "integer",
543 },
544 "session_table_percentage": {
545 "v_range": [["v7.6.1", "v7.6.1"]],
546 "type": "integer",
547 },
548 },
549 }
550
551
552 def main():
553 module_spec = schema_to_module_spec(versioned_schema)
554 mkeyname = None
555 fields = {
556 "access_token": {"required": False, "type": "str", "no_log": True},
557 "enable_log": {"required": False, "type": "bool", "default": False},
558 "vdom": {"required": False, "type": "str", "default": "root"},
559 "member_path": {"required": False, "type": "str"},
560 "member_state": {
561 "type": "str",
562 "required": False,
563 "choices": ["present", "absent"],
564 },
565 "dpdk_global": {
566 "required": False,
567 "type": "dict",
568 "default": None,
569 "options": {},
570 },
571 }
572 for attribute_name in module_spec["options"]:
573 fields["dpdk_global"]["options"][attribute_name] = module_spec["options"][
574 attribute_name
575 ]
576 if mkeyname and mkeyname == attribute_name:
577 fields["dpdk_global"]["options"][attribute_name]["required"] = True
578
579 module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
580 check_legacy_fortiosapi(module)
581
582 is_error = False
583 has_changed = False
584 result = None
585 diff = None
586
587 versions_check_result = None
588 if module._socket_path:
589 connection = Connection(module._socket_path)
590 if "access_token" in module.params:
591 connection.set_custom_option("access_token", module.params["access_token"])
592
593 if "enable_log" in module.params:
594 connection.set_custom_option("enable_log", module.params["enable_log"])
595 else:
596 connection.set_custom_option("enable_log", False)
597 fos = FortiOSHandler(connection, module, mkeyname, admin_passwd_header=False)
598 versions_check_result = check_schema_versioning(
599 fos, versioned_schema, "dpdk_global"
600 )
601
602 is_error, has_changed, result, diff = fortios_dpdk(
603 module.params, fos, module.check_mode
604 )
605
606 else:
607 module.fail_json(**FAIL_SOCKET_MSG)
608
609 if versions_check_result and versions_check_result["matched"] is False:
610 module.warn(
611 "Ansible has detected version mismatch between FortOS system and your playbook, see more details by specifying option -vvv"
612 )
613
614 if not is_error:
615 if versions_check_result and versions_check_result["matched"] is False:
616 module.exit_json(
617 changed=has_changed,
618 version_check_warning=versions_check_result,
619 meta=result,
620 diff=diff,
621 )
622 else:
623 module.exit_json(changed=has_changed, meta=result, diff=diff)
624 else:
625 if versions_check_result and versions_check_result["matched"] is False:
626 module.fail_json(
627 msg="Error in repo",
628 version_check_warning=versions_check_result,
629 meta=result,
630 )
631 else:
632 module.fail_json(msg="Error in repo", meta=result)
633
634
635 if __name__ == "__main__":
636 main()