-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: Add/update resource handler topics for datasource and app plugins #1081
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b2f7fff
Docs: Add/update resource handler topics for datasource and plugins
marefr c315749
Merge remote-tracking branch 'origin' into docs_878
marefr 11dcb01
Merge remote-tracking branch 'origin' into docs_878
marefr 3274dc0
Merge remote-tracking branch 'origin' into docs_878
marefr 9288065
create shared page and restructure content
marefr 91e2bd5
additional changes
marefr 8933532
simplify and changes after review
marefr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
docusaurus/docs/how-to-guides/app-plugins/add-resource-handler.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
--- | ||
id: add-resource-handler | ||
title: Add resource handler for app plugins | ||
description: Learn how to add a resource handler for app plugins. | ||
keywords: | ||
- grafana | ||
- plugins | ||
- plugin | ||
- app | ||
- resource | ||
- resource handler | ||
--- | ||
|
||
import ImplementResourceHandler from '@shared/implement-resource-handler.md'; | ||
|
||
# Add resource handler for app plugins | ||
|
||
You can add a resource handler to your app backend to extend the Grafana HTTP API with your own app-specific routes. This guide explains why you may want to add [resource](../../key-concepts/backend-plugins/#resources) handlers and some common ways for doing so. | ||
|
||
## Uses of resource handlers | ||
|
||
The use case and functionality for an app is very broad and therefore also for uses of resource handlers. But in general, an app normally integrates with a HTTP service of some kind, e.g. a 3rd party service, to retrieve and send data. For example, this service might have | ||
|
||
- specific authentication and authorization needs. | ||
- a format not suitable to return to Grafana and the plugin frontend. | ||
|
||
In addition, you might want to [secure your resources](implement-rbac-in-app-plugins.md#secure-backend-resources) so that only users with a certain permission can access those. | ||
|
||
Resource handlers are also useful for building control panels that allow the user to write back to the app. For example, you could add a resource handler to update the state of an IoT device. | ||
|
||
<ImplementResourceHandler /> | ||
|
||
## Accessing app resources | ||
|
||
Once implemented you can access the resources using the Grafana HTTP API and from the frontend. | ||
|
||
### Using the Grafana HTTP API | ||
|
||
You can access the resources through the Grafana HTTP API by using the endpoint, `http://<GRAFANA_HOSTNAME>:<PORT>/api/plugins/<PLUGIN_ID>/resources{/<RESOURCE>}`. The `PLUGIN_ID` is the plugin identifier that uniquely identifies your app and the `RESOURCE` depends on how the resource handler is implemented and what resources (routes) are supported. | ||
|
||
With the above example you can access the following resources: | ||
|
||
- HTTP GET `http://<GRAFANA_HOSTNAME>:<PORT>/api/plugins/<PLUGIN_ID>/resources/namespaces` | ||
- HTTP GET `http://<GRAFANA_HOSTNAME>:<PORT>/api/plugins/<PLUGIN_ID>/resources/projects` | ||
- HTTP POST `http://<GRAFANA_HOSTNAME>:<PORT>/api/plugins/<PLUGIN_ID>/resources/device` | ||
|
||
### From the frontend | ||
|
||
You can access your resources using the `get` and `post` functions from the `backendSrv` runtime service. To provide a nicer and more convenient API for your components it's recommended to provide a helper class with functions for each route as shown in the following example: | ||
marefr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```typescript | ||
import { getBackendSrv } from '@grafana/runtime'; | ||
|
||
export class API { | ||
private backend = getBackendSrv(); | ||
|
||
constructor(public PluginId: string) {} | ||
|
||
getNamespaces(): Promise<NamespacesResponse> { | ||
return this.backend.get(`/api/plugins/${this.PluginID}/resources/namespaces`); | ||
} | ||
|
||
getProjects(): Promise<ProjectsResponse> { | ||
return this.backend.get(`/api/plugins/${this.PluginID}/resources/projects`); | ||
} | ||
|
||
updateDevice(state: string): Promise<string> { | ||
return this.backend.post(`/api/plugins/${this.PluginID}/resources/device`, { state: state }); | ||
} | ||
} | ||
``` | ||
|
||
For example, in your app or component you can instantiate your API class and use `getNamespaces` to send a HTTP GET request to `http://<GRAFANA_HOSTNAME>:<PORT>/api/plugins/<PLUGIN_ID>/resources/namespaces` | ||
|
||
```typescript | ||
const api = new API('my-app-id'); | ||
marefr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const namespaces = await api.getNamespaces(); | ||
marefr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
As another example, you can use `updateDevice` to send a HTTP POST request to `http://<GRAFANA_HOSTNAME>:<PORT>/api/plugins/<PLUGIN_ID>/resources/device` with the provided JSON payload as the second argument: | ||
|
||
```typescript | ||
const result = await api.updateDevice('on'); | ||
``` | ||
|
||
## Additional examples | ||
|
||
Some other examples of using resource handlers and the [`httpadapter`](https://pkg.go.dev/github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter) package: | ||
|
||
- The [app-with-backend](https://github.com/grafana/grafana-plugin-examples/tree/main/examples/app-with-backend) example: | ||
- [create resource handler](https://github.com/grafana/grafana-plugin-examples/blob/main/examples/app-with-backend/pkg/plugin/app.go) and [register routes](https://github.com/grafana/grafana-plugin-examples/blob/main/examples/app-with-backend/pkg/plugin/resources.go) in the backend. | ||
- use [backendSrv](https://github.com/grafana/grafana-plugin-examples/blob/main/examples/app-with-backend/src/pages/PageOne/PageOne.tsx) to call resources. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use case and functionality for an app is very broad and therefore also for uses of resource handlers.
This sentence confused me a bit, could you rephrase it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PTAL