Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added DHCP Controlagent first draft #112

Merged
merged 4 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ not implemented => development => [testing](https://github.com/ansibleguy/collec
| **DHCP Relay** | ansibleguy.opnsense.dhcrelay | [Docs](https://opnsense.ansibleguy.net/en/latest/modules/dhcrelay_relay.html) | unstable |
| **DHCP Relay** | ansibleguy.opnsense.dhcrelay_destination | [Docs](https://opnsense.ansibleguy.net/en/latest/modules/dhcrelay_destination.html) | unstable |
| **DHCP Reservation** | ansibleguy.opnsense.dhcp_reservation | [Docs](https://opnsense.ansibleguy.net/en/latest/modules/dhcp.html) | unstable |
| **DHCP Controlagent** | ansibleguy.opnsense.dhcp_controlagent | [Docs](https://opnsense.ansibleguy.net/en/latest/modules/dhcp.html) | unstable |


### Roadmap
Expand Down
45 changes: 43 additions & 2 deletions docs/source/modules/dhcp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ DHCP

**STATE**: unstable

**TESTS**: `Playbook <https://github.com/ansibleguy/collection_opnsense/blob/latest/tests/dhcp_reservation.yml>`_
**TESTS**: `Reservation <https://github.com/ansibleguy/collection_opnsense/blob/latest/tests/dhcp_reservation.yml>`_ |
`ControlAgent <https://github.com/ansibleguy/collection_opnsense/blob/latest/tests/dhcp_controlagent.yml>`_

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

Expand All @@ -17,7 +18,7 @@ DHCP
Contribution
************

Thanks to `@KalleDK <https://github.com/KalleDK>`_ for helping with the Reservation module!
Thanks to `@KalleDK <https://github.com/KalleDK>`_ for developing these module!

----

Expand All @@ -40,6 +41,18 @@ ansibleguy.opnsense.dhcp_reservation
"description","string","false","","\-","Optional description"
"reload","boolean","false","true","\-", .. include:: ../_include/param_reload.rst

ansibleguy.opnsense.dhcp_controlagent
=====================================

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

"enabled","boolean","false","true","\-","Enable or disable the control agent"
"http_host","string","false","127.0.0.1","","Address on which the RESTful interface should be available"
"http_port","int","false","8000","","MAC/Ether address of the client in question"
"reload","boolean","false","true","\-", .. include:: ../_include/param_reload.rst

----

Examples
Expand Down Expand Up @@ -91,3 +104,31 @@ ansibleguy.opnsense.dhcp_reservation
- name: Show existing reservations
ansible.builtin.debug:
var: existing_entries.data

----

ansibleguy.opnsense.dhcp_controlagent
=====================================

.. 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'

tasks:
- name: Example
ansibleguy.opnsense.dhcp_controlagent:
enabled: true
http_host: 127.0.0.1
http_port: 8000
# reload: true
# debug: false

- name: Stopping
ansibleguy.opnsense.dhcp_controlagent:
enabled: false
reload: true
1 change: 1 addition & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ action_groups:
- ansibleguy.opnsense.dhcrelay_relay
dhcp:
- ansibleguy.opnsense.dhcp_reservation
- ansibleguy.opnsense.dhcp_controlagent
all:
- metadata:
extend_group:
Expand Down
44 changes: 44 additions & 0 deletions plugins/module_utils/main/dhcp_controlagent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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_ip, validate_port
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.cls import GeneralModule


class ControlAgent(GeneralModule):
FIELD_ID = 'ip'
CMDS = {
'set': 'set',
'search': 'get',
}
API_KEY_PATH = 'ctrlagent.general'
API_KEY_PATH_REQ = API_KEY_PATH
API_MOD = 'kea'
API_CONT = 'ctrl_agent'
API_CONT_REL = 'service'
API_CMD_REL = 'reconfigure'
FIELDS_CHANGE = [
'enabled', 'http_host', 'http_port'
]
FIELDS_ALL = [*FIELDS_CHANGE]
FIELDS_TYPING = {
'bool': ['enabled'],
'int': ['http_port'],
}
INT_VALIDATIONS = {
'http_port': {'min': 1, 'max': 65535},
}

def __init__(self, module: AnsibleModule, result: dict, session: Session = None):
GeneralModule.__init__(self=self, m=module, r=result, s=session)

def check(self) -> None:
if not validate_port(module=self.m, port=self.p['http_port']):
self.m.fail_json('The provided port is invalid!')

if not is_ip(self.p['http_host']):
self.m.fail_json('The provided IP is invalid!')

super().check()
66 changes: 66 additions & 0 deletions plugins/modules/dhcp_controlagent.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/plugins/wireguard.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, EN_ONLY_MOD_ARG, RELOAD_MOD_ARG
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.main.dhcp_controlagent import ControlAgent

except MODULE_EXCEPTIONS:
module_dependency_error()


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


def run_module():
module_args = dict(
http_port=dict(
type='int', required=False, default=8000,
description='Portnumber to use for the RESTful interface'
),
http_host=dict(
type='str', required=False, default='127.0.0.1', aliases=['host'],
description='Address on which the RESTful interface should be available'
),
**EN_ONLY_MOD_ARG,
**OPN_MOD_ARGS,
**RELOAD_MOD_ARG,
)

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

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

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


def main():
run_module()


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ run_test 'nginx_general' 1
run_test 'nginx_upstream_server' 1
run_test 'dhcrelay_destination' 1
run_test 'dhcrelay_relay' 1
run_test 'dhcp_controlagent' 1
run_test 'dhcp_reservation' 1
run_test 'system' 1
run_test 'package' 1
Expand Down
6 changes: 6 additions & 0 deletions tests/cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -669,3 +669,9 @@
loop:
- '192.168.69.76'
- '192.168.69.86'

- name: Cleanup DHCP-Controlagent
ansibleguy.opnsense.dhcp_controlagent:
enabled: false
http_host: '127.0.0.1'
http_port: 8000
69 changes: 69 additions & 0 deletions tests/dhcp_controlagent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---

- name: Testing DHCP-Controlagent
hosts: localhost
gather_facts: no
module_defaults:
group/ansibleguy.opnsense.all:
firewall: "{{ lookup('ansible.builtin.env', 'TEST_FIREWALL') }}"
api_credential_file: "{{ lookup('ansible.builtin.env', 'TEST_API_KEY') }}"
ssl_verify: false

tasks:
- name: Configuring
ansibleguy.opnsense.dhcp_controlagent:
enabled: true
register: opn1
failed_when: >
opn1.failed or
not opn1.changed

- name: Changing
ansibleguy.opnsense.dhcp_controlagent:
enabled: true
http_port: 8082
http_host: '192.168.0.55'
register: opn5
failed_when: >
opn5.failed or
not opn5.changed

- name: Disabling 1
ansibleguy.opnsense.dhcp_controlagent:
enabled: false
http_port: 8082
http_host: '192.168.0.55'
register: opn2
failed_when: >
opn2.failed or
not opn2.changed
when: not ansible_check_mode

- name: Disabling 1 - nothing changed
ansibleguy.opnsense.dhcp_controlagent:
enabled: false
http_port: 8082
http_host: '192.168.0.55'
register: opn3
failed_when: >
opn3.failed or
opn3.changed
when: not ansible_check_mode

- name: Enabling 1
ansibleguy.opnsense.dhcp_controlagent:
enabled: true
http_port: 8082
http_host: '192.168.0.55'
register: opn4
failed_when: >
opn4.failed or
not opn4.changed
when: not ansible_check_mode

- name: Cleanup
ansibleguy.opnsense.dhcp_controlagent:
enabled: false
http_host: '127.0.0.1'
http_port: 8000
when: not ansible_check_mode
Loading