Skip to content

Commit ac05724

Browse files
committed
Fix setting primary IP
1 parent 2a09044 commit ac05724

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

module/sources/openstack/connection.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,11 +1016,18 @@ def add_host(self, obj):
10161016
}
10171017
pnic_data_dict["eth0"] = pnic_data
10181018

1019+
ip_addr = obj.host_ip
1020+
prefix = None
1021+
matched_prefix = self.return_longest_matching_prefix_for_ip(ip_interface(ip_addr))
1022+
if matched_prefix is not None:
1023+
prefix = matched_prefix.data["prefix"].prefixlen
1024+
ip_addr = f"{ip_addr}/{prefix}"
1025+
10191026
vnic_ips = dict()
10201027
vnic_ips["eth0"] = list()
1021-
vnic_ips["eth0"].append(obj.host_ip)
1028+
vnic_ips["eth0"].append(ip_addr)
10221029

1023-
host_primary_ip4 = obj.host_ip
1030+
host_primary_ip4 = ip_addr
10241031
host_primary_ip6 = None
10251032

10261033
# add host to inventory
@@ -1141,11 +1148,19 @@ def add_virtual_machine(self, obj):
11411148
count += 1
11421149
nic_ips[network] = list()
11431150
for address in addresses:
1144-
nic_ips[network].append(address["addr"])
1151+
ip_addr = address["addr"]
1152+
prefix = None
1153+
1154+
matched_prefix = self.return_longest_matching_prefix_for_ip(ip_interface(ip_addr))
1155+
if matched_prefix is not None:
1156+
prefix = matched_prefix.data["prefix"].prefixlen
1157+
ip_addr = f"{ip_addr}/{prefix}"
1158+
1159+
nic_ips[network].append(ip_addr)
11451160
if int(address["version"]) == 4:
1146-
vm_primary_ip4 = address["addr"]
1161+
vm_primary_ip4 = ip_addr
11471162
if int(address["version"]) == 6:
1148-
vm_primary_ip6 = address["addr"]
1163+
vm_primary_ip6 = ip_addr
11491164
full_name = unquote(f"vNIC{count} ({network})")
11501165
vm_nic_data = {
11511166
"name": full_name,
@@ -1154,8 +1169,10 @@ def add_virtual_machine(self, obj):
11541169
"description": full_name,
11551170
"enabled": True,
11561171
}
1157-
if ip_valid_to_add_to_netbox(address["addr"], self.permitted_subnets, full_name) is True:
1172+
if ip_valid_to_add_to_netbox(ip_addr, self.permitted_subnets, full_name) is True:
11581173
vm_nic_dict[network] = vm_nic_data
1174+
else:
1175+
log.debug(f"Virtual machine '{name}' address '{ip_addr}' is not valid to add. Skipping")
11591176

11601177
# add VM to inventory
11611178
self.add_device_vm_to_inventory(NBVM, object_data=vm_data, vnic_data=vm_nic_dict,

0 commit comments

Comments
 (0)