Skip to content

Commit

Permalink
Rework dhcrelay_relay for simpler code
Browse files Browse the repository at this point in the history
  • Loading branch information
jiuka committed Oct 1, 2024
1 parent 3ae0d79 commit 36655d9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
41 changes: 21 additions & 20 deletions plugins/module_utils/main/dhcrelay_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.cls import BaseModule


class DhcRelayRelay(BaseModule):
Expand All @@ -14,45 +14,46 @@ class DhcRelayRelay(BaseModule):
'del': 'delRelay',
'set': 'setRelay',
'search': 'get',
'detail': 'getRelay'
'toggle': 'toggleRelay',
}
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_CHANGE = ['destination', 'agent_info']
FIELDS_ALL = [FIELD_ID, 'enabled']
FIELDS_ALL.extend(FIELDS_CHANGE)
FIELDS_VALUE_MAPPING = {}
FIELDS_TYPING = {
'select': ['interface'],
'select_opt_list': ['destination'],
'select': ['interface', 'destination'],
'bool': ['enabled', 'agent_info']
}
EXIST_ATTR = 'relay'
SEARCH_ADDITIONAL = {
'existing_destinations': 'dhcrelay.destinations',
}

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

self.existing_destinations = None

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()

if not is_unset(self.p['destination']) and self.existing_destinations:
for key, values in self.existing_destinations.items():
if values['name'] == self.p['destination']:
self.p['destination'] = key
break

def get_existing(self) -> list:
existing = self.b.get_existing()
for relay in existing:
relay['destination'] = self.existing_destinations[relay['destination']]['name']
return existing
14 changes: 7 additions & 7 deletions tests/dhcrelay_destination.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
- name: Removing - does not exist
ansibleguy.opnsense.dhcrelay_destination:
name: 'mydhcp'
name: 'ANSIBLE_TEST_1_1'
state: 'absent'
reload: false
register: opn2
Expand All @@ -34,7 +34,7 @@
- name: Adding - failing because of invalid values
ansibleguy.opnsense.dhcrelay_destination:
name: 'mydhcp'
name: 'ANSIBLE_TEST_1_1'
server: '{{ item }}'
reload: false
register: opn_fail1
Expand All @@ -46,7 +46,7 @@

- name: Adding 1
ansibleguy.opnsense.dhcrelay_destination:
name: 'mydhcp'
name: 'ANSIBLE_TEST_1_1'
server:
- '192.168.254.254'
reload: false # speed
Expand All @@ -57,7 +57,7 @@
- name: Adding 2
ansibleguy.opnsense.dhcrelay_destination:
name: 'myotherdhcp'
name: 'ANSIBLE_TEST_1_2'
server:
- '192.168.254.252'
- '192.168.254.253'
Expand All @@ -69,7 +69,7 @@
- name: Adding 2 - nothing changed
ansibleguy.opnsense.dhcrelay_destination:
name: 'myotherdhcp'
name: 'ANSIBLE_TEST_1_2'
server:
- '192.168.254.252'
- '192.168.254.253'
Expand All @@ -82,7 +82,7 @@

- name: Removing 2
ansibleguy.opnsense.dhcrelay_destination:
name: 'myotherdhcp'
name: 'ANSIBLE_TEST_1_2'
state: 'absent'
reload: false # speed
register: opn6
Expand All @@ -101,7 +101,7 @@

- name: Cleanup
ansibleguy.opnsense.dhcrelay_destination:
name: 'mydhcp'
name: 'ANSIBLE_TEST_1_1'
state: 'absent'
reload: false # speed
when: not ansible_check_mode
Expand Down
30 changes: 16 additions & 14 deletions tests/dhcrelay_relay.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
ansibleguy.opnsense.list:
target: 'dhcrelay_relay'

vars:
if_dhcrelay: "{{ lookup('ansible.builtin.env', 'TEST_DHCRELAY_IF') | default('lan', true) }}"

tasks:
- name: Listing
ansibleguy.opnsense.list:
Expand All @@ -32,15 +35,15 @@
- name: Adding - failing because of invalid interface
ansibleguy.opnsense.dhcrelay_relay:
interface: 'DOESNOTEXIST'
server: 'mydhcp'
server: 'ANSIBLE_TEST_1_1'
reload: false
register: opn_fail1
failed_when: not opn_fail1.failed
when: not ansible_check_mode

- name: Adding - failing because of invalid destination
ansibleguy.opnsense.dhcrelay_relay:
interface: 'lan'
interface: '{{ if_dhcrelay }}'
destination: 'DOESNOTEXIST'
reload: false
register: opn_fail2
Expand All @@ -49,16 +52,16 @@

- name: Adding destination
ansibleguy.opnsense.dhcrelay_destination:
name: 'mydhcp'
name: 'ANSIBLE_TEST_1_1'
server:
- '192.168.254.254'
reload: false # speed
when: not ansible_check_mode

- name: Adding 1
ansibleguy.opnsense.dhcrelay_relay:
interface: 'lan'
destination: 'mydhcp'
interface: '{{ if_dhcrelay }}'
destination: 'ANSIBLE_TEST_1_1'
reload: false # speed
register: opn3
failed_when: >
Expand All @@ -67,10 +70,9 @@
- name: Adding 1 - nothing changed
ansibleguy.opnsense.dhcrelay_relay:
interface: 'lan'
destination: 'mydhcp'
interface: '{{ if_dhcrelay }}'
destination: 'ANSIBLE_TEST_1_1'
reload: false # speed
debug: true
register: opn4
failed_when: >
opn4.failed or
Expand All @@ -79,8 +81,8 @@

- name: Enabling 1
ansibleguy.opnsense.dhcrelay_relay:
interface: 'lan'
destination: 'mydhcp'
interface: '{{ if_dhcrelay }}'
destination: 'ANSIBLE_TEST_1_1'
enabled: true
reload: false # speed
register: opn5
Expand All @@ -91,8 +93,8 @@

- name: Enabling 1 - nothing changed
ansibleguy.opnsense.dhcrelay_relay:
interface: 'lan'
destination: 'mydhcp'
interface: '{{ if_dhcrelay }}'
destination: 'ANSIBLE_TEST_1_1'
enabled: true
reload: false # speed
register: opn6
Expand All @@ -111,7 +113,7 @@

- name: Removing
ansibleguy.opnsense.dhcrelay_relay:
interface: 'lan'
interface: '{{ if_dhcrelay }}'
state: 'absent'
reload: false # speed
when: not ansible_check_mode
Expand All @@ -123,7 +125,7 @@

- name: Cleanup destination
ansibleguy.opnsense.dhcrelay_destination:
name: 'mydhcp'
name: 'ANSIBLE_TEST_1_1'
state: 'absent'
reload: false # speed
when: not ansible_check_mode
Expand Down

0 comments on commit 36655d9

Please sign in to comment.