|
| 1 | +from __future__ import (absolute_import, division, print_function) |
| 2 | + |
| 3 | +__metaclass__ = type |
| 4 | + |
| 5 | + |
| 6 | +from ansible.errors import AnsibleActionFail |
| 7 | +from ansible.plugins.action import ActionBase |
| 8 | +from ansible_collections.check_point.mgmt.plugins.module_utils.checkpoint import \ |
| 9 | + prepare_rule_params_for_execute_module, check_if_to_publish_for_action |
| 10 | + |
| 11 | + |
| 12 | +class ActionModule(ActionBase): |
| 13 | + |
| 14 | + def run(self, tmp=None, task_vars=None): |
| 15 | + |
| 16 | + module = super(ActionModule, self).run(tmp, task_vars) |
| 17 | + |
| 18 | + result = self._execute_module(module_name='check_point.mgmt.cp_mgmt_access_rules', module_args=self._task.args, |
| 19 | + task_vars=task_vars, tmp=tmp) |
| 20 | + |
| 21 | + if 'msg' in result.keys(): |
| 22 | + raise AnsibleActionFail(result['msg']) |
| 23 | + |
| 24 | + module_args = self._task.args |
| 25 | + |
| 26 | + fields = {'position', 'layer', 'auto_publish_session'} |
| 27 | + rules_list = module_args['rules'] |
| 28 | + for rule in rules_list: |
| 29 | + for field in fields: |
| 30 | + if field in rule.keys(): |
| 31 | + raise AnsibleActionFail('Unsupported parameter ' + field + ' for rule') |
| 32 | + # check_fields_for_rule_action_module(module_args) |
| 33 | + rules_list = self._task.args['rules'] |
| 34 | + position = 1 |
| 35 | + |
| 36 | + for rule in rules_list: |
| 37 | + rule, position = prepare_rule_params_for_execute_module(rule=rule, module_args=module_args, |
| 38 | + position=position) |
| 39 | + result['rule: ' + rule['name']] = self._execute_module(module_name='check_point.mgmt.cp_mgmt_access_rule', |
| 40 | + module_args=rule, |
| 41 | + task_vars=task_vars, tmp=tmp, wrap_async=False) |
| 42 | + if 'changed' in result['rule: ' + rule['name']].keys() and \ |
| 43 | + result['rule: ' + rule['name']]['changed'] is True: |
| 44 | + result['changed'] = True |
| 45 | + if 'failed' in result['rule: ' + rule['name']].keys() and result['rule: ' + rule['name']]['failed'] is True: |
| 46 | + temp = result['rule: ' + rule['name']].copy() |
| 47 | + result = {} |
| 48 | + result['rule: ' + rule['name']] = temp |
| 49 | + result['failed'] = True |
| 50 | + result['discard:'] = self._execute_module(module_name='check_point.mgmt.cp_mgmt_discard', |
| 51 | + module_args={}, task_vars=task_vars, tmp=tmp) |
| 52 | + break |
| 53 | + if check_if_to_publish_for_action(result, module_args): |
| 54 | + result['publish:'] = self._execute_module(module_name='check_point.mgmt.cp_mgmt_publish', module_args={}, |
| 55 | + task_vars=task_vars, tmp=tmp) |
| 56 | + |
| 57 | + return result |
0 commit comments