mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
API: Enable serving Swagger UI by default and add docs and guidelines (#63489)
* Enable serving Swagger UI by default It used to be served behind the `swaggerUi` feature toggle. * Remove `swaggerUi` feature toggle * Add docs and guidelines for updating swagger * Apply suggestions from code review Co-authored-by: Josh Hunt <joshhunt@users.noreply.github.com>
This commit is contained in:
parent
fbd049a094
commit
8aae7be4e5
@ -13,6 +13,7 @@ of content of the following files is expected:
|
|||||||
- [Services](/contribute/backend/services.md)
|
- [Services](/contribute/backend/services.md)
|
||||||
- [Communication](/contribute/backend/communication.md)
|
- [Communication](/contribute/backend/communication.md)
|
||||||
- [Database](/contribute/backend/database.md)
|
- [Database](/contribute/backend/database.md)
|
||||||
|
- [HTTP API](/pkg/api/README.md)
|
||||||
|
|
||||||
Reviewers who review large changes should additionally make a habit out
|
Reviewers who review large changes should additionally make a habit out
|
||||||
of familiarizing themselves with the content of
|
of familiarizing themselves with the content of
|
||||||
|
@ -19,6 +19,12 @@ weight: 100
|
|||||||
The Grafana backend exposes an HTTP API, which is the same API that is used by the frontend to do everything from saving
|
The Grafana backend exposes an HTTP API, which is the same API that is used by the frontend to do everything from saving
|
||||||
dashboards, creating users, and updating data sources.
|
dashboards, creating users, and updating data sources.
|
||||||
|
|
||||||
|
Since version 8.4, HTTP API details are [specified](https://editor.swagger.io/?url=https://raw.githubusercontent.com/grafana/grafana/main/public/api-merged.json) using OpenAPI v2.
|
||||||
|
|
||||||
|
Starting from version 9.1, there is also a [OpenAPI v3 specification](https://editor.swagger.io/?url=https://raw.githubusercontent.com/grafana/grafana/main/public/openapi3.json) (generated by the v2 one).
|
||||||
|
|
||||||
|
Users can browser and try out both via the Swagger UI editor (served by the grafana server) by navigating to `/swagger-ui` and `/openapi3` respectively.
|
||||||
|
|
||||||
## HTTP APIs
|
## HTTP APIs
|
||||||
|
|
||||||
- [Admin API]({{< relref "admin/" >}})
|
- [Admin API]({{< relref "admin/" >}})
|
||||||
|
@ -38,7 +38,6 @@ Some stable features are enabled by default. You can disable a stable feature by
|
|||||||
| `trimDefaults` | Use cue schema to remove values that will be applied automatically |
|
| `trimDefaults` | Use cue schema to remove values that will be applied automatically |
|
||||||
| `panelTitleSearch` | Search for dashboards using panel title |
|
| `panelTitleSearch` | Search for dashboards using panel title |
|
||||||
| `prometheusAzureOverrideAudience` | Experimental. Allow override default AAD audience for Azure Prometheus endpoint |
|
| `prometheusAzureOverrideAudience` | Experimental. Allow override default AAD audience for Azure Prometheus endpoint |
|
||||||
| `swaggerUi` | Serves swagger UI |
|
|
||||||
| `migrationLocking` | Lock database during migrations |
|
| `migrationLocking` | Lock database during migrations |
|
||||||
| `newDBLibrary` | Use jmoiron/sqlx rather than xorm for a few backend services |
|
| `newDBLibrary` | Use jmoiron/sqlx rather than xorm for a few backend services |
|
||||||
| `validateDashboardsOnSave` | Validate dashboard JSON POSTed to api/dashboards/db |
|
| `validateDashboardsOnSave` | Validate dashboard JSON POSTed to api/dashboards/db |
|
||||||
|
@ -30,7 +30,6 @@ export interface FeatureToggles {
|
|||||||
publicDashboardsEmailSharing?: boolean;
|
publicDashboardsEmailSharing?: boolean;
|
||||||
lokiLive?: boolean;
|
lokiLive?: boolean;
|
||||||
lokiDataframeApi?: boolean;
|
lokiDataframeApi?: boolean;
|
||||||
swaggerUi?: boolean;
|
|
||||||
featureHighlights?: boolean;
|
featureHighlights?: boolean;
|
||||||
dashboardComments?: boolean;
|
dashboardComments?: boolean;
|
||||||
annotationComments?: boolean;
|
annotationComments?: boolean;
|
||||||
|
82
pkg/api/README.md
Normal file
82
pkg/api/README.md
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
# OpenAPI specifications
|
||||||
|
|
||||||
|
Since version 8.4, HTTP API details are [specified](https://editor.swagger.io/?url=https://raw.githubusercontent.com/grafana/grafana/main/public/api-merged.json) using OpenAPI v2. Starting from version 9.1, there is also an [OpenAPI v3 specification](https://editor.swagger.io/?url=https://raw.githubusercontent.com/grafana/grafana/main/public/openapi3.json) (generated by the v2 one using [this script](https://github.com/grafana/grafana/blob/main/scripts/openapi3/openapi3conv.go)).
|
||||||
|
|
||||||
|
# OpenAPI annotations
|
||||||
|
|
||||||
|
The OpenAPI v2 specification is generated automatically from the annotated Go code using [go-swagger](https://github.com/go-swagger/go-swagger) which scans the source code for [annotation rules](https://goswagger.io/use/spec.html). Refer to [this getting started guide](https://medium.com/@pedram.esmaeeli/generate-swagger-specification-from-go-source-code-648615f7b9d9) for getting familiar with the toolkit.
|
||||||
|
|
||||||
|
Developers modifying the HTTP API endpoints need to make sure to add the necessary annotations so that their changes are reflected into the generated specifications.
|
||||||
|
|
||||||
|
## Example of endpoint annotation
|
||||||
|
|
||||||
|
The following route defines a `PATCH` endpoint under the `/serviceaccounts/{serviceAccountId}` path with tag `service_accounts` (used for grouping together several routes) and operation ID `updateServiceAccount` (used for uniquely identifying routes and associate parameters and response with them).
|
||||||
|
|
||||||
|
```go
|
||||||
|
|
||||||
|
// swagger:route PATCH /serviceaccounts/{serviceAccountId} service_accounts updateServiceAccount
|
||||||
|
//
|
||||||
|
// # Update service account
|
||||||
|
//
|
||||||
|
// Required permissions (See note in the [introduction](https://grafana.com/docs/grafana/latest/developers/http_api/serviceaccount/#service-account-api) for an explanation):
|
||||||
|
// action: `serviceaccounts:write` scope: `serviceaccounts:id:1` (single service account)
|
||||||
|
//
|
||||||
|
// Responses:
|
||||||
|
// 200: updateServiceAccountResponse
|
||||||
|
// 400: badRequestError
|
||||||
|
// 401: unauthorisedError
|
||||||
|
// 403: forbiddenError
|
||||||
|
// 404: notFoundError
|
||||||
|
// 500: internalServerError
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
The `go-swagger` can discover such annotations by scanning any code imported by [`pkg/server`](https://github.com/grafana/grafana/blob/main/pkg/server/server.go) but by convention we place the endpoint annotations above the endpoint definition.
|
||||||
|
|
||||||
|
## Example of endpoint parameters
|
||||||
|
|
||||||
|
The following struct defines the route parameters for the `updateServiceAccount` endpoint. The route expects:
|
||||||
|
* a path parameter denoting the service account identifier and
|
||||||
|
* a body parameter with the new values for the specific service account
|
||||||
|
|
||||||
|
```go
|
||||||
|
|
||||||
|
// swagger:parameters updateServiceAccount
|
||||||
|
type UpdateServiceAccountParams struct {
|
||||||
|
// in:path
|
||||||
|
ServiceAccountId int64 `json:"serviceAccountId"`
|
||||||
|
// in:body
|
||||||
|
Body serviceaccounts.UpdateServiceAccountForm
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Example of endpoint response
|
||||||
|
|
||||||
|
The following struct defines the response for the `updateServiceAccount` endpoint in case of a successful `200` response.
|
||||||
|
|
||||||
|
```go
|
||||||
|
|
||||||
|
// swagger:response updateServiceAccountResponse
|
||||||
|
type UpdateServiceAccountResponse struct {
|
||||||
|
// in:body
|
||||||
|
Body struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
ServiceAccount *serviceaccounts.ServiceAccountProfileDTO `json:"serviceaccount"`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
# OpenAPI generation
|
||||||
|
|
||||||
|
Developers can re-create the OpenAPI v2 and v3 specifications using the following command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
|
||||||
|
make clean-api-spec && make openapi3-gen
|
||||||
|
```
|
||||||
|
|
||||||
|
They can observe its output into the `public/api-merged.json` and `public/openapi3.json` files.
|
||||||
|
|
||||||
|
Finally, they can browser and try out both the OpenAPI v2 and v3 via the Swagger UI editor (served by the grafana server) by navigating to `/swagger-ui` and `/openapi3` respectivally.
|
@ -218,10 +218,8 @@ func (hs *HTTPServer) registerRoutes() {
|
|||||||
// expose plugin file system assets
|
// expose plugin file system assets
|
||||||
r.Get("/public/plugins/:pluginId/*", hs.getPluginAssets)
|
r.Get("/public/plugins/:pluginId/*", hs.getPluginAssets)
|
||||||
|
|
||||||
if hs.Features.IsEnabled(featuremgmt.FlagSwaggerUi) {
|
r.Get("/swagger-ui", swaggerUI)
|
||||||
r.Get("/swagger-ui", swaggerUI)
|
r.Get("/openapi3", openapi3)
|
||||||
r.Get("/openapi3", openapi3)
|
|
||||||
}
|
|
||||||
|
|
||||||
// authed api
|
// authed api
|
||||||
r.Group("/api", func(apiRoute routing.RouteRegister) {
|
r.Group("/api", func(apiRoute routing.RouteRegister) {
|
||||||
|
@ -83,11 +83,6 @@ var (
|
|||||||
Description: "Use experimental loki api for WebSocket streaming (early prototype)",
|
Description: "Use experimental loki api for WebSocket streaming (early prototype)",
|
||||||
State: FeatureStateAlpha,
|
State: FeatureStateAlpha,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
Name: "swaggerUi",
|
|
||||||
Description: "Serves swagger UI",
|
|
||||||
State: FeatureStateBeta,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
Name: "featureHighlights",
|
Name: "featureHighlights",
|
||||||
Description: "Highlight Grafana Enterprise features",
|
Description: "Highlight Grafana Enterprise features",
|
||||||
|
@ -63,10 +63,6 @@ const (
|
|||||||
// Use experimental loki api for WebSocket streaming (early prototype)
|
// Use experimental loki api for WebSocket streaming (early prototype)
|
||||||
FlagLokiDataframeApi = "lokiDataframeApi"
|
FlagLokiDataframeApi = "lokiDataframeApi"
|
||||||
|
|
||||||
// FlagSwaggerUi
|
|
||||||
// Serves swagger UI
|
|
||||||
FlagSwaggerUi = "swaggerUi"
|
|
||||||
|
|
||||||
// FlagFeatureHighlights
|
// FlagFeatureHighlights
|
||||||
// Highlight Grafana Enterprise features
|
// Highlight Grafana Enterprise features
|
||||||
FlagFeatureHighlights = "featureHighlights"
|
FlagFeatureHighlights = "featureHighlights"
|
||||||
|
Loading…
Reference in New Issue
Block a user