| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-03-15 22:52:29 +08:00
										 |  |  | 	"sort" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							|  |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/plugins" | 
					
						
							| 
									
										
										
										
											2016-05-04 01:00:42 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GetPluginList(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 	typeFilter := c.Query("type") | 
					
						
							|  |  |  | 	enabledFilter := c.Query("enabled") | 
					
						
							|  |  |  | 	embeddedFilter := c.Query("embedded") | 
					
						
							| 
									
										
										
										
											2016-04-09 04:42:33 +08:00
										 |  |  | 	coreFilter := c.Query("core") | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	pluginSettingsMap, err := plugins.GetPluginSettings(c.OrgId) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to get list of plugins", err) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 22:52:29 +08:00
										 |  |  | 	result := make(dtos.PluginList, 0) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	for _, pluginDef := range plugins.Plugins { | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 		// filter out app sub plugins
 | 
					
						
							|  |  |  | 		if embeddedFilter == "0" && pluginDef.IncludedInAppId != "" { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 04:42:33 +08:00
										 |  |  | 		// filter out core plugins
 | 
					
						
							|  |  |  | 		if coreFilter == "0" && pluginDef.IsCorePlugin { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 		// filter on type
 | 
					
						
							|  |  |  | 		if typeFilter != "" && typeFilter != pluginDef.Type { | 
					
						
							| 
									
										
										
										
											2016-03-08 18:29:36 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 22:52:29 +08:00
										 |  |  | 		listItem := dtos.PluginListItem{ | 
					
						
							| 
									
										
										
										
											2016-04-12 00:47:04 +08:00
										 |  |  | 			Id:            pluginDef.Id, | 
					
						
							|  |  |  | 			Name:          pluginDef.Name, | 
					
						
							|  |  |  | 			Type:          pluginDef.Type, | 
					
						
							|  |  |  | 			Info:          &pluginDef.Info, | 
					
						
							|  |  |  | 			LatestVersion: pluginDef.GrafanaNetVersion, | 
					
						
							|  |  |  | 			HasUpdate:     pluginDef.GrafanaNetHasUpdate, | 
					
						
							| 
									
										
										
										
											2016-05-04 01:00:42 +08:00
										 |  |  | 			DefaultNavUrl: pluginDef.DefaultNavUrl, | 
					
						
							| 
									
										
										
										
											2017-04-07 18:00:03 +08:00
										 |  |  | 			State:         pluginDef.State, | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if pluginSetting, exists := pluginSettingsMap[pluginDef.Id]; exists { | 
					
						
							|  |  |  | 			listItem.Enabled = pluginSetting.Enabled | 
					
						
							|  |  |  | 			listItem.Pinned = pluginSetting.Pinned | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 01:00:42 +08:00
										 |  |  | 		if listItem.DefaultNavUrl == "" || !listItem.Enabled { | 
					
						
							|  |  |  | 			listItem.DefaultNavUrl = setting.AppSubUrl + "/plugins/" + listItem.Id + "/edit" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 		// filter out disabled
 | 
					
						
							|  |  |  | 		if enabledFilter == "1" && !listItem.Enabled { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-14 19:00:56 +08:00
										 |  |  | 		// filter out built in data sources
 | 
					
						
							|  |  |  | 		if ds, exists := plugins.DataSources[pluginDef.Id]; exists { | 
					
						
							|  |  |  | 			if ds.BuiltIn { | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 		result = append(result, listItem) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 22:52:29 +08:00
										 |  |  | 	sort.Sort(result) | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, result) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | func GetPluginSettingByID(c *m.ReqContext) Response { | 
					
						
							|  |  |  | 	pluginID := c.Params(":pluginId") | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	def, exists := plugins.Plugins[pluginID] | 
					
						
							|  |  |  | 	if !exists { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(404, "Plugin not found, no installed plugin with that id", nil) | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	dto := &dtos.PluginSetting{ | 
					
						
							|  |  |  | 		Type:          def.Type, | 
					
						
							|  |  |  | 		Id:            def.Id, | 
					
						
							|  |  |  | 		Name:          def.Name, | 
					
						
							|  |  |  | 		Info:          &def.Info, | 
					
						
							|  |  |  | 		Dependencies:  &def.Dependencies, | 
					
						
							|  |  |  | 		Includes:      def.Includes, | 
					
						
							|  |  |  | 		BaseUrl:       def.BaseUrl, | 
					
						
							|  |  |  | 		Module:        def.Module, | 
					
						
							|  |  |  | 		DefaultNavUrl: def.DefaultNavUrl, | 
					
						
							|  |  |  | 		LatestVersion: def.GrafanaNetVersion, | 
					
						
							|  |  |  | 		HasUpdate:     def.GrafanaNetHasUpdate, | 
					
						
							|  |  |  | 		State:         def.State, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-25 22:56:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	query := m.GetPluginSettingByIdQuery{PluginId: pluginID, OrgId: c.OrgId} | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		if err != m.ErrPluginSettingNotFound { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 			return Error(500, "Failed to get login settings", nil) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		dto.Enabled = query.Result.Enabled | 
					
						
							|  |  |  | 		dto.Pinned = query.Result.Pinned | 
					
						
							|  |  |  | 		dto.JsonData = query.Result.JsonData | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, dto) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func UpdatePluginSetting(c *m.ReqContext, cmd m.UpdatePluginSettingCmd) Response { | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	pluginID := c.Params(":pluginId") | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	cmd.OrgId = c.OrgId | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	cmd.PluginId = pluginID | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if _, ok := plugins.Apps[cmd.PluginId]; !ok { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(404, "Plugin not installed.", nil) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to update plugin setting", err) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return Success("Plugin settings updated") | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-03-11 16:57:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GetPluginDashboards(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	pluginID := c.Params(":pluginId") | 
					
						
							| 
									
										
										
										
											2016-03-11 16:57:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	list, err := plugins.GetPluginDashboards(c.OrgId, pluginID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-03-11 16:57:20 +08:00
										 |  |  | 		if notfound, ok := err.(plugins.PluginNotFoundError); ok { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 			return Error(404, notfound.Error(), nil) | 
					
						
							| 
									
										
										
										
											2016-03-11 16:57:20 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to get plugin dashboards", err) | 
					
						
							| 
									
										
										
										
											2016-03-11 16:57:20 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, list) | 
					
						
							| 
									
										
										
										
											2016-03-11 16:57:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GetPluginMarkdown(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	pluginID := c.Params(":pluginId") | 
					
						
							| 
									
										
										
										
											2017-08-31 20:05:52 +08:00
										 |  |  | 	name := c.Params(":name") | 
					
						
							| 
									
										
										
										
											2016-03-14 02:21:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	content, err := plugins.GetPluginMarkdown(pluginID, name) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-03-14 02:21:44 +08:00
										 |  |  | 		if notfound, ok := err.(plugins.PluginNotFoundError); ok { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 			return Error(404, notfound.Error(), nil) | 
					
						
							| 
									
										
										
										
											2016-03-14 02:21:44 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Could not get markdown file", err) | 
					
						
							| 
									
										
										
										
											2016-03-14 02:21:44 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	resp := Respond(200, content) | 
					
						
							|  |  |  | 	resp.Header("Content-Type", "text/plain; charset=utf-8") | 
					
						
							|  |  |  | 	return resp | 
					
						
							| 
									
										
										
										
											2016-03-14 02:21:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func ImportDashboard(c *m.ReqContext, apiCmd dtos.ImportDashboardCommand) Response { | 
					
						
							| 
									
										
										
										
											2016-03-11 16:57:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-12 00:31:57 +08:00
										 |  |  | 	cmd := plugins.ImportDashboardCommand{ | 
					
						
							| 
									
										
										
										
											2016-04-26 18:52:44 +08:00
										 |  |  | 		OrgId:     c.OrgId, | 
					
						
							| 
									
										
										
										
											2018-02-19 18:12:56 +08:00
										 |  |  | 		User:      c.SignedInUser, | 
					
						
							| 
									
										
										
										
											2016-04-26 18:52:44 +08:00
										 |  |  | 		PluginId:  apiCmd.PluginId, | 
					
						
							|  |  |  | 		Path:      apiCmd.Path, | 
					
						
							|  |  |  | 		Inputs:    apiCmd.Inputs, | 
					
						
							|  |  |  | 		Overwrite: apiCmd.Overwrite, | 
					
						
							| 
									
										
										
										
											2016-05-14 16:00:43 +08:00
										 |  |  | 		Dashboard: apiCmd.Dashboard, | 
					
						
							| 
									
										
										
										
											2016-03-11 16:57:20 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to import dashboard", err) | 
					
						
							| 
									
										
										
										
											2016-03-11 16:57:20 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, cmd.Result) | 
					
						
							| 
									
										
										
										
											2016-03-11 16:57:20 +08:00
										 |  |  | } |