Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confirm portchannel and member ports exist before attempting delete operations #454

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
45 changes: 42 additions & 3 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,13 @@ def add_portchannel(ctx, portchannel_name, min_links, fallback):
def remove_portchannel(ctx, portchannel_name):
"""Remove port channel"""
db = ctx.obj['db']
db.set_entry('PORTCHANNEL', portchannel_name, None)
if len(db.get_entry('PORTCHANNEL', portchannel_name)) != 0:
keys = [(k,v) for k,v in db.get_table('PORTCHANNEL_MEMBER') if k == portchannel_name]
for k in keys:
db.set_entry('PORTCHANNEL_MEMBER', k, None)
db.set_entry('PORTCHANNEL', portchannel_name, None)
else:
ctx.fail("{} is not configured".format(portchannel_name))

@portchannel.group('member')
@click.pass_context
Expand All @@ -464,6 +470,22 @@ def portchannel_member(ctx):
def add_portchannel_member(ctx, portchannel_name, port_name):
"""Add member to port channel"""
db = ctx.obj['db']
port_channel = db.get_entry('PORTCHANNEL', portchannel_name)
if get_interface_naming_mode() == "alias":
port_name = interface_alias_to_name(port_name)
if port_name is None:
ctx.fail("'port_name' is None!")

if len(port_channel) == 0:
ctx.fail("{} doesn't exists".format(portchannel_name))

members = port_channel.get('members', [])
if port_name in members:
ctx.fail("{} is already a member of {}".format(port_name, portchannel_name))

members.append(port_name)
port_channel['members'] = members
db.set_entry('PORTCHANNEL', portchannel_name, port_channel)
db.set_entry('PORTCHANNEL_MEMBER', (portchannel_name, port_name),
{'NULL': 'NULL'})

Expand All @@ -474,9 +496,26 @@ def add_portchannel_member(ctx, portchannel_name, port_name):
def del_portchannel_member(ctx, portchannel_name, port_name):
"""Remove member from portchannel"""
db = ctx.obj['db']
port_channel = db.get_entry('PORTCHANNEL', portchannel_name)
if get_interface_naming_mode() == "alias":
port_name = interface_alias_to_name(port_name)
if port_name is None:
ctx.fail("'port_name' is None!")

if len(port_channel) == 0:
ctx.fail("{} doesn't exists".format(portchannel_name))

members = port_channel.get('members', [])
if port_name not in members:
ctx.fail("{} is not a member of {}".format(port_name, portchannel_name))

members.remove(port_name)
if len(members) == 0:
del port_channel['members']
else:
port_channel['members'] = members
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we need the **members** variable to delete the port channel when there are no members in it. AFAIK, we delete port channels when there are no members in it, not sure the SONiC use case. please advise.

Thanks
Madhu

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed the review comments and updated the PR. Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

there're alternative ways of checking if the port-channel has members or not. please do not use this approach. we are trying to make the code DRY by not having duplicated information on two places; this attribute won't be supported in the future release

db.set_entry('PORTCHANNEL', portchannel_name, port_channel)
db.set_entry('PORTCHANNEL_MEMBER', (portchannel_name, port_name), None)
db.set_entry('PORTCHANNEL_MEMBER', portchannel_name + '|' + port_name, None)


#
# 'mirror_session' group ('config mirror_session ...')
Expand Down