Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(RHINENG-5484): refactor operating_system filter #2141

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions api/filtering/db_custom_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()):
Comment on lines +119 to 120
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend checking for the key "name" between these 2 lines and doing the custom logic there:

Suggested change
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()):
for os_name in filter_param.keys():
if os_name == "name":
# Return an OsComparison object that implies all hosts with a matching OS name.
# Maybe if OsComparison.comparator is "eq" or "neq" and the major version is None?
elif os_name not in (os_names := _get_valid_os_names()):

raise ValidationException(f"operating_system filter only supports these OS names: {os_names}.")

Expand Down Expand Up @@ -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)

Expand All @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Around here is where you'd convert the OsComparison to an actual DB operation. If we went with the logic I posted above, this next section could be something like this:

Suggested change
print("~~~~~~~~~~~~~~~~~~~``", os_field["name"].astext == comparison.name)
os_filters = [
os_field["name"].astext == comparison.name,
]
if comparison.major is not None:
os_filters.append(os_field["major"].astext.cast(Integer) == comparison.major)
if comparison.minor:
os_filters.append(os_field["minor"].astext.cast(Integer) == comparison.minor)
os_filter_list.append(and_(*os_filters))

Of course, you'll need to handle "neq" as well :)

os_filters = [
os_field["name"].astext == comparison.name,
os_field["major"].astext.cast(Integer) == comparison.major,
Expand Down
46 changes: 46 additions & 0 deletions tests/test_api_hosts_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
(
Expand Down