Skip to content

Commit

Permalink
fix OS set name logic
Browse files Browse the repository at this point in the history
  • Loading branch information
FabriciaDinizRH committed Jan 16, 2025
1 parent 15e06d9 commit e3acacc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
15 changes: 9 additions & 6 deletions api/filtering/db_custom_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@
# The list of comparators can be seen in POSTGRES_COMPARATOR_LOOKUP
class OsFilter:
def __init__(self, name="", comparator="", version=None):
os_names = get_valid_os_names()

try:
if name and name.lower() not in [name.lower() for name in os_names]:
raise ValidationException(f"operating_system filter only supports these OS names: {os_names}.")

if version is None:
major, minor = None, None
else:
Expand Down Expand Up @@ -140,10 +135,12 @@ def separate_operating_system_filters(filter_url_params) -> list[OsFilter]:
# filter_url_params is a dict
for filter_key in filter_url_params.keys():
if filter_key == "name":
((os_comparator, os_name),) = filter_url_params["name"].items()
((os_comparator, os_name),) = filter_url_params[filter_key].items()
check_valid_os_name(os_name)
version_node = {os_comparator: [None]}
else:
os_name = filter_key
check_valid_os_name(os_name)
if not isinstance(version_node := filter_url_params[os_name]["version"], dict):
# If there's no comparator, treat it as "eq"
version_node = {"eq": version_node}
Expand Down Expand Up @@ -382,6 +379,12 @@ def build_system_profile_filter(system_profile_param: dict) -> tuple:
return system_profile_filter


def check_valid_os_name(name):
os_names = get_valid_os_names()
if name.lower() not in [name.lower() for name in os_names]:
raise ValidationException(f"operating_system filter only supports these OS names: {os_names}.")


def get_major_minor_from_version(version_split: list[str]):
if len(version_split) > 2:
raise ValidationException("operating_system filter can only have a major and minor version.")
Expand Down
1 change: 1 addition & 0 deletions tests/test_api_hosts_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,7 @@ def test_query_all_sp_filters_invalid_value(api_get, sp_filter_param):
(
"[operating_system][foo][version]=8.1", # Invalid OS name
"[operating_system][name][eq]=rhelz", # Invalid OS name
"[operating_system][][version][eq][]=7", # Invalid OS name
"[operating_system][RHEL][version]=bar", # Invalid OS version
),
)
Expand Down

0 comments on commit e3acacc

Please sign in to comment.