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

Hotfix user not returning list #139

Merged
merged 4 commits into from
Oct 3, 2023
Merged
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
8 changes: 5 additions & 3 deletions lib/openstack_query/queries/user_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ class UserQuery(QueryWrapper):

def _get_server_side_handler(self) -> ServerSideHandler:
"""
method to configure a server handler which can be used to get 'filter' keyword arguments that
can be passed to openstack function conn.compute.servers() to filter results for a valid preset-property pair
method to configure a server-side handler which can be used to get 'filter' keyword arguments that
can be passed to an openstack function to filter results for a valid preset-property
on the control plane, rather than locally.

valid filters documented here:
https://docs.openstack.org/openstacksdk/latest/user/proxies/compute.html
Expand All @@ -43,6 +44,7 @@ def _get_server_side_handler(self) -> ServerSideHandler:
QueryPresetsGeneric.EQUAL_TO: {
DavidFair marked this conversation as resolved.
Show resolved Hide resolved
UserProperties.USER_DOMAIN_ID: lambda value: {"domain_id": value},
UserProperties.USER_NAME: lambda value: {"name": value},
UserProperties.USER_ID: lambda value: {"id": value},
}
}
)
Expand All @@ -51,7 +53,7 @@ def _get_client_side_handlers(self) -> QueryClientSideHandlers:
"""
method to configure a set of client-side handlers which can be used to get local filter functions
corresponding to valid preset-property pairs. These filter functions can be used to filter results after
listing all servers.
listing all users.
"""
return QueryClientSideHandlers(
# set generic query preset mappings
Expand Down
8 changes: 7 additions & 1 deletion lib/openstack_query/runners/user_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _run_query(
conn: OpenstackConnection,
filter_kwargs: Optional[Dict[str, str]] = None,
**meta_params,
) -> List[User]:
) -> List[Optional[User]]:
"""
This method runs the query by running openstacksdk commands

Expand All @@ -91,6 +91,12 @@ def _run_query(
if not filter_kwargs:
filter_kwargs = {}

# having a filter of 'id' - will automatically mean no other filter kwargs
selected_id = filter_kwargs.get("id", None)
if selected_id:
val = conn.identity.find_user(selected_id, ignore_missing=True)
return [val] if val else []

if meta_params["domain_id"] and "domain_id" in filter_kwargs.keys():
raise ParseQueryError(
"This query uses a preset that requires searching on domain_ids "
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/openstack_query/runners/test_user_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ def test_run_query_no_meta_args(
assert res == ["user1", "user2"]


def test_run_query_returns_list(instance, mock_openstack_connection):
"""
Tests that run_query correctly returns a list of entries
"""
return_value = NonCallableMock()
mock_openstack_connection.identity.find_user.return_value = return_value

returned = instance._run_query(mock_openstack_connection, filter_kwargs={"id": "1"})
mock_openstack_connection.identity.find_user.assert_called_once_with(
"1", ignore_missing=True
)
assert [return_value] == returned


def test_run_query_with_from_domain_and_id_given(instance, mock_openstack_connection):
"""
Test error is raised when the domain name and domain id is provided at
Expand Down