Skip to content

Commit b2b9734

Browse files
[Banner] Added CLI commands to configure Banner and display current configuration (sonic-net#3021)
What I did Added CLI commands for Banner feature according to HLD: sonic-net/SONiC#1361 How I did it Added CLI commands to: Enable/disable Banner feature Configure Banner messages: login/motd/logout Related show command How to verify it Manual testing
1 parent d0aa94a commit b2b9734

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed

config/main.py

+53
Original file line numberDiff line numberDiff line change
@@ -8065,5 +8065,58 @@ def max_sessions(max_sessions):
80658065
{'max_sessions': max_sessions})
80668066

80678067

8068+
#
8069+
# 'banner' group ('config banner ...')
8070+
#
8071+
@config.group()
8072+
def banner():
8073+
"""Configuring system banner messages"""
8074+
pass
8075+
8076+
8077+
@banner.command()
8078+
@click.argument('state', metavar='<enabled|disabled>', required=True, type=click.Choice(['enabled', 'disabled']))
8079+
def state(state):
8080+
"""Set banner feature state"""
8081+
8082+
config_db = ConfigDBConnector()
8083+
config_db.connect()
8084+
config_db.mod_entry(swsscommon.CFG_BANNER_MESSAGE_TABLE_NAME, 'global',
8085+
{'state': state})
8086+
8087+
8088+
@banner.command()
8089+
@click.argument('message', metavar='<message>', required=True)
8090+
def login(message):
8091+
"""Set login message"""
8092+
8093+
config_db = ConfigDBConnector()
8094+
config_db.connect()
8095+
config_db.mod_entry(swsscommon.CFG_BANNER_MESSAGE_TABLE_NAME, 'global',
8096+
{'login': message})
8097+
8098+
8099+
@banner.command()
8100+
@click.argument('message', metavar='<message>', required=True)
8101+
def logout(message):
8102+
"""Set logout message"""
8103+
8104+
config_db = ConfigDBConnector()
8105+
config_db.connect()
8106+
config_db.mod_entry(swsscommon.CFG_BANNER_MESSAGE_TABLE_NAME, 'global',
8107+
{'logout': message})
8108+
8109+
8110+
@banner.command()
8111+
@click.argument('message', metavar='<message>', required=True)
8112+
def motd(message):
8113+
"""Set message of the day"""
8114+
8115+
config_db = ConfigDBConnector()
8116+
config_db.connect()
8117+
config_db.mod_entry(swsscommon.CFG_BANNER_MESSAGE_TABLE_NAME, 'global',
8118+
{'motd': message})
8119+
8120+
80688121
if __name__ == '__main__':
80698122
config()

doc/Command-Reference.md

+89
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@
225225
* [Static DNS show command](#static-dns-show-command)
226226
* [Wake-on-LAN Commands](#wake-on-lan-commands)
227227
* [Send Wake-on-LAN Magic Packet command](#send-wake-on-lan-magic-packet-command)
228+
* [Banner Commands](#banner-commands)
229+
* [Banner config commands](#banner-config-commands)
230+
* [Banner show command](#banner-show-command)
228231

229232
## Document History
230233

@@ -13819,3 +13822,89 @@ Sending 3 magic packet to 11:33:55:77:99:bb via interface Vlan1000
1381913822
```
1382013823
1382113824
For the 4th example, it specifise 2 target MAC addresses and `count` is 3. So it'll send 6 magic packets in total.
13825+
13826+
# Banner Commands
13827+
13828+
This sub-section explains the list of the configuration options available for Banner feature.
13829+
13830+
## Banner config commands
13831+
13832+
- Set banner feature state
13833+
13834+
```
13835+
admin@sonic:~$ config banner state <enabled|disabled>
13836+
Usage: config config banner state <enabled|disabled>
13837+
13838+
Set banner feature state
13839+
13840+
Options:
13841+
-?, -h, --help Show this message and exit.
13842+
```
13843+
13844+
- Set login message
13845+
13846+
```
13847+
admin@sonic:~$ config banner login <message>
13848+
Usage: config banner login <message>
13849+
13850+
Set login message
13851+
13852+
Options:
13853+
-?, -h, --help Show this message and exit.
13854+
```
13855+
13856+
- Set logout message
13857+
13858+
```
13859+
admin@sonic:~$ config banner logout <message>
13860+
Usage: config banner logout <message>
13861+
13862+
Set logout message
13863+
13864+
Options:
13865+
-?, -h, --help Show this message and exit.
13866+
```
13867+
13868+
- Set message of the day
13869+
13870+
```
13871+
admin@sonic:~$ config banner motd <message>
13872+
Usage: config banner motd <message>
13873+
13874+
Set message of the day
13875+
13876+
Options:
13877+
-?, -h, --help Show this message and exit.
13878+
```
13879+
13880+
## Banner show command
13881+
13882+
- how banner messages
13883+
13884+
```
13885+
admin@sonic:~$ show banner
13886+
Usage: show banner
13887+
13888+
Show banner messages
13889+
13890+
Options:
13891+
-h, -?, --help Show this message and exit.
13892+
```
13893+
```
13894+
admin@sonic:~$ show banner
13895+
state login motd logout
13896+
------- ------- ------------------------------------------------ --------
13897+
enabled Login You are on
13898+
Message ____ ___ _ _ _ ____
13899+
/ ___| / _ \| \ | (_)/ ___|
13900+
\___ \| | | | \| | | |
13901+
___) | |_| | |\ | | |___
13902+
|____/ \___/|_| \_|_|\____|
13903+
13904+
-- Software for Open Networking in the Cloud --
13905+
13906+
Unauthorized access and/or use are prohibited.
13907+
All access and/or use are subject to monitoring.
13908+
13909+
Help: https://sonic-net.github.io/SONiC/
13910+
```

show/main.py

+20
Original file line numberDiff line numberDiff line change
@@ -2616,6 +2616,26 @@ def ssh(db):
26162616
click.echo(tabulate(configuration, headers=hdrs, tablefmt='simple', missingval=''))
26172617

26182618

2619+
#
2620+
# 'banner' command group ("show banner ...")
2621+
#
2622+
@cli.group('banner', invoke_without_command=True)
2623+
@clicommon.pass_db
2624+
def banner(db):
2625+
"""Show banner messages"""
2626+
2627+
banner_table = db.cfgdb.get_entry('BANNER_MESSAGE', 'global')
2628+
2629+
hdrs = ['state', 'login', 'motd', 'logout']
2630+
data = []
2631+
2632+
for key in hdrs:
2633+
data.append(banner_table.get(key, '').replace('\\n', '\n'))
2634+
2635+
messages = [data]
2636+
click.echo(tabulate(messages, headers=hdrs, tablefmt='simple', missingval=''))
2637+
2638+
26192639
# Load plugins and register them
26202640
helper = util_base.UtilHelper()
26212641
helper.load_and_register_plugins(plugins, cli)

tests/config_test.py

+60
Original file line numberDiff line numberDiff line change
@@ -3905,3 +3905,63 @@ def teardown_class(cls):
39053905
from .mock_tables import mock_single_asic
39063906
importlib.reload(mock_single_asic)
39073907
dbconnector.load_database_config()
3908+
3909+
3910+
class TestConfigBanner(object):
3911+
@classmethod
3912+
def setup_class(cls):
3913+
print('SETUP')
3914+
import config.main
3915+
importlib.reload(config.main)
3916+
3917+
@patch('utilities_common.cli.run_command',
3918+
mock.MagicMock(side_effect=mock_run_command_side_effect))
3919+
def test_banner_state(self):
3920+
runner = CliRunner()
3921+
obj = {'db': Db().cfgdb}
3922+
3923+
result = runner.invoke(
3924+
config.config.commands['banner'].commands['state'],
3925+
['enabled'], obj=obj)
3926+
3927+
assert result.exit_code == 0
3928+
3929+
@patch('utilities_common.cli.run_command',
3930+
mock.MagicMock(side_effect=mock_run_command_side_effect))
3931+
def test_banner_login(self):
3932+
runner = CliRunner()
3933+
obj = {'db': Db().cfgdb}
3934+
3935+
result = runner.invoke(
3936+
config.config.commands['banner'].commands['login'],
3937+
['Login message'], obj=obj)
3938+
3939+
assert result.exit_code == 0
3940+
3941+
@patch('utilities_common.cli.run_command',
3942+
mock.MagicMock(side_effect=mock_run_command_side_effect))
3943+
def test_banner_logout(self):
3944+
runner = CliRunner()
3945+
obj = {'db': Db().cfgdb}
3946+
3947+
result = runner.invoke(
3948+
config.config.commands['banner'].commands['logout'],
3949+
['Logout message'], obj=obj)
3950+
3951+
assert result.exit_code == 0
3952+
3953+
@patch('utilities_common.cli.run_command',
3954+
mock.MagicMock(side_effect=mock_run_command_side_effect))
3955+
def test_banner_motd(self):
3956+
runner = CliRunner()
3957+
obj = {'db': Db().cfgdb}
3958+
3959+
result = runner.invoke(
3960+
config.config.commands['banner'].commands['motd'],
3961+
['Motd message'], obj=obj)
3962+
3963+
assert result.exit_code == 0
3964+
3965+
@classmethod
3966+
def teardown_class(cls):
3967+
print('TEARDOWN')

tests/show_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,12 @@ def test_show_ztp(self, mock_run_command):
10401040
assert result.exit_code == 0
10411041
mock_run_command.assert_called_with(['ztp', 'status', '--verbose'], display_cmd=True)
10421042

1043+
@patch('show.main.run_command')
1044+
def test_show_banner(self, mock_run_command):
1045+
runner = CliRunner()
1046+
result = runner.invoke(show.cli.commands['banner'])
1047+
assert result.exit_code == 0
1048+
10431049
def teardown(self):
10441050
print('TEAR DOWN')
10451051

0 commit comments

Comments
 (0)