def model_query(context, *args, **kwargs):
    """Query helper that accounts for context's `read_deleted` field.

    :param context: context to query under
    :param session: if present, the session to use
    :param read_deleted: if present, overrides context's read_deleted field.
    :param project_only: if present and context is user-type, then restrict
            query to match the context's project_id.
    """
    session = kwargs.get('session') or get_session()
    read_deleted = kwargs.get('read_deleted') or context.read_deleted
    project_only = kwargs.get('project_only')

    query = session.query(*args)

    if read_deleted == 'no':
        query = query.filter_by(deleted=False)
    elif read_deleted == 'yes':
        pass  # omit the filter to include deleted and active
    elif read_deleted == 'only':
        query = query.filter_by(deleted=True)
    else:
        raise Exception(
            _("Unrecognized read_deleted value '%s'") % read_deleted)

    if project_only and is_user_context(context):
        query = query.filter_by(project_id=context.project_id)

    return query
예제 #2
0
def model_query(context, *args, **kwargs):
    """Query helper that accounts for context's `read_deleted` field.

    :param context: context to query under
    :param session: if present, the session to use
    :param read_deleted: if present, overrides context's read_deleted field.
    :param project_only: if present and context is user-type, then restrict
            query to match the context's project_id.
    """
    session = kwargs.get('session') or get_session()
    read_deleted = kwargs.get('read_deleted') or context.read_deleted
    project_only = kwargs.get('project_only')

    query = session.query(*args)

    if read_deleted == 'no':
        query = query.filter_by(deleted=False)
    elif read_deleted == 'yes':
        pass  # omit the filter to include deleted and active
    elif read_deleted == 'only':
        query = query.filter_by(deleted=True)
    else:
        raise Exception(
                _("Unrecognized read_deleted value '%s'") % read_deleted)

    if project_only and is_user_context(context):
        query = query.filter_by(project_id=context.project_id)

    return query
def bm_node_update(context, bm_node_id, values, session=None):
    if not session:
        session = get_session()
    with session.begin():
        bm_node_ref = bm_node_get(context, bm_node_id, session=session)
        bm_node_ref.update(values)
        bm_node_ref.save(session=session)
예제 #4
0
def bm_node_update(context, bm_node_id, values, session=None):
    if not session:
        session = get_session()
    with session.begin():
        bm_node_ref = bm_node_get(context, bm_node_id, session=session)
        bm_node_ref.update(values)
        bm_node_ref.save(session=session)
def bm_pxe_ip_disassociate(context, bm_node_id, session=None):
    if not session:
        session = get_session()
    with session.begin():
        ip = bm_pxe_ip_get_by_bm_node_id(context, bm_node_id, session=session)
        if ip:
            ip.bm_node_id = None
            ip.save(session=session)
예제 #6
0
def bm_node_create(context, values, session=None):
    if not session:
        session = get_session()
    with session.begin():
        bm_node_ref = baremetal_models.BareMetalNode()
        bm_node_ref.update(values)
        bm_node_ref.save(session=session)
        return bm_node_ref
def bm_node_create(context, values, session=None):
    if not session:
        session = get_session()
    with session.begin():
        bm_node_ref = baremetal_models.BareMetalNode()
        bm_node_ref.update(values)
        bm_node_ref.save(session=session)
        return bm_node_ref
예제 #8
0
def bm_pxe_ip_disassociate(context, bm_node_id, session=None):
    if not session:
        session = get_session()
    with session.begin():
        ip = bm_pxe_ip_get_by_bm_node_id(context, bm_node_id, session=session)
        if ip:
            ip.bm_node_id = None
            ip.save(session=session)
def bm_pxe_ip_create_direct(context, bm_pxe_ip, session=None):
    if not session:
        session = get_session()
    with session.begin():
        ref = baremetal_models.BareMetalPxeIp()
        ref.update(bm_pxe_ip)
        ref.save(session=session)
        return ref
예제 #10
0
def bm_interface_create(context, bm_node_id, address, datapath_id, port_no, session=None):
    if not session:
        session = get_session()
    with session.begin():
        ref = baremetal_models.BareMetalInterface()
        ref.bm_node_id = bm_node_id
        ref.address = address
        ref.datapath_id = datapath_id
        ref.port_no = port_no
        ref.save(session=session)
        return ref.id
예제 #11
0
def bm_deployment_create(context, key, image_path, pxe_config_path, root_mb, swap_mb, session=None):
    if not session:
        session = get_session()
    with session.begin():
        ref = baremetal_models.BareMetalDeployment()
        ref.key = key
        ref.image_path = image_path
        ref.pxe_config_path = pxe_config_path
        ref.root_mb = root_mb
        ref.swap_mb = swap_mb
        ref.save(session=session)
        return ref.id
예제 #12
0
def bm_interface_set_vif_uuid(context, if_id, vif_uuid, session=None):
    if not session:
        session = get_session()
    with session.begin():
        ref = model_query(context, baremetal_models.BareMetalInterface, read_deleted="no", session=session).\
                         filter_by(id=if_id).\
                         with_lockmode('update').\
                         first()
        if not ref:
            raise exception.NovaException()
        ref.vif_uuid = vif_uuid
        session.add(ref)
def bm_interface_set_vif_uuid(context, if_id, vif_uuid, session=None):
    if not session:
        session = get_session()
    with session.begin():
        ref = model_query(context, baremetal_models.BareMetalInterface,
                          read_deleted="no", session=session).\
                         filter_by(id=if_id).\
                         with_lockmode('update').\
                         first()
        if not ref:
            raise exception.NovaException()
        ref.vif_uuid = vif_uuid
        session.add(ref)
def bm_interface_create(context,
                        bm_node_id,
                        address,
                        datapath_id,
                        port_no,
                        session=None):
    if not session:
        session = get_session()
    with session.begin():
        ref = baremetal_models.BareMetalInterface()
        ref.bm_node_id = bm_node_id
        ref.address = address
        ref.datapath_id = datapath_id
        ref.port_no = port_no
        ref.save(session=session)
        return ref.id
def bm_pxe_ip_create(context, address, server_address, session=None):
    if not session:
        session = get_session()
    ref = model_query(context, baremetal_models.BareMetalPxeIp,
                      read_deleted="no", session=session).\
                     filter_by(address=address).\
                     first()
    if not ref:
        ref = baremetal_models.BareMetalPxeIp()
        ref.address = address
        ref.server_address = server_address
        ref.save(session=session)
    else:
        if ref.server_address != server_address:
            raise exception.NovaException(
                'address exists, but server_address is not same')
    return ref.id
예제 #16
0
def bm_pxe_ip_create(context, address, server_address, service_host, session=None):
    if not session:
        session = get_session()
    ref = model_query(context, baremetal_models.BareMetalPxeIp, read_deleted="no", session=session).\
                     filter_by(address=address).\
                     filter_by(service_host=service_host).\
                     first()
    if not ref:
        ref = baremetal_models.BareMetalPxeIp()
        ref.address = address
        ref.server_address = server_address
        ref.service_host = service_host
        ref.save(session=session)
    else:
        if ref.server_address != server_address:
            raise exception.NovaException('address exists, but server_address is not same')
    return ref.id
def bm_deployment_create(context,
                         key,
                         image_path,
                         pxe_config_path,
                         root_mb,
                         swap_mb,
                         session=None):
    if not session:
        session = get_session()
    with session.begin():
        ref = baremetal_models.BareMetalDeployment()
        ref.key = key
        ref.image_path = image_path
        ref.pxe_config_path = pxe_config_path
        ref.root_mb = root_mb
        ref.swap_mb = swap_mb
        ref.save(session=session)
        return ref.id
예제 #18
0
def bm_pxe_ip_associate(context, bm_node_id, session=None):
    if not session:
        session = get_session()
    with session.begin():
        node_ref = bm_node_get(context, bm_node_id, session=session)
        if not node_ref:
            raise exception.NovaException()
        ip_ref = model_query(context, baremetal_models.BareMetalPxeIp, read_deleted="no", session=session).\
                         filter_by(service_host=node_ref.service_host).\
                         filter_by(bm_node_id=node_ref.id).\
                         first()
        if ip_ref:
            return ip_ref.id
        ip_ref = model_query(context, baremetal_models.BareMetalPxeIp, read_deleted="no", session=session).\
                         filter_by(service_host=node_ref.service_host).\
                         filter_by(bm_node_id=None).\
                         with_lockmode('update').\
                         first()
        if not ip_ref:
            raise exception.NovaException()
        ip_ref.bm_node_id = bm_node_id
        session.add(ip_ref)
        return ip_ref.id
def bm_pxe_ip_associate(context, bm_node_id, session=None):
    if not session:
        session = get_session()
    with session.begin():
        node_ref = bm_node_get(context, bm_node_id, session=session)
        if not node_ref:
            raise exception.NovaException("bm_node %s not found" % bm_node_id)
        ip_ref = model_query(context, baremetal_models.BareMetalPxeIp,
                             read_deleted="no", session=session).\
                         filter_by(bm_node_id=node_ref.id).\
                         first()
        if ip_ref:
            return ip_ref.id
        ip_ref = model_query(context, baremetal_models.BareMetalPxeIp,
                             read_deleted="no", session=session).\
                         filter_by(bm_node_id=None).\
                         with_lockmode('update').\
                         first()
        if not ip_ref:
            raise exception.NovaException("free bm_pxe_ip not found")
        ip_ref.bm_node_id = bm_node_id
        session.add(ip_ref)
        return ip_ref.id