Skip to content

Commit 5af914d

Browse files
authored
Merge 8d3ccc6 into 7f55514
2 parents 7f55514 + 8d3ccc6 commit 5af914d

File tree

2 files changed

+103
-6
lines changed

2 files changed

+103
-6
lines changed

grafana_api/user.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,53 @@ 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:
74+
sort: str = None
75+
) -> dict:
3476
"""The method includes a functionality to get all Grafana system users specified by the optional query and paging functionality
3577
3678
Required Permissions:
@@ -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)