Skip to content

Commit

Permalink
Pull 196: added support for use_logic_filter_rules and sanity fixes. (#…
Browse files Browse the repository at this point in the history
…233)

* [IMP] added support for use_logic_filter_rules
[UPD] added use_logic_filter_rules in example.
- added use_logic_filter_rules and logic_filter_rules documentation.

* [IMP] added unit testcases for logic_filter_rules
- corrected sanity test failures.

* - Updated unit testcases for logic_filter_rules with assert.

* Integration test cases for logic filters

* [FIX] Fixed sanity failure for too many blank lines

* - Updated NIOS SIM Container version to 4.0.0.

* - Updated wapi version to 2.12.3.

---------

Co-authored-by: Ubuntu <[email protected]>
  • Loading branch information
JchhatbarInfoblox and nitish-ks authored Jul 9, 2024
1 parent c4c02f3 commit 3f5025e
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ansible-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ jobs:
echo $ANSIBLE_NIOSSIM_CONTAINER
ansible-test integration -v --color --retry-on-error --continue-on-error --diff --python ${{ matrix.python-version }} --docker --coverage
env:
ANSIBLE_NIOSSIM_CONTAINER: quay.io/ansible/nios-test-container:3.0.0
ANSIBLE_NIOSSIM_CONTAINER: quay.io/ansible/nios-test-container:4.0.0
working-directory: /home/runner/.ansible/collections/ansible_collections/infoblox/nios_modules/

# ansible-test support producing code coverage date
Expand Down
2 changes: 1 addition & 1 deletion plugins/doc_fragments/nios.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ModuleDocFragment(object):
variable.
- Until ansible 2.8 the default WAPI was 1.4
type: str
default: '2.9'
default: '2.12.3'
max_results:
description:
- Specifies the maximum number of objects to be returned,
Expand Down
14 changes: 8 additions & 6 deletions plugins/lookup/nios_next_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@
- name: return next available IP address for network 192.168.10.0/24
ansible.builtin.set_fact:
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24',
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
- name: return next available IP address for network 192.168.10.0/24 from DHCP range
ansible.builtin.set_fact:
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', use_range=true,
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24',
use_range=true, provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
- name: return next available IP address for network 192.168.10.0/24 in a non-default network view
ansible.builtin.set_fact:
Expand All @@ -66,15 +66,17 @@
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', num=3, \
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
- name: return the next 3 available IP addresses for network 192.168.10.0/24 excluding ip addresses - ['192.168.10.1', '192.168.10.2']
- name: return the next 3 available IP addresses for network 192.168.10.0/24
excluding ip addresses - ['192.168.10.1', '192.168.10.2']
ansible.builtin.set_fact:
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', num=3, exclude=['192.168.10.1', '192.168.10.2'],
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', num=3,
exclude=['192.168.10.1', '192.168.10.2'],
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
- name: return next available IP address for network fd30:f52:2:12::/64
ansible.builtin.set_fact:
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', 'fd30:f52:2:12::/64',
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
"""

RETURN = """
Expand Down
2 changes: 1 addition & 1 deletion plugins/module_utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
'http_pool_connections': dict(type='int', default=10),
'http_pool_maxsize': dict(type='int', default=10),
'max_retries': dict(type='int', default=3, fallback=(env_fallback, ['INFOBLOX_MAX_RETRIES'])),
'wapi_version': dict(default='2.9', fallback=(env_fallback, ['INFOBLOX_WAPI_VERSION'])),
'wapi_version': dict(default='2.12.3', fallback=(env_fallback, ['INFOBLOX_WAPI_VERSION'])),
'max_results': dict(type='int', default=1000, fallback=(env_fallback, ['INFOBLOX_MAX_RESULTS']))
}

Expand Down
41 changes: 40 additions & 1 deletion plugins/modules/nios_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,28 @@
description:
- The name of the Nios member to be assigned to this network.
type: str
use_logic_filter_rules:
description:
- If set to true it'll override the logic filter list applied at an upper level.
type: bool
default: false
logic_filter_rules:
description:
- Configures the logic filter rules to be applied to the network object.
This argument accepts a list of logic filter rules (see suboptions). When omitted
a default value of an empty list is used.
type: list
default: []
elements: dict
suboptions:
filter:
description:
- The name of the logic filter to apply to the network object.
type: str
type:
description:
- The type of the logic filter to apply to the network object.
type: str
state:
description:
- Configures the intended state of the instance of the object on
Expand Down Expand Up @@ -186,6 +208,21 @@
password: admin
connection: local
- name: Set filters for a network ipv4
infoblox.nios_modules.nios_network:
network: 192.168.10.0/24
comment: this is a test comment
use_logic_filter_rules: true
logic_filter_rules:
- filter: PXE-UEFI
type: Option
state: present
provider:
host: "{{ inventory_hostname_short }}"
username: admin
password: admin
connection: local
- name: Remove a network ipv4
infoblox.nios_modules.nios_network:
network: 192.168.10.0/24
Expand Down Expand Up @@ -329,7 +366,9 @@ def main():
extattrs=dict(type='dict'),
comment=dict(),
container=dict(type='bool', ib_req=True),
members=dict(type='list', elements='dict', default=[])
members=dict(type='list', elements='dict', default=[]),
use_logic_filter_rules=dict(type='bool', default=False),
logic_filter_rules=dict(type='list', elements='dict', default=[])
)

argument_spec = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,53 @@
provider: "{{ nios_provider }}"
register: nios_ipv6_create1


- name: configure a network ipv6 with filter option
nios_network:
network: fe80::/64
comment: this is a test comment
use_logic_filter_rules: true
state: present
provider: "{{ nios_provider }}"
register: nios_ipv6_update1

- name: configure a network ipv4 with filter option
nios_network:
network: 192.168.11.0/24
comment: this is a test comment
use_logic_filter_rules: true
state: present
provider: "{{ nios_provider }}"
register: nios_ipv4_create3

- name: update a network ipv4 with filter option
nios_network:
network: 192.168.11.0/24
comment: this is a test comment
use_logic_filter_rules: false
state: present
provider: "{{ nios_provider }}"
register: nios_ipv4_update3

- name: Re-run with no changes
nios_network:
network: 192.168.11.0/24
comment: this is a test comment
use_logic_filter_rules: false
state: present
provider: "{{ nios_provider }}"
register: nios_ipv4_update4

- name: remove an ipv4 network
nios_network:
network: 192.168.11.0/24
comment: this is a test comment
use_logic_filter_rules: false
state: absent
provider: "{{ nios_provider }}"
register: nios_ipv4_remove3


- assert:
that:
- "nios_ipv4_create1.changed"
Expand All @@ -78,3 +125,9 @@
- "not nios_ipv4_update2.changed"
- "nios_ipv4_remove1.changed"
- "not nios_ipv4_remove2.changed"
- "nios_ipv4_create3.changed"
- "nios_ipv4_update3.changed"
- "nios_ipv6_create1.changed"
- "nios_ipv6_update1.changed"
- "not nios_ipv4_update4.changed"
- "nios_ipv4_remove3.changed"
70 changes: 67 additions & 3 deletions tests/unit/plugins/modules/test_nios_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ class TestNiosNetworkModule(TestNiosModule):

def setUp(self):
super(TestNiosNetworkModule, self).setUp()
self.module = MagicMock(name='ansible_collections.infoblox.nios_modules.plugins.modules.nios_network.WapiModule')
self.module = MagicMock(
name='ansible_collections.infoblox.nios_modules.plugins.modules.nios_network.WapiModule'
)
self.module.check_mode = False
self.module.params = {'provider': None}
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_network.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_network.WapiModule.run')
self.mock_wapi_run = patch(
'ansible_collections.infoblox.nios_modules.plugins.modules.nios_network.WapiModule.run'
)
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()

Expand All @@ -58,7 +62,8 @@ def _get_wapi(self, test_object):

def test_nios_network_ipv4_create(self):
self.module.params = {'provider': None, 'state': 'present', 'network': '192.168.10.0/24',
'comment': None, 'extattrs': None}
'comment': None, 'extattrs': None, 'use_logic_filter_rules': False,
'logic_filter_rules': []}

test_object = None
test_spec = {
Expand Down Expand Up @@ -250,3 +255,62 @@ def test_nios_networkcontainer_ipv6_create(self):

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'ipv6networkcontainer': 'fe80::/64'})

def test_nios_network_ipv4_create_with_use_logic_filter_rules(self):
self.module.params = {'provider': None, 'state': 'present', 'network': '192.168.10.0/24',
'comment': None, 'extattrs': None, 'use_logic_filter_rules': True,
'logic_filter_rules': []}

test_object = None
test_spec = {
"network": {"ib_req": True},
"comment": {},
"extattrs": {},
"use_logic_filter_rules": {},
"logic_filter_rules": {}
}

wapi = self._get_wapi(test_object)
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'network': '192.168.10.0/24',
'use_logic_filter_rules': True,
'logic_filter_rules': []
}
)

def test_nios_network_ipv4_update_with_use_logic_filter_rules(self):
self.module.params = {'provider': None, 'state': 'present', 'network': '192.168.10.0/24',
'comment': 'updated comment', 'extattrs': None, 'use_logic_filter_rules': True,
'logic_filter_rules': []}

ref = "network/ZG5zLm5ldHdvcmtfdmlldyQw:default/true"
test_object = [
{
"comment": "test comment",
"_ref": ref,
"network": "192.168.10.0/24",
"extattrs": {'options': {'name': 'test', 'value': 'ansible.com'}},
"use_logic_filter_rules": False,
"logic_filter_rules": []
}
]

test_spec = {
"network": {"ib_req": True},
"comment": {},
"extattrs": {},
"use_logic_filter_rules": {},
"logic_filter_rules": {}
}

wapi = self._get_wapi(test_object)
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.update_object.assert_called_once_with(ref, {'network': '192.168.10.0/24',
'comment': 'updated comment',
'use_logic_filter_rules': True,
'logic_filter_rules': []}
)

0 comments on commit 3f5025e

Please sign in to comment.