Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5871,7 +5871,6 @@ def frequency(ctx, interface_name, frequency):

clicommon.run_command(command)


#
# 'tx_power' subcommand ('config interface transceiver tx_power ...')
# For negative float use:-
Expand Down Expand Up @@ -7157,8 +7156,49 @@ def remove_vrrp_v6(ctx, interface_name, vrrp_id):


#
# 'vrf' group ('config vrf ...')
# 'tx_error_status' command ("config tx_tx_error_status")
#
@config.group(cls=clicommon.AbbreviationGroup, name='tx_error_status')
def tx_error_status():
pass


@tx_error_status.command('set')
@click.option('-t', '--threshold', type=click.IntRange(min=1),
help='Configure threshold for Tx error state - for all ports')
@click.option('-p', '--period', type=click.IntRange(min=1),
help='Configure time period for Tx error state - for all ports')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abelamit the best practice in CLI coding is to use the single (first) letter of the entire word, or a capital letter if the duplication is present. If more then two key words are present, the shortcut would be two (first) letters for each of the words:

Example:

@click.option('-t', '--threshold')
@click.option('-T', '--time-period')
@click.option('-tp', '--time-period')

The recommended notation is:

@click.option("-t", "--threshold")
@click.option("-p", "--period")

Useful stuff:

https://click.palletsprojects.com/en/stable/api/#types

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed; thanks for the reference

@clicommon.pass_db
@click.pass_context
def set_tx_error_status(ctx, db ,threshold, period):
"""Configure Tx error status threshold and/or time period"""
if threshold is None and period is None:
ctx.fail("Please provide either threshold or time period")

if threshold is not None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abelamit FYI. The option validation can be done also using a dedicated validator object:

https://github.com/sonic-net/sonic-utilities/blob/master/config/plugins/sonic-trimming.py#L110

I would recommend to learn the existing solutions available in config/show dirs to master your Click framework skills

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

db.cfgdb.mod_entry('TX_ERROR_CHECK_TABLE', 'TX_ERROR_CHECK', {'threshold': threshold})

if period is not None:
db.cfgdb.mod_entry('TX_ERROR_CHECK_TABLE', 'TX_ERROR_CHECK', {'time_period': period})


@tx_error_status.command('del')
@click.option('-t', '--threshold', type=bool,
help='Is delete threshold for Tx error state - for all ports')
@click.option('-p', '--period', type=bool,
help='Is delete time period for Tx error state - for all ports')
@clicommon.pass_db
@click.pass_context
def del_tx_error_status(ctx, db, threshold, period):
"""Delete configured Tx error status threshold and/or time period"""
if threshold is None and period is None:
ctx.fail("Please provide either threshold or time period")

if threshold is not None and threshold is True:
db.cfgdb.mod_entry('TX_ERROR_CHECK_TABLE', 'TX_ERROR_CHECK', {'threshold': None})

if period is not None and period is True:
db.cfgdb.mod_entry('TX_ERROR_CHECK_TABLE', 'TX_ERROR_CHECK', {'time_period': None})

@config.group(cls=clicommon.AbbreviationGroup, name='vrf')
@click.pass_context
Expand Down
16 changes: 16 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,22 @@ def event_counters():
else:
click.echo('No data available in COUNTERS_EVENTS\n')

#
# 'tx_error_status' command ("show tx_error_status")
#
@cli.command()
@clicommon.pass_db
@click.argument('port', required=True)
def tx_error_status(db, port):
"""Show details of the Tx error port status"""

full_table_id = "PORT_TABLE|" + port
port_state = db.db.get(db.db.STATE_DB, full_table_id, 'tx_error_port_state')

if port_state is None:
click.echo('No data available in Port table state for port: {}\n'.format(port))
else:
click.echo('State of Port {} is {}'.format(port, port_state))

#
# 'arp' command ("show arp")
Expand Down