Skip to content

Commit b4c62dc

Browse files
committed
Implement categories for source_nat
1 parent d2b465d commit b4c62dc

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

plugins/module_utils/main/source_nat.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.api import \
44
Session
5+
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.helper.category import \
6+
resolve_categories
57
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.helper.main import \
68
validate_int_fields, is_unset
79
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.helper.rule import \
@@ -23,7 +25,7 @@ class SNat(BaseModule):
2325
FIELDS_CHANGE = [
2426
'sequence', 'no_nat', 'interface', 'target', 'target_port', 'description',
2527
'ip_protocol', 'protocol', 'source_invert', 'source_net', 'source_port',
26-
'destination_invert', 'destination_net', 'destination_port', 'log',
28+
'destination_invert', 'destination_net', 'destination_port', 'log', 'categories',
2729
]
2830
FIELDS_ALL = ['enabled']
2931
FIELDS_ALL.extend(FIELDS_CHANGE)
@@ -35,7 +37,7 @@ class SNat(BaseModule):
3537
}
3638
FIELDS_TYPING = {
3739
'bool': ['enabled', 'log', 'source_invert', 'no_nat', 'destination_invert'],
38-
'list': [],
40+
'list': ['categories'],
3941
'select': ['interface', 'ip_protocol', 'protocol'],
4042
'int': [],
4143
}
@@ -63,6 +65,9 @@ def check(self) -> None:
6365

6466
validate_int_fields(module=self.m, data=self.p, field_minmax=self.INT_VALIDATIONS)
6567

68+
if not is_unset(self.p['categories']):
69+
resolve_categories(self, self.p)
70+
6671
self._build_log_name()
6772
self.b.find(match_fields=self.p['match_fields'])
6873

plugins/modules/source_nat.py

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def run_module():
4141
'log': RULE_MOD_ARGS['log'],
4242
'uuid': RULE_MOD_ARGS['uuid'],
4343
'description': RULE_MOD_ARGS['description'],
44+
'categories': RULE_MOD_ARGS['categories'],
4445
}
4546

4647
module_args = dict(

scripts/test.sh

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ run_test 'alias_category' 0 # check mode => dependency on category
6363
run_test 'rule' 1
6464
run_test 'rule_multi' 1
6565
run_test 'rule_purge' 0
66+
run_test 'rule_category' 0 # check mode => dependency on category
6667
run_test 'rule_interface_group' 1
6768
run_test 'savepoint' 1
6869
run_test 'cron' 1
@@ -92,6 +93,7 @@ run_test 'interface_vip' 1
9293
run_test 'interface_lagg' 1
9394
run_test 'interface_loopback' 1
9495
run_test 'source_nat' 1
96+
run_test 'source_nat_category' 0 # check mode => dependency on category
9597
run_test 'frr_diagnostic' 1
9698
run_test 'frr_general' 1
9799
run_test 'frr_bfd_general' 1

tests/source_nat_category.yml

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
- name: Setup Test dummy
3+
hosts: localhost
4+
gather_facts: no
5+
module_defaults:
6+
group/ansibleguy.opnsense.all:
7+
firewall: "{{ lookup('ansible.builtin.env', 'TEST_FIREWALL') }}"
8+
api_credential_file: "{{ lookup('ansible.builtin.env', 'TEST_API_KEY') }}"
9+
ssl_verify: false
10+
11+
tasks:
12+
- name: Adding dummy alias
13+
ansibleguy.opnsense.category:
14+
name: 'ANSIBLE_TEST_DUMMY_1_1'
15+
color: ff0000
16+
17+
- name: Adding dummy alias
18+
ansibleguy.opnsense.category:
19+
name: 'ANSIBLE_TEST_DUMMY_1_2'
20+
color: 00ff00
21+
22+
- name: Testing source-nat - category
23+
hosts: localhost
24+
gather_facts: no
25+
module_defaults:
26+
group/ansibleguy.opnsense.all:
27+
firewall: "{{ lookup('ansible.builtin.env', 'TEST_FIREWALL') }}"
28+
api_credential_file: "{{ lookup('ansible.builtin.env', 'TEST_API_KEY') }}"
29+
ssl_verify: false
30+
match_fields: ['description']
31+
32+
tasks:
33+
- name: Adding 1
34+
ansibleguy.opnsense.source_nat:
35+
description: 'ANSIBLE_TEST_1_1'
36+
interface: 'lan'
37+
destination: '192.168.0.1'
38+
target: '192.168.0.2'
39+
categories: 'ANSIBLE_TEST_DUMMY_1_1'
40+
register: opn1
41+
failed_when: >
42+
not opn1.changed or
43+
opn1.failed
44+
45+
- name: Nothing changed
46+
ansibleguy.opnsense.source_nat:
47+
description: 'ANSIBLE_TEST_1_1'
48+
interface: 'lan'
49+
destination: '192.168.0.1'
50+
target: '192.168.0.2'
51+
categories: 'ANSIBLE_TEST_DUMMY_1_1'
52+
register: opn2
53+
failed_when: >
54+
opn2.changed or
55+
opn2.failed
56+
57+
- name: Changing
58+
ansibleguy.opnsense.source_nat:
59+
description: 'ANSIBLE_TEST_1_1'
60+
interface: 'lan'
61+
destination: '192.168.0.1'
62+
target: '192.168.0.2'
63+
categories:
64+
- 'ANSIBLE_TEST_DUMMY_1_1'
65+
- 'ANSIBLE_TEST_DUMMY_1_2'
66+
register: opn3
67+
failed_when: >
68+
not opn3.changed or
69+
opn3.failed
70+
71+
- name: Removing
72+
ansibleguy.opnsense.source_nat:
73+
description: 'ANSIBLE_TEST_1_1'
74+
state: 'absent'
75+
register: opn4
76+
failed_when: >
77+
not opn4.changed or
78+
opn4.failed
79+
80+
- name: Cleanup Test dummy
81+
hosts: localhost
82+
gather_facts: no
83+
module_defaults:
84+
group/ansibleguy.opnsense.all:
85+
firewall: "{{ lookup('ansible.builtin.env', 'TEST_FIREWALL') }}"
86+
api_credential_file: "{{ lookup('ansible.builtin.env', 'TEST_API_KEY') }}"
87+
ssl_verify: false
88+
89+
tasks:
90+
- name: Adding dummy alias
91+
ansibleguy.opnsense.category:
92+
name: 'ANSIBLE_TEST_DUMMY_1_1'
93+
state: 'absent'
94+
95+
- name: Adding dummy alias
96+
ansibleguy.opnsense.category:
97+
name: 'ANSIBLE_TEST_DUMMY_1_2'
98+
state: 'absent'

0 commit comments

Comments
 (0)