feat(apps): lots of more work on apps, changed app_plugin to app_settings in order to to confuse the app plugin model (definition) and app org settings

This commit is contained in:
Torkel Ödegaard
2016-01-10 21:37:11 +01:00
parent ab79348af5
commit c1e94e61d0
23 changed files with 272 additions and 220 deletions
+5 -4
View File
@@ -41,8 +41,8 @@ func Register(r *macaron.Macaron) {
r.Get("/admin/orgs", reqGrafanaAdmin, Index)
r.Get("/admin/orgs/edit/:id", reqGrafanaAdmin, Index)
r.Get("/org/apps", reqSignedIn, Index)
r.Get("/org/apps/edit/*", reqSignedIn, Index)
r.Get("/apps", reqSignedIn, Index)
r.Get("/apps/edit/*", reqSignedIn, Index)
r.Get("/dashboard/*", reqSignedIn, Index)
r.Get("/dashboard-solo/*", reqSignedIn, Index)
@@ -119,8 +119,9 @@ func Register(r *macaron.Macaron) {
r.Patch("/invites/:code/revoke", wrap(RevokeInvite))
// apps
r.Get("/apps", wrap(GetAppPlugins))
r.Post("/apps", bind(m.UpdateAppPluginCmd{}), wrap(UpdateAppPlugin))
r.Get("/apps", wrap(GetOrgAppsList))
r.Get("/apps/:appId/settings", wrap(GetAppSettingsById))
r.Post("/apps/:appId/settings", bind(m.UpdateAppSettingsCmd{}), wrap(UpdateAppSettings))
}, reqOrgAdmin)
// create new org
-63
View File
@@ -1,63 +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 GetAppPlugins(c *middleware.Context) Response {
query := m.GetAppPluginsQuery{OrgId: c.OrgId}
if err := bus.Dispatch(&query); err != nil {
return ApiError(500, "Failed to list Plugin Bundles", err)
}
translateToDto := func(app *plugins.AppPlugin) *dtos.AppPlugin {
return &dtos.AppPlugin{
Name: app.Name,
Type: app.Type,
Enabled: app.Enabled,
Pinned: app.Pinned,
Module: app.Module,
Info: &app.Info,
}
}
seenApps := make(map[string]bool)
result := make([]*dtos.AppPlugin, 0)
for _, orgApp := range query.Result {
if def, ok := plugins.Apps[orgApp.Type]; ok {
pluginDto := translateToDto(def)
pluginDto.Enabled = orgApp.Enabled
pluginDto.JsonData = orgApp.JsonData
result = append(result, pluginDto)
seenApps[orgApp.Type] = true
}
}
for _, app := range plugins.Apps {
if _, ok := seenApps[app.Type]; !ok {
result = append(result, translateToDto(app))
}
}
return Json(200, result)
}
func UpdateAppPlugin(c *middleware.Context, cmd m.UpdateAppPluginCmd) Response {
cmd.OrgId = c.OrgId
if _, ok := plugins.Apps[cmd.Type]; !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")
}
+59
View File
@@ -0,0 +1,59 @@
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")
}
+9 -10
View File
@@ -118,18 +118,17 @@ func UpdateDataSource(c *middleware.Context, cmd m.UpdateDataSourceCommand) {
func GetDataSourcePlugins(c *middleware.Context) {
dsList := make(map[string]*plugins.DataSourcePlugin)
orgApps := m.GetAppPluginsQuery{OrgId: c.OrgId}
err := bus.Dispatch(&orgApps)
if err != nil {
if enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId); err != nil {
c.JsonApiErr(500, "Failed to get org apps", err)
}
enabledPlugins := plugins.GetEnabledPlugins(orgApps.Result)
return
} else {
for key, value := range enabledPlugins.DataSources {
if !value.BuiltIn {
dsList[key] = value
for key, value := range enabledPlugins.DataSources {
if !value.BuiltIn {
dsList[key] = value
}
}
}
c.JSON(200, dsList)
c.JSON(200, dsList)
}
}
-13
View File
@@ -1,13 +0,0 @@
package dtos
import "github.com/grafana/grafana/pkg/plugins"
type AppPlugin struct {
Name string `json:"name"`
Type string `json:"type"`
Enabled bool `json:"enabled"`
Pinned bool `json:"pinned"`
Module string `json:"module"`
Info *plugins.PluginInfo `json:"info"`
JsonData map[string]interface{} `json:"jsonData"`
}
+31
View File
@@ -0,0 +1,31 @@
package dtos
import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
)
type AppSettings struct {
Name string `json:"name"`
AppId string `json:"appId"`
Enabled bool `json:"enabled"`
Pinned bool `json:"pinned"`
Info *plugins.PluginInfo `json:"info"`
JsonData map[string]interface{} `json:"jsonData"`
}
func NewAppSettingsDto(def *plugins.AppPlugin, data *models.AppSettings) *AppSettings {
dto := &AppSettings{
AppId: def.Id,
Name: def.Name,
Info: &def.Info,
}
if data != nil {
dto.Enabled = data.Enabled
dto.Pinned = data.Pinned
dto.Info = &def.Info
}
return dto
}
+1 -4
View File
@@ -29,14 +29,11 @@ func getFrontendSettingsMap(c *middleware.Context) (map[string]interface{}, erro
datasources := make(map[string]interface{})
var defaultDatasource string
orgApps := m.GetAppPluginsQuery{OrgId: c.OrgId}
err := bus.Dispatch(&orgApps)
enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
if err != nil {
return nil, err
}
enabledPlugins := plugins.GetEnabledPlugins(orgApps.Result)
for _, ds := range orgDataSources {
url := ds.Url
+1 -5
View File
@@ -2,7 +2,6 @@ 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"
@@ -69,14 +68,11 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
})
}
orgApps := m.GetAppPluginsQuery{OrgId: c.OrgId}
err = bus.Dispatch(&orgApps)
enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
if err != nil {
return nil, err
}
enabledPlugins := plugins.GetEnabledPlugins(orgApps.Result)
for _, plugin := range enabledPlugins.Apps {
if plugin.Module != "" {
data.PluginModules = append(data.PluginModules, plugin.Module)