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
51 changes: 48 additions & 3 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,10 @@ 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:
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 +467,28 @@ 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:
if get_interface_naming_mode() == "alias":
port_name = interface_name_to_alias(port_name)
Copy link
Contributor

Choose a reason for hiding this comment

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

will this line duplicate with line 475?
if it is 'alias' mode and the port_name is already in members, it will try to convert the name again?

Copy link
Contributor Author

@madhupalu madhupalu Mar 7, 2019

Choose a reason for hiding this comment

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

Thanks for review comments, I'll fix and run the test cases, will update the PR shortly

if port_name is None:
ctx.fail("'port_name' is None!")
ctx.fail("{} is already a member of {}".format(port_name, portchannel_name))
else:
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 +499,29 @@ 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:
if get_interface_naming_mode() == "alias":
port_name = interface_name_to_alias(port_name)
if port_name is None:
ctx.fail("'port_name' is None!")
ctx.fail("{} is not a member of {}".format(port_name, portchannel_name))
else:
ctx.fail("{} is not a member of {}".format(port_name, portchannel_name))

members.remove(port_name)
port_channel['members'] = members
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