Skip to content

Commit

Permalink
Add modules for dhcrelay
Browse files Browse the repository at this point in the history
  • Loading branch information
jiuka committed Sep 25, 2024
1 parent 9b4c34f commit 3ae0d79
Show file tree
Hide file tree
Showing 11 changed files with 671 additions and 1 deletion.
82 changes: 82 additions & 0 deletions docs/source/modules/dhcrelay_destination.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
.. _modules_dhcrelay_destination:

.. include:: ../_include/head.rst

=============================
DHCRelay - Destinations
=============================

**STATE**: stable

**TESTS**: `Playbook <https://github.com/ansibleguy/collection_opnsense/blob/latest/tests/dhcrelay_destination.yml>`_

**API Docs**: `Core - DHCRelay <https://docs.opnsense.org/development/api/core/dhcrelay.html>`_

**Service Docs**: `DHCRelay <https://docs.opnsense.org/manual/dhcp.html#dhcrelay>`_


Definition
**********

.. csv-table:: Definition
:header: "Parameter", "Type", "Required", "Default", "Aliases", "Comment"
:widths: 15 10 10 10 10 45

"name","string","true","","\-","Unique name for this relay destination"
"server","list of strings","true","\-","\-","List of server IP addresses to relay DHCP requests to"
"reload","boolean","false","true","\-", .. include:: ../_include/param_reload.rst

.. include:: ../_include/param_basic.rst

Info
****

This module manages DHCRelay destinations. A destination can contain multiple IP addresses.


Examples
********

.. code-block:: yaml
- hosts: localhost
gather_facts: no
module_defaults:
group/ansibleguy.opnsense.all:
firewall: 'opnsense.template.ansibleguy.net'
api_credential_file: '/home/guy/.secret/opn.key'
ansibleguy.opnsense.list:
target: 'dhcrelay_destination'
tasks:
- name: Example
ansibleguy.opnsense.dhcrelay_destination:
name: 'mydhcp'
server:
- '192.168.0.1'
# state: 'present'
# reload: true
# debug: false
- name: Adding
ansibleguy.opnsense.dhcrelay_destination:
name: 'mydhcp'
server:
- '192.168.0.1'
- name: Removing
ansibleguy.opnsense.dhcrelay_destination:
name: 'mydhcp'
server:
- '192.168.0.1'
state: 'absent'
- name: Listing
ansibleguy.opnsense.list:
# target: 'dhcrelay_destination'
register: existing_entries
- name: Printing dhcrelay destinations
ansible.builtin.debug:
var: existing_entries.data
83 changes: 83 additions & 0 deletions docs/source/modules/dhcrelay_relay.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
.. _modules_dhcrelay_relay:

.. include:: ../_include/head.rst

=============================
DHCRelay - Relay
=============================

**STATE**: stable

**TESTS**: `Playbook <https://github.com/ansibleguy/collection_opnsense/blob/latest/tests/dhcrelay_destination.yml>`_

**API Docs**: `Core - DHCRelay <https://docs.opnsense.org/development/api/core/dhcrelay.html>`_

**Service Docs**: `DHCRelay <https://docs.opnsense.org/manual/dhcp.html#dhcrelay>`_


Definition
**********

.. csv-table:: Definition
:header: "Parameter", "Type", "Required", "Default", "Aliases", "Comment"
:widths: 15 10 10 10 10 45

"enabled","boolean","false","false","\-","Enable or disable this relay"
"interface","string","true","","i, int"," The interface to relay DHCP requests from"
"destination","string","true","\-","dest"," The destination server group to relay DHCP requests to"
"agent_info","boolean","false","false","\-","Add the relay agent information option"
"reload","boolean","false","true","\-", .. include:: ../_include/param_reload.rst

.. include:: ../_include/param_basic.rst

Info
****

This module manages DHCRelay relays. Each interface can be assigned a single relay.


Examples
********

.. code-block:: yaml
- hosts: localhost
gather_facts: no
module_defaults:
group/ansibleguy.opnsense.all:
firewall: 'opnsense.template.ansibleguy.net'
api_credential_file: '/home/guy/.secret/opn.key'
ansibleguy.opnsense.list:
target: 'dhcrelay_relay'
tasks:
- name: Example
ansibleguy.opnsense.dhcrelay_relay:
interface: 'lan'
destination: mydhcp
# enabled: false
# agent_info: false
# state: 'present'
# reload: true
# debug: false
- name: Adding
ansibleguy.opnsense.dhcrelay_relay:
interface: 'lan'
destination: mydhcp
- name: Removing
ansibleguy.opnsense.dhcrelay_relay:
interface: 'lan'
destination: mydhcp
state: 'absent'
- name: Listing
ansibleguy.opnsense.list:
# target: 'dhcrelay_relay'
register: existing_entries
- name: Printing dhcrelay relays
ansible.builtin.debug:
var: existing_entries.data
4 changes: 4 additions & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ action_groups:
- ansibleguy.opnsense.openvpn_client_override
- ansibleguy.opnsense.openvpn_client_template
- ansibleguy.opnsense.openvpn_client_export
dhcrelay:
- ansibleguy.opnsense.dhcrelay_destination
- ansibleguy.opnsense.dhcrelay_relay
all:
- metadata:
extend_group:
Expand All @@ -140,6 +143,7 @@ action_groups:
- ansibleguy.opnsense.system
- ansibleguy.opnsense.ids
- ansibleguy.opnsense.openvpn
- ansibleguy.opnsense.dhcrelay

plugin_routing:
modules:
Expand Down
42 changes: 42 additions & 0 deletions plugins/module_utils/main/dhcrelay_destination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from ansible.module_utils.basic import AnsibleModule

from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.api import \
Session
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.helper.main import \
is_unset
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.cls import BaseModule


class DhcRelayDestination(BaseModule):
FIELD_ID = 'name'
CMDS = {
'add': 'addDest',
'del': 'delDest',
'set': 'setDest',
'search': 'get',
}
API_KEY_PATH = 'dhcrelay.destinations'
API_KEY_PATH_REQ = 'destination'
API_MOD = 'dhcrelay'
API_CONT = 'settings'
API_CONT_REL = 'service'
API_CMD_REL = 'reconfigure'
FIELDS_CHANGE = ['server']
FIELDS_ALL = [FIELD_ID]
FIELDS_ALL.extend(FIELDS_CHANGE)
FIELDS_TYPING = {
'list': ['server'],
}
EXIST_ATTR = 'destination'

def __init__(self, module: AnsibleModule, result: dict, session: Session = None):
BaseModule.__init__(self=self, m=module, r=result, s=session)
self.destination = {}

def check(self) -> None:

if self.p['state'] == 'present':
if is_unset(self.p['server']):
self.m.fail_json("You need to provide list of 'server' to create a dhcrelay_destination!")

self._base_check()
58 changes: 58 additions & 0 deletions plugins/module_utils/main/dhcrelay_relay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from ansible.module_utils.basic import AnsibleModule

from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.api import \
Session
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.helper.main import \
is_unset
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.cls import BaseModule


class DhcRelayRelay(BaseModule):
FIELD_ID = 'interface'
CMDS = {
'add': 'addRelay',
'del': 'delRelay',
'set': 'setRelay',
'search': 'get',
'detail': 'getRelay'
}
API_KEY_PATH = 'dhcrelay.relays'
API_KEY_PATH_REQ = 'relay'
API_MOD = 'dhcrelay'
API_CONT = 'settings'
API_CONT_REL = 'service'
API_CMD_REL = 'reconfigure'
FIELDS_CHANGE = ['enabled', 'destination', 'agent_info']
FIELDS_ALL = [FIELD_ID]
FIELDS_ALL.extend(FIELDS_CHANGE)
FIELDS_VALUE_MAPPING = {}
FIELDS_TYPING = {
'select': ['interface'],
'select_opt_list': ['destination'],
'bool': ['enabled', 'agent_info']
}
EXIST_ATTR = 'relay'

def __init__(self, module: AnsibleModule, result: dict, session: Session = None):
BaseModule.__init__(self=self, m=module, r=result, s=session)
self.relay = {}


def check(self) -> None:
if self.p['state'] == 'present':
if is_unset(self.p['destination']):
self.m.fail_json("You need to provide a 'destination' to create a dhcrelay_relay!")

if self.p['state'] == 'present':
template = self.s.get({
**self.call_cnf,
'command': self.CMDS['detail'],
})

if template['relay']['destination']:
self.FIELDS_VALUE_MAPPING['destination'] = {
v['value']:k
for k,v in template['relay']['destination'].items()
}

self._base_check()
66 changes: 66 additions & 0 deletions plugins/modules/dhcrelay_destination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: (C) 2024, AnsibleGuy <[email protected]>
# GNU General Public License v3.0+ (see https://www.gnu.org/licenses/gpl-3.0.txt)

# see: https://docs.opnsense.org/development/api/core/dhcrelay.html

from ansible.module_utils.basic import AnsibleModule

from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.handler import \
module_dependency_error, MODULE_EXCEPTIONS

try:
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.helper.wrapper import module_wrapper
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.defaults.main import \
OPN_MOD_ARGS, STATE_ONLY_MOD_ARG, RELOAD_MOD_ARG
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.main.dhcrelay_destination import \
DhcRelayDestination

except MODULE_EXCEPTIONS:
module_dependency_error()


# DOCUMENTATION = 'https://opnsense.ansibleguy.net/en/latest/modules/dhcrelay_destination.html'
# EXAMPLES = 'https://opnsense.ansibleguy.net/en/latest/modules/dhcrelay_destination.html'


def run_module():
module_args = dict(
name=dict(
type='str', required=True,
description='A unique name for this relay destination.',
),
server=dict(
type='list', elements='str', required=False,
description='A list of server IP addresses to relay DHCP requests to.'
),
**RELOAD_MOD_ARG,
**STATE_ONLY_MOD_ARG,
**OPN_MOD_ARGS,
)

result = dict(
changed=False,
diff={
'before': {},
'after': {},
}
)

module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True,
)

module_wrapper(DhcRelayDestination(module=module, result=result))
module.exit_json(**result)


def main():
run_module()


if __name__ == '__main__':
main()
Loading

0 comments on commit 3ae0d79

Please sign in to comment.