Plugins: Support for link extensions (#61663)

* added extensions to plugin.json and exposing it via frontend settings.

* added extensions to the plugin.json schema.

* changing the extensions in frontend settings to a map instead of an array.

* wip

* feat(pluginregistry): begin wiring up registry

* feat(pluginextensions): prevent duplicate links and clean up

* added test case for link extensions.

* added tests and implemented the getPluginLink function.

* wip

* feat(pluginextensions): expose plugin extension registry

* fix(pluginextensions): appease the typescript gods post rename

* renamed file and will throw error if trying to call setExtensionsRegistry if trying to call it twice.

* added reafactorings.

* fixed failing test.

* minor refactorings to make sure we only include extensions if the app is enabled.

* fixed some nits.

* Update public/app/features/plugins/extensions/registry.test.ts

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>

* Update packages/grafana-runtime/src/services/pluginExtensions/registry.ts

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>

* Update packages/grafana-runtime/src/services/pluginExtensions/registry.ts

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>

* Update public/app/features/plugins/extensions/registry.test.ts

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>

* Moved types for extensions from data to runtime.

* added a small example on how you could consume link extensions.

* renamed after feedback from levi.

* updated the plugindef.cue.

* using the generated plugin def.

* added tests for apps and extensions.

* fixed linting issues.

* wip

* wip

* wip

* wip

* test(extensions): fix up failing tests

* feat(extensions): freeze registry extension arrays, include type in registry items

* added restrictions in the pugindef cue schema.

* wip

* added required fields.

* added key to uniquely identify each item.

* test(pluginextensions): align tests with implementation

* chore(schema): refresh reference.md

---------

Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>
This commit is contained in:
Marcus Andersson
2023-02-07 17:20:05 +01:00
committed by GitHub
co-authored by Levente Balogh Jack Westbrook
parent 8a94688114
commit 1cfd3f81fb
24 changed files with 812 additions and 98 deletions
+6
View File
@@ -334,6 +334,12 @@ func (l *Loader) readPluginJSON(pluginJSONPath string) (plugins.JSONData, error)
}
}
for i, extension := range plugin.Extensions {
if !filepath.IsAbs(extension.Path) {
plugin.Extensions[i].Path = path.Join("/", extension.Path)
}
}
return plugin, nil
}
+66
View File
@@ -8,6 +8,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/plugins/manager/loader/assetpath"
"github.com/grafana/grafana/pkg/plugins/plugindef"
"github.com/grafana/grafana/pkg/plugins/pluginscdn"
"github.com/google/go-cmp/cmp"
@@ -462,7 +463,72 @@ func TestLoader_Load(t *testing.T) {
},
},
},
{
name: "Load an app with link extensions",
class: plugins.External,
cfg: &config.Cfg{
PluginsAllowUnsigned: []string{"test-app"},
},
pluginPaths: []string{"../testdata/test-app-with-link-extensions"},
want: []*plugins.Plugin{
{JSONData: plugins.JSONData{
ID: "test-app",
Type: "app",
Name: "Test App",
Info: plugins.Info{
Author: plugins.InfoLink{
Name: "Test Inc.",
URL: "http://test.com",
},
Description: "Official Grafana Test App & Dashboard bundle",
Version: "1.0.0",
Links: []plugins.InfoLink{
{Name: "Project site", URL: "http://project.com"},
{Name: "License & Terms", URL: "http://license.com"},
},
Logos: plugins.Logos{
Small: "public/img/icn-app.svg",
Large: "public/img/icn-app.svg",
},
Updated: "2015-02-10",
},
Dependencies: plugins.Dependencies{
GrafanaDependency: ">=8.0.0",
GrafanaVersion: "*",
Plugins: []plugins.Dependency{},
},
Includes: []*plugins.Includes{
{Name: "Root Page (react)", Type: "page", Role: "Viewer", Path: "/a/my-simple-app", DefaultNav: true, AddToNav: true, Slug: "root-page-react"},
},
Extensions: []*plugindef.ExtensionsLink{
{
Target: "plugins/grafana-slo-app/slo-breach",
Title: "Declare incident",
Type: plugindef.ExtensionsLinkTypeLink,
Description: "Declares a new incident",
Path: "/incidents/declare",
},
{
Target: "plugins/grafana-slo-app/slo-breach",
Title: "Declare incident",
Type: plugindef.ExtensionsLinkTypeLink,
Description: "Declares a new incident (path without backslash)",
Path: "/incidents/declare",
},
},
Backend: false,
},
DefaultNavURL: "/plugins/test-app/page/root-page-react",
PluginDir: filepath.Join(parentDir, "testdata/test-app-with-link-extensions"),
Class: plugins.External,
Signature: plugins.SignatureUnsigned,
Module: "plugins/test-app/module",
BaseURL: "public/plugins/test-app",
},
},
},
}
for _, tt := range tests {
reg := fakes.NewFakePluginRegistry()
storage := fakes.NewFakePluginStorage()
@@ -0,0 +1,56 @@
{
"type": "app",
"name": "Test App",
"id": "test-app",
"info": {
"description": "Official Grafana Test App & Dashboard bundle",
"author": {
"name": "Test Inc.",
"url": "http://test.com"
},
"keywords": [
"test"
],
"links": [
{
"name": "Project site",
"url": "http://project.com"
},
{
"name": "License & Terms",
"url": "http://license.com"
}
],
"version": "1.0.0",
"updated": "2015-02-10"
},
"includes": [
{
"type": "page",
"name": "Root Page (react)",
"path": "/a/my-simple-app",
"role": "Viewer",
"addToNav": true,
"defaultNav": true
}
],
"extensions": [
{
"target": "plugins/grafana-slo-app/slo-breach",
"type": "link",
"title": "Declare incident",
"description": "Declares a new incident",
"path": "/incidents/declare"
},
{
"target": "plugins/grafana-slo-app/slo-breach",
"type": "link",
"title": "Declare incident",
"description": "Declares a new incident (path without backslash)",
"path": "incidents/declare"
}
],
"dependencies": {
"grafanaDependency": ">=8.0.0"
}
}
+9 -5
View File
@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"github.com/grafana/grafana/pkg/plugins/plugindef"
"github.com/grafana/grafana/pkg/services/org"
)
@@ -253,6 +254,14 @@ type PanelDTO struct {
Module string `json:"module"`
}
type AppDTO struct {
ID string `json:"id"`
Path string `json:"path"`
Version string `json:"version"`
Preload bool `json:"preload"`
Extensions []*plugindef.ExtensionsLink `json:"extensions,omitempty"`
}
const (
signatureMissing ErrorCode = "signatureMissing"
signatureModified ErrorCode = "signatureModified"
@@ -266,11 +275,6 @@ type Error struct {
PluginID string `json:"pluginId,omitempty"`
}
type PreloadPlugin struct {
Path string `json:"path"`
Version string `json:"version"`
}
// Access-Control related definitions
// RoleRegistration stores a role and its assignments to basic roles
+4
View File
@@ -84,6 +84,10 @@ func TestParsePluginTestdata(t *testing.T) {
rootid: "test-app",
skip: "has a 'page'-type include which isn't a known part of spec",
},
"test-app-with-link-extensions": {
rootid: "test-app",
skip: "has a 'page'-type include which isn't a known part of spec",
},
"test-app-with-roles": {
rootid: "test-app",
},
+22 -5
View File
@@ -1,9 +1,9 @@
package plugindef
import (
"strings"
"regexp"
"strings"
"github.com/grafana/thema"
)
@@ -122,6 +122,23 @@ seqs: [
...
}
#ExtensionsLink: {
// Target where the link will be rendered
target: =~"^(plugins|grafana)\/[a-z-/0-9]*$"
// Type of extension
type: "link"
// Title that will be displayed for the rendered link
title: string & strings.MinRunes(3) & strings.MaxRunes(22)
// Description for the rendered link
description: string & strings.MaxRunes(200)
// Path relative to the extending plugin e.g. /incidents/declare
path: =~"^\/.*"
...
}
// Extensions made by the current plugin.
extensions?: [...#ExtensionsLink]
// For data source plugins, if the plugin supports logs.
logs?: bool
@@ -175,9 +192,9 @@ seqs: [
// each of which has an action and an optional scope.
// Example: the role 'Schedules Reader' bundles permissions to view all schedules of the plugin.
#Role: {
name: string,
name: =~"^([A-Z][0-9A-Za-z ]+)$"
description: string,
name: string
name: =~"^([A-Z][0-9A-Za-z ]+)$"
description: string
permissions: [...#Permission]
}
@@ -29,6 +29,11 @@ const (
DependencyTypePanel DependencyType = "panel"
)
// Defines values for ExtensionsLinkType.
const (
ExtensionsLinkTypeLink ExtensionsLinkType = "link"
)
// Defines values for IncludeRole.
const (
IncludeRoleAdmin IncludeRole = "Admin"
@@ -148,6 +153,27 @@ type Dependency struct {
// DependencyType defines model for Dependency.Type.
type DependencyType string
// ExtensionsLink defines model for ExtensionsLink.
type ExtensionsLink struct {
// Description for the rendered link
Description string `json:"description"`
// Path relative to the extending plugin e.g. /incidents/declare
Path string `json:"path"`
// Target where the link will be rendered
Target string `json:"target"`
// Title that will be displayed for the rendered link
Title string `json:"title"`
// Type of extension
Type ExtensionsLinkType `json:"type"`
}
// Type of extension
type ExtensionsLinkType string
// Header describes an HTTP header that is forwarded with a proxied request for
// a plugin route.
type Header struct {
@@ -314,6 +340,9 @@ type PluginDef struct {
// https://golang.org/doc/install/source#environment.
Executable *string `json:"executable,omitempty"`
// Extensions made by the current plugin.
Extensions *[]ExtensionsLink `json:"extensions,omitempty"`
// For data source plugins, include hidden queries in the data
// request.
HiddenQueries *bool `json:"hiddenQueries,omitempty"`
+3 -1
View File
@@ -15,6 +15,7 @@ import (
"github.com/grafana/grafana/pkg/plugins/backendplugin"
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
"github.com/grafana/grafana/pkg/plugins/backendplugin/secretsmanagerplugin"
"github.com/grafana/grafana/pkg/plugins/plugindef"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/util"
)
@@ -145,7 +146,8 @@ type JSONData struct {
SkipDataQuery bool `json:"skipDataQuery"`
// App settings
AutoEnabled bool `json:"autoEnabled"`
AutoEnabled bool `json:"autoEnabled"`
Extensions []*plugindef.ExtensionsLink `json:"extensions"`
// Datasource settings
Annotations bool `json:"annotations"`