Skip to content

Commit 33b9a94

Browse files
committed
fix: Adjust the user search functionality
1 parent 7f55514 commit 33b9a94

File tree

3 files changed

+143
-11
lines changed

3 files changed

+143
-11
lines changed

docs/content/grafana_api/user.md

Lines changed: 39 additions & 4 deletions
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,44 @@ 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
60+
61+
Required Permissions:
62+
Action: users:read
63+
Scope: global.users:*
64+
65+
**Arguments**:
66+
67+
- `results_per_page` _int_ - Specify the results_per_page as integer (default 1000)
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)
70+
71+
72+
**Raises**:
73+
74+
- `Exception` - Unspecified error by executing the API call
75+
76+
77+
**Returns**:
78+
79+
- `api_call` _list_ - Returns the list of Grafana users
80+
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+
pages: 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
5993

6094
Required Permissions:
6195
Action: users:read
@@ -66,6 +100,7 @@ Scope: global.users:*
66100
- `results_per_page` _int_ - Specify the results_per_page as integer (default 1000)
67101
- `pages` _int_ - Specify the pages as integer (default 1)
68102
- `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)
69104

70105

71106
**Raises**:
@@ -75,7 +110,7 @@ Scope: global.users:*
75110

76111
**Returns**:
77112

78-
- `api_call` _list_ - Returns the list of Grafana users
113+
- `api_call` _dict_ - Returns the Grafana users
79114

80115
<a id="user.User.get_user_by_id"></a>
81116

grafana_api/user.py

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,54 @@ def __init__(self, grafana_api_model: APIModel):
2626
self.grafana_api_model = grafana_api_model
2727

2828
def search_users(
29+
self,
30+
results_per_page: int = 1000,
31+
page: int = 1,
32+
sort: str = None,
33+
) -> list:
34+
"""The method includes a functionality to get all Grafana system users specified by the optional results_per_page, page and sort option
35+
36+
Required Permissions:
37+
Action: users:read
38+
Scope: global.users:*
39+
40+
Args:
41+
results_per_page (int): Specify the results_per_page as integer (default 1000)
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)
44+
45+
Raises:
46+
Exception: Unspecified error by executing the API call
47+
48+
Returns:
49+
api_call (list): Returns the list of Grafana users
50+
"""
51+
52+
api_request_url: str = (
53+
f"{APIEndpoints.USERS.value}?perpage={results_per_page}&page={page}"
54+
)
55+
56+
if sort is not None and len(sort) != 0:
57+
api_request_url: str = f"{api_request_url}&sort={sort}"
58+
59+
api_call: list = Api(self.grafana_api_model).call_the_api(
60+
api_request_url,
61+
)
62+
63+
if api_call == list() or api_call[0].get("id") is None:
64+
logging.error(f"Check the error: {api_call}.")
65+
raise Exception
66+
else:
67+
return api_call
68+
69+
def search_users_with_paging(
2970
self,
3071
results_per_page: int = 1000,
3172
pages: int = 1,
3273
query: str = None,
33-
) -> list:
34-
"""The method includes a functionality to get all Grafana system users specified by the optional query and paging functionality
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
3577
3678
Required Permissions:
3779
Action: users:read
@@ -41,12 +83,13 @@ def search_users(
4183
results_per_page (int): Specify the results_per_page as integer (default 1000)
4284
pages (int): Specify the pages as integer (default 1)
4385
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)
4487
4588
Raises:
4689
Exception: Unspecified error by executing the API call
4790
4891
Returns:
49-
api_call (list): Returns the list of Grafana users
92+
api_call (dict): Returns the Grafana users
5093
"""
5194

5295
api_request_url: str = (
@@ -56,11 +99,14 @@ def search_users(
5699
if query is not None and len(query) != 0:
57100
api_request_url: str = f"{api_request_url}&query={query}"
58101

59-
api_call: list = Api(self.grafana_api_model).call_the_api(
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(
60106
api_request_url,
61107
)
62108

63-
if api_call == list() or api_call[0].get("id") is None:
109+
if api_call == dict() or api_call.get("users") is None:
64110
logging.error(f"Check the error: {api_call}.")
65111
raise Exception
66112
else:

tests/unittests/test_user.py

Lines changed: 53 additions & 2 deletions
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)