feat(plugins): began refactoring AppSettings -> PluginSettings, and have the plugins list view and plugin edit view be common for all plugins
This commit is contained in:
+3
-3
@@ -126,9 +126,9 @@ func Register(r *macaron.Macaron) {
|
||||
r.Patch("/invites/:code/revoke", wrap(RevokeInvite))
|
||||
|
||||
// apps
|
||||
r.Get("/apps", wrap(GetOrgAppsList))
|
||||
r.Get("/apps/:appId/settings", wrap(GetAppSettingsById))
|
||||
r.Post("/apps/:appId/settings", bind(m.UpdateAppSettingsCmd{}), wrap(UpdateAppSettings))
|
||||
r.Get("/plugins", wrap(GetPluginList))
|
||||
r.Get("/plugins/:pluginId/settings", wrap(GetPluginSettingById))
|
||||
r.Post("/plugins/:pluginId/settings", bind(m.UpdatePluginSettingCmd{}), wrap(UpdatePluginSetting))
|
||||
}, reqOrgAdmin)
|
||||
|
||||
// create new org
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
)
|
||||
|
||||
func GetOrgAppsList(c *middleware.Context) Response {
|
||||
orgApps, err := plugins.GetOrgAppSettings(c.OrgId)
|
||||
|
||||
if err != nil {
|
||||
return ApiError(500, "Failed to list of apps", err)
|
||||
}
|
||||
|
||||
result := make([]*dtos.AppSettings, 0)
|
||||
for _, app := range plugins.Apps {
|
||||
orgApp := orgApps[app.Id]
|
||||
result = append(result, dtos.NewAppSettingsDto(app, orgApp))
|
||||
}
|
||||
|
||||
return Json(200, result)
|
||||
}
|
||||
|
||||
func GetAppSettingsById(c *middleware.Context) Response {
|
||||
appId := c.Params(":appId")
|
||||
|
||||
if pluginDef, exists := plugins.Apps[appId]; !exists {
|
||||
return ApiError(404, "PluginId not found, no installed plugin with that id", nil)
|
||||
} else {
|
||||
orgApps, err := plugins.GetOrgAppSettings(c.OrgId)
|
||||
if err != nil {
|
||||
return ApiError(500, "Failed to get org app settings ", nil)
|
||||
}
|
||||
orgApp := orgApps[appId]
|
||||
|
||||
return Json(200, dtos.NewAppSettingsDto(pluginDef, orgApp))
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateAppSettings(c *middleware.Context, cmd m.UpdateAppSettingsCmd) Response {
|
||||
appId := c.Params(":appId")
|
||||
|
||||
cmd.OrgId = c.OrgId
|
||||
cmd.AppId = appId
|
||||
|
||||
if _, ok := plugins.Apps[cmd.AppId]; !ok {
|
||||
return ApiError(404, "App type not installed.", nil)
|
||||
}
|
||||
|
||||
err := bus.Dispatch(&cmd)
|
||||
if err != nil {
|
||||
return ApiError(500, "Failed to update App Plugin", err)
|
||||
}
|
||||
|
||||
return ApiSuccess("App updated")
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
)
|
||||
|
||||
type AppSettings struct {
|
||||
type PluginSetting struct {
|
||||
Name string `json:"name"`
|
||||
AppId string `json:"appId"`
|
||||
Enabled bool `json:"enabled"`
|
||||
@@ -18,8 +18,17 @@ type AppSettings struct {
|
||||
JsonData map[string]interface{} `json:"jsonData"`
|
||||
}
|
||||
|
||||
func NewAppSettingsDto(def *plugins.AppPlugin, data *models.AppSettings) *AppSettings {
|
||||
dto := &AppSettings{
|
||||
type PluginListItem struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
PluginId string `json:"pluginId"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Pinned bool `json:"pinned"`
|
||||
Info *plugins.PluginInfo `json:"info"`
|
||||
}
|
||||
|
||||
func NewPluginSettingDto(def *plugins.AppPlugin, data *models.PluginSetting) *PluginSetting {
|
||||
dto := &PluginSetting{
|
||||
AppId: def.Id,
|
||||
Name: def.Name,
|
||||
Info: &def.Info,
|
||||
@@ -0,0 +1,68 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
)
|
||||
|
||||
func GetPluginList(c *middleware.Context) Response {
|
||||
pluginSettingsMap, err := plugins.GetPluginSettings(c.OrgId)
|
||||
|
||||
if err != nil {
|
||||
return ApiError(500, "Failed to get list of plugins", err)
|
||||
}
|
||||
|
||||
result := make([]*dtos.PluginListItem, 0)
|
||||
for _, pluginDef := range plugins.Plugins {
|
||||
listItem := &dtos.PluginListItem{
|
||||
PluginId: pluginDef.Id,
|
||||
Name: pluginDef.Name,
|
||||
Type: pluginDef.Type,
|
||||
Info: &pluginDef.Info,
|
||||
}
|
||||
|
||||
if pluginSetting, exists := pluginSettingsMap[pluginDef.Id]; exists {
|
||||
listItem.Enabled = pluginSetting.Enabled
|
||||
listItem.Pinned = pluginSetting.Pinned
|
||||
}
|
||||
|
||||
result = append(result, listItem)
|
||||
}
|
||||
|
||||
return Json(200, result)
|
||||
}
|
||||
|
||||
func GetPluginSettingById(c *middleware.Context) Response {
|
||||
pluginId := c.Params(":pluginId")
|
||||
|
||||
if pluginDef, exists := plugins.Apps[pluginId]; !exists {
|
||||
return ApiError(404, "PluginId not found, no installed plugin with that id", nil)
|
||||
} else {
|
||||
query := m.GetPluginSettingByIdQuery{PluginId: pluginId, OrgId: c.OrgId}
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "Failed to get login settings", nil)
|
||||
}
|
||||
|
||||
return Json(200, dtos.NewPluginSettingDto(pluginDef, query.Result))
|
||||
}
|
||||
}
|
||||
|
||||
func UpdatePluginSetting(c *middleware.Context, cmd m.UpdatePluginSettingCmd) Response {
|
||||
pluginId := c.Params(":pluginId")
|
||||
|
||||
cmd.OrgId = c.OrgId
|
||||
cmd.PluginId = pluginId
|
||||
|
||||
if _, ok := plugins.Apps[cmd.PluginId]; !ok {
|
||||
return ApiError(404, "Plugin not installed.", nil)
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
return ApiError(500, "Failed to update plugin setting", err)
|
||||
}
|
||||
|
||||
return ApiSuccess("Plugin settings updated")
|
||||
}
|
||||
@@ -26,7 +26,7 @@ type templateData struct {
|
||||
func getHeaders(route *plugins.AppPluginRoute, orgId int64, appId string) (http.Header, error) {
|
||||
result := http.Header{}
|
||||
|
||||
query := m.GetAppSettingByAppIdQuery{OrgId: orgId, AppId: appId}
|
||||
query := m.GetPluginSettingByIdQuery{OrgId: orgId, PluginId: appId}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user