Edit JsonData object

I’m writing a datasource plugin with a backend component and everything is working fine.

I’d like to be able to edit the jsonData object of the datasource that will be persisted by Grafana. Either from the datasource class of the frontend or from the backend part. Can I do this?

Yes, if you inject the backendSrv service into your constructor then you have access to the Grafana api and can update the jsonData for the datasource.

You need to fetch the datasource first. Here is an example from the kubernetes app:

Then you can save it with backendSrv.put:

1 Like

Thanks a lot!

Is something similar possible from a datasource backend plugin component? I see that built-in datasources have access to it, e.g. stackdriver, see https://github.com/grafana/grafana/blob/6724aaeff9a332dc73b4ee0f8abe0621f7253142/pkg/tsdb/stackdriver/stackdriver.go#L96. Is something like this possible from a datasource backend plugin?

You mean if it is possible to get access to the jsonData blob from the backend in Go?

Yes, the DatasourceRequest object has a DatasourceInfo field. It is passed in as the second parameter to the Query function.

But can I also write to it? I don’t think I can edit it, right?

My specific use-case: I’m using a backend datasource plugin to deal with some non-standard OAuth authentication issue I’m having. I can’t use Grafana’s OAuth support because of some detail. Everything works great with my backend plugin but it would be great if it could store e.g. the refresh token in a secure way. But I don’t think this is possible for datasource backend plugins. Is this correct? Could I submit this as a feature request to the Github repository?