diff --git a/.github/workflows/ansible-test.yml b/.github/workflows/ansible-test.yml index cbf2666e..52191faa 100644 --- a/.github/workflows/ansible-test.yml +++ b/.github/workflows/ansible-test.yml @@ -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 diff --git a/plugins/module_utils/api.py b/plugins/module_utils/api.py index f78c5cee..f5431d88 100644 --- a/plugins/module_utils/api.py +++ b/plugins/module_utils/api.py @@ -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"] @@ -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 diff --git a/plugins/modules/nios_network.py b/plugins/modules/nios_network.py index 532cc8e4..1f108cc1 100644 --- a/plugins/modules/nios_network.py +++ b/plugins/modules/nios_network.py @@ -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: @@ -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 @@ -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: @@ -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(), @@ -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(), diff --git a/plugins/modules/nios_vlan.py b/plugins/modules/nios_vlan.py index b769d377..209dc193 100644 --- a/plugins/modules/nios_vlan.py +++ b/plugins/modules/nios_vlan.py @@ -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 @@ -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 @@ -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 }}" @@ -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: @@ -116,6 +116,7 @@ infoblox.nios_modules.nios_vlan: name: ansible id: 10 + parent: my_vlanview state: absent provider: host: "{{ inventory_hostname_short }}" @@ -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: "itlab@email.com" + department: "IT" + description: "test" + reserved: True + extattrs: + Site: "HQ" state: present provider: host: "{{ inventory_hostname_short }}" @@ -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 diff --git a/tests/integration/targets/nios_vlan/aliases b/tests/integration/targets/nios_vlan/aliases new file mode 100644 index 00000000..b3138dc7 --- /dev/null +++ b/tests/integration/targets/nios_vlan/aliases @@ -0,0 +1,3 @@ +shippable/cloud/group1 +cloud/nios +destructive diff --git a/tests/integration/targets/nios_vlan/defaults/main.yaml b/tests/integration/targets/nios_vlan/defaults/main.yaml new file mode 100644 index 00000000..9ef5ba51 --- /dev/null +++ b/tests/integration/targets/nios_vlan/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +testcase: "*" +test_items: [] diff --git a/tests/integration/targets/nios_vlan/meta/main.yaml b/tests/integration/targets/nios_vlan/meta/main.yaml new file mode 100644 index 00000000..a4ad32ff --- /dev/null +++ b/tests/integration/targets/nios_vlan/meta/main.yaml @@ -0,0 +1,3 @@ +--- +dependencies: + - prepare_nios_tests diff --git a/tests/integration/targets/nios_vlan/tasks/main.yaml b/tests/integration/targets/nios_vlan/tasks/main.yaml new file mode 100644 index 00000000..1862fa6e --- /dev/null +++ b/tests/integration/targets/nios_vlan/tasks/main.yaml @@ -0,0 +1,3 @@ +--- +- name: Include idempotence tasks for vlan + ansible.builtin.include_tasks: nios_vlan_idempotence.yml diff --git a/tests/integration/targets/nios_vlan/tasks/nios_vlan_idempotence.yml b/tests/integration/targets/nios_vlan/tasks/nios_vlan_idempotence.yml new file mode 100644 index 00000000..0da35124 --- /dev/null +++ b/tests/integration/targets/nios_vlan/tasks/nios_vlan_idempotence.yml @@ -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: 'example@email.com' + 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: 'example@email.com' + 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: 'example@email.com' + 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: 'example@email.com' + 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 diff --git a/tests/unit/plugins/modules/test_extensible_attribute.py b/tests/unit/plugins/modules/test_extensible_attribute.py index faafcc89..5dceeb97 100644 --- a/tests/unit/plugins/modules/test_extensible_attribute.py +++ b/tests/unit/plugins/modules/test_extensible_attribute.py @@ -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() diff --git a/tests/unit/plugins/modules/test_nios_a_record.py b/tests/unit/plugins/modules/test_nios_a_record.py index 9234f9c7..7d58603e 100644 --- a/tests/unit/plugins/modules/test_nios_a_record.py +++ b/tests/unit/plugins/modules/test_nios_a_record.py @@ -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() diff --git a/tests/unit/plugins/modules/test_nios_aaaa_record.py b/tests/unit/plugins/modules/test_nios_aaaa_record.py index 10b6e18d..ecd0c65d 100644 --- a/tests/unit/plugins/modules/test_nios_aaaa_record.py +++ b/tests/unit/plugins/modules/test_nios_aaaa_record.py @@ -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() diff --git a/tests/unit/plugins/modules/test_nios_cname_record.py b/tests/unit/plugins/modules/test_nios_cname_record.py index 3ad17abd..d5a9aebe 100644 --- a/tests/unit/plugins/modules/test_nios_cname_record.py +++ b/tests/unit/plugins/modules/test_nios_cname_record.py @@ -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() diff --git a/tests/unit/plugins/modules/test_nios_dns_view.py b/tests/unit/plugins/modules/test_nios_dns_view.py index ba003c8f..ad94a1a1 100644 --- a/tests/unit/plugins/modules/test_nios_dns_view.py +++ b/tests/unit/plugins/modules/test_nios_dns_view.py @@ -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() diff --git a/tests/unit/plugins/modules/test_nios_dtc_monitor_http.py b/tests/unit/plugins/modules/test_nios_dtc_monitor_http.py index 037ea92b..fb72b5a3 100644 --- a/tests/unit/plugins/modules/test_nios_dtc_monitor_http.py +++ b/tests/unit/plugins/modules/test_nios_dtc_monitor_http.py @@ -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() diff --git a/tests/unit/plugins/modules/test_nios_dtc_monitor_icmp.py b/tests/unit/plugins/modules/test_nios_dtc_monitor_icmp.py index 78f13365..3a7078c2 100644 --- a/tests/unit/plugins/modules/test_nios_dtc_monitor_icmp.py +++ b/tests/unit/plugins/modules/test_nios_dtc_monitor_icmp.py @@ -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() diff --git a/tests/unit/plugins/modules/test_nios_dtc_monitor_pdp.py b/tests/unit/plugins/modules/test_nios_dtc_monitor_pdp.py index 0fb63682..eb0d076e 100644 --- a/tests/unit/plugins/modules/test_nios_dtc_monitor_pdp.py +++ b/tests/unit/plugins/modules/test_nios_dtc_monitor_pdp.py @@ -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() diff --git a/tests/unit/plugins/modules/test_nios_dtc_monitor_sip.py b/tests/unit/plugins/modules/test_nios_dtc_monitor_sip.py index 6e170d75..584074c6 100644 --- a/tests/unit/plugins/modules/test_nios_dtc_monitor_sip.py +++ b/tests/unit/plugins/modules/test_nios_dtc_monitor_sip.py @@ -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() diff --git a/tests/unit/plugins/modules/test_nios_dtc_monitor_snmp.py b/tests/unit/plugins/modules/test_nios_dtc_monitor_snmp.py index 374b5a67..f0c31a5a 100644 --- a/tests/unit/plugins/modules/test_nios_dtc_monitor_snmp.py +++ b/tests/unit/plugins/modules/test_nios_dtc_monitor_snmp.py @@ -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() diff --git a/tests/unit/plugins/modules/test_nios_dtc_monitor_tcp.py b/tests/unit/plugins/modules/test_nios_dtc_monitor_tcp.py index 8de3d25f..93728dff 100644 --- a/tests/unit/plugins/modules/test_nios_dtc_monitor_tcp.py +++ b/tests/unit/plugins/modules/test_nios_dtc_monitor_tcp.py @@ -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() diff --git a/tests/unit/plugins/modules/test_nios_dtc_topology.py b/tests/unit/plugins/modules/test_nios_dtc_topology.py index 6a11515b..43d23404 100644 --- a/tests/unit/plugins/modules/test_nios_dtc_topology.py +++ b/tests/unit/plugins/modules/test_nios_dtc_topology.py @@ -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() diff --git a/tests/unit/plugins/modules/test_nios_fixed_address.py b/tests/unit/plugins/modules/test_nios_fixed_address.py index ba435b76..394cda29 100644 --- a/tests/unit/plugins/modules/test_nios_fixed_address.py +++ b/tests/unit/plugins/modules/test_nios_fixed_address.py @@ -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): diff --git a/tests/unit/plugins/modules/test_nios_host_record.py b/tests/unit/plugins/modules/test_nios_host_record.py index 7bc72337..ae309e8d 100644 --- a/tests/unit/plugins/modules/test_nios_host_record.py +++ b/tests/unit/plugins/modules/test_nios_host_record.py @@ -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') diff --git a/tests/unit/plugins/modules/test_nios_member.py b/tests/unit/plugins/modules/test_nios_member.py index c8e02ce0..0ee75a7e 100644 --- a/tests/unit/plugins/modules/test_nios_member.py +++ b/tests/unit/plugins/modules/test_nios_member.py @@ -36,7 +36,6 @@ def setUp(self): self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_member.WapiModule') self.exec_command = self.mock_wapi.start() self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_member.WapiModule.run') - self.mock_wapi_run.start() self.load_config = self.mock_wapi_run.start() def tearDown(self): diff --git a/tests/unit/plugins/modules/test_nios_mx_record.py b/tests/unit/plugins/modules/test_nios_mx_record.py index 7f8db39e..12f395b6 100644 --- a/tests/unit/plugins/modules/test_nios_mx_record.py +++ b/tests/unit/plugins/modules/test_nios_mx_record.py @@ -38,7 +38,6 @@ def setUp(self): self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_mx_record.WapiModule') self.exec_command = self.mock_wapi.start() self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_mx_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() diff --git a/tests/unit/plugins/modules/test_nios_naptr_record.py b/tests/unit/plugins/modules/test_nios_naptr_record.py index 38c3f059..a562f374 100644 --- a/tests/unit/plugins/modules/test_nios_naptr_record.py +++ b/tests/unit/plugins/modules/test_nios_naptr_record.py @@ -38,7 +38,6 @@ def setUp(self): self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_naptr_record.WapiModule') self.exec_command = self.mock_wapi.start() self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_naptr_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() diff --git a/tests/unit/plugins/modules/test_nios_network.py b/tests/unit/plugins/modules/test_nios_network.py index df9b0464..b74be41a 100644 --- a/tests/unit/plugins/modules/test_nios_network.py +++ b/tests/unit/plugins/modules/test_nios_network.py @@ -40,7 +40,6 @@ def setUp(self): 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() def tearDown(self): @@ -314,3 +313,115 @@ def test_nios_network_ipv4_update_with_use_logic_filter_rules(self): 'use_logic_filter_rules': True, 'logic_filter_rules': []} ) + + def test_nios_network_ipv4_create_with_vlan(self): + self.module.params = {'provider': None, 'state': 'present', 'network': '192.168.10.0/24', + 'comment': None, 'extattrs': None, 'vlans': [{'name': 'ansible_vlan', + 'parent': 'default', 'id': '10'}]} + + test_object = None + test_spec = { + "network": {"ib_req": True}, + "comment": {}, + "extattrs": {}, + "vlans": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('NIOS_IPV4_NETWORK', test_spec) + + self.assertTrue(res['changed']) + wapi.create_object.assert_called_once_with( + 'NIOS_IPV4_NETWORK', + { + 'network': '192.168.10.0/24', + 'vlans': [ + { + 'name': 'ansible_vlan', + 'parent': 'default', + 'id': '10' + } + ] + } + ) + + def test_nios_network_ipv4_update_vlan(self): + self.module.params = {'provider': None, 'state': 'present', 'network': '192.168.10.0/24', + 'comment': None, 'extattrs': None, 'vlans': [{'name': 'ansible_vlan1', + 'parent': 'default', 'id': '10'}, + {'name': 'ansible_vlan2', + 'parent': 'default', 'id': '20'}]} + ref = "network/ZG5zLm5ldHdvcmtfdmlldyQw:default/true" + + test_object = [ + { + "comment": "test comment", + "_ref": ref, + "network": "192.168.10.0/24", + "vlans": [{'name': 'ansible_vlan1', 'parent': 'default', 'id': '10'}] + } + ] + test_spec = { + "network": {"ib_req": True}, + "comment": {}, + "extattrs": {}, + "vlans": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('NIOS_IPV4_NETWORK', test_spec) + + self.assertTrue(res['changed']) + + wapi.update_object.assert_called_once_with( + ref, + { + 'network': '192.168.10.0/24', + 'vlans': [ + { + 'name': 'ansible_vlan1', + 'parent': 'default', + 'id': '10' + }, + { + 'name': 'ansible_vlan2', + 'parent': 'default', + 'id': '20' + } + ] + } + ) + + def test_nios_network_ipv4_remove_vlan(self): + self.module.params = {'provider': None, 'state': 'present', 'network': '192.168.10.0/24', + 'comment': None, 'extattrs': None, 'vlans': []} + ref = "network/ZG5zLm5ldHdvcmtfdmlldyQw:default/true" + + test_object = [ + { + "comment": "test comment", + "_ref": ref, + "network": "192.168.10.0/24", + "vlans": [{'name': 'ansible_vlan1', 'parent': 'default', 'id': '10'}, + {'name': 'ansible_vlan2', 'parent': 'default', 'id': '20'} + ] + } + ] + test_spec = { + "network": {"ib_req": True}, + "comment": {}, + "extattrs": {}, + "vlans": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('NIOS_IPV4_NETWORK', test_spec) + + self.assertTrue(res['changed']) + wapi.update_object.assert_called_once_with( + ref, + { + 'network': '192.168.10.0/24', + 'vlans': [] + } + ) diff --git a/tests/unit/plugins/modules/test_nios_network_view.py b/tests/unit/plugins/modules/test_nios_network_view.py index 24ee5fbd..0fc64eaf 100644 --- a/tests/unit/plugins/modules/test_nios_network_view.py +++ b/tests/unit/plugins/modules/test_nios_network_view.py @@ -38,7 +38,6 @@ def setUp(self): self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_network_view.WapiModule') self.exec_command = self.mock_wapi.start() self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_network_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() diff --git a/tests/unit/plugins/modules/test_nios_nsgroup.py b/tests/unit/plugins/modules/test_nios_nsgroup.py index 195ea7a7..a9452d2c 100644 --- a/tests/unit/plugins/modules/test_nios_nsgroup.py +++ b/tests/unit/plugins/modules/test_nios_nsgroup.py @@ -38,7 +38,6 @@ def setUp(self): self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_nsgroup.WapiModule') self.exec_command = self.mock_wapi.start() self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_nsgroup.WapiModule.run') - self.mock_wapi_run.start() self.load_config = self.mock_wapi_run.start() diff --git a/tests/unit/plugins/modules/test_nios_ptr_record.py b/tests/unit/plugins/modules/test_nios_ptr_record.py index 68f69b9c..4840f278 100644 --- a/tests/unit/plugins/modules/test_nios_ptr_record.py +++ b/tests/unit/plugins/modules/test_nios_ptr_record.py @@ -38,7 +38,6 @@ def setUp(self): self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_ptr_record.WapiModule') self.exec_command = self.mock_wapi.start() self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_ptr_record.WapiModule.run') - self.mock_wapi_run.start() self.load_config = self.mock_wapi_run.start() diff --git a/tests/unit/plugins/modules/test_nios_srv_record.py b/tests/unit/plugins/modules/test_nios_srv_record.py index 5c41cd4c..ed66886b 100644 --- a/tests/unit/plugins/modules/test_nios_srv_record.py +++ b/tests/unit/plugins/modules/test_nios_srv_record.py @@ -38,7 +38,6 @@ def setUp(self): self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_srv_record.WapiModule') self.exec_command = self.mock_wapi.start() self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_srv_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() diff --git a/tests/unit/plugins/modules/test_nios_vlan.py b/tests/unit/plugins/modules/test_nios_vlan.py new file mode 100644 index 00000000..e120f474 --- /dev/null +++ b/tests/unit/plugins/modules/test_nios_vlan.py @@ -0,0 +1,185 @@ +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish + + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + + +from ansible_collections.infoblox.nios_modules.plugins.modules import nios_vlan +from ansible_collections.infoblox.nios_modules.plugins.module_utils import api +from ansible_collections.infoblox.nios_modules.tests.unit.compat.mock import patch, MagicMock, Mock +from .test_nios_module import TestNiosModule, load_fixture + + +class TestNiosVlanModule(TestNiosModule): + + module = nios_vlan + + def setUp(self): + super(TestNiosVlanModule, self).setUp() + self.module = MagicMock(name='ansible_collections.infoblox.nios_modules.plugins.modules.nios_vlan.WapiModule') + self.module.check_mode = False + self.module.params = {'provider': None} + self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_vlan.WapiModule') + self.exec_command = self.mock_wapi.start() + self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_vlan.WapiModule.run') + 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() + + def tearDown(self): + super(TestNiosVlanModule, self).tearDown() + self.mock_wapi.stop() + self.mock_wapi_run.stop() + self.mock_check_type_dict.stop() + + def _get_wapi(self, test_object): + wapi = api.WapiModule(self.module) + wapi.get_object = Mock(name='get_object', return_value=test_object) + wapi.create_object = Mock(name='create_object') + wapi.update_object = Mock(name='update_object') + wapi.delete_object = Mock(name='delete_object') + return wapi + + def load_fixtures(self, commands=None): + self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None) + self.load_config.return_value = dict(diff=None, session='session') + + def test_nios_vlan_create(self): + self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible_vlan', + 'parent': 'default', 'id': '10', 'comment': None, 'extattrs': None} + + test_object = None + + test_spec = { + "name": {"ib_req": True}, + "parent": {"ib_req": True}, + "id": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + print("WAPI: ", wapi.__dict__) + res = wapi.run('NIOS_VLAN', test_spec) + + self.assertTrue(res['changed']) + wapi.create_object.assert_called_once_with( + 'NIOS_VLAN', + { + 'name': 'ansible_vlan', + 'parent': 'default', + 'id': '10' + } + ) + + def test_nios_vlan_update_comment(self): + self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible_vlan', + 'parent': 'default', 'id': '10', 'comment': 'updated comment', + 'contact': 'contact@email.com', 'department': 'IT', 'description': 'test', + 'reserved': True, 'extattrs': None} + + ref = "vlan/ZG5zLm5ldHdvcmtfdmlldyQw:ansible_vlan" + test_object = [ + { + "comment": "test comment", + "_ref": ref, + "name": "ansible_vlan", + "parent": "default", + "id": "10", + "extattrs": {} + } + ] + + test_spec = { + "name": {"ib_req": True}, + "parent": {"ib_req": True}, + "id": {"ib_req": True}, + "comment": {}, + "contact": {}, + "department": {}, + "description": {}, + "reserved": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('NIOS_VLAN', test_spec) + self.assertTrue(res['changed']) + wapi.update_object.assert_called_once_with( + ref, {'comment': 'updated comment', 'parent': 'default', 'id': '10', 'name': 'ansible_vlan', + 'contact': 'contact@email.com', 'department': 'IT', 'description': 'test', 'reserved': True} + ) + + def test_nios_vlan_remove(self): + self.module.params = {'provider': None, 'state': 'absent', 'name': 'ansible_vlan', + 'parent': 'default', 'id': '10', 'comment': None, 'extattrs': None} + + ref = "vlan/ZG5zLm5ldHdvcmtfdmlldyQw:ansible_vlan" + + test_object = [{ + "comment": "test comment", + "_ref": ref, + "name": "ansible_vlan", + "parent": "default", + "id": "10", + "extattrs": {'Site': {'value': 'test'}} + }] + + test_spec = { + "name": {"ib_req": True}, + "parent": {"ib_req": True}, + "id": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('NIOS_VLAN', test_spec) + + self.assertTrue(res['changed']) + wapi.delete_object.assert_called_once_with(ref) + + def test_nios_vlan_update_record_name(self): + self.module.params = {'provider': None, 'state': 'present', 'name': {'new_name': 'ansible_new_vlan', 'old_name': 'ansible_vlan'}, + 'parent': 'default', 'id': '10', 'comment': 'comment', 'extattrs': None} + + ref = "vlan/ZG5zLm5ldHdvcmtfdmlldyQw:ansible_new_vlan" + test_object = [ + { + "comment": "test comment", + "_ref": ref, + "name": "ansible_vlan", + "extattrs": {} + } + ] + + test_spec = { + "name": {"ib_req": True}, + "parent": {"ib_req": True}, + "id": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('NIOS_VLAN', test_spec) + + self.assertTrue(res['changed']) + wapi.update_object.assert_called_once_with(ref, {'name': 'ansible_new_vlan', 'parent': 'default', 'id': '10', + 'comment': 'comment'}) diff --git a/tests/unit/plugins/modules/test_nios_zone.py b/tests/unit/plugins/modules/test_nios_zone.py index 20230c8e..ccea7b95 100644 --- a/tests/unit/plugins/modules/test_nios_zone.py +++ b/tests/unit/plugins/modules/test_nios_zone.py @@ -38,7 +38,6 @@ def setUp(self): self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_zone.WapiModule') self.exec_command = self.mock_wapi.start() self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_zone.WapiModule.run') - self.mock_wapi_run.start() self.load_config = self.mock_wapi_run.start() def tearDown(self):