Example of backend datasource plugins writing data back to store?

From the documentation, I found this:

Send a command to a device, such as a micro controller or IOT device.

Are there any examples that would show how this is done?

I’m working on integration with Simple IoT where we want to change configuration settings in a Grafana dashboard that then get written to SIOT.

I’ve experimented with the Button panel, but cors issues makes this messy. It would be much cleaner if I could run some Go code in the backend to talk NATS directly to SIOT.

For a frontend data source solution, you can check out this code example.

For a backend data source solution, you can register a resource handler, which extends the Grafana HTTP API with plugin-specific endpoints, which you can then call from your dashboard.

Since you specifically asked for examples of data source plugins, here’s how you extend your backend plugin with a resource handler:

func (d *SampleDatasource) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
	items, err := d.client.ListItems()
	if err != nil {
		return err
	}

	b, err := json.Marshal(items)
	if err != nil {
		return err
	}

	return sender.Send(&backend.CallResourceResponse{
		Status: http.StatusOK,
		Body:   b,
	})
}

You can then access the plugin resource from: /api/datasources/<your-plugin-id>/resources/, where your-plugin-id is an integer that identifies the data source instance you want to access.

Here’s the frontend code to query the data source resource:

const items = await getBackendSrv()
                .fetch<any>({
                  url: `/api/datasources/${props.datasource.id}/resources/`,
                })
                .toPromise();
2 Likes

thanks for the pointers!

1 Like

For anyone dropping in here from Google, I’ve shared a few more examples on how to use resource handlers in this post:

https://localhost:3000/t/how-to-add-a-resource-handler-for-your-data-source/58924

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.