Skip to content
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
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ The repository includes an SDK for the Grafana API. It's possible to communicate
- Get all Alertmanager alerts
- Create or update Alertmanager alerts
- Get Alertmanager group alerts

- Get all Alertmanager silences
- Get Alertmanager silence by id
- Create or update Alertmanager silence
Expand Down Expand Up @@ -94,13 +93,35 @@ The repository includes an SDK for the Grafana API. It's possible to communicate
- Delete a notification channel by uid
- Test a notification channel

### Organization
- Get current organisation
- Update the current organisation name
- Add a new user and the role to the current organisation
- Get all users from current organisation
- Get all users from current organisation (lookup)
- Update the role of an organisation user by the user id
- Delete an organisation user by the user id
- Get an organisation by the id
- Get an organisation by the name
- Get all organisations
- Create an organisation
- Update an organisation
- Delete an organisation
- Get organisation users
- Add a new organisation user
- Update an organisation user
- Delete an organisation user

### Short URL
- Create a short url

## Feature timeline

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.

| API endpoint group | Implementation week | Maintainer | PR | State |
|:------------------:|:-------------------:|:----------:|:--:|:-----:|
| [Admin HTTP API](https://grafana.com/docs/grafana/latest/http_api/admin/) | | | | |
| [Admin HTTP API](https://grafana.com/docs/grafana/latest/http_api/admin/) | 16 | [ZPascal](https://github.com/ZPascal) | | |
| [Annotations HTTP API](https://grafana.com/docs/grafana/latest/http_api/annotations/) | | | | |
| [Authentication HTTP API](https://grafana.com/docs/grafana/latest/http_api/auth/) | | | | |
| [External Group Sync HTTP API](https://grafana.com/docs/grafana/latest/http_api/external_group_sync/) | | | | |
Expand All @@ -109,13 +130,11 @@ The following table describes the plan to implement the rest of the Grafana API
| [HTTP Snapshot API](https://grafana.com/docs/grafana/latest/http_api/snapshot/) | | | | |
| [Library Element HTTP API](https://grafana.com/docs/grafana/latest/http_api/library_element/) | | | | |
| [Licensing HTTP API](https://grafana.com/docs/grafana/latest/http_api/licensing/) | | | | |
| [Organization HTTP API](https://grafana.com/docs/grafana/latest/http_api/org/) | 13 | | | In process |
| [Other HTTP API](https://grafana.com/docs/grafana/latest/http_api/other/) | | | | |
| [Playlist HTTP API](https://grafana.com/docs/grafana/latest/http_api/playlist/) | | | | |
| [Reporting API](https://grafana.com/docs/grafana/latest/http_api/reporting/) | | | | |
| [Short URL HTTP API](https://grafana.com/docs/grafana/latest/http_api/short_url/) | 13 | | | In process |
| [Team HTTP API](https://grafana.com/docs/grafana/latest/http_api/team/) | | | | |
| [User HTTP API](https://grafana.com/docs/grafana/latest/http_api/user/) | | | | |
| [User HTTP API](https://grafana.com/docs/grafana/latest/http_api/user/) | 16 | [ZPascal](https://github.com/ZPascal) | | |

## Installation

Expand Down
2 changes: 2 additions & 0 deletions docs/content/grafana_api/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ The class includes all necessary variables to establish a connection to the Graf

- `host` _str_ - Specify the host of the Grafana system
- `token` _str_ - Specify the access token of the Grafana system
- `username` _str_ - Specify the username of the Grafana system
- `password` _str_ - Specify the password of the Grafana system

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

Expand Down
4 changes: 2 additions & 2 deletions docs/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
requests
pydoc-markdown
pydoc-markdown==4.6.2
mkdocs
mkdocs-material
27 changes: 23 additions & 4 deletions src/grafana_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,22 @@ def call_the_api(
"""

api_url: str = f"{self.grafana_api_model.host}{api_call}"
headers: dict = dict(
{"Authorization": f"Bearer {self.grafana_api_model.token}"}
)

if (
self.grafana_api_model.username is not None
and self.grafana_api_model.password is not None
):
url: str = (
f"{self.grafana_api_model.username}:{self.grafana_api_model.password}@"
)
api_url = api_url.replace("https://", f"https://{url}")
api_url = api_url.replace("http://", f"http://{url}")
else:
headers["Content-Type"] = "application/json"

headers: dict = {
"Authorization": f"Bearer {self.grafana_api_model.token}",
"Content-Type": "application/json",
}
try:
if method.value == RequestsMethods.GET.value:
return Api.__check_the_api_call_response(
Expand All @@ -64,6 +75,14 @@ def call_the_api(
else:
logging.error("Please define the json_complete.")
raise Exception
elif method.value == RequestsMethods.PATCH.value:
if json_complete is not None:
return Api.__check_the_api_call_response(
requests.patch(api_url, data=json_complete, headers=headers)
)
else:
logging.error("Please define the json_complete.")
raise Exception
elif method.value == RequestsMethods.DELETE.value:
return Api.__check_the_api_call_response(
requests.delete(api_url, headers=headers)
Expand Down
10 changes: 9 additions & 1 deletion src/grafana_api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class APIEndpoints(Enum):
ALERTS_NGALERT = "/api/v1/ngalert"
DATASOURCES = "/api/datasources"
DATASOURCE_QUERY = "/api/tsdb/query"
SHORT_URLS = "/api/short-urls"
ORGANISATION = "/api/org"
ORGANISATIONS = "/api/orgs"


class RequestsMethods(Enum):
Expand All @@ -28,6 +31,7 @@ class RequestsMethods(Enum):
GET = "GET"
PUT = "PUT"
POST = "POST"
PATCH = "PATCH"
DELETE = "DELETE"


Expand All @@ -37,10 +41,14 @@ class APIModel(NamedTuple):
Args:
host (str): Specify the host of the Grafana system
token (str): Specify the access token of the Grafana system
username (str): Specify the username of the Grafana system
password (str): Specify the password of the Grafana system
"""

host: str
token: str
token: str = None
username: str = None
password: str = None


class DatasourceQuery(NamedTuple):
Expand Down
Loading