Skip to content

Commit

Permalink
Assign secondary mgmt IPs in access device init
Browse files Browse the repository at this point in the history
This updates init_access_device_step1 to fetch, reserv and assign
secondary management IPs from any dual-stack Mgmtdomain.
  • Loading branch information
lunkwill42 committed Mar 31, 2023
1 parent fd8c026 commit 1a69c5a
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/cnaas_nms/devicehandler/init_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,25 +496,48 @@ def init_access_device_step1(
# Select a new management IP for the device
ReservedIP.clean_reservations(session, device=dev)
session.commit()
mgmt_ip = mgmtdomain.find_free_mgmt_ip(session)
mgmt_ip = mgmtdomain.find_free_primary_mgmt_ip(session)
if not mgmt_ip:
raise Exception(
"Could not find free management IP for management domain {}/{}".format(
"Could not find free primary management IP for management domain {}/{}".format(
mgmtdomain.id, mgmtdomain.description
)
)
reserved_ip = ReservedIP(device=dev, ip=mgmt_ip)
session.add(reserved_ip)

secondary_mgmt_ip = None
if mgmtdomain.is_dual_stack:
secondary_mgmt_ip = mgmtdomain.find_free_secondary_mgmt_ip()
if not secondary_mgmt_ip:
raise Exception(
"Could not find free secondary management IP for management domain {}/{}".format(
mgmtdomain.id, mgmtdomain.description
)
)
reserved_ip = ReservedIP(device=dev, ip=secondary_mgmt_ip)
session.add(reserved_ip)

session.commit()
# Populate variables for template rendering
mgmt_gw_ipif = ip_interface(mgmtdomain.ipv6_gw or mgmtdomain.ipv4_gw)
mgmt_gw_ipif = ip_interface(mgmtdomain.primary_gw)
mgmt_variables = {
"mgmt_ipif": str(ip_interface("{}/{}".format(mgmt_ip, mgmt_gw_ipif.network.prefixlen))),
"mgmt_ip": str(mgmt_ip),
"mgmt_prefixlen": int(mgmt_gw_ipif.network.prefixlen),
"mgmt_vlan_id": mgmtdomain.vlan,
"mgmt_gw": mgmt_gw_ipif.ip,
}
if secondary_mgmt_ip:
secondary_mgmt_gw_ipif = ip_interface(mgmtdomain.secondary_gw)
mgmt_variables.update(
{
"secondary_mgmt_ipif": str(
ip_interface("{}/{}".format(secondary_mgmt_ip, secondary_mgmt_gw_ipif.network.prefixlen))
),
"secondary_mgmt_ip": secondary_mgmt_ip,
}
)
device_variables = populate_device_vars(session, dev, new_hostname, DeviceType.ACCESS)
device_variables = {**device_variables, **mgmt_variables}
# Update device state
Expand Down Expand Up @@ -555,17 +578,19 @@ def init_access_device_step1(
linknets = dev.get_linknets(session)
for linknet in linknets:
session.delete(linknet)
reserved_ip = session.query(ReservedIP).filter(ReservedIP.device == dev).one_or_none()
if reserved_ip:
reserved_ips = session.query(ReservedIP).filter(ReservedIP.device == dev).all()
for reserved_ip in reserved_ips:
session.delete(reserved_ip)
return NornirJobResult(nrresult=nrresult)

dev.management_ip = device_variables["mgmt_ip"]
if secondary_mgmt_ip:
dev.secondary_management_ip = secondary_mgmt_ip
dev.state = DeviceState.INIT
dev.device_type = DeviceType.ACCESS
# Remove the reserved IP since it's now saved in the device database instead
reserved_ip = session.query(ReservedIP).filter(ReservedIP.device == dev).one_or_none()
if reserved_ip:
reserved_ips = session.query(ReservedIP).filter(ReservedIP.device == dev).all()
for reserved_ip in reserved_ips:
session.delete(reserved_ip)

# Plugin hook, allocated IP
Expand Down

0 comments on commit 1a69c5a

Please sign in to comment.