ZPascal / grafana_api_sdk

The repository includes a Python SDK for the Grafana API
https://zpascal.github.io/grafana_api_sdk/
Apache License 2.0
20 stars 7 forks source link
grafana python3 sdk

Grafana API SDK Coverage report

The repository includes an SDK for the Grafana API. It's possible to interact with all publicly available Grafana HTTP API endpoints.

Differences between grafana-client, grafana_api and the grafana_api_sdk

The grafana-client is only a fork of the non-maintained grafana_api repository. In general, the grafana-client project started at the same time, as I started this project. The corresponding SDK is a completely new project based on nonother projects and includes a few features that are currently not implemented inside the grafana-client.

The core features that are implemented inside this library:

In general, my focus on this project is to implement and deliver old and new features from the Grafana API, to document all features and functionality clear,ly and to increase the overall test coverage of the project.

Currently, supported features

Dashboard

Folder

Search

Datasource

Datasource permissions

Legacy Datasource permissions

Datasource query and resource caching

Legacy Alerting

Alerting

Alerting Channels

Alerting Provisioning

Organization

Short URL

User

Snapshot

Team

Legacy Playlist

Playlist

Reporting

Query History

Other HTTP

Licensing

Annotation

External Groups

Authentication

Preferences

Admin

Service Account

RBAC

Library

Correlations

SSO Settings

Installation

Please be aware to not install the grafana-api and grafana-api-sdk packages in parallel and the same environment. This result in name clashes, and it's not possible to use the Grafana API SDK.

pip install grafana-api-sdk

Example

import json

from grafana_api.model import APIModel
from grafana_api.dashboard import Dashboard

model: APIModel = APIModel(host="test", token="test")

dashboard: Dashboard = Dashboard(model)

with open("/tmp/test/test.json") as file:
    json_dashboard = json.load(file)

dashboard.create_or_update_dashboard(message="Create a new test dashboard", dashboard_json=json_dashboard, dashboard_path="test")

TLS/ mTLS

It is possible to pass a custom ssl_context to the underlying library to perform the requests to the HTTP API. For this step and to support custom TLS/ mTLS, there is an option to inject the Python ssl_context. More information can be found here and a dummy TLS/ mTLS implementation below.

TLS

import ssl

from grafana_api.model import APIModel

ssl_ctx = ssl.create_default_context(
    ssl.Purpose.SERVER_AUTH,
    cafile="/test/path/ca.crt"
)
ssl_ctx.verify_mode = ssl.CERT_REQUIRED

model: APIModel = APIModel(host="test", token="test", ssl_context=ssl_ctx)

mTLS

import ssl

from grafana_api.model import APIModel

ssl_ctx = ssl.create_default_context(
    ssl.Purpose.SERVER_AUTH,
    cafile="/test/path/ca.crt",
)
ssl_ctx.verify_mode = ssl.CERT_REQUIRED
ssl_ctx.load_cert_chain(certfile="/test/path/client.crt", keyfile="/test/path/client.key",)

model: APIModel = APIModel(host="test", token="test", ssl_context=ssl_ctx)

Templating

If you want to template your JSON document based on a predefined folder structure you can check out one of my other project and integrate the functionality inside your code.

Contribution

If you would like to contribute something, have an improvement request, or want to make a change inside the code, please open a pull request.

Support

If you need support, or you encounter a bug, please don't hesitate to open an issue.

Donations

If you would like to support my work, I ask you to take an unusual action inside the open source community. Donate the money to a non-profit organization like Doctors Without Borders or the Children's Cancer Aid. I will continue to build tools because I like it, and it is my passion to develop and share applications.

License

This product is available under the Apache 2.0 license.