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
22 changes: 22 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[run]
branch = True
omit =
*/setup.py
source = .

[report]
omit =
*/setup.py
fail_under = 80
show_missing = True
skip_covered = False
exclude_lines =
# Have to re-enable the standard pragma
pragma: no cover

# Don't complain if tests don't hit defensive assertion code:
raise AssertionError
raise NotImplementedError

# Don't complain if non-runnable code isn't run:
if __name__ == .__main__.:
6 changes: 6 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[flake8]
ignore = E203, E266, E501, W503
max-line-length = 120
max-complexity = 20
select = B,C,D,E,F,W,T4,B902,B950
exclude = venc,.git
26 changes: 26 additions & 0 deletions .github/workflows/integrationtest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Integrationtest

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:

test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Install the requirements
run: pip install -r requirements.txt

- name: Execute the integrationtests
run: python3 -m unittest discover tests/integrationtest
env:
GRAFANA_HOST: ${{ secrets.GRAFANA_HOST }}
GRAFANA_TOKEN: ${{ secrets.GRAFANA_TOKEN }}
GRAFANA_DASHBOARD_PATH: ${{ secrets.GRAFANA_DASHBOARD_PATH }}
GRAFANA_DASHBOARD_NAME: ${{ secrets.GRAFANA_DASHBOARD_NAME }}
48 changes: 48 additions & 0 deletions .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build and publish

on:
release:
types: [ published ]

jobs:

build-and-publish:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.x']

steps:
- name: Checkout the repository and the branch
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: x64
cache: 'pip'

- name: Install the requirements
run: pip install -r requirements.txt

- name: Install pypa/build
run: >-
python -m
pip install
build
--user

- name: Build a binary wheel and a source tarball
run: >-
python -m
build
--sdist
--wheel
--outdir dist/

- name: Publish distribution package to PyPI
if: startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.PYPI_API_TOKEN }}
114 changes: 114 additions & 0 deletions .github/workflows/pull-request-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
name: PR checks

on:
pull_request:
branches: [ main ]

jobs:

test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ '3.x' ]

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: x64
cache: 'pip'

- name: Install the requirements
run: pip install -r requirements.txt

- name: Execute the unittests
run: python3 -m unittest discover tests/unittests

lint:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ '3.x' ]

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: x64
cache: 'pip'

- name: Install the requirements
run: pip install -r requirements.txt

- name: Execute the linting checks
uses: reviewdog/action-flake8@v3.2.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
flake8_args: --config=.flake8

coverage:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ '3.x' ]

steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: x64
cache: 'pip'

- name: Install the requirements
run: pip install -r requirements.txt && pip install pytest pytest-cov coverage-badge

- name: Generate the coverage report
run: export PYTHONPATH=$PWD && pytest --junitxml=pytest.xml --cov=. tests/unittests | tee pytest-coverage.txt

- name: Execute the coverage checks
uses: MishaKav/pytest-coverage-comment@v1.1.16
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
pytest-coverage-path: ./pytest-coverage.txt
junitxml-path: ./pytest.xml
hide-badge: true
create-new-commit: true

- name: "Check if coverage badge file existence"
id: check_files
uses: andstor/file-existence-action@v1
with:
files: "docs/coverage.svg"

- name: Generate coverage badge
if: steps.check_files.outputs.files_exists == 'false'
run: coverage-badge -o docs/coverage.svg -f

- name: Commit files
if: steps.check_files.outputs.files_exists == 'false'
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add --force docs/coverage.svg
git commit -m "Add coverage badge"

- name: Push changes
if: steps.check_files.outputs.files_exists == 'false'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.head_ref }}
force: true
88 changes: 73 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,84 @@
# grafana_api_sdk
The repository includes an SDK for the Grafana API

## TODO
- Documentation
- Docstrings
- PYPI support
# Grafana API SDK ![Coverage report](https://github.com/ZPascal/grafana_api_sdk/blob/main/docs/coverage.svg)
The repository includes an SDK for the Grafana API. It's possible to communicate with the Grafana API endpoints. Another feature of the SDK is the possibility to specify the used folder for the dashboard.

## Currently, supported features

- Get a Dashboard by uid
- Get folder id by dashboard path
- Get all folder ids and folder names
- Specify the grafana dashboard folder
### Dashboard
- Create/ Update a dashboard
- Delete a dashboard
- Get permissions of a dashboard
- Update the permissions of a dashboard
- Get all dashboard versions
- Get dashboard version of a specific dashboard
- Restore a dashboard version of a specific dashboard
- Compare two dashboard versions and extract the diff between booth dashboards

### Folder
- Get folder id by dashboard path
- Get all folder ids and folder names
- Get all folders
- Get folder by uid
- Get folder by id
- Create a folder
- Update a folder
- Delete a folder
- Get permissions for a folder
- Update permissions for a folder

### Search
- Execute a custom query against the Grafana search endpoint

## 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/) | | | | |
| [Alerting HTTP API](https://grafana.com/docs/grafana/latest/http_api/alerting/) | 4 | [ZPascal](https://github.com/ZPascal) | | Planned |
| [Alerting Notification Channels HTTP API](https://grafana.com/docs/grafana/latest/http_api/alerting_notification_channels/) | 4 | [ZPascal](https://github.com/ZPascal) | | Planned |
| [Annotations HTTP API](https://grafana.com/docs/grafana/latest/http_api/annotations/) | | | | |
| [Authentication HTTP API](https://grafana.com/docs/grafana/latest/http_api/auth/) | | | | |
| [Data source HTTP API](https://grafana.com/docs/grafana/latest/http_api/data_source/) | 5 | [ZPascal](https://github.com/ZPascal) | | Planned |
| [Datasource Permissions HTTP API](https://grafana.com/docs/grafana/latest/http_api/datasource_permissions/) | | | | |
| [External Group Sync HTTP API](https://grafana.com/docs/grafana/latest/http_api/external_group_sync/) | | | | |
| [Fine-grained access control HTTP API](https://grafana.com/docs/grafana/latest/http_api/access_control/) | | | | |
| [HTTP Preferences API](https://grafana.com/docs/grafana/latest/http_api/preferences/) | | | | |
| [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/) | | | | |
| [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/) | | | | |
| [Team HTTP API](https://grafana.com/docs/grafana/latest/http_api/team/) | | | | |
| [User HTTP API](https://grafana.com/docs/grafana/latest/http_api/user/) | | | | |

## Installation

`pip install grafana-api-sdk`

## Example

```python
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)

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

### Programs & tools to install
dashboard.create_or_update_dashboard(message="Create a new test dashboard", dashboard_json=json_dashboard, dashboard_path="test")
```

- json-extensions
- requests
## Templating
If you want to template your JSON document based on a predefined folder structure you can check out one of my other [project](https://github.com/ZPascal/grafana_dashboard_templater) 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.
Expand Down
File renamed without changes.
21 changes: 21 additions & 0 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.
Loading