diff --git a/plugins/module_utils/main/dhcrelay_relay.py b/plugins/module_utils/main/dhcrelay_relay.py index 9430189..af14006 100644 --- a/plugins/module_utils/main/dhcrelay_relay.py +++ b/plugins/module_utils/main/dhcrelay_relay.py @@ -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): @@ -14,7 +14,7 @@ class DhcRelayRelay(BaseModule): 'del': 'delRelay', 'set': 'setRelay', 'search': 'get', - 'detail': 'getRelay' + 'toggle': 'toggleRelay', } API_KEY_PATH = 'dhcrelay.relays' API_KEY_PATH_REQ = 'relay' @@ -22,37 +22,38 @@ class DhcRelayRelay(BaseModule): 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 diff --git a/tests/dhcrelay_destination.yml b/tests/dhcrelay_destination.yml index 2159baf..c67a588 100644 --- a/tests/dhcrelay_destination.yml +++ b/tests/dhcrelay_destination.yml @@ -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 @@ -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 @@ -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 @@ -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' @@ -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' @@ -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 @@ -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 diff --git a/tests/dhcrelay_relay.yml b/tests/dhcrelay_relay.yml index c1f0981..719faf8 100644 --- a/tests/dhcrelay_relay.yml +++ b/tests/dhcrelay_relay.yml @@ -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: @@ -32,7 +35,7 @@ - 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 @@ -40,7 +43,7 @@ - name: Adding - failing because of invalid destination ansibleguy.opnsense.dhcrelay_relay: - interface: 'lan' + interface: '{{ if_dhcrelay }}' destination: 'DOESNOTEXIST' reload: false register: opn_fail2 @@ -49,7 +52,7 @@ - name: Adding destination ansibleguy.opnsense.dhcrelay_destination: - name: 'mydhcp' + name: 'ANSIBLE_TEST_1_1' server: - '192.168.254.254' reload: false # speed @@ -57,8 +60,8 @@ - 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: > @@ -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 @@ -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 @@ -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 @@ -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 @@ -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