feat(netbox): primary IP: fallback to next IPs in the list of addresses

Fixes #6978
This commit is contained in:
Pierre Donias
2023-08-17 16:01:11 +02:00
committed by Julien Fontanet
parent 006f12f17f
commit 8ebc0dba4f
2 changed files with 28 additions and 8 deletions

View File

@@ -9,6 +9,7 @@
- [Backups] Ability to set the NBD mode per backup job in the UI instead of globally in the config file (PR [#6995](https://github.com/vatesfr/xen-orchestra/pull/6995))
- [Netbox] Better handle cases where the IP addresses reported by XAPI are malformed (PR [#6989](https://github.com/vatesfr/xen-orchestra/pull/6989))
- [Netbox] Fallback to other VIF's IPs when first VIF doesn't have an IP [#6978](https://github.com/vatesfr/xen-orchestra/issues/6978) (PR [#6989](https://github.com/vatesfr/xen-orchestra/pull/6989))
### Bug fixes

View File

@@ -699,6 +699,25 @@ class Netbox {
// Use the first IPs found in vm.addresses as the VMs' primary IPs in
// Netbox, both for IPv4 and IPv6
const getPrimaryIps = addresses => {
const primaryIps = {}
const keys = Object.keys(addresses)
let i = 0
while ((primaryIps.ipv4 === undefined || primaryIps.ipv6 === undefined) && i < keys.length) {
const key = keys[i++]
const ip = addresses[key]
if (key.includes('ipv4') && primaryIps.ipv4 === undefined) {
primaryIps.ipv4 = ip
}
if (key.includes('ipv6') && primaryIps.ipv6 === undefined) {
primaryIps.ipv6 = ip
}
}
return primaryIps
}
log.info("Setting VMs' primary IPs")
const vmsToUpdate2 = []
@@ -712,7 +731,8 @@ class Netbox {
const nbVmIps = filter(nbIps, { assigned_object: { virtual_machine: { id: nbVm.id } } })
const ipv4 = xoVm.addresses['0/ipv4/0']
const { ipv4, ipv6 } = getPrimaryIps(xoVm.addresses)
if (ipv4 === undefined && nbVm.primary_ip4 !== null) {
patch.primary_ip4 = null
} else if (ipv4 !== undefined) {
@@ -729,18 +749,17 @@ class Netbox {
}
}
const _ipv6 = xoVm.addresses['0/ipv6/0']
let ipv6
let compactIpv6
try {
// For IPv6, compare with the compact notation
ipv6 = _ipv6 && ipaddr.parse(_ipv6).toString()
compactIpv6 = ipv6 && ipaddr.parse(ipv6).toString()
} catch (error) {
log.error('Cannot parse IP address', { error, ip: _ipv6 })
log.error('Cannot parse IP address', { error, ip: ipv6 })
}
if (ipv6 === undefined && nbVm.primary_ip6 !== null) {
if (compactIpv6 === undefined && nbVm.primary_ip6 !== null) {
patch.primary_ip6 = null
} else if (ipv6 !== undefined) {
const nbIp = nbVmIps.find(nbIp => nbIp.address.split('/')[0] === ipv6)
} else if (compactIpv6 !== undefined) {
const nbIp = nbVmIps.find(nbIp => nbIp.address.split('/')[0] === compactIpv6)
if (nbIp === undefined && nbVm.primary_ip6 !== null) {
patch.primary_ip6 = null
} else if (nbIp !== undefined && nbIp.id !== nbVm.primary_ip6?.id) {