From 20272d7d8bfaa1316adbf141f3c18b7e8151c213 Mon Sep 17 00:00:00 2001 From: madhu Pal Date: Sun, 3 Feb 2019 00:00:21 +0000 Subject: [PATCH 1/6] [config/main.py] Fixed - added validations such that portchannel and members add/delete only when it is configured (#277) Signed-off-by: madhu Pal --- config/main.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/config/main.py b/config/main.py index 99955d90c3..22d761dd18 100755 --- a/config/main.py +++ b/config/main.py @@ -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 @@ -464,8 +467,12 @@ def portchannel_member(ctx): def add_portchannel_member(ctx, portchannel_name, port_name): """Add member to port channel""" db = ctx.obj['db'] - db.set_entry('PORTCHANNEL_MEMBER', (portchannel_name, port_name), - {'NULL': 'NULL'}) + if len(db.get_entry('PORTCHANNEL', portchannel_name)) != 0: + db.set_entry('PORTCHANNEL_MEMBER', (portchannel_name, port_name), + {'NULL': 'NULL'}) + else: + ctx.fail("{} is not configured to add {}".format(portchannel_name, port_name)) + @portchannel_member.command('del') @click.argument('portchannel_name', metavar='', required=True) @@ -474,8 +481,12 @@ 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'] - db.set_entry('PORTCHANNEL_MEMBER', (portchannel_name, port_name), None) - db.set_entry('PORTCHANNEL_MEMBER', portchannel_name + '|' + port_name, None) + if len(db.get_entry('PORTCHANNEL_MEMBER', portchannel_name + '|' + port_name)) != 0: + db.set_entry('PORTCHANNEL_MEMBER', (portchannel_name, port_name), None) + db.set_entry('PORTCHANNEL_MEMBER', portchannel_name + '|' + port_name, None) + else: + ctx.fail("{} is not part of {}".format(port_name, portchannel_name)) + # From 5539480aba6d32b6a55ca56cfc29a471a2c46a6a Mon Sep 17 00:00:00 2001 From: madhu Pal Date: Sun, 3 Feb 2019 00:00:21 +0000 Subject: [PATCH 2/6] Added validations to portchannel & members deletes only it is configured (#277) Signed-off-by: madhu Pal --- config/main.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/config/main.py b/config/main.py index 99955d90c3..22d761dd18 100755 --- a/config/main.py +++ b/config/main.py @@ -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 @@ -464,8 +467,12 @@ def portchannel_member(ctx): def add_portchannel_member(ctx, portchannel_name, port_name): """Add member to port channel""" db = ctx.obj['db'] - db.set_entry('PORTCHANNEL_MEMBER', (portchannel_name, port_name), - {'NULL': 'NULL'}) + if len(db.get_entry('PORTCHANNEL', portchannel_name)) != 0: + db.set_entry('PORTCHANNEL_MEMBER', (portchannel_name, port_name), + {'NULL': 'NULL'}) + else: + ctx.fail("{} is not configured to add {}".format(portchannel_name, port_name)) + @portchannel_member.command('del') @click.argument('portchannel_name', metavar='', required=True) @@ -474,8 +481,12 @@ 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'] - db.set_entry('PORTCHANNEL_MEMBER', (portchannel_name, port_name), None) - db.set_entry('PORTCHANNEL_MEMBER', portchannel_name + '|' + port_name, None) + if len(db.get_entry('PORTCHANNEL_MEMBER', portchannel_name + '|' + port_name)) != 0: + db.set_entry('PORTCHANNEL_MEMBER', (portchannel_name, port_name), None) + db.set_entry('PORTCHANNEL_MEMBER', portchannel_name + '|' + port_name, None) + else: + ctx.fail("{} is not part of {}".format(port_name, portchannel_name)) + # From 448d9e21cba5a50e7a0431e2b3d87c97247fb6a3 Mon Sep 17 00:00:00 2001 From: madhu Pal Date: Tue, 12 Feb 2019 04:35:24 +0000 Subject: [PATCH 3/6] [config/main.py] added validations for portchannel/members on crud operations, covered vendor interfaces as well. (#277) Signed-off-by: madhu Pal --- config/main.py | 60 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/config/main.py b/config/main.py index 22d761dd18..b52272891d 100755 --- a/config/main.py +++ b/config/main.py @@ -467,12 +467,30 @@ def portchannel_member(ctx): def add_portchannel_member(ctx, portchannel_name, port_name): """Add member to port channel""" db = ctx.obj['db'] - if len(db.get_entry('PORTCHANNEL', portchannel_name)) != 0: - db.set_entry('PORTCHANNEL_MEMBER', (portchannel_name, port_name), - {'NULL': 'NULL'}) - else: - ctx.fail("{} is not configured to add {}".format(portchannel_name, port_name)) - + 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) + 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'}) @portchannel_member.command('del') @click.argument('portchannel_name', metavar='', required=True) @@ -481,13 +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'] - if len(db.get_entry('PORTCHANNEL_MEMBER', portchannel_name + '|' + port_name)) != 0: - db.set_entry('PORTCHANNEL_MEMBER', (portchannel_name, port_name), None) - db.set_entry('PORTCHANNEL_MEMBER', portchannel_name + '|' + port_name, None) - else: - ctx.fail("{} is not part of {}".format(port_name, portchannel_name)) - - + 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) # # 'mirror_session' group ('config mirror_session ...') From 2e153a822feeae272e0eb965a0f02cf4c4a24023 Mon Sep 17 00:00:00 2001 From: madhu Pal Date: Tue, 12 Feb 2019 04:53:26 +0000 Subject: [PATCH 4/6] Fix merge conflicts Signed-off-by: madhu Pal --- config/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/config/main.py b/config/main.py index d9ed25074b..b52272891d 100755 --- a/config/main.py +++ b/config/main.py @@ -467,7 +467,6 @@ def portchannel_member(ctx): def add_portchannel_member(ctx, portchannel_name, port_name): """Add member to port channel""" db = ctx.obj['db'] -<<<<<<< HEAD port_channel = db.get_entry('PORTCHANNEL', portchannel_name) if get_interface_naming_mode() == "alias": port_name = interface_alias_to_name(port_name) From b3a5e7bc74e54cecd0a1c3e8c46c2fb10ce77b24 Mon Sep 17 00:00:00 2001 From: madhu Pal Date: Sun, 17 Feb 2019 00:17:10 +0000 Subject: [PATCH 5/6] [config/main.py] Confirm portchannel and member ports exist before attempting delete operations (#277) Signed-off-by: madhu Pal --- config/main.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/config/main.py b/config/main.py index b52272891d..afa729179a 100755 --- a/config/main.py +++ b/config/main.py @@ -451,6 +451,9 @@ def remove_portchannel(ctx, portchannel_name): """Remove port channel""" db = ctx.obj['db'] 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)) @@ -519,7 +522,10 @@ def del_portchannel_member(ctx, portchannel_name, port_name): ctx.fail("{} is not a member of {}".format(port_name, portchannel_name)) members.remove(port_name) - port_channel['members'] = members + if len(members) == 0: + del port_channel['members'] + else: + port_channel['members'] = members db.set_entry('PORTCHANNEL', portchannel_name, port_channel) db.set_entry('PORTCHANNEL_MEMBER', (portchannel_name, port_name), None) From 33b0979c5692d8c832882c4d7e27438b27d4513e Mon Sep 17 00:00:00 2001 From: madhu Pal Date: Thu, 7 Mar 2019 05:00:00 +0000 Subject: [PATCH 6/6] [config/main.py] addressed review comments and fixed indent issue with remove_portchannel Signed-off-by: madhu Pal --- config/main.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/config/main.py b/config/main.py index afa729179a..61fa84365c 100755 --- a/config/main.py +++ b/config/main.py @@ -454,7 +454,7 @@ def remove_portchannel(ctx, portchannel_name): 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) + db.set_entry('PORTCHANNEL', portchannel_name, None) else: ctx.fail("{} is not configured".format(portchannel_name)) @@ -481,13 +481,7 @@ def add_portchannel_member(ctx, portchannel_name, port_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) - 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)) + ctx.fail("{} is already a member of {}".format(port_name, portchannel_name)) members.append(port_name) port_channel['members'] = members @@ -513,13 +507,7 @@ def del_portchannel_member(ctx, portchannel_name, port_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)) + ctx.fail("{} is not a member of {}".format(port_name, portchannel_name)) members.remove(port_name) if len(members) == 0: