def allocate_floating_ip(request): """Allocate a floating IP.""" req = utils.get_json_body(request) floating_ip_dict = api.utils.get_attribute(req, "floatingip", required=True, attr_type=dict) userid = request.user_uniq project = floating_ip_dict.get("project", None) log.info('allocate_floating_ip user: %s request: %s', userid, req) # the network_pool is a mandatory field network_id = api.utils.get_attribute(floating_ip_dict, "floating_network_id", required=False, attr_type=(basestring, int)) if network_id is None: floating_ip = ips.create_floating_ip(userid, project=project) else: try: network_id = int(network_id) except ValueError: raise faults.BadRequest("Invalid networkd ID.") network = util.get_network(network_id, userid, for_update=True, non_deleted=True) address = api.utils.get_attribute(floating_ip_dict, "floating_ip_address", required=False, attr_type=basestring) floating_ip = ips.create_floating_ip(userid, network, address, project=project) log.info("User '%s' allocated floating IP '%s'", userid, floating_ip) request.serialization = "json" data = json.dumps({"floatingip": ip_to_dict(floating_ip)}) return HttpResponse(data, status=200)
def allocate_floating_ip(request): """Allocate a floating IP.""" req = utils.get_request_dict(request) floating_ip_dict = api.utils.get_attribute(req, "floatingip", required=True, attr_type=dict) userid = request.user_uniq log.info('allocate_floating_ip user: %s request: %s', userid, req) # the network_pool is a mandatory field network_id = api.utils.get_attribute(floating_ip_dict, "floating_network_id", required=False, attr_type=(basestring, int)) if network_id is None: floating_ip = ips.create_floating_ip(userid) else: try: network_id = int(network_id) except ValueError: raise faults.BadRequest("Invalid networkd ID.") network = util.get_network(network_id, userid, for_update=True, non_deleted=True) address = api.utils.get_attribute(floating_ip_dict, "floating_ip_address", required=False, attr_type=basestring) floating_ip = ips.create_floating_ip(userid, network, address) log.info("User '%s' allocated floating IP '%s'", userid, floating_ip) request.serialization = "json" data = json.dumps({"floatingip": ip_to_dict(floating_ip)}) return HttpResponse(data, status=200)
def allocate_floating_ip(request): """Allocate a floating IP.""" req = utils.get_json_body(request) log.debug("User: %s, Action: create_floating_ip, Request: %s", request.user_uniq, req) floating_ip_dict = api.utils.get_attribute(req, "floatingip", required=True, attr_type=dict) userid = request.user_uniq project = floating_ip_dict.get("project", None) shared_to_project = floating_ip_dict.get("shared_to_project", False) # the network_pool is a mandatory field network_id = api.utils.get_attribute(floating_ip_dict, "floating_network_id", required=False, attr_type=(basestring, int)) if network_id is None: floating_ip = \ ips.create_floating_ip(userid, project=project, shared_to_project=shared_to_project) else: try: network_id = int(network_id) except ValueError: raise faults.BadRequest("Invalid networkd ID.") network = util.get_network(network_id, userid, request.user_projects, for_update=True, non_deleted=True) address = api.utils.get_attribute(floating_ip_dict, "floating_ip_address", required=False, attr_type=basestring) floating_ip = \ ips.create_floating_ip(userid, network, address, project=project, shared_to_project=shared_to_project) log.info("User %s created floating IP %s, network %s, address %s", userid, floating_ip.id, floating_ip.network_id, floating_ip.address) request.serialization = "json" data = json.dumps({"floatingip": ip_to_dict(floating_ip)}) return HttpResponse(data, status=200)
def handle(self, *args, **options): if args: raise CommandError("Command doesn't accept any arguments") network_id = options['network_id'] address = options['address'] user = options['user'] project = options['project'] if not user: raise CommandError("'user' is required for floating IP creation") if not project: project = user if network_id is not None: network = common.get_resource("network", network_id) if network.deleted: raise CommandError("Network '%s' is deleted" % network.id) if not network.floating_ip_pool: raise CommandError("Network '%s' is not a floating IP pool." % network) credentials = Credentials(user) floating_ip = ips.create_floating_ip(credentials, project=project, network_id=network_id, address=address) self.stdout.write("Created floating IP '%s'.\n" % floating_ip)
def handle(self, *args, **options): if args: raise CommandError("Command doesn't accept any arguments") network_id = options['network_id'] address = options['address'] owner = options['owner'] if not owner: raise CommandError("'owner' is required for floating IP creation") if network_id is not None: network = util.get_network(network_id, owner, for_update=True, non_deleted=True) if not network.floating_ip_pool: raise CommandError("Network '%s' is not a floating IP pool." % network) else: network = None floating_ip = ips.create_floating_ip(userid=owner, network=network, address=address) self.stdout.write("Created floating IP '%s'.\n" % floating_ip)
def handle(self, *args, **options): if args: raise CommandError("Command doesn't accept any arguments") network_id = options['network_id'] address = options['address'] user = options['user'] if not user: raise CommandError("'user' is required for floating IP creation") if network_id is not None: network = common.get_resource("network", network_id, for_update=True) if network.deleted: raise CommandError("Network '%s' is deleted" % network.id) if not network.floating_ip_pool: raise CommandError("Network '%s' is not a floating IP pool." % network) else: network = None floating_ip = ips.create_floating_ip(userid=user, network=network, address=address) self.stdout.write("Created floating IP '%s'.\n" % floating_ip)
def test_create(self): """Test if a floating IP is created properly.""" with mocked_quotaholder(): ip = ips.create_floating_ip( self.credentials, network_id=self.network.id) self.assertEqual(len(self.network.ips.all()), 1) self.assertEqual(self.network.ips.all()[0], ip)
def test_create(self): """Test if a floating IP is created properly.""" with mocked_quotaholder(): ip = ips.create_floating_ip(self.credentials, network_id=self.network.id) self.assertEqual(len(self.network.ips.all()), 1) self.assertEqual(self.network.ips.all()[0], ip)
def allocate_floating_ip(request): """Allocate a floating IP.""" req = utils.get_json_body(request) credentials = request.credentials log.debug("User: %s, Action: create_floating_ip, Request: %s", credentials.userid, req) floating_ip_dict = api.utils.get_attribute(req, "floatingip", required=True, attr_type=dict) userid = credentials.userid project = floating_ip_dict.get("project", None) shared_to_project = floating_ip_dict.get("shared_to_project", False) # the network_pool is a mandatory field network_id = api.utils.get_attribute(floating_ip_dict, "floating_network_id", required=False, attr_type=(basestring, int)) if network_id is None: floating_ip = \ ips.create_floating_ip(credentials, project=project, shared_to_project=shared_to_project) else: try: network_id = int(network_id) except ValueError: raise faults.BadRequest("Invalid networkd ID.") address = api.utils.get_attribute(floating_ip_dict, "floating_ip_address", required=False, attr_type=basestring) floating_ip = \ ips.create_floating_ip(credentials, network_id, address, project=project, shared_to_project=shared_to_project) log.info("User %s created floating IP %s, network %s, address %s", userid, floating_ip.id, floating_ip.network_id, floating_ip.address) return HttpResponse(_floatingip_details_view(floating_ip), status=200)
def compute_allocate_floating_ip(request): """Allocate a floating IP.""" log.debug("User: %s, Action: compute_create_floating_ip, Request: %s", request.user_uniq, request) userid = request.user_uniq floating_ip = ips.create_floating_ip(userid) log.info("User %s created floating IP %s, network %s, address %s", userid, floating_ip.id, floating_ip.network_id, floating_ip.address) return HttpResponse(_compute_floatingip_details_view(floating_ip), status=200)
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 compute_allocate_floating_ip(request): """Allocate a floating IP.""" credentials = request.credentials userid = credentials.userid log.debug("User: %s, Action: compute_create_floating_ip, Request: %s", userid, request) floating_ip = ips.create_floating_ip(credentials) log.info("User %s created floating IP %s, network %s, address %s", userid, floating_ip.id, floating_ip.network_id, floating_ip.address) return HttpResponse( _compute_floatingip_details_view(floating_ip), status=200)