Skip to content

Commit

Permalink
Remove disconnected IPs: #957
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Dec 12, 2019
1 parent 90e2462 commit 7f06ffd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 40 deletions.
24 changes: 24 additions & 0 deletions IM/VirtualMachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,3 +1079,27 @@ def get_ssh_command(self):
ssh.host))

return command

@staticmethod
def delete_public_nets(radl):
"""
Helper function to correctly delete references to public nets in an RADL
"""
nets_id = [net.id for net in radl.networks if net.isPublic()]
system = radl.systems[0]

i = 0
while system.getValue('net_interface.%d.connection' % i):
next_net = system.getValue('net_interface.%d.connection' % (i + 1))
next_dns = system.getValue('net_interface.%d.connection' % (i + 1))
f = system.getFeature("net_interface.%d.connection" % i)
if f.value in nets_id:
if next_net:
system.setValue('net_interface.%d.connection' % i, next_net)
system.setValue('net_interface.%d.dns_name' % i, next_dns)
else:
system.delValue('net_interface.%d.connection' % i)
system.delValue('net_interface.%d.dns_name' % i)
if system.getValue('net_interface.%d.ip' % i):
system.delValue('net_interface.%d.ip' % i)
i += 1
13 changes: 1 addition & 12 deletions IM/connectors/OCCI.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,18 +1193,7 @@ def manage_nics(self, vm, radl, auth_data, auth_header):

if success:
# Remove all public net connections in the Requested RADL
nets_id = [net.id for net in vm.requested_radl.networks if net.isPublic()]
system = vm.requested_radl.systems[0]

i = 0
while system.getValue('net_interface.%d.connection' % i):
f = system.getFeature("net_interface.%d.connection" % i)
if f.value in nets_id:
system.delValue('net_interface.%d.connection' % i)
if system.getValue('net_interface.%d.ip' % i):
system.delValue('net_interface.%d.ip' % i)
i += 1

vm.delete_public_nets(vm.requested_radl)
return True, ""
else:
return False, msg
Expand Down
32 changes: 5 additions & 27 deletions IM/connectors/OpenStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,7 @@ def update_system_info_from_instance(self, system, instance_type):
if instance_type:
LibCloudCloudConnector.update_system_info_from_instance(system, instance_type)
if instance_type.vcpus:
system.addFeature(
Feature("cpu.count", "=", instance_type.vcpus), conflict="me", missing="other")
system.addFeature(Feature("cpu.count", "=", instance_type.vcpus), conflict="me", missing="other")

@staticmethod
def get_ost_net(driver, name=None, netid=None):
Expand Down Expand Up @@ -1088,7 +1087,7 @@ def manage_elastic_ips(self, vm, node, public_ips):
while vm.getRequestedSystem().getValue("net_interface." + str(n) + ".connection"):
net_conn = vm.getRequestedSystem().getValue('net_interface.' + str(n) + '.connection')
net = vm.info.get_network_by_id(net_conn)
if net.isPublic():
if net and net.isPublic():
fixed_ip = vm.getRequestedSystem().getValue("net_interface." + str(n) + ".ip")
pool_name = net.getValue("provider_id")
requested_ips.append((fixed_ip, pool_name))
Expand Down Expand Up @@ -1610,6 +1609,7 @@ def alter_public_ips(self, vm, radl, auth_data):
current_public_ip = vm.getPublicIP()
new_has_public_ip = radl.hasPublicNet(vm.info.systems[0].name)
if new_has_public_ip and not current_public_ip:
self.log_info("Adding Public IP.")
for net in radl.networks:
if net.isPublic():
new_public_net = net.clone()
Expand All @@ -1622,34 +1622,12 @@ def alter_public_ips(self, vm, radl, auth_data):

if not new_has_public_ip and current_public_ip:
floating_ip = node.driver.ex_get_floating_ip(current_public_ip)
self.log_info("Removing Public IP: %s." % floating_ip)
if node.driver.ex_detach_floating_ip_from_node(node, floating_ip):
floating_ip.delete()

# Remove all public net connections in the Requested RADL
nets_id = [net.id for net in vm.requested_radl.networks if net.isPublic()]
system = vm.requested_radl.systems[0]

i = 0
while system.getValue('net_interface.%d.connection' % i):
f = system.getFeature("net_interface.%d.connection" % i)
if f.value in nets_id:
system.delValue('net_interface.%d.connection' % i)
if system.getValue('net_interface.%d.ip' % i):
system.delValue('net_interface.%d.ip' % i)
i += 1

# Also remove them from the info RADL
nets_id = [net.id for net in vm.info.networks if net.isPublic()]
system = vm.info.systems[0]

i = 0
while system.getValue('net_interface.%d.connection' % i):
f = system.getFeature("net_interface.%d.connection" % i)
if f.value in nets_id:
system.delValue('net_interface.%d.connection' % i)
if system.getValue('net_interface.%d.ip' % i):
system.delValue('net_interface.%d.ip' % i)
i += 1
vm.delete_public_nets(vm.requested_radl)

return True, ""
else:
Expand Down
4 changes: 3 additions & 1 deletion test/unit/connectors/OpenStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ def test_55_alter(self, add_elastic_ip_from_pool, get_driver):
node.id = "1"
node.state = "running"
node.extra = {'flavorId': 'small', 'vm_state': 'resized'}
node.public_ips = ['158.42.1.1']
node.public_ips = []
node.private_ips = ['10.0.0.1']
node.driver = driver
driver.ex_get_node_details.return_value = node
Expand All @@ -547,6 +547,7 @@ def test_55_alter(self, add_elastic_ip_from_pool, get_driver):
node_size.vcpus = 2
node_size.name = "small"
driver.list_sizes.return_value = [node_size]
driver.ex_get_size.return_value = node_size

driver.ex_resize.return_value = True
driver.ex_confirm_resize.return_value = True
Expand Down Expand Up @@ -618,6 +619,7 @@ def test_55_alter(self, add_elastic_ip_from_pool, get_driver):
fip.delete.return_value = True
driver.ex_get_floating_ip.return_value = fip
driver.ex_detach_floating_ip_from_node.return_value = True
node.public_ips = ['158.42.1.1']

success, _ = ost_cloud.alterVM(vm, new_radl, auth)
self.assertTrue(success, msg="ERROR: modifying VM info.")
Expand Down

0 comments on commit 7f06ffd

Please sign in to comment.