Skip to content

Commit

Permalink
adjust error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jun 28, 2022
1 parent dd2e2fd commit 3c69396
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
21 changes: 10 additions & 11 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1895,10 +1895,7 @@ def portchannel(db, ctx, namespace):
def add_portchannel(ctx, portchannel_name, min_links, fallback):
"""Add port channel"""

error_map = {}
error_map[ValueError] = "{} is invalid!, name should have prefix '{}' and suffix '{}'".format(portchannel_name, CFG_PORTCHANNEL_PREFIX, CFG_PORTCHANNEL_NO)

db = validate(ctx.obj['db'], error_map, ctx)
db = validate(ctx.obj['db'])

if is_portchannel_present_in_db(db, portchannel_name):
ctx.fail("{} already exists!".format(portchannel_name))
Expand All @@ -1910,24 +1907,26 @@ def add_portchannel(ctx, portchannel_name, min_links, fallback):
fvs['min_links'] = str(min_links)
if fallback != 'false':
fvs['fallback'] = 'true'
db.set_entry('PORTCHANNEL', portchannel_name, fvs)
try:
db.set_entry('PORTCHANNEL', portchannel_name, fvs)
except ValueError:
ctx.fail("{} is invalid!, name should have prefix '{}' and suffix '{}'".format(portchannel_name, CFG_PORTCHANNEL_PREFIX, CFG_PORTCHANNEL_NO))

@portchannel.command('del')
@click.argument('portchannel_name', metavar='<portchannel_name>', required=True)
@click.pass_context
def remove_portchannel(ctx, portchannel_name):
"""Remove port channel"""

error_map = {}
error_map[ValueError] = "{} is invalid!, name should have prefix '{}' and suffix '{}'".format(portchannel_name, CFG_PORTCHANNEL_PREFIX, CFG_PORTCHANNEL_NO)
error_map[JsonPatchConflict] = "{} is not present.".format(portchannel_name)

db = validate(ctx.obj['db'], error_map, ctx)
db = validate(ctx.obj['db'])

if len([(k, v) for k, v in db.get_table('PORTCHANNEL_MEMBER') if k == portchannel_name]) != 0:
click.echo("Error: Portchannel {} contains members. Remove members before deleting Portchannel!".format(portchannel_name))
else:
db.set_entry('PORTCHANNEL', portchannel_name, None)
try:
db.set_entry('PORTCHANNEL', portchannel_name, None)
except JsonPatchConflict:
ctx.fail("{} is not present.".format(portchannel_name))

@portchannel.group(cls=clicommon.AbbreviationGroup, name='member')
@click.pass_context
Expand Down
10 changes: 2 additions & 8 deletions config/validated_config_db_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from generic_config_updater.generic_updater import GenericUpdater, ConfigFormat

def validate(config_db_connector, error_map, ctx=None):
def validate(config_db_connector):

def validated_set_entry(table, key, value):
if value:
Expand All @@ -20,13 +20,7 @@ def validated_set_entry(table, key, value):
gcu_patch = jsonpatch.JsonPatch(gcu_json_input)
format = ConfigFormat.CONFIGDB.name
config_format = ConfigFormat[format.upper()]
try:
GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None)
except Exception as e:
if ctx:
ctx.fail(error_map[type(e)])
else:
print("Case specific handling of errors, not all use ctx object")
GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None)

config_db_connector.set_entry = validated_set_entry
return config_db_connector
10 changes: 5 additions & 5 deletions tests/portchannel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_add_portchannel_with_invalid_name(self):
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert 'does not satisfy the constraint "PortChannel[0-9]{1,4}"' in result.output
assert "Error: PortChan005 is invalid!, name should have prefix 'PortChannel' and suffix '<0-9999>'" in result.output

def test_delete_portchannel_with_invalid_name(self):
runner = CliRunner()
Expand All @@ -35,8 +35,8 @@ def test_delete_portchannel_with_invalid_name(self):
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "can't remove a non-existent object" in result.output

assert "Error: PortChan005 is not present." in result.output
def test_add_existing_portchannel_again(self):
runner = CliRunner()
db = Db()
Expand All @@ -59,8 +59,8 @@ def test_delete_non_existing_portchannel(self):
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "can't remove a non-existent object" in result.output
assert "Error: PortChannel0005 is not present." in result.output

def test_add_portchannel_member_with_invalid_name(self):
runner = CliRunner()
db = Db()
Expand Down

0 comments on commit 3c69396

Please sign in to comment.