Implement eos_banner for EAPI (#22609)

On EAPI, the multi-line commands are expected to be a dict,
with key/value pairs 'cmd'/'input' .
This change implements that behaviour and fixes the idempotency
on EAPI as well.

Fixes #22494
This commit is contained in:
Ricardo Carrillo Cruz 2017-03-14 21:00:17 +01:00 committed by GitHub
parent 38eb388154
commit 604a38cac1
8 changed files with 51 additions and 20 deletions

View file

@ -106,9 +106,15 @@ def map_obj_to_commands(updates, module):
elif state == 'present':
if want['text'] and (want['text'] != have.get('text')):
commands.append('banner %s' % module.params['banner'])
commands.extend(want['text'].strip().split('\n'))
commands.append('EOF')
if module.params['transport'] == 'cli':
commands.append('banner %s' % module.params['banner'])
commands.extend(want['text'].strip().split('\n'))
commands.append('EOF')
else:
# For EAPI we need to construct a dict with cmd/input
# key/values for the banner
commands.append({'cmd': 'banner %s' % module.params['banner'],
'input': want['text'].strip('\n')})
return commands
@ -116,7 +122,12 @@ def map_config_to_obj(module):
output = run_commands(module, ['show banner %s' % module.params['banner']])
obj = {'banner': module.params['banner'], 'state': 'absent'}
if output:
obj['text'] = output[0]
if module.params['transport'] == 'cli':
obj['text'] = output[0]
else:
# On EAPI we need to extract the banner text from dict key
# 'loginBanner'
obj['text'] = output[0]['loginBanner'].strip('\n')
obj['state'] = 'present'
return obj

View file

@ -62,9 +62,9 @@ class AnsibleFailJson(Exception):
class TestEosModule(unittest.TestCase):
def execute_module(self, failed=False, changed=False, commands=None,
sort=True, defaults=False):
sort=True, defaults=False, transport='cli'):
self.load_fixtures(commands)
self.load_fixtures(commands, transport=transport)
if failed:
result = self.failed()
@ -108,6 +108,6 @@ class TestEosModule(unittest.TestCase):
self.assertEqual(result['changed'], changed, result)
return result
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, transport='cli'):
pass

View file

@ -39,22 +39,42 @@ class TestEosBannerModule(TestEosModule):
self.mock_run_commands.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
self.run_commands.return_value = [load_fixture('eos_banner_show_banner.txt').strip()]
def load_fixtures(self, commands=None, transport='cli'):
if transport == 'cli':
self.run_commands.return_value = [load_fixture('eos_banner_show_banner.txt').strip()]
else:
self.run_commands.return_value = [{'loginBanner': load_fixture('eos_banner_show_banner.txt').strip()}]
self.load_config.return_value = dict(diff=None, session='session')
def test_eos_banner_create(self):
set_module_args(dict(banner='login', text='test\nbanner\nstring'))
def test_eos_banner_create_with_cli_transport(self):
set_module_args(dict(banner='login', text='test\nbanner\nstring',
transport='cli'))
commands = ['banner login', 'test', 'banner', 'string', 'EOF']
self.execute_module(changed=True, commands=commands)
def test_eos_banner_remove(self):
set_module_args(dict(banner='login', state='absent'))
def test_eos_banner_create_with_eapi_transport(self):
set_module_args(dict(banner='login', text='test\nbanner\nstring',
transport='eapi'))
commands = [{'cmd': 'banner login', 'input': 'test\nbanner\nstring'}]
self.execute_module(changed=True, commands=commands, transport='eapi')
def test_eos_banner_remove_with_cli_transport(self):
set_module_args(dict(banner='login', state='absent', transport='cli'))
commands = ['no banner login']
self.execute_module(changed=True, commands=commands)
def test_eos_banner_nochange(self):
def test_eos_banner_remove_with_eapi_transport(self):
set_module_args(dict(banner='login', state='absent', transport='eapi'))
commands = ['no banner login']
self.execute_module(changed=True, commands=commands, transport='eapi')
def test_eos_banner_nochange_with_cli_transport(self):
banner_text = load_fixture('eos_banner_show_banner.txt').strip()
set_module_args(dict(banner='login', text=banner_text))
set_module_args(dict(banner='login', text=banner_text, transport='cli'))
self.execute_module()
def test_eos_banner_nochange_with_eapi_transport(self):
banner_text = load_fixture('eos_banner_show_banner.txt').strip()
set_module_args(dict(banner='login', text=banner_text, transport='eapi'))
self.execute_module(transport='eapi')

View file

@ -36,7 +36,7 @@ class TestEosCommandModule(TestEosModule):
def tearDown(self):
self.mock_run_commands.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, transport='cli'):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()

View file

@ -41,7 +41,7 @@ class TestEosConfigModule(TestEosModule):
self.mock_get_config.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, transport='cli'):
self.get_config.return_value = load_fixture('eos_config_config.cfg')
self.load_config.return_value = dict(diff=None, session='session')

View file

@ -53,7 +53,7 @@ class TestEosEapiModule(TestEosModule):
except RuntimeError:
pass
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, transport='eapi'):
def run_commands(module, commands, **kwargs):
output = list()
for cmd in commands:

View file

@ -40,7 +40,7 @@ class TestEosSystemModule(TestEosModule):
self.mock_get_config.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, transport='cli'):
self.get_config.return_value = load_fixture('eos_system_config.cfg')
self.load_config.return_value = dict(diff=None, session='session')

View file

@ -39,7 +39,7 @@ class TestEosUserModule(TestEosModule):
self.mock_get_config.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, transport='cli'):
self.get_config.return_value = load_fixture('eos_user_config.cfg')
self.load_config.return_value = dict(diff=None, session='session')