From 73767778e01b3d682878e60d3ddbd1bf77bea229 Mon Sep 17 00:00:00 2001 From: sushma-alethea <52454757+sushma-alethea@users.noreply.github.com> Date: Wed, 28 Aug 2019 18:26:31 +0530 Subject: [PATCH] icx: new module icx_lldp (#61310) * new module * icx_cliconf * icx test units module * icx units module * added notes * new changes * new module * new fix * new changes * new fixes * new changes * Rebase --- lib/ansible/modules/network/icx/icx_lldp.py | 184 ++++++++++++++++++ .../network/icx/fixtures/icx_lldp_None | 0 .../network/icx/fixtures/icx_lldp_absent | 9 + .../network/icx/fixtures/icx_lldp_present | 1 + .../modules/network/icx/test_icx_lldp.py | 99 ++++++++++ 5 files changed, 293 insertions(+) create mode 100644 lib/ansible/modules/network/icx/icx_lldp.py create mode 100644 test/units/modules/network/icx/fixtures/icx_lldp_None create mode 100644 test/units/modules/network/icx/fixtures/icx_lldp_absent create mode 100644 test/units/modules/network/icx/fixtures/icx_lldp_present create mode 100644 test/units/modules/network/icx/test_icx_lldp.py diff --git a/lib/ansible/modules/network/icx/icx_lldp.py b/lib/ansible/modules/network/icx/icx_lldp.py new file mode 100644 index 00000000000..b1d35329b9c --- /dev/null +++ b/lib/ansible/modules/network/icx/icx_lldp.py @@ -0,0 +1,184 @@ +#!/usr/bin/python +# Copyright: Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + + +DOCUMENTATION = """ +--- +module: icx_lldp +version_added: "2.9" +author: "Ruckus Wireless (@Commscope)" +short_description: Manage LLDP configuration on Ruckus ICX 7000 series switches +description: + - This module provides declarative management of LLDP service on ICX network devices. +notes: + - Tested against ICX 10.1. + - For information on using ICX platform, see L(the ICX OS Platform Options guide,../network/user_guide/platform_icx.html). +options: + interfaces: + description: + - specify interfaces + suboptions: + name: + description: + - List of ethernet ports to enable lldp. To add a range of ports use 'to' keyword. See the example. + type: list + state: + description: + - State of lldp configuration for interfaces + type: str + choices: ['present', 'absent', 'enabled', 'disabled'] + type: list + check_running_config: + description: + - Check running configuration. This can be set as environment variable. + Module will use environment variable value(default:True), unless it is overriden, by specifying it as module parameter. + type: bool + default: yes + state: + description: + - Enables the receipt and transmission of Link Layer Discovery Protocol (LLDP) globally. + type: str + choices: ['present', 'absent', 'enabled', 'disabled'] +""" + +EXAMPLES = """ +- name: Disable LLDP + icx_lldp: + state: absent + +- name: Enable LLDP + icx_lldp: + state: present + +- name: Disable LLDP on ports 1/1/1 - 1/1/10, 1/1/20 + icx_lldp: + interfaces: + - name: + - ethernet 1/1/1 to 1/1/10 + - ethernet 1/1/20 + state: absent + state: present + +- name: Enable LLDP on ports 1/1/5 - 1/1/10 + icx_lldp: + interfaces: + - name: + - ethernet 1/1/1 to 1/1/10 +""" + +RETURN = """ +commands: + description: The list of configuration mode commands to send to the device + returned: always, except for the platforms that use Netconf transport to manage the device. + type: list + sample: + - lldp run + - no lldp run +""" + +from ansible.module_utils.basic import AnsibleModule, env_fallback +from ansible.module_utils.network.icx.icx import load_config, run_commands + + +def has_lldp(module): + run_commands(module, ['skip']) + output = run_commands(module, ['show lldp']) + is_lldp_enable = False + if len(output) > 0 and "LLDP is not running" not in output[0]: + is_lldp_enable = True + + return is_lldp_enable + + +def map_obj_to_commands(module, commands): + interfaces = module.params.get('interfaces') + for item in interfaces: + state = item.get('state') + if state == 'present': + for port in item.get('name'): + if 'all' in port: + module.fail_json(msg='cannot enable on all the ports') + else: + commands.append('lldp enable ports {0}'.format(str(port))) + elif state == 'absent': + for port in item.get('name'): + if 'all' in port: + module.fail_json(msg='cannot enable on all the ports') + else: + commands.append('no lldp enable ports {0}'.format(str(port))) + + +def main(): + """ main entry point for module execution + """ + interfaces_spec = dict( + name=dict(type='list'), + state=dict(choices=['present', 'absent', + 'enabled', 'disabled']) + ) + + argument_spec = dict( + interfaces=dict(type='list', elements='dict', options=interfaces_spec), + state=dict(choices=['present', 'absent', + 'enabled', 'disabled']), + check_running_config=dict(default=True, type='bool', fallback=(env_fallback, ['ANSIBLE_CHECK_ICX_RUNNING_CONFIG'])) + ) + + module = AnsibleModule(argument_spec=argument_spec, + supports_check_mode=True) + + warnings = list() + + result = {'changed': False} + + if warnings: + result['warnings'] = warnings + + if module.params['check_running_config'] is False: + HAS_LLDP = None + else: + HAS_LLDP = has_lldp(module) + + commands = [] + state = module.params['state'] + + if state is None: + if HAS_LLDP: + map_obj_to_commands(module, commands) + else: + module.fail_json(msg='LLDP is not running') + else: + if state == 'absent' and HAS_LLDP is None: + commands.append('no lldp run') + + if state == 'absent' and HAS_LLDP: + commands.append('no lldp run') + + elif state == 'present': + if not HAS_LLDP: + commands.append('lldp run') + if module.params.get('interfaces'): + map_obj_to_commands(module, commands) + + result['commands'] = commands + + if commands: + if not module.check_mode: + load_config(module, commands) + + result['changed'] = True + + module.exit_json(**result) + + +if __name__ == '__main__': + main() diff --git a/test/units/modules/network/icx/fixtures/icx_lldp_None b/test/units/modules/network/icx/fixtures/icx_lldp_None new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/units/modules/network/icx/fixtures/icx_lldp_absent b/test/units/modules/network/icx/fixtures/icx_lldp_absent new file mode 100644 index 00000000000..163439739c2 --- /dev/null +++ b/test/units/modules/network/icx/fixtures/icx_lldp_absent @@ -0,0 +1,9 @@ +LLDP transmit interval : 30 seconds +LLDP transmit hold multiplier : 4 (transmit TTL: 120 seconds) +LLDP transmit delay : 2 seconds +LLDP SNMP notification interval : 5 seconds +LLDP reinitialize delay : 2 seconds +LLDP-MED fast start repeat count : 3 + +LLDP maximum neighbors : 2048 +LLDP maximum neighbors per port : 4 diff --git a/test/units/modules/network/icx/fixtures/icx_lldp_present b/test/units/modules/network/icx/fixtures/icx_lldp_present new file mode 100644 index 00000000000..680c076709e --- /dev/null +++ b/test/units/modules/network/icx/fixtures/icx_lldp_present @@ -0,0 +1 @@ +LLDP is not running \ No newline at end of file diff --git a/test/units/modules/network/icx/test_icx_lldp.py b/test/units/modules/network/icx/test_icx_lldp.py new file mode 100644 index 00000000000..be94ed31928 --- /dev/null +++ b/test/units/modules/network/icx/test_icx_lldp.py @@ -0,0 +1,99 @@ +# Copyright: (c) 2019, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type +from units.compat.mock import patch +from ansible.modules.network.icx import icx_lldp +from units.modules.utils import set_module_args +from .icx_module import TestICXModule, load_fixture + + +class TestICXlldpModule(TestICXModule): + + module = icx_lldp + + def setUp(self): + super(TestICXlldpModule, self).setUp() + + self.mock_load_config = patch('ansible.modules.network.icx.icx_lldp.load_config') + self.load_config = self.mock_load_config.start() + + self.mock_run_commands = patch('ansible.modules.network.icx.icx_lldp.run_commands') + self.run_commands = self.mock_run_commands.start() + + self.set_running_config() + + def tearDown(self): + super(TestICXlldpModule, self).tearDown() + self.mock_load_config.stop() + self.mock_run_commands.stop() + + def load_fixtures(self, commands=None): + def load_from_file(*args, **kwargs): + compares = None + module, commands = args + state = module.params['state'] + if module.params['check_running_config'] is True: + return load_fixture('icx_lldp_%s' % state).strip() + else: + return '' + + self.run_commands.side_effect = load_from_file + + def test_icx_lldp_enable_state_None(self): + interfaces_spec = [dict(name='ethernet 1/1/9', state='present')] + set_module_args(dict(interfaces=interfaces_spec)) + if not self.ENV_ICX_USE_DIFF: + result = self.execute_module(failed=True) + else: + result = self.execute_module(failed=True) + + def test_icx_lldp_enable_state_absent_compare(self): + interfaces_spec = [dict(name='ethernet 1/1/9', state='present')] + set_module_args(dict(interfaces=interfaces_spec, state='absent', check_running_config=True)) + if self.get_running_config(compare=True): + if not self.ENV_ICX_USE_DIFF: + result = self.execute_module(changed=True) + self.assertEqual(result['commands'], ['no lldp run']) + else: + result = self.execute_module(changed=True) + self.assertEqual(result['commands'], ['no lldp run']) + + def test_icx_lldp_enable_state_present(self): + interfaces_spec = [dict(name='ethernet 1/1/9', state='present')] + set_module_args(dict(interfaces=interfaces_spec, state='present')) + if not self.ENV_ICX_USE_DIFF: + result = self.execute_module(changed=True) + self.assertEqual(result['commands'], ['lldp enable ports ethernet 1/1/9']) + + else: + result = self.execute_module(changed=True) + self.assertEqual(result['commands'], ['lldp enable ports ethernet 1/1/9']) + + def test_icx_lldp_multi_enable_state_present(self): + interfaces_spec = [dict(name=['ethernet 1/1/9', 'ethernet 1/1/1 to 1/1/6'], state='present')] + set_module_args(dict(interfaces=interfaces_spec, state='present')) + if not self.ENV_ICX_USE_DIFF: + result = self.execute_module(changed=True) + self.assertEqual(result['commands'], ['lldp enable ports ethernet 1/1/9', 'lldp enable ports ethernet 1/1/1 to 1/1/6']) + else: + result = self.execute_module(changed=True) + self.assertEqual(result['commands'], ['lldp enable ports ethernet 1/1/9', 'lldp enable ports ethernet 1/1/1 to 1/1/6']) + + def test_icx_lldp_multi_disable_state_present(self): + interfaces_spec = [dict(name=['ethernet 1/1/9', 'ethernet 1/1/1 to 1/1/6'], state='absent')] + set_module_args(dict(interfaces=interfaces_spec, state='present')) + if not self.ENV_ICX_USE_DIFF: + result = self.execute_module(changed=True) + self.assertEqual(result['commands'], ['no lldp enable ports ethernet 1/1/9', 'no lldp enable ports ethernet 1/1/1 to 1/1/6']) + else: + result = self.execute_module(changed=True) + self.assertEqual(result['commands'], ['no lldp enable ports ethernet 1/1/9', 'no lldp enable ports ethernet 1/1/1 to 1/1/6']) + + def test_icx_lldp_all_error(self): + interfaces_spec = [dict(name=['ethernet all'], state='absent')] + set_module_args(dict(interfaces=interfaces_spec, state='present')) + if not self.ENV_ICX_USE_DIFF: + self.execute_module(failed=True) + else: + self.execute_module(failed=True)