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

Add feature for bonding/vlan interface in the firewall_interfaces #246

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Expand Up @@ -433,10 +433,27 @@ def _compute_command(
:param opr: operation flag.
:return: generated command.
"""

#Append vif if interface contains a dot
vlan = None
interface_real = name
if '.' in name:
interface_real, vlan = name.split('.')

if vlan is not None:
interface_real = interface_real + " vif " + vlan

#if interface name is bondX, then it's a bonding interface. Everything else is an ethernet
if 'bond' in interface_real:
iftype = 'bonding'
else:
iftype = 'ethernet'

if not opr:
cmd = "delete interfaces ethernet" + " " + name + " firewall"
cmd = "delete interfaces " + iftype + " " + interface_real + " firewall"
else:
cmd = "set interfaces ethernet" + " " + name + " firewall"
cmd = "set interfaces " + iftype + " " + interface_real + " firewall"

if attrib:
cmd += " " + attrib
if afi:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ def populate_facts(self, connection, ansible_facts, data=None):
# using mock data instead
data = self.get_device_data(connection)
objs = []
interfaces = findall(
r"^set interfaces ethernet (?:\'*)(\S+)(?:\'*)", data, M
#Search all set from configuration with set interface, including ethernet and bonding
interfaces_raw = findall(
r"^set interfaces (?:ethernet|bonding) (\S+) firewall (?:\'*)", data, M
)
interfaces_vif = findall(
r"^set interfaces (?:ethernet|bonding) (\S+) vif (\d+)* firewall (?:\'*)", data, M
)
interfaces = interfaces_raw + interfaces_vif

if interfaces:
objs = self.get_names(data, interfaces)
ansible_facts["ansible_network_resources"].pop(
Expand Down Expand Up @@ -88,10 +94,22 @@ def get_names(self, data, interfaces):
"""
names = []
for r in set(interfaces):
int_regex = r" %s .+$" % r.strip("'")
cfg = findall(int_regex, data, M)
fi = self.render_config(cfg)
fi["name"] = r.strip("'")
myvif = None
if type(r) is tuple:
Copy link
Contributor

Choose a reason for hiding this comment

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

The recommended expression here is if isinstance(r, tuple):

myinterface, myvif = r
else:
myinterface = r
#Parse interfaces that contains string or tuple when the interface is in a vlan
if myvif is not None:
int_regex = r" %s vif \d+ firewall .+$" % myinterface
cfg = findall(int_regex, data, M)
fi = self.render_config(cfg)
fi["name"] = myinterface + '.' + myvif
else:
int_regex = r" %s firewall .+$" % myinterface
cfg = findall(int_regex, data, M)
fi = self.render_config(cfg)
fi["name"] = myinterface
names.append(fi)
if names:
names = sorted(names, key=lambda i: i["name"])
Expand Down