grafana_api
grafana_api copied to clipboard
Error when switching to another organization
What API call you use? Provide a small example to reproduce the bug.
grafana_api = GrafanaFace(auth=(config["grafana_user"],config["grafana_pass"]),host=config["grafana_url"]) print(grafana_api.organizations.list_organization()) print(grafana_api.organization.get_current_organization()) grafana_api.organizations.switch_organization(organization_id=15)
The first two queries work fine, the last one returns a JSON error.
What is the actual output? Copy or describe the actual result.
Traceback (most recent call last): File "provision_api.py", line 19, in
grafana_api.organizations.switch_organization(organization_id=15) File "/home/yentel/.local/lib/python3.7/site-packages/grafana_api/api/organization.py", line 132, in switch_organization r = self.api.POST(switch_user_organization) File "/home/yentel/.local/lib/python3.7/site-packages/grafana_api/grafana_api.py", line 136, in __request_runnner return r.json() File "/usr/lib/python3/dist-packages/requests/models.py", line 897, in json return complexjson.loads(self.text, **kwargs) File "/usr/lib/python3/dist-packages/simplejson/init.py", line 518, in loads return _default_decoder.decode(s) File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode obj, end = self.raw_decode(s) File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode return self.scan_once(s, idx=_w(s, idx).end()) simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
What is the expected output? Something without an error that tells me I've switched to organization with id 15? :-)
FYI: I've been testing different servers. It works on my personal grafana instance but not on the one we use at work. So far, after setting server settings to the exact same values, this does not work on my work instance.
The only difference so far is that the one on my work is using version "Grafana v7.1.3 (5723d951af)"
Deleting a datasource also seems to work... which is also a post request... I'm out of options here
It should either output the 200 response with:
{"message":"Active organization changed"}
Or some other message, I was not able to reproduce it with multiple Grafana versions.
From the error, it's successfully switches the organization (or at least returning status code less than 400) and returns non-JSON response:
File "/home/yentel/.local/lib/python3.7/site-packages/grafana_api/grafana_api.py", line 136, in __request_runnner
return r.json()
I'd recommend to test cURL to see what's going on (see below), unfortunately, right now the library doesn't have nice debug capabilities (though, I have this in mind).
As for cURL, you can run the following command and see what's going on:
curl -X POST -u myusername:mypassword http://somesite.com:port/api/user/using/15
Sorry for the late reply, some other project got prio above this provisioning grafana side project.
curl -X POST -u admin:somethingsecret https://dashboard.myorg.com/api/user/using/7
{"message":"Active organization changed}
However, if I run my script, I get the same error as above :-( Code:
grafana_api = GrafanaFace(auth=(config["grafana_user"],config["grafana_pass"]),host=config["grafana_url"])
print(grafana_api.organizations.list_organization())
print(grafana_api.organization.get_current_organization())
grafana_api.organizations.switch_organization(organization_id=1)
The first two prints work fine, thus, authentication seems to work. However, the last part, switching orgs, fails with this error:
Traceback (most recent call last):
File "provision_api.py", line 18, in <module>
grafana_api.organizations.switch_organization(organization_id=1)
File "/usr/local/lib/python3.7/site-packages/grafana_api/api/organization.py", line 132, in switch_organization
r = self.api.POST(switch_user_organization)
File "/usr/local/lib/python3.7/site-packages/grafana_api/grafana_api.py", line 136, in __request_runnner
return r.json()
File "/usr/local/lib/python3.7/site-packages/requests/models.py", line 898, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
python3 --version
Python 3.7.7
pip3 install grafana_api
Requirement already satisfied: grafana_api in /usr/local/lib/python3.7/site-packages (1.0.3)
Requirement already satisfied: requests>=2.23.0 in /usr/local/lib/python3.7/site-packages (from grafana_api) (2.24.0)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/site-packages (from requests>=2.23.0->grafana_api) (1.25.8)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/site-packages (from requests>=2.23.0->grafana_api) (2.8)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/site-packages (from requests>=2.23.0->grafana_api) (2019.11.28)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/site-packages (from requests>=2.23.0->grafana_api) (3.0.4)