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

Unable to Authenticate with HTTPS Grafana host with `APIModel()` #26

Closed m1kewu closed 11 months ago

m1kewu commented 1 year ago

Slight change from example:

import json

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

model: APIModel = APIModel(
    host="https://www.my-grafana-site.com", 
    token="my-api-token"    
    )

dashboard: Dashboard = Dashboard(model)
dashboard.get_dashboard_by_uid(uid="abcd-efgh")

And the error is:

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    dashboard.get_dashboard_by_uid(uid="...")
  File "/Users/.../site-packages/grafana_api/dashboard.py", line 133, in get_dashboard_by_uid
    if api_call.get("dashboard") is None:
AttributeError: 'HTTPResponse' object has no attribute 'get'

Do I have to provide a value for ssl_context for HTTPS host? What value would that be? I didn't find any info from docs I'm afraid.

ZPascal commented 1 year ago

Hi @mikewu-tl, It's not necessary to provide a value for the ssl_context because it's an optional parameter. By default, the functionality uses at the end the default ssl context, but the context is in general helpful, if you are using custom certificates or mTLS.

I didn't find any info from docs I'm afraid.

Thank you for pointing it out. I'll add as a follow up a few sentence to the documentation to make it more clear.

Could you please provide me the used version? I'll setup a test case.

m1kewu commented 1 year ago

Hi there, the version of this issue is v9.2.1 (34f7baebda). Thanks. Was the code I provided using Bearer API token correct? Please let me know what I can look into to pull the list of folders.

For context, I'm trying to programmatically scrape all dashboards from all folders as json, and import them into a newer Grafana host (9.5+).

ZPascal commented 1 year ago

Hi @mikewu-tl, are you using the latest version of the grafana-api-sdk ?

Was the code I provided using Bearer API token correct? Please let me know what I can look into to pull the list of folders.

The way you handled it looks too good for me. Could you please describe the way you created the API token/service account? What access rights did you use? My current guess is that the uid is not matched/correctly passed or there is an issue with the token. Do you use a self-signed certificate for the Grafana instances?

The case you just tried should actually work as expected and is being tested here in an integration test.

For context, I'm trying to programmatically scrape all dashboards from all folders as json, and import them into a newer Grafana host (9.5+).

I've got a similar case to back up the dashboards from one system and move it to another Grafana system.

m1kewu commented 1 year ago

hey @ZPascal thanks for looking into this. After some tinkering I finally found out this: the Grafana HTTP API and the request library only supports http/1.1, which means, for companies that self host Grafana on public cloud (most commonly behind a load balancer) often has http/2 for the endpoint, which is unsupported. In Python, httpx would be more suitable for this.

In addition, community discussion (https://community.grafana.com/t/export-dashboard-for-external-use-via-http-api/50716) indicates that there is no programmatic way to fetch exportable JSON, but only in UI.

Thanks again for your time in this, I believe we can close this one.

ZPascal commented 1 year ago

Hi @mikewu-tl

thanks for looking into this. No, problem.

After some tinkering I finally found out this: the Grafana HTTP API and the request library only supports http/1.1, which means, for companies that self host Grafana on public cloud (most commonly behind a load balancer) often has http/2 for the endpoint, which is unsupported. In Python, httpx would be more suitable for this.

Thank you for the update. That explains the case. In general, why should I not support http/2? Feel free to open a feature request!

In addition, community discussion (https://community.grafana.com/t/export-dashboard-for-external-use-via-http-api/50716) indicates that there is no programmatic way to fetch exportable JSON, but only in UI.

I'm currently using the SDK to export dashboards and forward them to another directory inside the same Grafana instance. From my understanding, it should be possible to create the exportable JSON.

ZPascal commented 11 months ago

I'll close this case and further document the new feature request (HTTP/2 support) inside this #33 issue.

ZPascal commented 11 months ago

Hi @mikewu-tl, FYI I've implemented HTTP/2 support for the Grafana API SDK.