Skip to content

Commit

Permalink
[FIX] Resolved issues in vLAN module (#277)
Browse files Browse the repository at this point in the history
* Fix for vLan modules

* Fixed sanity issues related to vlan parent

* Removed trailing whitespaces

* Fixed vLan parent transform

* Fixed vLan transform in network

* Added unit tests for vLAN

* Updated examples for vLAN

* Fix sanity and unit tests

* Fix sanity and added vLAN integration tests

* corrected file permissions
  • Loading branch information
nitish-ks authored Jan 10, 2025
1 parent e85fcf4 commit 5a52457
Show file tree
Hide file tree
Showing 33 changed files with 446 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ansible-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,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:6.0.0
ANSIBLE_NIOSSIM_CONTAINER: quay.io/ansible/nios-test-container:7.0.0
working-directory: /home/runner/.ansible/collections/ansible_collections/infoblox/nios_modules/

# ansible-test support producing code coverage date
Expand Down
6 changes: 5 additions & 1 deletion plugins/module_utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ def run(self, ib_obj_type, ib_spec):
if 'default_value' in obj:
obj['default_value'] = str(obj['default_value'])

if (ib_obj_type == NIOS_VLAN):
if 'parent' in current_object:
current_object['parent'] = current_object['parent']['_ref']

# checks if the 'text' field has to be updated for the TXT Record
if (ib_obj_type == NIOS_TXT_RECORD):
text_obj = proposed_object["text"]
Expand Down Expand Up @@ -696,7 +700,7 @@ def compare_objects(self, current_object, proposed_object):
# If the lists are of a different length, the objects cannot be
# equal, and False will be returned before comparing the list items
# this code part will work for members' assignment
if (key in ('members', 'options', 'delegate_to', 'forwarding_servers', 'stub_members')
if (key in ('members', 'options', 'delegate_to', 'forwarding_servers', 'stub_members', 'vlans')
and (len(proposed_item) != len(current_item))):
return False

Expand Down
14 changes: 8 additions & 6 deletions plugins/modules/nios_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
of values (see suboptions). When configuring suboptions at
least one of C(name) or C(id) must be specified.
type: list
default: []
elements: dict
suboptions:
name:
Expand All @@ -102,6 +103,7 @@
description:
- The name of the parent vlanview or vlanrange.
type: str
default: default
extattrs:
description:
- Allows for the configuration of Extensible Attributes on the
Expand Down Expand Up @@ -393,13 +395,13 @@ def vlans(module):
if 'parent' in vlan_filtered:
obj_vlanview = wapi.get_object('vlanview', {'name': vlan_filtered['parent']})
obj_vlanrange = wapi.get_object('vlanrange', {'name': vlan_filtered['parent']})
if obj_vlanview and not obj_vlanrange:
vlan_filtered['parent'] = obj_vlanview[0]['_ref']
elif not obj_vlanview and obj_vlanrange:
if obj_vlanrange:
vlan_filtered['parent'] = obj_vlanrange[0]['_ref']
elif obj_vlanview:
vlan_filtered['parent'] = obj_vlanview[0]['_ref']
else:
module.fail_json(msg='VLAN View/Range \'%s\' cannot be found.' % vlan_filtered['parent'])

obj_vlan = wapi.get_object('vlan', vlan_filtered)

if obj_vlan:
Expand All @@ -408,7 +410,7 @@ def vlans(module):
module.fail_json(msg='VLAN `%s` cannot be found.' % vlan['name'])

return vlans_list

option_spec = dict(
# one of name or num is required; enforced by the function options()
name=dict(),
Expand All @@ -430,7 +432,7 @@ def vlans(module):
network=dict(required=True, aliases=['name', 'cidr'], ib_req=True),
network_view=dict(default='default', ib_req=True),
options=dict(type='list', elements='dict', options=option_spec, transform=options, default=[]),
vlans=dict(type='list', elements='dict', options=vlans_spec, transform=vlans),
vlans=dict(type='list', elements='dict', options=vlans_spec, transform=vlans, default=[]),
template=dict(type='str'),
extattrs=dict(type='dict'),
comment=dict(),
Expand Down
36 changes: 29 additions & 7 deletions plugins/modules/nios_vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
module: nios_vlan
author: "Christoph Spatt (@edeka-spatt)"
short_description: Configure Infoblox NIOS VLANs
version_added: "1.4.3"
version_added: "1.8.0"
description:
- Adds and/or removes instances of vlan objects from
Infoblox NIOS servers. This module manages NIOS C(vlan) objects
Expand Down Expand Up @@ -39,11 +39,10 @@
description:
- Specifies the vlan parent to add or remove from
the system. Can be either a C(vlanview) or C(vlanrange)
name. Feteches the required _ref object automatically.
name. Fetches the required _ref object automatically.
If not specified defaults to vlan view C(default).
type: str
default: default
required: true
comment:
description:
- Configures a text string comment to be associated with the instance
Expand Down Expand Up @@ -92,7 +91,7 @@
infoblox.nios_modules.nios_vlan:
name: ansible
id: 10
parent: default
parent: my_vlanview
state: present
provider:
host: "{{ inventory_hostname_short }}"
Expand All @@ -104,6 +103,7 @@
infoblox.nios_modules.nios_vlan:
name: ansible
id: 10
parent: my_vlanview
comment: this is an example comment
state: present
provider:
Expand All @@ -116,6 +116,7 @@
infoblox.nios_modules.nios_vlan:
name: ansible
id: 10
parent: my_vlanview
state: absent
provider:
host: "{{ inventory_hostname_short }}"
Expand All @@ -126,6 +127,27 @@
- name: Update an existing vlan
infoblox.nios_modules.nios_vlan:
name: {new_name: ansible-new, old_name: ansible}
id: 10
parent: my_vlanview
state: present
provider:
host: "{{ inventory_hostname_short }}"
username: admin
password: admin
connection: local
- name: Create vlan with extensible attributes
infoblox.nios_modules.nios_vlan:
name: ansible
id: 11
parent: my_vlanview
comment: "this is an example comment"
contact: "[email protected]"
department: "IT"
description: "test"
reserved: True
extattrs:
Site: "HQ"
state: present
provider:
host: "{{ inventory_hostname_short }}"
Expand All @@ -151,10 +173,10 @@ def parent_transform(module):
if module.params['parent']:
parent_obj_vlanview = wapi.get_object('vlanview', {'name': module.params['parent']})
parent_obj_vlanrange = wapi.get_object('vlanrange', {'name': module.params['parent']})
if parent_obj_vlanview and not parent_obj_vlanrange:
parent_ref = parent_obj_vlanview[0]['_ref']
elif not parent_obj_vlanview and parent_obj_vlanrange:
if parent_obj_vlanrange:
parent_ref = parent_obj_vlanrange[0]['_ref']
elif parent_obj_vlanview:
parent_ref = parent_obj_vlanview[0]['_ref']
else:
module.fail_json(msg='VLAN View/Range \'%s\' cannot be found.' % module.params['parent'])
return parent_ref
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/targets/nios_vlan/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
shippable/cloud/group1
cloud/nios
destructive
3 changes: 3 additions & 0 deletions tests/integration/targets/nios_vlan/defaults/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
testcase: "*"
test_items: []
3 changes: 3 additions & 0 deletions tests/integration/targets/nios_vlan/meta/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
dependencies:
- prepare_nios_tests
3 changes: 3 additions & 0 deletions tests/integration/targets/nios_vlan/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- name: Include idempotence tasks for vlan
ansible.builtin.include_tasks: nios_vlan_idempotence.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
- name: Cleanup VLAN
nios_vlan:
name: ansible-vlan
id: 10
parent: default
state: absent
provider: "{{ nios_provider }}"

- name: Configure a VLAN on the system
infoblox.nios_modules.nios_vlan:
name: ansible-vlan
id: 10
parent: default
contact: '[email protected]'
department: 'IT'
description: 'This is an example VLAN'
reserved: true
state: present
provider: "{{ nios_provider }}"
register: vlan_create1

- name: Configure another VLAN on the system
infoblox.nios_modules.nios_vlan:
name: ansible-vlan
id: 10
parent: default
contact: '[email protected]'
department: 'IT'
description: 'This is an example VLAN'
reserved: true
state: present
provider: "{{ nios_provider }}"
register: vlan_create2

- name: Update the comment and ext attributes for an existing VLAN
infoblox.nios_modules.nios_vlan:
name: ansible-vlan
id: 10
parent: default
contact: '[email protected]'
department: 'IT'
description: 'This is an example VLAN'
reserved: true
comment: this is an example comment
extattrs:
Site: west-dc
state: present
provider: "{{ nios_provider }}"
register: vlan_update1

- name: Update again the comment and ext attributes for an existing VLAN
infoblox.nios_modules.nios_vlan:
name: ansible-vlan
id: 10
parent: default
contact: '[email protected]'
department: 'IT'
description: 'This is an example VLAN'
reserved: true
comment: this is an example comment
extattrs:
Site: west-dc
state: present
provider: "{{ nios_provider }}"
register: vlan_update2

- name: Remove the VLAN
infoblox.nios_modules.nios_vlan:
name: ansible-vlan
id: 10
parent: default
state: absent
provider: "{{ nios_provider }}"
register: vlan_delete1

- name: Remove again the VLAN
infoblox.nios_modules.nios_vlan:
name: ansible-vlan
id: 10
parent: default
state: absent
provider: "{{ nios_provider }}"
register: vlan_delete2

- name: Assert changes in VLAN
ansible.builtin.assert:
that:
- vlan_create1.changed
- not vlan_create2.changed
- vlan_update1.changed
- not vlan_update2.changed
- vlan_delete1.changed
- not vlan_delete2.changed
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_extensible_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_extensible_attribute.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_extensible_attribute.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
Expand Down
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_nios_a_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_a_record.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_a_record.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
Expand Down
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_nios_aaaa_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_aaaa_record.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_aaaa_record.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
Expand Down
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_nios_cname_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_cname_record.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_cname_record.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
Expand Down
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_nios_dns_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dns_view.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dns_view.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
Expand Down
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_nios_dtc_monitor_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_monitor_http.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_monitor_http.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
Expand Down
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_nios_dtc_monitor_icmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_monitor_icmp.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_monitor_icmp.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
Expand Down
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_nios_dtc_monitor_pdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_monitor_pdp.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_monitor_pdp.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
Expand Down
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_nios_dtc_monitor_sip.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_monitor_sip.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_monitor_sip.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
Expand Down
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_nios_dtc_monitor_snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_monitor_snmp.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_monitor_snmp.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
Expand Down
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_nios_dtc_monitor_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_monitor_tcp.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_monitor_tcp.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
Expand Down
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_nios_dtc_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_topology.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_dtc_topology.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
Expand Down
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_nios_fixed_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_fixed_address.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_fixed_address.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()

def tearDown(self):
Expand Down
1 change: 0 additions & 1 deletion tests/unit/plugins/modules/test_nios_host_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def setUp(self):
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_host_record.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_host_record.WapiModule.run')
self.mock_wapi_run.start()

self.load_config = self.mock_wapi_run.start()
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
Expand Down
Loading

0 comments on commit 5a52457

Please sign in to comment.