Exemplo n.º 1
0
    def handle(self, *args, **options):
        if not args:
            raise CommandError("Please provide a volume ID")

        force = options['force']
        message = "volumes" if len(args) > 1 else "volume"
        self.confirm_deletion(force, message, args)

        for volume_id in args:
            self.stdout.write("\n")
            try:
                volume = common.get_resource("volume", volume_id,
                                             for_update=True)
                volumes.delete(volume)

                wait = parse_bool(options["wait"])
                if volume.machine is not None:
                    volume.machine.task_job_id = volume.backendjobid
                    common.wait_server_task(volume.machine, wait,
                                            stdout=self.stdout)
                else:
                    self.stdout.write("Successfully removed volume %s\n"
                                      % volume)
            except CommandError as e:
                self.stdout.write("Error -- %s\n" % e.message)
Exemplo n.º 2
0
    def handle(self, *args, **options):
        if not args:
            raise CommandError("Please provide a volume ID")

        force = options['force']
        message = "volumes" if len(args) > 1 else "volume"
        self.confirm_deletion(force, message, args)

        for volume_id in args:
            self.stdout.write("\n")
            try:
                volume = common.get_resource("volume",
                                             volume_id,
                                             for_update=True)
                volumes.delete(volume)

                wait = parse_bool(options["wait"])
                if volume.machine is not None:
                    volume.machine.task_job_id = volume.backendjobid
                    common.wait_server_task(volume.machine,
                                            wait,
                                            stdout=self.stdout)
                else:
                    self.stdout.write("Successfully removed volume %s\n" %
                                      volume)
            except CommandError as e:
                self.stdout.write("Error -- %s\n" % e.message)
Exemplo n.º 3
0
    def test_delete(self, mrapi):
        # We can not deleted detached volumes
        vol = mf.VolumeFactory(machine=None, status="AVAILABLE")
        self.assertRaises(faults.BadRequest,
                          volumes.delete,
                          vol)

        vm = mf.VirtualMachineFactory(userid=vol.userid)
        # Also we cannot delete root volume
        vol.index = 0
        vol.machine = vm
        vol.status = "IN_USE"
        vol.save()
        self.assertRaises(faults.BadRequest,
                          volumes.delete,
                          vol)

        # We can delete everything else
        vol.index = 1
        mrapi().ModifyInstance.return_value = 42
        with mocked_quotaholder():
            volumes.delete(vol)
        self.assertEqual(vol.backendjobid, 42)
        args, kwargs = mrapi().ModifyInstance.call_args
        self.assertEqual(kwargs["instance"], vm.backend_vm_id)
        self.assertEqual(kwargs["disks"][0], ("remove",
                                              vol.backend_volume_uuid, {}))
Exemplo n.º 4
0
    def test_delete_attached(self, mrapi):
        # Test that we cannot delete root volume
        vm = mf.VirtualMachineFactory(userid=self.userid)
        vol = mf.VolumeFactory(machine=vm,
                               userid=self.userid,
                               status="IN_USE",
                               index=0,
                               size=self.size)
        self.assertRaises(faults.BadRequest,
                          volumes.delete,
                          vol.id,
                          credentials=self.credentials)

        # We can delete everything else
        vol.index = 1
        vol.save()
        mrapi().ModifyInstance.return_value = 42
        with mocked_quotaholder() as m:
            volumes.delete(vol.id, credentials=self.credentials)
        vol = Volume.objects.get(id=vol.id)
        self.assertEqual(vol.backendjobid, 42)
        args, kwargs = mrapi().ModifyInstance.call_args

        expected_commission = {
            (self.userid, "cyclades.disk"): -(self.size << 30)
        }
        self.assertCommissionEqual(m, expected_commission)
        self.assertEqual(vol.status, "DELETING")

        self.assertEqual(kwargs["instance"], vm.backend_vm_id)
        self.assertEqual(kwargs["disks"][0],
                         ("remove", vol.backend_volume_uuid, {}))
Exemplo n.º 5
0
    def test_delete_attached(self, mrapi):
        # Test that we cannot delete root volume
        vm = mf.VirtualMachineFactory(userid=self.userid)
        vol = mf.VolumeFactory(machine=vm, userid=self.userid, status="IN_USE",
                               index=0, size=self.size)
        self.assertRaises(faults.BadRequest,
                          volumes.delete,
                          vol.id, credentials=self.credentials)

        # We can delete everything else
        vol.index = 1
        vol.save()
        mrapi().ModifyInstance.return_value = 42
        with mocked_quotaholder() as m:
            volumes.delete(vol.id, credentials=self.credentials)
        vol = Volume.objects.get(id=vol.id)
        self.assertEqual(vol.backendjobid, 42)
        args, kwargs = mrapi().ModifyInstance.call_args

        expected_commission = {(self.userid, "cyclades.disk"):
                               - (self.size << 30)}
        self.assertCommissionEqual(m, expected_commission)
        self.assertEqual(vol.status, "DELETING")

        self.assertEqual(kwargs["instance"], vm.backend_vm_id)
        self.assertEqual(kwargs["disks"][0], ("remove",
                                              vol.backend_volume_uuid, {}))
Exemplo n.º 6
0
def delete_volume(request, volume_id):
    log.debug("delete_volume volume_id: %s", volume_id)

    volume = util.get_volume(request.user_uniq, volume_id, for_update=True,
                             non_deleted=True)
    volumes.delete(volume)

    return HttpResponse(status=202)
Exemplo n.º 7
0
def remove_volume(volume_id):
    try:
        objs = Volume.objects.select_for_update()
        volume = objs.get(id=volume_id)
        machine = volume.machine
        if not machine.deleted and machine.task != "DESTROY":
            volumes_logic.delete(volume)
        return True
    except BaseException:
        return False
Exemplo n.º 8
0
def delete_volume(request, volume_id):
    log.debug("delete_volume volume_id: %s", volume_id)

    volume = util.get_volume(request.user_uniq,
                             volume_id,
                             for_update=True,
                             non_deleted=True)
    volumes.delete(volume)

    return HttpResponse(status=202)
Exemplo n.º 9
0
def remove_volume(volume_id):
    try:
        objs = Volume.objects.select_for_update()
        volume = objs.get(id=volume_id)
        machine = volume.machine
        if not machine.deleted and machine.task != "DESTROY":
            volumes_logic.delete(volume)
        return True
    except BaseException:
        return False
Exemplo n.º 10
0
def remove_volume(volume_id):
    try:
        volume = Volume.objects.get(id=volume_id)
        machine = volume.machine
        if not machine or not machine.deleted and machine.task != "DESTROY":
            volumes_logic.delete(volume_id, credentials=credentials)
        return True
    except BaseException as e:
        logger.error("Error while removing volume '%s':" % volume_id)
        logger.exception(e)
        return False
Exemplo n.º 11
0
def remove_volume(volume_id):
    try:
        volume = Volume.objects.get(id=volume_id)
        machine = volume.machine
        if not machine or not machine.deleted and machine.task != "DESTROY":
            volumes_logic.delete(volume_id, credentials=credentials)
        return True
    except BaseException as e:
        logger.error(
            "Error while removing volume '%s':" % volume_id)
        logger.exception(e)
        return False
Exemplo n.º 12
0
def delete_volume(request, volume_id):
    credentials = request.credentials
    userid = credentials.userid

    log.debug("User: %s, Volume: %s Action: delete_volume",
              userid, volume_id)

    volumes.delete(volume_id, credentials)

    log.info("User %s deleted volume %s", userid, volume_id)

    return HttpResponse(status=202)
Exemplo n.º 13
0
def delete_volume(request, volume_id):
    log.debug("User: %s, Volume: %s Action: delete_volume", request.user_uniq,
              volume_id)

    volume = util.get_volume(request.user_uniq,
                             request.user_projects,
                             volume_id,
                             for_update=True,
                             non_deleted=True)
    volumes.delete(volume)

    log.info("User %s deleted volume %s", request.user_uniq, volume.id)

    return HttpResponse(status=202)
Exemplo n.º 14
0
    def test_delete_uninitialized(self, mrapi):
        """Test if we can delete an uninitialized volume."""
        # We cannot delete an uninitialized volume in invalid state
        vol = mf.VolumeFactory(machine=None, status="DELETING",
                               userid=self.userid, size=self.size)
        message = "Volume is in invalid state: %s" % vol.status
        with self.assertRaisesMessage(faults.BadRequest, message):
            volumes.delete(vol.id, credentials=self.credentials)

        # Test if the deletion and quota logic works as expected
        vol.status = "AVAILABLE"
        vol.save()
        with mocked_quotaholder() as m:
            vol = volumes.delete(vol.id, credentials=self.credentials)
        expected_commission = {(self.userid, "cyclades.disk"):
                               - (self.size << 30)}
        self.assertCommissionEqual(m, expected_commission)
        self.assertAcceptedSerial(m, vol.serial)
        self.assertEqual(vol.machine, None)
        self.assertEqual(vol.status, "DELETED")
        self.assertEqual(vol.deleted, True)
Exemplo n.º 15
0
    def test_delete_uninitialized(self, mrapi):
        """Test if we can delete an uninitialized volume."""
        # We cannot delete an uninitialized volume in invalid state
        vol = mf.VolumeFactory(machine=None,
                               status="DELETING",
                               userid=self.userid,
                               size=self.size)
        message = "Volume is in invalid state: %s" % vol.status
        with self.assertRaisesMessage(faults.BadRequest, message):
            volumes.delete(vol.id, credentials=self.credentials)

        # Test if the deletion and quota logic works as expected
        vol.status = "AVAILABLE"
        vol.save()
        with mocked_quotaholder() as m:
            vol = volumes.delete(vol.id, credentials=self.credentials)
        expected_commission = {
            (self.userid, "cyclades.disk"): -(self.size << 30)
        }
        self.assertCommissionEqual(m, expected_commission)
        self.assertAcceptedSerial(m, vol.serial)
        self.assertEqual(vol.machine, None)
        self.assertEqual(vol.status, "DELETED")
        self.assertEqual(vol.deleted, True)
Exemplo n.º 16
0
    def test_delete_detached(self, mrapi):
        """Test the deletion of a detached volume."""
        vol = mf.VolumeFactory(machine=None, status="AVAILABLE",
                               userid=self.userid, backendjobid=42,
                               volume_type=self.archip_vt)
        vm = mf.VirtualMachineFactory(userid=self.userid, helper=True,
                                      operstate="STOPPED",
                                      flavor__volume_type=self.archip_vt)

        mrapi().ModifyInstance.return_value = 42
        with mocked_quotaholder() as m:
            vol = volumes.delete(vol.id, credentials=self.credentials)

        self.assertNoCommission(m)
        self.assertEqual(vol.machine, vm)
        self.assertEqual(vol.status, "DELETING")
        self.assertEqual(vol.deleted, False)
Exemplo n.º 17
0
    def test_delete_detached(self, mrapi):
        """Test the deletion of a detached volume."""
        vol = mf.VolumeFactory(machine=None,
                               status="AVAILABLE",
                               userid=self.userid,
                               backendjobid=42,
                               volume_type=self.archip_vt)
        vm = mf.VirtualMachineFactory(userid=self.userid,
                                      helper=True,
                                      operstate="STOPPED",
                                      flavor__volume_type=self.archip_vt)

        mrapi().ModifyInstance.return_value = 42
        with mocked_quotaholder() as m:
            vol = volumes.delete(vol.id, credentials=self.credentials)

        self.assertNoCommission(m)
        self.assertEqual(vol.machine, vm)
        self.assertEqual(vol.status, "DELETING")
        self.assertEqual(vol.deleted, False)