Пример #1
0
def test_update_host_with_tags(db_create_host):
    insights_id = str(uuid.uuid4())
    old_tags = Tag("Sat", "env", "prod").to_nested()
    existing_host = db_create_host(
        extra_data={
            "canonical_facts": {
                "insights_id": insights_id
            },
            "display_name": "tagged",
            "tags": old_tags
        })

    assert existing_host.tags == old_tags

    # On update each namespace in the input host's tags should be updated.
    new_tags = Tag.create_nested_from_tags(
        [Tag("Sat", "env", "ci"),
         Tag("AWS", "env", "prod")])
    input_host = db_create_host(
        extra_data={
            "canonical_facts": {
                "insights_id": insights_id
            },
            "display_name": "tagged",
            "tags": new_tags
        })

    existing_host.update(input_host)

    assert existing_host.tags == new_tags
Пример #2
0
def test_update_host_with_tags(flask_app_fixture):
    insights_id = str(uuid.uuid4())
    old_tags = Tag("Sat", "env", "prod").to_nested()
    existing_host = _create_host(insights_id=insights_id, display_name="tagged", tags=old_tags)

    assert existing_host.tags == old_tags

    # On update each namespace in the input host's tags should be updated.
    new_tags = Tag.create_nested_from_tags([Tag("Sat", "env", "ci"), Tag("AWS", "env", "prod")])
    input_host = _create_host(insights_id=insights_id, display_name="tagged", tags=new_tags)
    existing_host.update(input_host)

    assert existing_host.tags == new_tags
def _query_filters(fqdn, display_name, hostname_or_id, insights_id, tags, staleness):
    if fqdn:
        query_filters = ({"fqdn": fqdn},)
    elif display_name:
        query_filters = ({"display_name": string_contains(display_name)},)
    elif hostname_or_id:
        contains = string_contains(hostname_or_id)
        hostname_or_id_filters = ({"display_name": contains}, {"fqdn": contains})
        try:
            id = UUID(hostname_or_id)
        except ValueError:
            # Do not filter using the id
            logger.debug("The hostname (%s) could not be converted into a UUID", hostname_or_id, exc_info=True)
        else:
            logger.debug("Adding id (uuid) to the filter list")
            hostname_or_id_filters += ({"id": str(id)},)
        query_filters = ({"OR": hostname_or_id_filters},)
    elif insights_id:
        query_filters = ({"insights_id": insights_id},)
    else:
        query_filters = ()

    if tags:
        query_filters += tuple({"tag": Tag().from_string(string_tag).data()} for string_tag in tags)
    if staleness:
        staleness_filters = tuple(staleness_filter(staleness))
        query_filters += ({"OR": staleness_filters},)

    return query_filters
Пример #4
0
def get_tags(search=None, tags=None, order_by=None, order_how=None, page=None, per_page=None, staleness=None):
    if not xjoin_enabled():
        flask.abort(503)

    limit, offset = pagination_params(page, per_page)

    variables = {
        "order_by": order_by,
        "order_how": order_how,
        "limit": limit,
        "offset": offset,
        "hostFilter": {
            # we're not indexing null timestamps in ES
            "OR": list(staleness_filter(staleness))
        },
    }

    if search:
        variables["filter"] = {
            # Escaped to prevent ReDoS
            "name": f".*{re.escape(url_quote(search, safe=''))}.*"
        }

    if tags:
        variables["hostFilter"]["AND"] = [{"tag": Tag().from_string(tag).data()} for tag in tags]

    response = graphql_query(TAGS_QUERY, variables)
    data = response["hostTags"]

    check_pagination(offset, data["meta"]["total"])

    return flask_json_response(build_collection_response(data["data"], page, per_page, data["meta"]["total"]))
Пример #5
0
def find_hosts_by_tag(account_number, string_tags, query):
    tags = []

    for string_tag in string_tags:
        tags.append(Tag().from_string(string_tag))

    tags_to_find = Tag.create_nested_from_tags(tags)

    return query.filter(Host.tags.contains(tags_to_find))
Пример #6
0
def test_update_host_with_no_tags(flask_app_fixture):
    insights_id = str(uuid.uuid4())
    old_tags = Tag("Sat", "env", "prod").to_nested()
    existing_host = _create_host(insights_id=insights_id, display_name="tagged", tags=old_tags)

    # Updating a host should not remove any existing tags if tags are missing from the input host
    input_host = _create_host(insights_id=insights_id, display_name="tagged")
    existing_host.update(input_host)

    assert existing_host.tags == old_tags
def _find_hosts_by_tag(string_tags, query):
    logger.debug("_find_hosts_by_tag(%s)", string_tags)

    tags = []

    for string_tag in string_tags:
        tags.append(Tag().from_string(string_tag))

    tags_to_find = Tag.create_nested_from_tags(tags)

    return query.filter(Host.tags.contains(tags_to_find))
Пример #8
0
def test_update_host_with_no_tags(db_create_host):
    insights_id = str(uuid.uuid4())
    old_tags = Tag("Sat", "env", "prod").to_nested()
    existing_host = db_create_host(
        extra_data={"canonical_facts": {"insights_id": insights_id}, "display_name": "tagged", "tags": old_tags}
    )

    # Updating a host should not remove any existing tags if tags are missing from the input host
    input_host = db_create_host(extra_data={"canonical_facts": {"insights_id": insights_id}, "display_name": "tagged"})
    existing_host.update(input_host)

    assert existing_host.tags == old_tags