Skip to content

Commit

Permalink
fix dhcp_general module
Browse files Browse the repository at this point in the history
  • Loading branch information
superstes committed Jan 11, 2025
1 parent 0e79d2a commit ad40bca
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 16 deletions.
18 changes: 11 additions & 7 deletions plugins/module_utils/main/dhcp_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class General(GeneralModule):
FIELD_ID = 'name'
CMDS = {
'set': 'set',
'search': 'get'
Expand All @@ -17,17 +16,22 @@ class General(GeneralModule):
API_CONT = 'dhcpv4'
API_CONT_REL = 'service'
FIELDS_CHANGE = [
'enabled', 'interfaces', 'dhcp_socket_type', 'fwrules', 'valid_lifetime'
'enabled', 'interfaces', 'socket_type', 'fw_rules', 'lifetime'
]

FIELDS_ALL = FIELDS_CHANGE
FIELDS_TRANSLATE = {
'lifetime': 'valid_lifetime',
'fw_rules': 'fwrules',
'socket_type': 'dhcp_socket_type',
}
FIELDS_TYPING = {
'bool': ['enabled', 'fwrules',],
'int': ['valid_lifetime'],
'bool': ['enabled', 'fw_rules'],
'int': ['lifetime'],
'list': ['interfaces'],
'select': ['socket_type'],
}

INT_VALIDATIONS = {
'valid_lifetime': {'min': 0},
'lifetime': {'min': 0},
}

def __init__(self, module: AnsibleModule, result: dict, session: Session = None):
Expand Down
23 changes: 17 additions & 6 deletions plugins/modules/dhcp_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,29 @@
except MODULE_EXCEPTIONS:
module_dependency_error()

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


def run_module():
module_args = dict(
interfaces=dict(type='str', required=False, default='LAN',
interfaces=dict(
type='list', elements='str', required=False, default=[], aliases=['ints'],
description='Comma separated list of network interfaces to listen on for DHCP requests'
),
dhcp_socket_type=dict(type='str', required=False, default='raw', choices=['raw', 'udp'], description='Socket type used for DHCP communication'),
fwrules=dict(type='bool', required=False, default=True, description='Automatically add a basic set of firewall rules to allow dhcp traffic, more fine grained controls can be offered manually when disabling this option'),
valid_lifetime=dict(type='int', required=False, default=4000, description='Defines how long the addresses (leases) given out by the server are valid (in seconds)'),
socket_type=dict(
type='str', required=False, default='raw', choices=['raw', 'udp'], aliases=['dhcp_socket_type'],
description='Socket type used for DHCP communication',
),
fw_rules=dict(
type='bool', required=False, default=True, aliases=['fwrules', 'rules'],
description='Automatically add a basic set of firewall rules to allow dhcp traffic, '
'more fine grained controls can be offered manually when disabling this option',
),
lifetime=dict(
type='int', required=False, default=4000, aliases=['valid_lifetime'],
description='Defines how long the addresses (leases) given out by the server are valid (in seconds)',
),
**EN_ONLY_MOD_ARG,
**RELOAD_MOD_ARG,
**OPN_MOD_ARGS,
Expand Down
4 changes: 1 addition & 3 deletions plugins/modules/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ def run_module():
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.main.acme_account import \
Account as Target_Obj

<<<<<<< Updated upstream
elif target == 'acme_validation':
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.main.acme_validation import \
Validation as Target_Obj
Expand All @@ -419,11 +418,10 @@ def run_module():
elif target == 'acme_certificate':
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.main.acme_certificate import \
Certificate as Target_Obj
=======

elif target == 'dhcp_general':
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.main.dhcp_general import \
General as Target_Obj
>>>>>>> Stashed changes

except AttributeError:
module_dependency_error()
Expand Down
1 change: 1 addition & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,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_general' 1
run_test 'dhcp_controlagent' 1
run_test 'dhcp_reservation' 1
run_test 'system' 1
Expand Down
4 changes: 4 additions & 0 deletions tests/1_cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -717,3 +717,7 @@
- 'ANSIBLE_TEST_1_9'
- 'ANSIBLE_TEST_1_10'
- 'ANSIBLE_TEST_DUMMY_1_1'

- name: Cleanup DHCP Settings
ansibleguy.opnsense.dhcp_general:
enabled: false
64 changes: 64 additions & 0 deletions tests/dhcp_general.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---

- name: Testing DHCP Setting
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

ansibleguy.opnsense.list:
target: 'dhcp_general'

tasks:
- name: Listing
ansibleguy.opnsense.list:
register: opn_pre1
failed_when: >
opn_pre1.failed or
'data' not in opn_pre1
- name: Configuring - failing because of invalid lifetime
ansibleguy.opnsense.dhcp_general:
lifetime: -1
register: opn_fail1
failed_when: not opn_fail1.failed

- name: Configuring
ansibleguy.opnsense.dhcp_general:
enabled: true
interfaces: ['opt1']
register: opn1
failed_when: >
opn1.failed or
not opn1.changed
- name: Changing
ansibleguy.opnsense.dhcp_general:
enabled: true
interfaces: ['opt1']
fw_rules: false
lifetime: 5000
register: opn2
failed_when: >
opn2.failed or
not opn2.changed
when: not ansible_check_mode

- name: Nothing changed
ansibleguy.opnsense.dhcp_general:
enabled: true
interfaces: ['opt1']
fw_rules: false
lifetime: 5000
register: opn3
failed_when: >
opn3.failed or
opn3.changed
when: not ansible_check_mode

- name: Cleanup
ansibleguy.opnsense.dhcp_general:
enabled: false

0 comments on commit ad40bca

Please sign in to comment.