Example #1
0
def reservation_destroy(reservation_id):
    session = get_session()
    with session.begin():
        reservation = _reservation_get(session, reservation_id)

        if not reservation:
            # raise not found error
            raise db_exc.ClimateDBNotFound(id=reservation_id,
                                           model='Reservation')

        # XXX For some reason when running test_delete_host_reservation,
        # computehost_reservations is not a list but a ComputeHostReservation
        # object.
        if reservation.computehost_reservations is not None:
            if isinstance(reservation.computehost_reservations, list):
                for computehost_reservation in reservation.computehost_reservations:
                    computehost_reservation.soft_delete(session=session)
            else:
                reservation.computehost_reservations.soft_delete(
                    session=session)

        if reservation.computehost_allocations is not None:
            for computehost_allocation in reservation.computehost_allocations:
                computehost_allocation.soft_delete(session=session)

        session.delete(reservation)
Example #2
0
def host_destroy(host_id):
    session = get_session()
    with session.begin():
        host = _host_get(session, host_id)

        if not host:
            # raise not found error
            raise db_exc.ClimateDBNotFound(id=host_id, model='Host')

        session.delete(host)
Example #3
0
def event_destroy(event_id):
    session = get_session()
    with session.begin():
        event = _event_get(session, event_id)

        if not event:
            # raise not found error
            raise db_exc.ClimateDBNotFound(id=event_id, model='Event')

        session.delete(event)
Example #4
0
def lease_destroy(lease_id):
    session = get_session()
    with session.begin():
        lease = _lease_get(session, lease_id)

        if not lease:
            # raise not found error
            raise db_exc.ClimateDBNotFound(id=lease_id, model='Lease')

        session.delete(lease)
Example #5
0
def host_allocation_destroy(host_allocation_id):
    session = get_session()
    with session.begin():
        host_allocation = _host_allocation_get(session, host_allocation_id)

        if not host_allocation:
            # raise not found error
            raise db_exc.ClimateDBNotFound(id=host_allocation_id,
                                           model='ComputeHostAllocation')

        session.delete(host_allocation)
Example #6
0
def reservation_destroy(reservation_id):
    session = get_session()
    with session.begin():
        reservation = _reservation_get(session, reservation_id)

        if not reservation:
            # raise not found error
            raise db_exc.ClimateDBNotFound(id=reservation_id,
                                           model='Reservation')

        session.delete(reservation)
Example #7
0
def host_extra_capability_destroy(host_extra_capability_id):
    session = get_session()
    with session.begin():
        host_extra_capability = (_host_extra_capability_get(
            session, host_extra_capability_id))

        if not host_extra_capability:
            # raise not found error
            raise db_exc.ClimateDBNotFound(id=host_extra_capability_id,
                                           model='ComputeHostExtraCapability')

        session.delete(host_extra_capability)
Example #8
0
def lease_destroy(lease_id):
    session = get_session()
    with session.begin():
        lease = _lease_get(session, lease_id)

        if not lease:
            # raise not found error
            raise db_exc.ClimateDBNotFound(id=lease_id, model='Lease')

        for reservation in lease.reservations:
            reservation.soft_delete(session=session)

        for event in lease.events:
            event.soft_delete(session=session)

        lease.soft_delete(session=session)
Example #9
0
def host_get_all_by_queries(queries):
    """Returns hosts filtered by an array of queries.

    :param queries: array of queries "key op value" where op can be
        http://docs.sqlalchemy.org/en/rel_0_7/core/expression_api.html
            #sqlalchemy.sql.operators.ColumnOperators

    """
    hosts_query = model_query(models.ComputeHost, get_session())

    oper = {
        '<': ['lt', lambda a, b: a >= b],
        '>': ['gt', lambda a, b: a <= b],
        '<=': ['le', lambda a, b: a > b],
        '>=': ['ge', lambda a, b: a < b],
        '==': ['eq', lambda a, b: a != b],
        '!=': ['ne', lambda a, b: a == b],
    }

    hosts = []
    for query in queries:
        try:
            key, op, value = query.split(' ', 3)
        except ValueError:
            raise db_exc.ClimateDBInvalidFilter(query_filter=query)

        column = getattr(models.ComputeHost, key, None)
        if column:
            if op == 'in':
                filt = column.in_(value.split(','))
            else:
                if op in oper:
                    op = oper[op][0]
                try:
                    attr = filter(lambda e: hasattr(column, e % op),
                                  ['%s', '%s_', '__%s__'])[0] % op
                except IndexError:
                    raise db_exc.ClimateDBInvalidFilterOperator(
                        filter_operator=op)

                if value == 'null':
                    value = None

                filt = getattr(column, attr)(value)

            hosts_query = hosts_query.filter(filt)
        else:
            # looking for extra capabilities matches
            extra_filter = model_query(
                models.ComputeHostExtraCapability, get_session()).filter(
                    models.ComputeHostExtraCapability.capability_name ==
                    key).all()
            if not extra_filter:
                raise db_exc.ClimateDBNotFound(
                    id=key, model='ComputeHostExtraCapability')

            for host in extra_filter:
                if op in oper and oper[op][1](host.capability_value, value):
                    hosts.append(host.computehost_id)
                elif op not in oper:
                    msg = 'Operator %s for extra capabilities not implemented'
                    raise NotImplementedError(msg % op)

    return hosts_query.filter(~models.ComputeHost.id.in_(hosts)).all()