def get_host_list( display_name, fqdn, hostname_or_id, insights_id, provider_id, provider_type, tags, page, per_page, param_order_by, param_order_how, staleness, registered_with, filter, fields, ): limit, offset = pagination_params(page, per_page) xjoin_order_by, xjoin_order_how = params_to_order(param_order_by, param_order_how) all_filters = query_filters( fqdn, display_name, hostname_or_id, insights_id, provider_id, provider_type, tags, staleness, registered_with, filter, ) current_identity = get_current_identity() if current_identity.identity_type == IdentityType.SYSTEM and current_identity.auth_type != AuthType.CLASSIC: all_filters += owner_id_filter() additional_fields = tuple() system_profile_fields = [] if fields.get("system_profile"): additional_fields = ("system_profile", ) system_profile_fields = list(fields.get("system_profile").keys()) variables = { "limit": limit, "offset": offset, "order_by": xjoin_order_by, "order_how": xjoin_order_how, "filter": all_filters, "fields": system_profile_fields, } response = graphql_query(QUERY, variables, log_get_host_list_failed)["hosts"] total = response["meta"]["total"] check_pagination(offset, total) return map(deserialize_host, response["data"]), total, additional_fields
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"]))
def get_sap_system(tags=None, page=None, per_page=None, staleness=None, registered_with=None, filter=None): if not xjoin_enabled(): flask.abort(503) limit, offset = pagination_params(page, per_page) variables = { "hostFilter": { # we're not indexing null timestamps in ES "OR": list(staleness_filter(staleness)) } } hostfilter_and_variables = () if tags: hostfilter_and_variables = build_tag_query_dict_tuple(tags) if registered_with: variables["hostFilter"]["NOT"] = {"insights_id": {"eq": None}} if filter: if filter.get("system_profile"): if filter["system_profile"].get("sap_system"): hostfilter_and_variables += build_sap_system_filters( filter["system_profile"].get("sap_system")) if filter["system_profile"].get("sap_sids"): hostfilter_and_variables += build_sap_sids_filter( filter["system_profile"]["sap_sids"]) # TODO enable owner_id filtering after all hosts've been updated with "owner_id" # current_identity = get_current_identity() # if ( # current_identity.identity_type == "System" # and current_identity.auth_type != "classic-proxy" # and current_identity.system["cert_type"] == "system" # ): # hostfilter_and_variables += owner_id_filter() if hostfilter_and_variables != (): variables["hostFilter"]["AND"] = hostfilter_and_variables response = graphql_query(SAP_SYSTEM_QUERY, variables, log_get_sap_system_failed) data = response["hostSystemProfile"] check_pagination(offset, data["sap_system"]["meta"]["total"]) log_get_sap_system_succeeded(logger, data) return flask_json_response( build_collection_response(data["sap_system"]["data"], page, per_page, data["sap_system"]["meta"]["total"]))
def get_tags( search=None, tags=None, order_by=None, order_how=None, page=None, per_page=None, staleness=None, registered_with=None, filter=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)) }, } hostfilter_and_variables = () if search: variables["filter"] = { # Escaped so that the string literals are not interpreted as regex "search": {"regex": f".*{re.escape(search)}.*"} } if tags: hostfilter_and_variables = build_tag_query_dict_tuple(tags) if registered_with: variables["hostFilter"]["NOT"] = {"insights_id": {"eq": None}} if filter: if filter.get("system_profile"): if filter["system_profile"].get("sap_system"): hostfilter_and_variables += build_sap_system_filters(filter["system_profile"].get("sap_system")) if filter["system_profile"].get("sap_sids"): hostfilter_and_variables += build_sap_sids_filter(filter["system_profile"]["sap_sids"]) if hostfilter_and_variables != (): variables["hostFilter"]["AND"] = hostfilter_and_variables 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"]))
def get_sap_system(tags=None, page=None, per_page=None, staleness=None, registered_with=None, filter=None): if not xjoin_enabled(): logger.error("xjoin-search not enabled") flask.abort(503) limit, offset = pagination_params(page, per_page) variables = { "hostFilter": { # we're not indexing null timestamps in ES "OR": list(staleness_filter(staleness)) }, "limit": limit, "offset": offset, } hostfilter_and_variables = () if tags: hostfilter_and_variables = build_tag_query_dict_tuple(tags) if registered_with: hostfilter_and_variables += build_registered_with_filter( registered_with) if filter: for key in filter: if key == "system_profile": hostfilter_and_variables += build_system_profile_filter( filter["system_profile"]) else: raise ValidationException("filter key is invalid") current_identity = get_current_identity() if current_identity.identity_type == IdentityType.SYSTEM: hostfilter_and_variables += owner_id_filter() if hostfilter_and_variables != (): variables["hostFilter"]["AND"] = hostfilter_and_variables response = graphql_query(SAP_SYSTEM_QUERY, variables, log_get_sap_system_failed) data = response["hostSystemProfile"] check_pagination(offset, data["sap_system"]["meta"]["total"]) log_get_sap_system_succeeded(logger, data) return flask_json_response( build_collection_response(data["sap_system"]["data"], page, per_page, data["sap_system"]["meta"]["total"]))
def get_sap_sids(search=None, tags=None, page=None, per_page=None, staleness=None, registered_with=None, filter=None): if not xjoin_enabled(): logger.error("xjoin-search not enabled") flask.abort(503) limit, offset = pagination_params(page, per_page) variables = { "hostFilter": { # we're not indexing null timestamps in ES "OR": list(staleness_filter(staleness)) }, "limit": limit, "offset": offset, } hostfilter_and_variables = () if tags: hostfilter_and_variables = build_tag_query_dict_tuple(tags) if registered_with: variables["hostFilter"]["NOT"] = {"insights_id": {"eq": None}} if search: variables["filter"] = { # Escaped so that the string literals are not interpreted as regex "search": {"regex": f".*{custom_escape(search)}.*"} } if filter: for key in filter: if key == "system_profile": hostfilter_and_variables += build_system_profile_filter(filter["system_profile"]) else: raise ValidationException("filter key is invalid") current_identity = get_current_identity() if current_identity.identity_type == IdentityType.SYSTEM and current_identity.auth_type != AuthType.CLASSIC: hostfilter_and_variables += owner_id_filter() if hostfilter_and_variables != (): variables["hostFilter"]["AND"] = hostfilter_and_variables response = graphql_query(SAP_SIDS_QUERY, variables, log_get_sap_sids_failed) data = response["hostSystemProfile"] check_pagination(offset, data["sap_sids"]["meta"]["total"]) log_get_sap_sids_succeeded(logger, data) return flask_json_response( build_collection_response(data["sap_sids"]["data"], page, per_page, data["sap_sids"]["meta"]["total"]) )
def get_operating_system( tags=None, page: Optional[int] = None, per_page: Optional[int] = None, staleness: Optional[str] = None, registered_with: Optional[str] = None, filter=None, ): limit, offset = pagination_params(page, per_page) variables = { "hostFilter": { # we're not indexing null timestamps in ES "OR": list(staleness_filter(staleness)) }, "limit": limit, "offset": offset, } hostfilter_and_variables = () if tags: hostfilter_and_variables = build_tag_query_dict_tuple(tags) if registered_with: variables["hostFilter"]["NOT"] = {"insights_id": {"eq": None}} if filter: for key in filter: if key == "system_profile": hostfilter_and_variables += build_system_profile_filter( filter["system_profile"]) else: raise ValidationException("filter key is invalid") current_identity = get_current_identity() if current_identity.identity_type == IdentityType.SYSTEM: hostfilter_and_variables += owner_id_filter() if hostfilter_and_variables != (): variables["hostFilter"]["AND"] = hostfilter_and_variables response = graphql_query(OPERATING_SYSTEM_QUERY, variables, log_get_operating_system_failed) data = response["hostSystemProfile"] check_pagination(offset, data["operating_system"]["meta"]["total"]) log_get_operating_system_succeeded(logger, data) return flask_json_response( build_collection_response(data["operating_system"]["data"], page, per_page, data["operating_system"]["meta"]["total"]))
def get_sap_system(tags=None, page=None, per_page=None, staleness=None, registered_with=None, filter=None): if not xjoin_enabled(): flask.abort(503) limit, offset = pagination_params(page, per_page) variables = { "hostFilter": { # we're not indexing null timestamps in ES "OR": list(staleness_filter(staleness)) } } hostfilter_and_variables = () if tags: hostfilter_and_variables = build_tag_query_dict_tuple(tags) if registered_with: variables["hostFilter"]["NOT"] = {"insights_id": {"eq": None}} if filter: if filter.get("system_profile"): if filter["system_profile"].get("sap_system"): hostfilter_and_variables += build_sap_system_filters( filter["system_profile"].get("sap_system")) if filter["system_profile"].get("sap_sids"): hostfilter_and_variables += build_sap_sids_filter( filter["system_profile"]["sap_sids"]) if hostfilter_and_variables != (): variables["hostFilter"]["AND"] = hostfilter_and_variables response = graphql_query(SAP_SYSTEM_QUERY, variables) data = response["hostSystemProfile"] check_pagination(offset, data["sap_system"]["meta"]["total"]) return flask_json_response( build_collection_response(data["sap_system"]["data"], page, per_page, data["sap_system"]["meta"]["total"]))
def get_host_list( display_name, fqdn, hostname_or_id, insights_id, tags, page, per_page, param_order_by, param_order_how, staleness, registered_with, filter, ): limit, offset = pagination_params(page, per_page) xjoin_order_by, xjoin_order_how = _params_to_order(param_order_by, param_order_how) all_filters = _query_filters(fqdn, display_name, hostname_or_id, insights_id, tags, staleness, registered_with, filter) # TODO enable owner_id filtering after all hosts've been updated with "owner_id" # current_identity = get_current_identity() # if ( # current_identity.identity_type == "System" # and current_identity.auth_type != "classic-proxy" # and current_identity.system["cert_type"] == "system" # ): # all_filters += owner_id_filter() variables = { "limit": limit, "offset": offset, "order_by": xjoin_order_by, "order_how": xjoin_order_how, "filter": all_filters, } response = graphql_query(QUERY, variables, log_get_host_list_failed)["hosts"] total = response["meta"]["total"] check_pagination(offset, total) return map(deserialize_host, response["data"]), total
def get_host_list( display_name, fqdn, hostname_or_id, insights_id, tags, page, per_page, param_order_by, param_order_how, staleness ): limit, offset = pagination_params(page, per_page) xjoin_order_by, xjoin_order_how = _params_to_order(param_order_by, param_order_how) variables = { "limit": limit, "offset": offset, "order_by": xjoin_order_by, "order_how": xjoin_order_how, "filter": _query_filters(fqdn, display_name, hostname_or_id, insights_id, tags, staleness), } response = graphql_query(QUERY, variables)["hosts"] total = response["meta"]["total"] check_pagination(offset, total) return map(deserialize_host, response["data"]), total
def get_sparse_system_profile(host_id_list, page, per_page, order_by, order_how, fields): if not fields.get("system_profile"): flask.abort(400, status.HTTP_400_BAD_REQUEST) limit, offset = pagination_params(page, per_page) try: order_by, order_how = params_to_order(order_by, order_how) except ValueError as e: flask.abort(400, str(e)) host_ids = [{"id": {"eq": host_id}} for host_id in host_id_list] system_profile_fields = list(fields.get("system_profile").keys()) if len(system_profile_fields) > 0: variables = { "host_ids": host_ids, "fields": system_profile_fields, "limit": limit, "offset": offset, "order_by": order_by, "order_how": order_how, } response = graphql_query(SYSTEM_PROFILE_QUERY, variables, log_get_sparse_system_profile_failed) response_data = response["hosts"] check_pagination(offset, response_data["meta"]["total"]) total = response_data["meta"]["total"] response_list = [ serialize_host_system_profile_xjoin(host_data) for host_data in response_data["data"] ] log_get_sparse_system_profile_succeeded(logger, response_data) return total, response_list
def get_tags( search=None, tags=None, order_by=None, order_how=None, page=None, per_page=None, staleness=None, registered_with=None, filter=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)) }, } hostfilter_and_variables = () if search: variables["filter"] = { # Escaped so that the string literals are not interpreted as regex "search": { "regex": f".*{re.escape(search)}.*" } } if tags: hostfilter_and_variables = build_tag_query_dict_tuple(tags) if registered_with: variables["hostFilter"]["NOT"] = {"insights_id": {"eq": None}} if filter: if filter.get("system_profile"): if filter["system_profile"].get("sap_system"): hostfilter_and_variables += build_sap_system_filters( filter["system_profile"].get("sap_system")) if filter["system_profile"].get("sap_sids"): hostfilter_and_variables += build_sap_sids_filter( filter["system_profile"]["sap_sids"]) # TODO enable owner_id filtering after all hosts've been updated with "owner_id" # current_identity = get_current_identity() # if ( # current_identity.identity_type == "System" # and current_identity.auth_type != "classic-proxy" # and current_identity.system["cert_type"] == "system" # ): # hostfilter_and_variables += owner_id_filter() if hostfilter_and_variables != (): variables["hostFilter"]["AND"] = hostfilter_and_variables response = graphql_query(TAGS_QUERY, variables, log_get_tags_failed) data = response["hostTags"] check_pagination(offset, data["meta"]["total"]) log_get_tags_succeeded(logger, data) return flask_json_response( build_collection_response(data["data"], page, per_page, data["meta"]["total"]))
def get_tags( search=None, tags=None, display_name=None, fqdn=None, hostname_or_id=None, insights_id=None, provider_id=None, provider_type=None, order_by=None, order_how=None, page=None, per_page=None, staleness=None, registered_with=None, filter=None, ): if not xjoin_enabled(): logger.error("xjoin-search not 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)) }, } hostfilter_and_variables = query_filters( fqdn, display_name, hostname_or_id, insights_id, provider_id, provider_type, tags, None, registered_with, filter, ) if search: variables["filter"] = { # Escaped so that the string literals are not interpreted as regex "search": { "regex": f".*{custom_escape(search)}.*" } } current_identity = get_current_identity() if current_identity.identity_type == IdentityType.SYSTEM: hostfilter_and_variables += owner_id_filter() if hostfilter_and_variables != (): variables["hostFilter"]["AND"] = hostfilter_and_variables response = graphql_query(TAGS_QUERY, variables, log_get_tags_failed) data = response["hostTags"] check_pagination(offset, data["meta"]["total"]) log_get_tags_succeeded(logger, data) return flask_json_response( build_collection_response(data["data"], page, per_page, data["meta"]["total"]))