Skip to content

Commit 91f43cd

Browse files
committed
fix: Adjust the user search functionality (#107)
1 parent 7f55514 commit 91f43cd

File tree

4 files changed

+148
-16
lines changed

4 files changed

+148
-16
lines changed

docs/content/grafana_api/user.md

+40-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [user](#user)
44
* [User](#user.User)
55
* [search\_users](#user.User.search_users)
6+
* [search\_users\_with\_paging](#user.User.search_users_with_paging)
67
* [get\_user\_by\_id](#user.User.get_user_by_id)
78
* [get\_user\_by\_username\_or\_email](#user.User.get_user_by_username_or_email)
89
* [update\_user](#user.User.update_user)
@@ -51,11 +52,11 @@ HINT: Note Grafana Enterprise API need required permissions if fine-grained acce
5152

5253
```python
5354
def search_users(results_per_page: int = 1000,
54-
pages: int = 1,
55-
query: str = None) -> list
55+
page: int = 1,
56+
sort: str = None) -> list
5657
```
5758

58-
The method includes a functionality to get all Grafana system users specified by the optional query and paging functionality
59+
The method includes a functionality to get all Grafana system users specified by the optional results_per_page, page and sort option
5960

6061
Required Permissions:
6162
Action: users:read
@@ -64,8 +65,8 @@ Scope: global.users:*
6465
**Arguments**:
6566

6667
- `results_per_page` _int_ - Specify the results_per_page as integer (default 1000)
67-
- `pages` _int_ - Specify the pages as integer (default 1)
68-
- `query` _str_ - Specify the query (default None)
68+
- `page` _int_ - Specify the page as integer (default 1)
69+
- `sort` _str_ - Specify the sort option. Valid values are login-asc, login-desc, email-asc, email-desc, name-asc, name-desc, lastSeenAtAge-asc and lastSeenAtAge-desc. By default, if sort is not specified, the user list will be ordered by login, email in ascending order (default None)
6970

7071

7172
**Raises**:
@@ -77,6 +78,40 @@ Scope: global.users:*
7778

7879
- `api_call` _list_ - Returns the list of Grafana users
7980

81+
<a id="user.User.search_users_with_paging"></a>
82+
83+
#### search\_users\_with\_paging
84+
85+
```python
86+
def search_users_with_paging(results_per_page: int = 1000,
87+
page: int = 1,
88+
query: str = None,
89+
sort: str = None) -> dict
90+
```
91+
92+
The method includes a functionality to get all Grafana system users specified by the optional results_per_page, page, query, sort and general paging functionality
93+
94+
Required Permissions:
95+
Action: users:read
96+
Scope: global.users:*
97+
98+
**Arguments**:
99+
100+
- `results_per_page` _int_ - Specify the results_per_page as integer (default 1000)
101+
- `page` _int_ - Specify the page as integer (default 1)
102+
- `query` _str_ - Specify the query (default None)
103+
- `sort` _str_ - Specify the sort option. Valid values are login-asc, login-desc, email-asc, email-desc, name-asc, name-desc, lastSeenAtAge-asc and lastSeenAtAge-desc. By default, if sort is not specified, the user list will be ordered by login, email in ascending order (default None)
104+
105+
106+
**Raises**:
107+
108+
- `Exception` - Unspecified error by executing the API call
109+
110+
111+
**Returns**:
112+
113+
- `api_call` _dict_ - Returns the Grafana users
114+
80115
<a id="user.User.get_user_by_id"></a>
81116

82117
#### get\_user\_by\_id

grafana_api/user.py

+54-8
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ def __init__(self, grafana_api_model: APIModel):
2828
def search_users(
2929
self,
3030
results_per_page: int = 1000,
31-
pages: int = 1,
32-
query: str = None,
31+
page: int = 1,
32+
sort: str = None,
3333
) -> list:
34-
"""The method includes a functionality to get all Grafana system users specified by the optional query and paging functionality
34+
"""The method includes a functionality to get all Grafana system users specified by the optional results_per_page, page and sort option
3535
3636
Required Permissions:
3737
Action: users:read
3838
Scope: global.users:*
3939
4040
Args:
4141
results_per_page (int): Specify the results_per_page as integer (default 1000)
42-
pages (int): Specify the pages as integer (default 1)
43-
query (str): Specify the query (default None)
42+
page (int): Specify the page as integer (default 1)
43+
sort (str): Specify the sort option. Valid values are login-asc, login-desc, email-asc, email-desc, name-asc, name-desc, lastSeenAtAge-asc and lastSeenAtAge-desc. By default, if sort is not specified, the user list will be ordered by login, email in ascending order (default None)
4444
4545
Raises:
4646
Exception: Unspecified error by executing the API call
@@ -50,11 +50,11 @@ def search_users(
5050
"""
5151

5252
api_request_url: str = (
53-
f"{APIEndpoints.USERS.value}/search?perpage={results_per_page}&page={pages}"
53+
f"{APIEndpoints.USERS.value}?perpage={results_per_page}&page={page}"
5454
)
5555

56-
if query is not None and len(query) != 0:
57-
api_request_url: str = f"{api_request_url}&query={query}"
56+
if sort is not None and len(sort) != 0:
57+
api_request_url: str = f"{api_request_url}&sort={sort}"
5858

5959
api_call: list = Api(self.grafana_api_model).call_the_api(
6060
api_request_url,
@@ -66,6 +66,52 @@ def search_users(
6666
else:
6767
return api_call
6868

69+
def search_users_with_paging(
70+
self,
71+
results_per_page: int = 1000,
72+
page: int = 1,
73+
query: str = None,
74+
sort: str = None
75+
) -> dict:
76+
"""The method includes a functionality to get all Grafana system users specified by the optional results_per_page, page, query, sort and general paging functionality
77+
78+
Required Permissions:
79+
Action: users:read
80+
Scope: global.users:*
81+
82+
Args:
83+
results_per_page (int): Specify the results_per_page as integer (default 1000)
84+
page (int): Specify the page as integer (default 1)
85+
query (str): Specify the query (default None)
86+
sort (str): Specify the sort option. Valid values are login-asc, login-desc, email-asc, email-desc, name-asc, name-desc, lastSeenAtAge-asc and lastSeenAtAge-desc. By default, if sort is not specified, the user list will be ordered by login, email in ascending order (default None)
87+
88+
Raises:
89+
Exception: Unspecified error by executing the API call
90+
91+
Returns:
92+
api_call (dict): Returns the Grafana users
93+
"""
94+
95+
api_request_url: str = (
96+
f"{APIEndpoints.USERS.value}/search?perpage={results_per_page}&page={page}"
97+
)
98+
99+
if query is not None and len(query) != 0:
100+
api_request_url: str = f"{api_request_url}&query={query}"
101+
102+
if sort is not None and len(sort) != 0:
103+
api_request_url: str = f"{api_request_url}&sort={sort}"
104+
105+
api_call: dict = Api(self.grafana_api_model).call_the_api(
106+
api_request_url,
107+
)
108+
109+
if api_call == dict() or api_call.get("users") is None:
110+
logging.error(f"Check the error: {api_call}.")
111+
raise Exception
112+
else:
113+
return api_call
114+
69115
def get_user_by_id(self, id: int) -> dict:
70116
"""The method includes a functionality to get a specific user by the id
71117

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setuptools.setup(
1010
name="grafana-api-sdk",
11-
version="0.6.1",
11+
version="0.7.0",
1212
author="Pascal Zimmermann",
1313
author_email="[email protected]",
1414
description="A Grafana API SDK",

tests/unittests/test_user.py

+53-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_search_users(self, call_the_api_mock):
1818
self.assertEqual(list([{"id": 1}]), user.search_users())
1919

2020
@patch("grafana_api.api.Api.call_the_api")
21-
def test_search_users_query(self, call_the_api_mock):
21+
def test_search_users_sort(self, call_the_api_mock):
2222
model: APIModel = APIModel(
2323
host=MagicMock(), username=MagicMock(), password=MagicMock()
2424
)
@@ -28,7 +28,7 @@ def test_search_users_query(self, call_the_api_mock):
2828

2929
self.assertEqual(
3030
list([{"id": 1}]),
31-
user.search_users(query="Test"),
31+
user.search_users(sort="login-asc"),
3232
)
3333

3434
@patch("grafana_api.api.Api.call_the_api")
@@ -43,6 +43,57 @@ def test_search_users_no_users(self, call_the_api_mock):
4343
with self.assertRaises(Exception):
4444
user.search_users()
4545

46+
@patch("grafana_api.api.Api.call_the_api")
47+
def test_search_users_with_paging(self, call_the_api_mock):
48+
model: APIModel = APIModel(
49+
host=MagicMock(), username=MagicMock(), password=MagicMock()
50+
)
51+
user: User = User(grafana_api_model=model)
52+
53+
call_the_api_mock.return_value = dict({"users": []})
54+
55+
self.assertEqual(dict({"users": []}), user.search_users_with_paging())
56+
57+
@patch("grafana_api.api.Api.call_the_api")
58+
def test_search_users_with_paging_query(self, call_the_api_mock):
59+
model: APIModel = APIModel(
60+
host=MagicMock(), username=MagicMock(), password=MagicMock()
61+
)
62+
user: User = User(grafana_api_model=model)
63+
64+
call_the_api_mock.return_value = dict({"users": []})
65+
66+
self.assertEqual(
67+
dict({"users": []}),
68+
user.search_users_with_paging(query="test"),
69+
)
70+
71+
@patch("grafana_api.api.Api.call_the_api")
72+
def test_search_users_with_paging_sort(self, call_the_api_mock):
73+
model: APIModel = APIModel(
74+
host=MagicMock(), username=MagicMock(), password=MagicMock()
75+
)
76+
user: User = User(grafana_api_model=model)
77+
78+
call_the_api_mock.return_value = dict({"users": []})
79+
80+
self.assertEqual(
81+
dict({"users": []}),
82+
user.search_users_with_paging(sort="login-asc"),
83+
)
84+
85+
@patch("grafana_api.api.Api.call_the_api")
86+
def test_search_users_with_paging_no_users(self, call_the_api_mock):
87+
model: APIModel = APIModel(
88+
host=MagicMock(), username=MagicMock(), password=MagicMock()
89+
)
90+
user: User = User(grafana_api_model=model)
91+
92+
call_the_api_mock.return_value = dict()
93+
94+
with self.assertRaises(Exception):
95+
user.search_users_with_paging()
96+
4697
@patch("grafana_api.api.Api.call_the_api")
4798
def test_get_user_by_id(self, call_the_api_mock):
4899
model: APIModel = APIModel(

0 commit comments

Comments
 (0)