From 5c45f3cc527b9a0cb98625f57204854860fc1c13 Mon Sep 17 00:00:00 2001 From: Duane Toler Date: Sun, 14 Jan 2024 10:01:17 -0500 Subject: [PATCH] Add loopback interface modules Modify checkpoint.py to support special loopback interface params --- plugins/module_utils/checkpoint.py | 3 + plugins/modules/cp_gaia_loopback_interface.py | 125 ++++++++++++++ .../cp_gaia_loopback_interface_facts.py | 162 ++++++++++++++++++ 3 files changed, 290 insertions(+) create mode 100644 plugins/modules/cp_gaia_loopback_interface.py create mode 100644 plugins/modules/cp_gaia_loopback_interface_facts.py diff --git a/plugins/module_utils/checkpoint.py b/plugins/module_utils/checkpoint.py index 0b973ee..fda84fa 100644 --- a/plugins/module_utils/checkpoint.py +++ b/plugins/module_utils/checkpoint.py @@ -251,6 +251,9 @@ def chkp_api_call(module, api_call_object, has_add_api, ignore=None, show_params if add_params: [module.params.pop(key) for key in show_params if key not in add_params] module.params.update(add_params) + if 'loopback-interface' == api_call_object: # loopback doesn't take 'name' for add-... api + if 'name' in module.params: + module.params.pop("name") code, res = api_call(module, target_version, api_call_object="add-{0}".format(api_call_object)) else: # some requests like static-route don't have add, try set instead code, res = api_call(module, target_version, api_call_object="set-{0}".format(api_call_object)) diff --git a/plugins/modules/cp_gaia_loopback_interface.py b/plugins/modules/cp_gaia_loopback_interface.py new file mode 100644 index 0000000..dd9e0a6 --- /dev/null +++ b/plugins/modules/cp_gaia_loopback_interface.py @@ -0,0 +1,125 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# Ansible module to manage CheckPoint Firewall (c) 2019 +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# + +from __future__ import (absolute_import, division, print_function) + +__metaclass__ = type + +DOCUMENTATION = """ +module: cp_gaia_loopback_interface +author: Duane Toler (@duanetoler) +description: +- Modify loopback interface. +short_description: Modify loopback interface. +version_added: '5.0.x' +notes: +- Supports C(check_mode). +options: + version: + description: Gaia API version for example 1.6. + required: false + type: str + state: + description: Ansible state which can be C(present) or C(absent). + required: false + type: str + default: present + choices: [present, absent] + name: + description: + - Interface name with format C(loop), for example "loop00", "loop01" + - Not required when adding new loopback interface + - Newly-created loopback interface name returned in dict details + required: false + type: str + ipv4_address: + description: Interface IPv4 address. + required: false + type: str + ipv4_mask_length: + description: Interface IPv4 address mask length. + required: false + type: int + ipv6_address: + description: Interface IPv6 address. + required: false + type: str + ipv6_autoconfig: + description: Configure IPv6 auto-configuration. + required: false + type: bool + ipv6_mask_length: + description: Interface IPv6 address mask length. + required: false + type: int + comments: + description: Interface Comments. + required: false + type: str + enabled: + description: Interface State. + required: false + type: bool +""" + +EXAMPLES = """ +- name: Set comment field of a loopback interface + check_point.gaia.cp_gaia_loopback_interface: + comments: "loop01 interface" + name: loop01 + +""" + +RETURN = """ +loopback_interface: + description: The updated interface details. + returned: always. + type: dict +""" + +from ansible.module_utils.basic import AnsibleModule +from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_api_call, checkpoint_argument_spec_for_all + + +def main(): + # arguments for the module: + fields = dict( + state=dict(type='str', default='present', choices=['present', 'absent']), + name=dict(type='str'), + enabled=dict(type='bool'), + comments=dict(type='str'), + ipv4_address=dict(type='str'), + ipv4_mask_length=dict(type='int'), + ipv6_address=dict(type='str'), + ipv6_autoconfig=dict(type='bool'), + ipv6_mask_length=dict(type='int') + ) + fields.update(checkpoint_argument_spec_for_all) + module = AnsibleModule(argument_spec=fields, supports_check_mode=True) + api_call_object = 'loopback-interface' + ignore = ['status'] + show_params = ['name'] + add_params = {} + + res = chkp_api_call(module, api_call_object, True, ignore=ignore, show_params=show_params, add_params=add_params) + module.exit_json(**res) + + +if __name__ == "__main__": + main() diff --git a/plugins/modules/cp_gaia_loopback_interface_facts.py b/plugins/modules/cp_gaia_loopback_interface_facts.py new file mode 100644 index 0000000..8a09feb --- /dev/null +++ b/plugins/modules/cp_gaia_loopback_interface_facts.py @@ -0,0 +1,162 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# Ansible module to manage CheckPoint Firewall (c) 2019 +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# + +from __future__ import (absolute_import, division, print_function) + +__metaclass__ = type + +DOCUMENTATION = """ +module: cp_gaia_loopback_interface_facts +author: Duane Toler (@duanetoler) +description: +- Show loopback interface. +short_description: Show loopback interface/s. +version_added: '5.0.x' +notes: +- Supports C(check_mode). +options: + version: + description: Gaia API version for example 1.6. + required: False + type: str + name: + description: Interface name to show. If not specified, all loopback interfaces information is returned. + required: false + type: str + +""" + +EXAMPLES = """ +- name: Show loopback interface + check_point.gaia.cp_gaia_loopback_interface_facts: + +- name: Show loopback interface by specifying it's name + check_point.gaia.cp_gaia_loopback_interface_facts: + name: loop01 + +""" + +RETURN = """ +ansible_facts: + description: The interface/s facts. + returned: always. + type: dict + contains: + objects: + description: + - List of interfaces. + returned: always + type: list + elements: dict + contains: + name: + description: + - Interface name. + returned: always + type: str + ipv4_address: + description: Interface IPv4 address. + returned: always + type: str + ipv4_mask_length: + description: Interface IPv4 address mask length. + returned: always + type: int + ipv6_address: + description: Interface IPv6 address. + returned: always + type: str + ipv6_autoconfig: + description: Configure IPv6 auto-configuration. + returned: always + type: bool + ipv6_mask_length: + description: Interface IPv6 address mask length. + returned: always + type: int + comments: + description: Interface Comments. + returned: always + type: str + enabled: + description: Interface State. + returned: always + type: bool + mtu: + description: Interface mtu. + returned: always + type: int + ipv6_local_link_address: + description: Interface ipv6 local link address. + returned: always + type: str + status: + description: Interface data. + returned: always + type: dict + contains: + link_state: + description: Link status. + returned: always + type: bool + speed: + description: Speed. + returned: always + type: str + duplex: + description: Duplex. + returned: always + type: str + tx_bytes: + description: TX bytes. + returned: always + type: int + tx_packets: + description: TX packets. + returned: always + type: int + rx_bytes: + description: RX bytes. + returned: always + type: int + rx_packets: + description: RX packets. + returned: always + type: int +""" + +from ansible.module_utils.basic import AnsibleModule +from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_facts_api_call, checkpoint_argument_spec_for_all + + +def main(): + # arguments for the module: + fields = dict( + name=dict(required=False, type='str') + ) + fields.update(checkpoint_argument_spec_for_all) + module = AnsibleModule(argument_spec=fields, supports_check_mode=True) + api_call_object = "loopback-interface" + + res = chkp_facts_api_call(module, api_call_object, True) + module.exit_json(ansible_facts=res["ansible_facts"]) + + +if __name__ == "__main__": + main()