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

Fix purge disable #116

Merged
merged 3 commits into from
Nov 22, 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
6 changes: 3 additions & 3 deletions plugins/module_utils/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def _change_enabled_state(self) -> dict:
'params': [getattr(self.i, self.i.EXIST_ATTR)[self.field_pk]],
})

def _is_enabled(self, invert: bool) -> bool:
def is_enabled(self, invert: bool = False) -> bool:
is_enabled = getattr(self.i, self.i.EXIST_ATTR)['enabled']

if invert:
Expand All @@ -373,7 +373,7 @@ def _is_enabled(self, invert: bool) -> bool:
return is_enabled

def enable(self, invert: bool = False) -> dict:
if self.i.exists and not self._is_enabled(invert=invert):
if self.i.exists and not self.is_enabled(invert=invert):
self.i.r['changed'] = True
if not invert:
self.i.r['diff']['before'] = {'enabled': False}
Expand All @@ -387,7 +387,7 @@ def enable(self, invert: bool = False) -> dict:
return self._change_enabled_state()

def disable(self, invert: bool = False) -> dict:
if self.i.exists and self._is_enabled(invert=invert):
if self.i.exists and self.is_enabled(invert=invert):
self.i.r['changed'] = True
if not invert:
self.i.r['diff']['before'] = {'enabled': True}
Expand Down
10 changes: 5 additions & 5 deletions plugins/module_utils/helper/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ def purge(
result['diff']['before'][item_to_purge[diff_param]] = item_to_purge
result['diff']['after'][item_to_purge[diff_param]] = None

else:
result['diff']['before'][item_to_purge[diff_param]] = {'enabled': True}
result['diff']['after'][item_to_purge[diff_param]] = {'enabled': False}

if not module.check_mode:
_obj = obj_func(item_to_purge)
_obj.exists = True

if module.params['action'] == 'delete':
_obj.delete()

else:
_obj.b.disable()
if _obj.b.is_enabled():
result['diff']['before'][item_to_purge[diff_param]] = {'enabled': True}
result['diff']['after'][item_to_purge[diff_param]] = {'enabled': False}
_obj.b.disable()


def check_purge_filter(module: AnsibleModule, item: dict) -> bool:
Expand Down
5 changes: 3 additions & 2 deletions plugins/module_utils/main/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ def check(self) -> None:
self._base_check()

def _simplify_existing(self, route: dict) -> dict:
# makes processing easier
simple = simplify_translate(
existing=route,
typing=self.FIELDS_TYPING,
translate=self.FIELDS_TRANSLATE,
bool_invert=self.FIELDS_BOOL_INVERT,
)
simple['gateway'] = simple['gateway'].rsplit('-', 1)[0].strip()
if simple['gateway'].find(' - ') != -1:
simple['gateway'] = simple['gateway'].rsplit('-', 1)[0].strip()

return simple
10 changes: 9 additions & 1 deletion tests/alias_purge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@
ANSIBLE_TEST_4_2:
action: 'disable'
register: opn1
when: not ansible_check_mode
failed_when: >
opn1.failed or
not opn1.changed
when: not ansible_check_mode

- name: Simple disable-purge - nothing changed
ansibleguy.opnsense.alias_purge:
aliases:
ANSIBLE_TEST_4_2:
action: 'disable'
register: opn11
when: not ansible_check_mode

- name: Simple purge
ansibleguy.opnsense.alias_purge:
Expand Down
19 changes: 15 additions & 4 deletions tests/route.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,35 @@
ansibleguy.opnsense.route:
description: 'ANSIBLE_TEST_2'
network: '4.4.4.2/32'
gateway: 'LAN_GW'
gateway: 'TEST-GW'
register: opn5
failed_when: >
opn5.failed or
not opn5.changed

- name: Adding 2 - nothing changed with match-fields network & gateway
- name: Adding 2 - nothing changed
ansibleguy.opnsense.route:
description: 'ANSIBLE_TEST_2'
network: '4.4.4.2/32'
gateway: 'LAN_GW'
match_fields: ['network', 'gateway']
gateway: 'TEST-GW'
register: opn9
failed_when: >
opn9.failed or
opn9.changed
when: not ansible_check_mode

- name: Adding 2 - nothing changed with match-fields network & gateway
ansibleguy.opnsense.route:
description: 'ANSIBLE_TEST_2'
network: '4.4.4.2/32'
gateway: 'TEST-GW'
match_fields: ['network', 'gateway']
register: opn10
failed_when: >
opn10.failed or
opn10.changed
when: not ansible_check_mode

- name: Disabling 1
ansibleguy.opnsense.route:
description: 'ANSIBLE_TEST_1'
Expand Down
Loading