Skip to content

Commit 7115e2d

Browse files
authored
Add the short url and organization feature (#5)
* Add the short url feature * Add basic organisation support * Add organisation feature and unittests * Add integrationtest and update the documentation * Add coverage badge and documentation
1 parent 3888346 commit 7115e2d

File tree

16 files changed

+1464
-74
lines changed

16 files changed

+1464
-74
lines changed

README.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ The repository includes an SDK for the Grafana API. It's possible to communicate
5757
- Get all Alertmanager alerts
5858
- Create or update Alertmanager alerts
5959
- Get Alertmanager group alerts
60-
6160
- Get all Alertmanager silences
6261
- Get Alertmanager silence by id
6362
- Create or update Alertmanager silence
@@ -94,13 +93,35 @@ The repository includes an SDK for the Grafana API. It's possible to communicate
9493
- Delete a notification channel by uid
9594
- Test a notification channel
9695

96+
### Organization
97+
- Get current organisation
98+
- Update the current organisation name
99+
- Add a new user and the role to the current organisation
100+
- Get all users from current organisation
101+
- Get all users from current organisation (lookup)
102+
- Update the role of an organisation user by the user id
103+
- Delete an organisation user by the user id
104+
- Get an organisation by the id
105+
- Get an organisation by the name
106+
- Get all organisations
107+
- Create an organisation
108+
- Update an organisation
109+
- Delete an organisation
110+
- Get organisation users
111+
- Add a new organisation user
112+
- Update an organisation user
113+
- Delete an organisation user
114+
115+
### Short URL
116+
- Create a short url
117+
97118
## Feature timeline
98119

99120
The following table describes the plan to implement the rest of the Grafana API functionality. Please, open an issue and vote them up, if you prefer a faster implementation of an API functionality.
100121

101122
| API endpoint group | Implementation week | Maintainer | PR | State |
102123
|:------------------:|:-------------------:|:----------:|:--:|:-----:|
103-
| [Admin HTTP API](https://grafana.com/docs/grafana/latest/http_api/admin/) | | | | |
124+
| [Admin HTTP API](https://grafana.com/docs/grafana/latest/http_api/admin/) | 16 | [ZPascal](https://github.com/ZPascal) | | |
104125
| [Annotations HTTP API](https://grafana.com/docs/grafana/latest/http_api/annotations/) | | | | |
105126
| [Authentication HTTP API](https://grafana.com/docs/grafana/latest/http_api/auth/) | | | | |
106127
| [External Group Sync HTTP API](https://grafana.com/docs/grafana/latest/http_api/external_group_sync/) | | | | |
@@ -109,13 +130,11 @@ The following table describes the plan to implement the rest of the Grafana API
109130
| [HTTP Snapshot API](https://grafana.com/docs/grafana/latest/http_api/snapshot/) | | | | |
110131
| [Library Element HTTP API](https://grafana.com/docs/grafana/latest/http_api/library_element/) | | | | |
111132
| [Licensing HTTP API](https://grafana.com/docs/grafana/latest/http_api/licensing/) | | | | |
112-
| [Organization HTTP API](https://grafana.com/docs/grafana/latest/http_api/org/) | 13 | | | In process |
113133
| [Other HTTP API](https://grafana.com/docs/grafana/latest/http_api/other/) | | | | |
114134
| [Playlist HTTP API](https://grafana.com/docs/grafana/latest/http_api/playlist/) | | | | |
115135
| [Reporting API](https://grafana.com/docs/grafana/latest/http_api/reporting/) | | | | |
116-
| [Short URL HTTP API](https://grafana.com/docs/grafana/latest/http_api/short_url/) | 13 | | | In process |
117136
| [Team HTTP API](https://grafana.com/docs/grafana/latest/http_api/team/) | | | | |
118-
| [User HTTP API](https://grafana.com/docs/grafana/latest/http_api/user/) | | | | |
137+
| [User HTTP API](https://grafana.com/docs/grafana/latest/http_api/user/) | 16 | [ZPascal](https://github.com/ZPascal) | | |
119138

120139
## Installation
121140

docs/content/grafana_api/model.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ The class includes all necessary variables to establish a connection to the Graf
5050

5151
- `host` _str_ - Specify the host of the Grafana system
5252
- `token` _str_ - Specify the access token of the Grafana system
53+
- `username` _str_ - Specify the username of the Grafana system
54+
- `password` _str_ - Specify the password of the Grafana system
5355

5456
<a id="grafana_api.model.DatasourceQuery"></a>
5557

docs/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
requests
2-
pydoc-markdown
2+
pydoc-markdown==4.6.2
33
mkdocs
44
mkdocs-material

src/grafana_api/api.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,22 @@ def call_the_api(
3838
"""
3939

4040
api_url: str = f"{self.grafana_api_model.host}{api_call}"
41+
headers: dict = dict(
42+
{"Authorization": f"Bearer {self.grafana_api_model.token}"}
43+
)
44+
45+
if (
46+
self.grafana_api_model.username is not None
47+
and self.grafana_api_model.password is not None
48+
):
49+
url: str = (
50+
f"{self.grafana_api_model.username}:{self.grafana_api_model.password}@"
51+
)
52+
api_url = api_url.replace("https://", f"https://{url}")
53+
api_url = api_url.replace("http://", f"http://{url}")
54+
else:
55+
headers["Content-Type"] = "application/json"
4156

42-
headers: dict = {
43-
"Authorization": f"Bearer {self.grafana_api_model.token}",
44-
"Content-Type": "application/json",
45-
}
4657
try:
4758
if method.value == RequestsMethods.GET.value:
4859
return Api.__check_the_api_call_response(
@@ -64,6 +75,14 @@ def call_the_api(
6475
else:
6576
logging.error("Please define the json_complete.")
6677
raise Exception
78+
elif method.value == RequestsMethods.PATCH.value:
79+
if json_complete is not None:
80+
return Api.__check_the_api_call_response(
81+
requests.patch(api_url, data=json_complete, headers=headers)
82+
)
83+
else:
84+
logging.error("Please define the json_complete.")
85+
raise Exception
6786
elif method.value == RequestsMethods.DELETE.value:
6887
return Api.__check_the_api_call_response(
6988
requests.delete(api_url, headers=headers)

src/grafana_api/model.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class APIEndpoints(Enum):
2020
ALERTS_NGALERT = "/api/v1/ngalert"
2121
DATASOURCES = "/api/datasources"
2222
DATASOURCE_QUERY = "/api/tsdb/query"
23+
SHORT_URLS = "/api/short-urls"
24+
ORGANISATION = "/api/org"
25+
ORGANISATIONS = "/api/orgs"
2326

2427

2528
class RequestsMethods(Enum):
@@ -28,6 +31,7 @@ class RequestsMethods(Enum):
2831
GET = "GET"
2932
PUT = "PUT"
3033
POST = "POST"
34+
PATCH = "PATCH"
3135
DELETE = "DELETE"
3236

3337

@@ -37,10 +41,14 @@ class APIModel(NamedTuple):
3741
Args:
3842
host (str): Specify the host of the Grafana system
3943
token (str): Specify the access token of the Grafana system
44+
username (str): Specify the username of the Grafana system
45+
password (str): Specify the password of the Grafana system
4046
"""
4147

4248
host: str
43-
token: str
49+
token: str = None
50+
username: str = None
51+
password: str = None
4452

4553

4654
class DatasourceQuery(NamedTuple):

0 commit comments

Comments
 (0)