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 7 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 @@ -49,11 +49,11 @@ def __init__(self, **kwargs):
'no_advertise': {'type': 'bool'},
'no_export': {'type': 'bool'},
'no_peer': {'type': 'bool'},
'permit': {'type': 'bool'},
'permit': {'type': 'bool', 'default': False},
ArunSaravananBalachandran marked this conversation as resolved.
Show resolved Hide resolved
'type': {'choices': ['standard', 'expanded'],
'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'] = False
result['no_advertise'] = False
result['no_export'] = False
result['no_peer'] = False
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,4 @@ 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
99 changes: 87 additions & 12 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 @@ -67,6 +67,7 @@
type: bool
description:
- Permits or denies this community.
default: False
aann:
required: False
type: str
Expand Down Expand Up @@ -120,6 +121,8 @@
choices:
- merged
- deleted
- replaced
- overridden
default: merged
"""
EXAMPLES = """
Expand All @@ -138,10 +141,11 @@
- name: Deletes BGP community member
dellemc.enterprise_sonic.sonic_bgp_communities:
config:
- name: test
- name: test1
type: expanded
members:
regex:
- 201
- 302
state: deleted

# After state:
Expand All @@ -150,8 +154,10 @@
# show bgp community-list
# Standard community list test: match: ANY
# 101
# 201
# Standard community list test1: match: ANY
# 301
# 302


# Using deleted
Expand All @@ -169,6 +175,7 @@
dellemc.enterprise_sonic.sonic_bgp_communities:
config:
- name: test
type: standard
members:
state: deleted

Expand Down Expand Up @@ -218,6 +225,7 @@
dellemc.enterprise_sonic.sonic_bgp_communities:
config:
- name: test
type: standard
members:
regex:
state: deleted
Expand All @@ -236,23 +244,90 @@
# Before state:
# -------------
#
# show bgp as-path-access-list
# AS path list test:
# show bgp community-list
# Standard community list test: match: ANY
# 201

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

# After state:
# ------------
#
# show bgp as-path-access-list
# AS path list test:
# members: 909.*
# show bgp community-list
# Standard community list test: match: ANY
# 201
# Expanded community list test1: match: ANY
# 909


# Using replaced

# Before state:
# -------------
#
# show bgp community-list
# Standard community list test: match: ANY
# 101
# 102
# Expanded community list test1: match: ANY
# 201

- name: Replacing a single BGP community
dellemc.enterprise_sonic.sonic_bgp_communities:
config:
- name: test
type: expanded
members:
regex:
- 301
state: replaced

# After state:
# ------------
#
# show bgp community-list
# Expanded community list test: match: ANY
# 301
# Expanded community list test1: match: ANY
# 201


# Using overridden

# Before state:
# -------------
#
# show bgp community-list
# Standard community list test: match: ANY
# 101
# 102
# Expanded community list test1: match: ANY
# 201

- name: Override the entire list of BGP communities.
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
# 301


"""
Expand Down
Loading