Fix nbhelper bugs
- Remove the main interface with public IP from "ethernet" block
- Remote "non-fabric" virtual interface routes
- Remove the netmask in UE routing source NAT address
REF: AETHER-2083
Change-Id: I5693055bd4a008391e80f8c7a0ebaa07d62f4544
diff --git a/scripts/nbhelper.py b/scripts/nbhelper.py
index ecdfc66..d91108f 100644
--- a/scripts/nbhelper.py
+++ b/scripts/nbhelper.py
@@ -143,12 +143,16 @@
# Get the Device and Virtual Machines from Netbox API
for device_data in netboxapi.dcim.devices.filter(tenant=self.tenant.slug):
- self.devices.append(NBDevice(device_data))
+ device = NBDevice(device_data)
+ device.tenant = self
+ self.devices.append(device)
for vm_data in netboxapi.virtualization.virtual_machines.filter(
tenant=self.tenant.slug
):
- self.vms.append(NBVirtualMachine(vm_data))
+ vm = NBVirtualMachine(vm_data)
+ vm.tenant = self
+ self.vms.append(vm)
def get_prefixes(self):
"""Get the IP Prefixes owns by current tenant"""
@@ -292,6 +296,12 @@
if prefix.reserved_ips:
return list(prefix.reserved_ips.values())
+ def check_ip_belonging(self, ip):
+ """
+ Check if an IP address is belonging to this prefix
+ """
+ return ip in netaddr.IPSet([self.data.prefix])
+
def parent(self):
"""
Get the parent prefix to this prefix
@@ -374,6 +384,7 @@
self.data = data
# The AssignedObject attributes
+ self.tenant = None
self.id = data.id
self.name = data.name
self.ips = dict()
@@ -573,6 +584,14 @@
if interface.mgmt_only is True or str(interface.type) == "Virtual":
continue
+ if not any(
+ [
+ p.check_ip_belonging(address)
+ for p in self.tenant.prefixes.values()
+ ]
+ ):
+ continue
+
self.netplan_config["ethernets"].setdefault(interface.name, {})
self.netplan_config["ethernets"][interface.name].setdefault(
"addresses", []
@@ -636,7 +655,8 @@
"addresses": [ip.address for ip in virtual_if_ips],
}
- if routes:
+ # Only the fabric virtual interface will need to route to other network segments
+ if routes and "fab" in virtual_if.name:
self.netplan_config["vlans"][virtual_if.name]["routes"] = routes
return self.netplan_config
@@ -695,7 +715,9 @@
):
for ip, device in prefix.aos.items():
if device.name == self.name:
- ret["ue_routing"]["snat_addr"] = ip
+ ret["ue_routing"]["snat_addr"] = str(
+ netaddr.IPNetwork(ip).ip
+ )
break
return ret