def release_floating_ip(request, floating_ip_id): """Release a floating IP.""" userid = request.user_uniq log.info("release_floating_ip '%s'. User '%s'.", floating_ip_id, userid) floating_ip = util.get_floating_ip_by_id(userid, floating_ip_id, for_update=True) ips.delete_floating_ip(floating_ip) log.info("User '%s' released IP '%s", userid, floating_ip) return HttpResponse(status=204)
def release_floating_ip(request, floating_ip_id): """Release a floating IP.""" credentials = request.credentials log.debug("User: %s, Floating IP: %s, Action: delete", credentials.userid, floating_ip_id) ips.delete_floating_ip(floating_ip_id, credentials) log.info("User %s deleted floating IP %s", credentials.userid, floating_ip_id) return HttpResponse(status=204)
def remove_ip(ip_id): try: ip = IPAddress.objects.select_for_update().get(id=ip_id) port_id = ip.nic_id if port_id: delete_port(port_id) ip = wait_for_ip(ip_id) logic_ips.delete_floating_ip(ip) return True except BaseException as e: logger.error("Error while removing IP '%s':" % ip_id) logger.exception(e) return False
def remove_ip(ip_id): try: ip = IPAddress.objects.select_for_update().get(id=ip_id) port_id = ip.nic_id if port_id: r = delete_port(port_id) if not r: return False ip = wait_for_ip(ip_id) logic_ips.delete_floating_ip(ip) return True except BaseException: return False
def remove_ip(ip_id): try: ip = IPAddress.objects.get(id=ip_id) port_id = ip.nic_id if port_id: delete_port(port_id) wait_for_ip(ip_id) logic_ips.delete_floating_ip(ip_id, credentials) return True except BaseException as e: logger.error( "Error while removing IP '%s':" % ip_id) logger.exception(e) return False
def test_delete(self): """Test if the delete action succeeds/fails properly.""" # Create a floating IP and force-attach it to a NIC instance. vm = mfactory.VirtualMachineFactory() nic = mfactory.NetworkInterfaceFactory(network=self.network, machine=vm) with mocked_quotaholder(): ip = ips.create_floating_ip( self.credentials, network_id=self.network.id) ip.nic = nic ip.save() # Test 1 - Check if we can delete an IP attached to a VM. # # The validate function and the action should both fail with the # following message. expected_msg = "IP '{}' is used by server '{}'".format(ip.id, vm.id) # Verify that the validate function fails in silent mode. res, msg = ips.validate_ip_action(ip, "DELETE", silent=True) self.assertFalse(res) self.assertEqual(msg, expected_msg) # Verify that the validate function fails in non-silent mode. with self.assertRaises(faults.Conflict) as cm: ips.validate_ip_action(ip, "DELETE", silent=False) self.assertEqual(cm.exception.message, expected_msg) # Verify that the delete action fails with exception. with mocked_quotaholder(): with self.assertRaises(faults.Conflict) as cm: ips.delete_floating_ip(ip.id, self.credentials) self.assertEqual(cm.exception.message, expected_msg) # Test 2 - Check if we can delete a free IP. # # Force-detach IP from NIC. ip.nic = None ip.save() # Verify that the validate function passes in silent mode. res, _ = ips.validate_ip_action(ip, "DELETE", silent=True) self.assertTrue(res) # Verify that the delete action succeeds. with mocked_quotaholder(): ips.delete_floating_ip(ip.id, self.credentials) with self.assertRaises(ObjectDoesNotExist): IPAddress.objects.get(id=ip.id)
def test_delete(self): """Test if the delete action succeeds/fails properly.""" # Create a floating IP and force-attach it to a NIC instance. vm = mfactory.VirtualMachineFactory() nic = mfactory.NetworkInterfaceFactory(network=self.network, machine=vm) with mocked_quotaholder(): ip = ips.create_floating_ip(self.credentials, network_id=self.network.id) ip.nic = nic ip.save() # Test 1 - Check if we can delete an IP attached to a VM. # # The validate function and the action should both fail with the # following message. expected_msg = "IP '{}' is used by server '{}'".format(ip.id, vm.id) # Verify that the validate function fails in silent mode. res, msg = ips.validate_ip_action(ip, "DELETE", silent=True) self.assertFalse(res) self.assertEqual(msg, expected_msg) # Verify that the validate function fails in non-silent mode. with self.assertRaises(faults.Conflict) as cm: ips.validate_ip_action(ip, "DELETE", silent=False) self.assertEqual(cm.exception.message, expected_msg) # Verify that the delete action fails with exception. with mocked_quotaholder(): with self.assertRaises(faults.Conflict) as cm: ips.delete_floating_ip(ip.id, self.credentials) self.assertEqual(cm.exception.message, expected_msg) # Test 2 - Check if we can delete a free IP. # # Force-detach IP from NIC. ip.nic = None ip.save() # Verify that the validate function passes in silent mode. res, _ = ips.validate_ip_action(ip, "DELETE", silent=True) self.assertTrue(res) # Verify that the delete action succeeds. with mocked_quotaholder(): ips.delete_floating_ip(ip.id, self.credentials) with self.assertRaises(ObjectDoesNotExist): IPAddress.objects.get(id=ip.id)
def remove_ip(ip_id): try: ip = IPAddress.objects.select_for_update().get(id=ip_id) port_id = ip.nic_id if port_id: objs = NetworkInterface.objects.select_for_update() port = objs.get(id=port_id) servers.delete_port(port) if port.machine: wait_server_job(port.machine) ip = wait_for_ip(ip_id) logic_ips.delete_floating_ip(ip) return True except BaseException: return False
def release_floating_ip(request, floating_ip_id): """Release a floating IP.""" userid = request.user_uniq log.debug("User: %s, Floating IP: %s, Action: delete", request.user_uniq, floating_ip_id) floating_ip = util.get_floating_ip_by_id(userid, request.user_projects, floating_ip_id, for_update=True) ips.delete_floating_ip(floating_ip) log.info("User %s deleted floating IP %s", request.user_uniq, floating_ip.id) return HttpResponse(status=204)
def handle(self, *args, **options): if not args: raise CommandError("Please provide a floating-ip ID") force = options['force'] message = "floating IPs" if len(args) > 1 else "floating IP" self.confirm_deletion(force, message, args) credentials = Credentials("snf-manage", is_admin=True) for floating_ip_id in args: self.stdout.write("\n") try: ips.delete_floating_ip(floating_ip_id, credentials) self.stdout.write("Deleted floating IP '%s'.\n" % floating_ip_id) except CommandError as e: self.stdout.write("Error -- %s\n" % e.message)
def handle(self, *args, **options): if not args: raise CommandError("Please provide a floating-ip ID") force = options['force'] message = "floating IPs" if len(args) > 1 else "floating IP" self.confirm_deletion(force, message, args) for floating_ip_id in args: self.stdout.write("\n") try: floating_ip = common.get_floating_ip_by_id(floating_ip_id, for_update=True) ips.delete_floating_ip(floating_ip) self.stdout.write("Deleted floating IP '%s'.\n" % floating_ip_id) except CommandError as e: self.stdout.write("Error -- %s\n" % e.message)