Esempio n. 1
0
def update_facts_by_namespace(operation, host_id_list, namespace, fact_dict):

    current_identity = get_current_identity()
    query = Host.query.filter(
        (Host.account == current_identity.account_number)
        & Host.id.in_(host_id_list)
        & Host.facts.has_key(
            namespace)  # noqa: W601 JSONB query filter, not a dict
    )

    hosts_to_update = find_non_culled_hosts(
        update_query_for_owner_id(current_identity, query)).all()

    logger.debug("hosts_to_update:%s", hosts_to_update)

    if len(hosts_to_update) != len(host_id_list):
        error_msg = (
            "ERROR: The number of hosts requested does not match the number of hosts found in the host database.  "
            "This could happen if the namespace does not exist or the account number associated with the call does "
            "not match the account number associated with one or more the hosts.  Rejecting the fact change request."
        )
        logger.debug(error_msg)
        return error_msg, 400

    for host in hosts_to_update:
        if operation is FactOperations.replace:
            host.replace_facts_in_namespace(namespace, fact_dict)
        else:
            host.merge_facts_in_namespace(namespace, fact_dict)

    db.session.commit()

    logger.debug("hosts_to_update:%s", hosts_to_update)

    return 200
Esempio n. 2
0
def _find_all_hosts():
    identity = get_current_identity()
    query = Host.query.filter(Host.account == identity.account_number)
    return update_query_for_owner_id(identity, query)
Esempio n. 3
0
def _get_host_list_by_id_list(host_id_list):
    current_identity = get_current_identity()
    query = Host.query.filter((Host.account == current_identity.account_number)
                              & Host.id.in_(host_id_list))
    return find_non_culled_hosts(
        update_query_for_owner_id(current_identity, query))