Skip to content

Commit 604a38c

Browse files
Implement eos_banner for EAPI (ansible#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 ansible#22494
1 parent 38eb388 commit 604a38c

File tree

8 files changed

+51
-20
lines changed

8 files changed

+51
-20
lines changed

lib/ansible/modules/network/eos/eos_banner.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,28 @@ def map_obj_to_commands(updates, module):
106106

107107
elif state == 'present':
108108
if want['text'] and (want['text'] != have.get('text')):
109-
commands.append('banner %s' % module.params['banner'])
110-
commands.extend(want['text'].strip().split('\n'))
111-
commands.append('EOF')
109+
if module.params['transport'] == 'cli':
110+
commands.append('banner %s' % module.params['banner'])
111+
commands.extend(want['text'].strip().split('\n'))
112+
commands.append('EOF')
113+
else:
114+
# For EAPI we need to construct a dict with cmd/input
115+
# key/values for the banner
116+
commands.append({'cmd': 'banner %s' % module.params['banner'],
117+
'input': want['text'].strip('\n')})
112118

113119
return commands
114120

115121
def map_config_to_obj(module):
116122
output = run_commands(module, ['show banner %s' % module.params['banner']])
117123
obj = {'banner': module.params['banner'], 'state': 'absent'}
118124
if output:
119-
obj['text'] = output[0]
125+
if module.params['transport'] == 'cli':
126+
obj['text'] = output[0]
127+
else:
128+
# On EAPI we need to extract the banner text from dict key
129+
# 'loginBanner'
130+
obj['text'] = output[0]['loginBanner'].strip('\n')
120131
obj['state'] = 'present'
121132
return obj
122133

test/units/modules/network/eos/eos_module.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ class AnsibleFailJson(Exception):
6262
class TestEosModule(unittest.TestCase):
6363

6464
def execute_module(self, failed=False, changed=False, commands=None,
65-
sort=True, defaults=False):
65+
sort=True, defaults=False, transport='cli'):
6666

67-
self.load_fixtures(commands)
67+
self.load_fixtures(commands, transport=transport)
6868

6969
if failed:
7070
result = self.failed()
@@ -108,6 +108,6 @@ def exit_json(*args, **kwargs):
108108
self.assertEqual(result['changed'], changed, result)
109109
return result
110110

111-
def load_fixtures(self, commands=None):
111+
def load_fixtures(self, commands=None, transport='cli'):
112112
pass
113113

test/units/modules/network/eos/test_eos_banner.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,42 @@ def tearDown(self):
3939
self.mock_run_commands.stop()
4040
self.mock_load_config.stop()
4141

42-
def load_fixtures(self, commands=None):
43-
self.run_commands.return_value = [load_fixture('eos_banner_show_banner.txt').strip()]
42+
def load_fixtures(self, commands=None, transport='cli'):
43+
if transport == 'cli':
44+
self.run_commands.return_value = [load_fixture('eos_banner_show_banner.txt').strip()]
45+
else:
46+
self.run_commands.return_value = [{'loginBanner': load_fixture('eos_banner_show_banner.txt').strip()}]
47+
4448
self.load_config.return_value = dict(diff=None, session='session')
4549

46-
def test_eos_banner_create(self):
47-
set_module_args(dict(banner='login', text='test\nbanner\nstring'))
50+
def test_eos_banner_create_with_cli_transport(self):
51+
set_module_args(dict(banner='login', text='test\nbanner\nstring',
52+
transport='cli'))
4853
commands = ['banner login', 'test', 'banner', 'string', 'EOF']
4954
self.execute_module(changed=True, commands=commands)
5055

51-
def test_eos_banner_remove(self):
52-
set_module_args(dict(banner='login', state='absent'))
56+
def test_eos_banner_create_with_eapi_transport(self):
57+
set_module_args(dict(banner='login', text='test\nbanner\nstring',
58+
transport='eapi'))
59+
commands = [{'cmd': 'banner login', 'input': 'test\nbanner\nstring'}]
60+
self.execute_module(changed=True, commands=commands, transport='eapi')
61+
62+
def test_eos_banner_remove_with_cli_transport(self):
63+
set_module_args(dict(banner='login', state='absent', transport='cli'))
5364
commands = ['no banner login']
5465
self.execute_module(changed=True, commands=commands)
5566

56-
def test_eos_banner_nochange(self):
67+
def test_eos_banner_remove_with_eapi_transport(self):
68+
set_module_args(dict(banner='login', state='absent', transport='eapi'))
69+
commands = ['no banner login']
70+
self.execute_module(changed=True, commands=commands, transport='eapi')
71+
72+
def test_eos_banner_nochange_with_cli_transport(self):
5773
banner_text = load_fixture('eos_banner_show_banner.txt').strip()
58-
set_module_args(dict(banner='login', text=banner_text))
74+
set_module_args(dict(banner='login', text=banner_text, transport='cli'))
5975
self.execute_module()
6076

77+
def test_eos_banner_nochange_with_eapi_transport(self):
78+
banner_text = load_fixture('eos_banner_show_banner.txt').strip()
79+
set_module_args(dict(banner='login', text=banner_text, transport='eapi'))
80+
self.execute_module(transport='eapi')

test/units/modules/network/eos/test_eos_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def setUp(self):
3636
def tearDown(self):
3737
self.mock_run_commands.stop()
3838

39-
def load_fixtures(self, commands=None):
39+
def load_fixtures(self, commands=None, transport='cli'):
4040
def load_from_file(*args, **kwargs):
4141
module, commands = args
4242
output = list()

test/units/modules/network/eos/test_eos_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def tearDown(self):
4141
self.mock_get_config.stop()
4242
self.mock_load_config.stop()
4343

44-
def load_fixtures(self, commands=None):
44+
def load_fixtures(self, commands=None, transport='cli'):
4545
self.get_config.return_value = load_fixture('eos_config_config.cfg')
4646
self.load_config.return_value = dict(diff=None, session='session')
4747

test/units/modules/network/eos/test_eos_eapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def tearDown(self):
5353
except RuntimeError:
5454
pass
5555

56-
def load_fixtures(self, commands=None):
56+
def load_fixtures(self, commands=None, transport='eapi'):
5757
def run_commands(module, commands, **kwargs):
5858
output = list()
5959
for cmd in commands:

test/units/modules/network/eos/test_eos_system.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def tearDown(self):
4040
self.mock_get_config.stop()
4141
self.mock_load_config.stop()
4242

43-
def load_fixtures(self, commands=None):
43+
def load_fixtures(self, commands=None, transport='cli'):
4444
self.get_config.return_value = load_fixture('eos_system_config.cfg')
4545
self.load_config.return_value = dict(diff=None, session='session')
4646

test/units/modules/network/eos/test_eos_user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def tearDown(self):
3939
self.mock_get_config.stop()
4040
self.mock_load_config.stop()
4141

42-
def load_fixtures(self, commands=None):
42+
def load_fixtures(self, commands=None, transport='cli'):
4343
self.get_config.return_value = load_fixture('eos_user_config.cfg')
4444
self.load_config.return_value = dict(diff=None, session='session')
4545

0 commit comments

Comments
 (0)