0

I have a datasource created in Grafana and attempting to update it to refresh the bearer token for auth access.

However, I'm receiving a 404 Not Found error from the grafana api when making a request to localhost:3000/api/datasources/uid/:uid with a uid just received from the datasources/name api - attempting to update as per the documentation https://grafana.com/docs/grafana/latest/developers/http_api/data_source/#update-an-existing-data-source

I'm using the grafana opensource docker container with the Infinity plugin.

docker run -d -p 3000:3000 --name=grafana -e "GF_INSTALL_PLUGINS=yesoreyeram-infinity-datasource" grafana/grafana-oss

I'm able to create a datasource via the api, just can't update an existing one.

My code is:-

grafana_api_token = '<my api token>'
new_access_token = '<my new bearer token>'
my_data_source = 'my_data_source'

grafana_header = {"authorization": f"Bearer {grafana_api_token}", "content-type":"application/json;charset=UTF-8"}
grafana_datasource_url = f"http://localhost:3000/api/datasources/name/{my_data_source}"

firebolt_datasource_resp = get(url=grafana_datasource_url, headers=grafana_header)
full_datasource = loads(firebolt_datasource_resp.content.decode("utf-8"))
datasource_uid = full_datasource["uid"]

update_token_url = f"http://localhost:3000/api/datasources/uid/{datasource_uid}"

new_data = {"id": full_datasource["id"],
            "uid": full_datasource["uid"],
            "orgId": full_datasource["orgId"],
            "name": "new_data_source",
            "type": full_datasource["type"],
            "access": full_datasource["access"],
            "url": full_datasource["url"],
            "user": full_datasource["user"],
            "database": full_datasource["database"],
            "basicAuth": full_datasource["basicAuth"],
            "basicAuthUser": full_datasource["basicAuthUser"],
            "withCredentials": full_datasource["withCredentials"],
            "isDefault": full_datasource["isDefault"],
            "jsonData": full_datasource["jsonData"],
            "secureJsonData": {            
                    "bearerToken": new_access_token
                }
}

update_bearer_token_resp = post(url=update_token_url, data=dumps(new_data), headers=grafana_header)
Matthew Darwin
  • 325
  • 1
  • 10

1 Answers1

-1

Oh, oh, oh, idiot mode. Using post rather than put. Doh.

Matthew Darwin
  • 325
  • 1
  • 10