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

bgp_communities - Add support for replaced and overridden states #251

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- sonic_bgp_communities - Add support for replaced and overridden states (https://github.com/ansible-collections/dellemc.enterprise_sonic/pull/251).
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# -*- coding: utf-8 -*-
# Copyright 2020 Dell Inc. or its subsidiaries. All Rights Reserved
# Copyright 2023 Dell Inc. or its subsidiaries. All Rights Reserved
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

Expand Down Expand Up @@ -54,6 +54,6 @@ def __init__(self, **kwargs):
'default': 'standard',
'type': 'str'}},
'type': 'list'},
'state': {'choices': ['merged', 'deleted'],
'state': {'choices': ['merged', 'deleted', 'replaced', 'overridden'],
'default': 'merged',
'type': 'str'}} # pylint: disable=C0301

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,34 @@ def get_bgp_communities(self):
match = member_config['match-set-options']
permit_str = member_config.get('openconfig-bgp-policy-ext:action', None)
members = member_config.get("community-member", [])
result['name'] = name
result['name'] = str(name)
result['match'] = match
result['members'] = None
result['permit'] = False
if permit_str and permit_str == 'PERMIT':
result['permit'] = True
else:
result['permit'] = False
if members:
result['type'] = 'expanded' if 'REGEX' in members[0] else 'standard'
else:
result['type'] = ''
if result['type'] == 'expanded':
members = [':'.join(i.split(':')[1:]) for i in members]
result['local_as'] = True if "NO_EXPORT_SUBCONFED" in members else False
result['no_advertise'] = True if "NO_ADVERTISE" in members else False
result['no_export'] = True if "NO_EXPORT" in members else False
result['no_peer'] = True if "NOPEER" in members else False
result['members'] = {'regex': members}
members.sort()
result['members'] = {'regex': members}
else:
result['local_as'] = None
result['no_advertise'] = None
result['no_export'] = None
result['no_peer'] = None
for i in members:
if "NO_EXPORT_SUBCONFED" in i:
result['local_as'] = True
elif "NO_ADVERTISE" in i:
result['no_advertise'] = True
elif "NO_EXPORT" in i:
result['no_export'] = True
elif "NOPEER" in i:
result['no_peer'] = True

bgp_communities_configs.append(result)
# with open('/root/ansible_log.log', 'a+') as fp:
# fp.write('bgp_communities: ' + str(bgp_communities_configs) + '\n')
return bgp_communities_configs

def populate_facts(self, connection, ansible_facts, data=None):
Expand Down Expand Up @@ -128,17 +136,5 @@ def render_config(self, spec, conf):
:rtype: dictionary
:returns: The generated config
"""
config = deepcopy(spec)
try:
config['name'] = str(conf['name'])
config['members'] = conf['members']
config['match'] = conf['match']
config['type'] = conf['type']
config['permit'] = conf['permit']
except TypeError:
config['name'] = None
config['members'] = None
config['match'] = None
config['type'] = None
config['permit'] = None
return utils.remove_empties(config)

return conf
169 changes: 133 additions & 36 deletions plugins/modules/sonic_bgp_communities.py
ArunSaravananBalachandran marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2020 Dell Inc. or its subsidiaries. All Rights Reserved
# Copyright 2023 Dell Inc. or its subsidiaries. All Rights Reserved
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

Expand Down Expand Up @@ -52,7 +52,7 @@
required: True
type: str
description:
- Name of the BGP communitylist.
- Name of the BGP community-list.
type:
type: str
description:
Expand All @@ -67,6 +67,7 @@
type: bool
description:
- Permits or denies this community.
- Default value while adding a new community-list is C(False).
aann:
required: False
type: str
Expand Down Expand Up @@ -120,6 +121,8 @@
choices:
- merged
- deleted
- replaced
- overridden
default: merged
"""
EXAMPLES = """
Expand All @@ -130,28 +133,32 @@
#
# show bgp community-list
# Standard community list test: match: ANY
# 101
# 201
# Standard community list test1: match: ANY
# 301
# permit local-as
# permit no-peer
# Expanded community list test1: match: ANY
# deny 101
# deny 302

- name: Deletes BGP community member
- name: Delete a BGP community-list member
dellemc.enterprise_sonic.sonic_bgp_communities:
config:
- name: test
- name: test1
type: expanded
permit: false
members:
regex:
- 201
- 302
state: deleted

# After state:
# ------------
#
# show bgp community-list
# Standard community list test: match: ANY
# 101
# Standard community list test1: match: ANY
# 301
# permit local-as
# permit no-peer
# Expanded community list test1: match: ANY
# deny 101


# Using deleted
Expand All @@ -161,23 +168,26 @@
#
# show bgp community-list
# Standard community list test: match: ANY
# 101
# permit local-as
# permit no-peer
# Expanded community list test1: match: ANY
# 201
# deny 101
# deny 302

- name: Deletes a single BGP community
- name: Delete a single BGP community-list
dellemc.enterprise_sonic.sonic_bgp_communities:
config:
- name: test
members:
type: standard
state: deleted

# After state:
# ------------
#
# show bgp community-list
# Expanded community list test1: match: ANY
# 201
# deny 101
# deny 302


# Using deleted
Expand All @@ -187,11 +197,13 @@
#
# show bgp community-list
# Standard community list test: match: ANY
# 101
# permit local-as
# permit no-peer
# Expanded community list test1: match: ANY
# 201
# deny 101
# deny 302

- name: Delete All BGP communities
- name: Delete All BGP community-lists
dellemc.enterprise_sonic.sonic_bgp_communities:
config:
state: deleted
Expand All @@ -210,14 +222,17 @@
#
# show bgp community-list
# Standard community list test: match: ANY
# 101
# permit local-as
# permit no-peer
# Expanded community list test1: match: ANY
# 201
# deny 101
# deny 302

- name: Deletes all members in a single BGP community
- name: Delete all members in a single BGP community-list
dellemc.enterprise_sonic.sonic_bgp_communities:
config:
- name: test
- name: test1
type: expanded
members:
regex:
state: deleted
Expand All @@ -226,33 +241,115 @@
# ------------
#
# show bgp community-list
# Expanded community list test: match: ANY
# Expanded community list test1: match: ANY
# 201
# Standard community list test: match: ANY
# permit local-as
# permit no-peer


# Using merged

# Before state:
# -------------
#
# show bgp as-path-access-list
# AS path list test:
# show bgp community-list
# Expanded community list test1: match: ANY
# permit 101
# permit 302

- name: Adds 909.* to test as-path list
dellemc.enterprise_sonic.sonic_bgp_as_paths:
- name: Add a new BGP community-list
dellemc.enterprise_sonic.sonic_bgp_communities:
config:
- name: test
- name: test2
type: expanded
permit: true
members:
- 909.*
regex:
- 909
state: merged

# After state:
# ------------
#
# show bgp as-path-access-list
# AS path list test:
# members: 909.*
# show bgp community-list
# Expanded community list test1: match: ANY
# permit 101
# permit 302
# Expanded community list test2: match: ANY
# permit 909


# Using replaced

# Before state:
# -------------
#
# show bgp community-list
# Standard community list test: match: ANY
# permit local-as
# permit no-peer
# Expanded community list test1: match: ANY
# deny 101
# deny 302

- name: Replacing a single BGP community-list
dellemc.enterprise_sonic.sonic_bgp_communities:
config:
- name: test
type: expanded
members:
regex:
- 301
- name: test3
type: standard
no_advertise: true
no_peer: true
permit: false
match: ALL
state: replaced

# After state:
# ------------
#
# show bgp community-list
# Expanded community list test: match: ANY
# deny 301
# Expanded community list test1: match: ANY
# deny 101
# deny 302
# Standard community list test3: match: ALL
# deny no-advertise
# deny no-peer


# Using overridden

# Before state:
# -------------
#
# show bgp community-list
# Standard community list test: match: ANY
# permit local-as
# permit no-peer
# Expanded community list test1: match: ANY
# deny 101
# deny 302

- name: Override entire BGP community-lists
dellemc.enterprise_sonic.sonic_bgp_communities:
config:
- name: test3
type: expanded
members:
regex:
- 301
state: overridden

# After state:
# ------------
#
# show bgp community-list
# Expanded community list test3: match: ANY
# deny 301


"""
Expand Down
Loading
Loading