diff --git a/api/filtering/db_custom_filters.py b/api/filtering/db_custom_filters.py index 3ea39f718..5affff2ac 100644 --- a/api/filtering/db_custom_filters.py +++ b/api/filtering/db_custom_filters.py @@ -116,7 +116,7 @@ def separate_operating_system_filters(filter_param) -> list[OsComparison]: return [OsComparison(comparator=filter_param)] # filter_param is a dict - for os_name in filter_param.keys(): + for os_name in filter_param.keys(): # this doesn't account for "os_name" instead of os names if os_name not in (os_names := _get_valid_os_names()): raise ValidationException(f"operating_system filter only supports these OS names: {os_names}.") @@ -147,9 +147,20 @@ def separate_operating_system_filters(filter_param) -> list[OsComparison]: def build_operating_system_filter(filter_param: dict) -> tuple: os_filter_list = [] # Top-level filter os_range_filter_list = [] # Contains the OS filters that use range operations - separated_filters = separate_operating_system_filters(filter_param["operating_system"]) os_field = Host.system_profile_facts["operating_system"] + # if isinstance(filter_param["operating_system"], dict) and "name" in filter_param["operating_system"].keys(): + # for os in filter_param["operating_system"]["name"].values(): + # print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> found name", os_field["name"].astext == os) + # # os_filter_list.append(and_([os_field["name"].astext == os])) + + # return os_filter_list + + if "name" in filter_param.keys(): + os_filter_list.append() + + separated_filters = separate_operating_system_filters(filter_param["operating_system"]) + for comparison in separated_filters: comparator = POSTGRES_COMPARATOR_LOOKUP.get(comparison.comparator) @@ -158,6 +169,7 @@ def build_operating_system_filter(filter_param: dict) -> tuple: os_filter_list.append(os_field.astext.operate(comparator, None)) elif comparison.comparator == "eq": + print("~~~~~~~~~~~~~~~~~~~``", os_field["name"].astext == comparison.name) os_filters = [ os_field["name"].astext == comparison.name, os_field["major"].astext.cast(Integer) == comparison.major, diff --git a/tests/test_api_hosts_get.py b/tests/test_api_hosts_get.py index 71b4a137a..0052d8ddf 100644 --- a/tests/test_api_hosts_get.py +++ b/tests/test_api_hosts_get.py @@ -1395,6 +1395,52 @@ def test_query_all_sp_filters_operating_system(db_create_host, api_get, sp_filte assert nomatch_host_id_3 not in response_ids +@pytest.mark.parametrize( + "sp_filter_param", + ( + "[name][eq]=CentOS", + "[name][eq]=centos", + "[name][eq]=CENTOS", + ), +) +def test_query_sp_filters_operating_system_name(db_create_host, api_get, sp_filter_param): + # Create host with this OS + match_sp_data = { + "system_profile_facts": { + "operating_system": { + "name": "CentOS", + "major": "8", + "minor": "11", + } + } + } + match_host_id = str(db_create_host(extra_data=match_sp_data).id) + + # Create host with differing OS + nomatch_sp_data = { + "system_profile_facts": { + "operating_system": { + "name": "RHEL", + "major": "7", + "minor": "12", + } + } + } + nomatch_host_id = str(db_create_host(extra_data=nomatch_sp_data).id) + + url = build_hosts_url(query=f"?filter[system_profile][operating_system]{sp_filter_param}") + + with patch("api.host.get_flag_value", return_value=True): + response_status, response_data = api_get(url) + + assert response_status == 200 + + # Assert that only the matching host is returned + response_ids = [result["id"] for result in response_data["results"]] + assert match_host_id in response_ids + assert nomatch_host_id not in response_ids + + @pytest.mark.parametrize( "os_match_data_list,os_nomatch_data_list,sp_filter_param_list", (