Ejemplo n.º 1
0
def reassign_volume(volume, project, shared_to_project):
    if volume.index == 0:
        raise faults.Conflict("Cannot reassign: %s is a system volume" %
                              volume.id)

    server = volume.machine
    if server is not None:
        commands.validate_server_action(server, "REASSIGN")

    if volume.project == project:
        if volume.shared_to_project != shared_to_project:
            log.info("%s volume %s to project %s",
                "Sharing" if shared_to_project else "Unsharing",
                volume, project)
            volume.shared_to_project = shared_to_project
            volume.save()
    else:
        action_fields = {"to_project": project, "from_project": volume.project}
        log.info("Reassigning volume %s from project %s to %s, shared: %s",
                volume, volume.project, project, shared_to_project)
        volume.project = project
        volume.shared_to_project = shared_to_project
        volume.save()
        quotas.issue_and_accept_commission(volume, action="REASSIGN",
                                           action_fields=action_fields)
    return volume
Ejemplo n.º 2
0
def reassign_volume(volume_id, project, shared_to_project, credentials,
                    atomic_context=None):
    volume = util.get_volume(credentials,
                             volume_id, for_update=True, non_deleted=True)

    if not credentials.is_admin and credentials.userid != volume.userid:
        raise faults.Forbidden("Action 'reassign' is allowed only to the owner"
                               " of the volume.")

    if volume.index == 0:
        raise faults.Conflict("Cannot reassign: %s is a system volume" %
                              volume.id)

    server = volume.machine
    if server is not None:
        commands.validate_server_action(server, "REASSIGN")

    if volume.project == project:
        if volume.shared_to_project != shared_to_project:
            log.info("%s volume %s to project %s",
                "Sharing" if shared_to_project else "Unsharing",
                volume, project)
            volume.shared_to_project = shared_to_project
            volume.save()
    else:
        action_fields = {"to_project": project, "from_project": volume.project}
        log.info("Reassigning volume %s from project %s to %s, shared: %s",
                volume, volume.project, project, shared_to_project)
        volume.project = project
        volume.shared_to_project = shared_to_project
        volume.save()
        quotas.issue_and_accept_commission(volume, action="REASSIGN",
                                           action_fields=action_fields,
                                           atomic_context=atomic_context)
    return volume
Ejemplo n.º 3
0
def reassign_volume(volume, project, shared_to_project):
    if volume.index == 0:
        raise faults.Conflict("Cannot reassign: %s is a system volume" %
                              volume.id)

    server = volume.machine
    if server is not None:
        commands.validate_server_action(server, "REASSIGN")

    if volume.project == project:
        if volume.shared_to_project != shared_to_project:
            log.info("%s volume %s to project %s",
                "Sharing" if shared_to_project else "Unsharing",
                volume, project)
            volume.shared_to_project = shared_to_project
            volume.save()
    else:
        action_fields = {"to_project": project, "from_project": volume.project}
        log.info("Reassigning volume %s from project %s to %s, shared: %s",
                volume, volume.project, project, shared_to_project)
        volume.project = project
        volume.shared_to_project = shared_to_project
        volume.save()
        quotas.issue_and_accept_commission(volume, action="REASSIGN",
                                           action_fields=action_fields)
    return volume
Ejemplo n.º 4
0
def reassign(vm, project, shared_to_project):
    commands.validate_server_action(vm, "REASSIGN")

    if vm.project == project:
        if vm.shared_to_project != shared_to_project:
            log.info("%s VM %s to project %s",
                     "Sharing" if shared_to_project else "Unsharing", vm,
                     project)
            vm.shared_to_project = shared_to_project
            vm.volumes.filter(index=0, deleted=False)\
                      .update(shared_to_project=shared_to_project)
            vm.save()
    else:
        action_fields = {"to_project": project, "from_project": vm.project}
        log.info("Reassigning VM %s from project %s to %s, shared: %s", vm,
                 vm.project, project, shared_to_project)
        if not (vm.backend.public
                or vm.backend.projects.filter(project=project).exists()):
            raise faults.BadRequest("Cannot reassign VM. Target project "
                                    "doesn't have access to the VM's backend.")
        vm.project = project
        vm.shared_to_project = shared_to_project
        vm.save()
        vm.volumes.filter(index=0, deleted=False).update(
            project=project, shared_to_project=shared_to_project)
        quotas.issue_and_accept_commission(vm,
                                           action="REASSIGN",
                                           action_fields=action_fields)
    return vm
Ejemplo n.º 5
0
def reassign(vm, project):
    commands.validate_server_action(vm, "REASSIGN")
    action_fields = {"to_project": project, "from_project": vm.project}
    log.info("Reassigning VM %s from project %s to %s",
             vm, vm.project, project)
    vm.project = project
    vm.save()
    vm.volumes.filter(index=0, deleted=False).update(project=project)
    quotas.issue_and_accept_commission(vm, action="REASSIGN",
                                       action_fields=action_fields)
Ejemplo n.º 6
0
def reassign(vm, project):
    commands.validate_server_action(vm, "REASSIGN")
    action_fields = {"to_project": project, "from_project": vm.project}
    log.info("Reassigning VM %s from project %s to %s", vm, vm.project,
             project)
    vm.project = project
    vm.save()
    vm.volumes.filter(index=0, deleted=False).update(project=project)
    quotas.issue_and_accept_commission(vm,
                                       action="REASSIGN",
                                       action_fields=action_fields)
Ejemplo n.º 7
0
def check_vm_action(action):
    if action == 'SUSPEND':
        return lambda vm: not vm.suspended and not vm.deleted
    elif action == 'UNSUSPEND':
        return lambda vm: vm.suspended and not vm.deleted
    else:
        return lambda vm: validate_server_action(vm, action)
Ejemplo n.º 8
0
def reassign_volume(volume, project):
    if volume.index == 0:
        raise faults.Conflict("Cannot reassign: %s is a system volume" %
                              volume.id)
    if volume.machine_id is not None:
        server = util.get_server(volume.userid, volume.machine_id,
                                 for_update=True, non_deleted=True,
                                 exception=faults.BadRequest)
        commands.validate_server_action(server, "REASSIGN")
    action_fields = {"from_project": volume.project, "to_project": project}
    log.info("Reassigning volume %s from project %s to %s",
             volume.id, volume.project, project)
    volume.project = project
    volume.save()
    quotas.issue_and_accept_commission(volume, action="REASSIGN",
                                       action_fields=action_fields)
Ejemplo n.º 9
0
def reassign_volume(volume, project):
    if volume.index == 0:
        raise faults.Conflict("Cannot reassign: %s is a system volume" %
                              volume.id)
    if volume.machine_id is not None:
        server = util.get_server(volume.userid,
                                 volume.machine_id,
                                 for_update=True,
                                 non_deleted=True,
                                 exception=faults.BadRequest)
        commands.validate_server_action(server, "REASSIGN")
    action_fields = {"from_project": volume.project, "to_project": project}
    log.info("Reassigning volume %s from project %s to %s", volume.id,
             volume.project, project)
    volume.project = project
    volume.save()
    quotas.issue_and_accept_commission(volume,
                                       action="REASSIGN",
                                       action_fields=action_fields)
Ejemplo n.º 10
0
def reassign(server_id,
             project,
             shared_to_project,
             credentials=None,
             atomic_context=None):
    vm = util.get_vm(server_id,
                     credentials,
                     for_update=True,
                     non_deleted=True,
                     non_suspended=True)
    commands.validate_server_action(vm, "REASSIGN")

    if vm.project == project:
        if vm.shared_to_project != shared_to_project:
            log.info("%s VM %s to project %s",
                     "Sharing" if shared_to_project else "Unsharing", vm,
                     project)
            vm.shared_to_project = shared_to_project
            vm.volumes.filter(index=0, deleted=False)\
                      .update(shared_to_project=shared_to_project)
            vm.save()
    else:
        action_fields = {"to_project": project, "from_project": vm.project}
        log.info("Reassigning VM %s from project %s to %s, shared: %s", vm,
                 vm.project, project, shared_to_project)
        if not (vm.backend.public
                or vm.backend.projects.filter(project=project).exists()):
            raise faults.Forbidden("Cannot reassign VM. Target project "
                                   "doesn't have access to the VM's backend.")
        if not FlavorPolicy.has_access_to_flavor(
                vm.flavor, credentials, project=project):
            raise faults.Forbidden("Cannot reassign VM. Target project "
                                   "doesn't have access to the VM's flavor.")
        vm.project = project
        vm.shared_to_project = shared_to_project
        vm.save()
        vm.volumes.filter(index=0, deleted=False)\
                  .update(project=project, shared_to_project=shared_to_project)
        quotas.issue_and_accept_commission(vm,
                                           action="REASSIGN",
                                           action_fields=action_fields,
                                           atomic_context=atomic_context)
    return vm
Ejemplo n.º 11
0
def reassign(vm, project, shared_to_project):
    commands.validate_server_action(vm, "REASSIGN")

    if vm.project == project:
        if vm.shared_to_project != shared_to_project:
            log.info("%s VM %s to project %s",
                "Sharing" if shared_to_project else "Unsharing",
                vm, project)
            vm.shared_to_project = shared_to_project
            vm.volumes.filter(index=0, deleted=False)\
                      .update(shared_to_project=shared_to_project)
            vm.save()
    else:
        action_fields = {"to_project": project, "from_project": vm.project}
        log.info("Reassigning VM %s from project %s to %s, shared: %s",
                vm, vm.project, project, shared_to_project)
        vm.project = project
        vm.shared_to_project = shared_to_project
        vm.save()
        vm.volumes.filter(index=0, deleted=False).update(project=project,
            shared_to_project=shared_to_project)
        quotas.issue_and_accept_commission(vm, action="REASSIGN",
                action_fields=action_fields)
    return vm
Ejemplo n.º 12
0
def reassign(server_id, project, shared_to_project, credentials=None,
             atomic_context=None):
    vm = util.get_vm(server_id, credentials,
                     for_update=True, non_deleted=True, non_suspended=True)
    commands.validate_server_action(vm, "REASSIGN")

    if vm.project == project:
        if vm.shared_to_project != shared_to_project:
            log.info("%s VM %s to project %s",
                     "Sharing" if shared_to_project else "Unsharing",
                     vm, project)
            vm.shared_to_project = shared_to_project
            vm.volumes.filter(index=0, deleted=False)\
                      .update(shared_to_project=shared_to_project)
            vm.save()
    else:
        action_fields = {"to_project": project, "from_project": vm.project}
        log.info("Reassigning VM %s from project %s to %s, shared: %s",
                 vm, vm.project, project, shared_to_project)
        if not (vm.backend.public or
                vm.backend.projects.filter(project=project).exists()):
            raise faults.Forbidden("Cannot reassign VM. Target project "
                                   "doesn't have access to the VM's backend.")
        if not FlavorPolicy.has_access_to_flavor(vm.flavor, credentials,
                                                 project=project):
            raise faults.Forbidden("Cannot reassign VM. Target project "
                                   "doesn't have access to the VM's flavor.")
        vm.project = project
        vm.shared_to_project = shared_to_project
        vm.save()
        vm.volumes.filter(index=0, deleted=False)\
                  .update(project=project, shared_to_project=shared_to_project)
        quotas.issue_and_accept_commission(vm, action="REASSIGN",
                                           action_fields=action_fields,
                                           atomic_context=atomic_context)
    return vm
Ejemplo n.º 13
0
def check_vm_action(action):
    return lambda vm: validate_server_action(vm, action)
Ejemplo n.º 14
0
def check_vm_action(action):
    return lambda vm: validate_server_action(vm, action)