2014-12-16 04:25:02 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-01-13 22:38:54 +08:00
|
|
|
"github.com/go-macaron/binding"
|
2016-02-21 06:51:22 +08:00
|
|
|
"github.com/grafana/grafana/pkg/api/avatar"
|
2015-02-05 17:37:13 +08:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2014-12-16 04:25:02 +08:00
|
|
|
)
|
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// Register adds http routes
|
2016-12-21 21:36:32 +08:00
|
|
|
func (hs *HttpServer) registerRoutes() {
|
2017-09-13 20:53:38 +08:00
|
|
|
macaronR := hs.macaron
|
2015-01-15 19:16:54 +08:00
|
|
|
reqSignedIn := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})
|
2015-01-16 21:32:18 +08:00
|
|
|
reqGrafanaAdmin := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})
|
2015-01-20 01:01:04 +08:00
|
|
|
reqEditorRole := middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)
|
2015-12-03 23:43:55 +08:00
|
|
|
reqOrgAdmin := middleware.RoleAuth(m.ROLE_ADMIN)
|
2015-09-11 23:17:10 +08:00
|
|
|
quota := middleware.Quota
|
2015-01-16 18:54:19 +08:00
|
|
|
bind := binding.Bind
|
2014-12-16 04:25:02 +08:00
|
|
|
|
2016-08-29 20:45:28 +08:00
|
|
|
// automatically set HEAD for every GET
|
2017-09-13 20:53:38 +08:00
|
|
|
macaronR.SetAutoHead(true)
|
|
|
|
|
2017-09-14 21:05:49 +08:00
|
|
|
r := newRouteRegister(middleware.RequestMetrics, middleware.RequestTracing)
|
2016-08-29 20:45:28 +08:00
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// not logged in views
|
2015-01-16 18:54:19 +08:00
|
|
|
r.Get("/", reqSignedIn, Index)
|
2015-01-29 22:46:54 +08:00
|
|
|
r.Get("/logout", Logout)
|
2015-09-11 23:17:10 +08:00
|
|
|
r.Post("/login", quota("session"), bind(dtos.LoginCommand{}), wrap(LoginPost))
|
|
|
|
r.Get("/login/:name", quota("session"), OAuthLogin)
|
2015-01-27 17:09:54 +08:00
|
|
|
r.Get("/login", LoginView)
|
2015-08-10 19:46:59 +08:00
|
|
|
r.Get("/invite/:code", Index)
|
2014-12-16 04:25:02 +08:00
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// authed views
|
2015-01-20 02:10:29 +08:00
|
|
|
r.Get("/profile/", reqSignedIn, Index)
|
2016-04-05 01:52:41 +08:00
|
|
|
r.Get("/profile/password", reqSignedIn, Index)
|
2016-04-10 01:27:06 +08:00
|
|
|
r.Get("/profile/switch-org/:id", reqSignedIn, ChangeActiveOrgAndRedirectToHome)
|
2015-02-25 21:27:34 +08:00
|
|
|
r.Get("/org/", reqSignedIn, Index)
|
2015-06-11 14:16:09 +08:00
|
|
|
r.Get("/org/new", reqSignedIn, Index)
|
2015-02-25 21:27:34 +08:00
|
|
|
r.Get("/datasources/", reqSignedIn, Index)
|
2016-06-17 19:35:29 +08:00
|
|
|
r.Get("/datasources/new", reqSignedIn, Index)
|
2015-02-28 15:25:13 +08:00
|
|
|
r.Get("/datasources/edit/*", reqSignedIn, Index)
|
2017-12-21 04:20:12 +08:00
|
|
|
r.Get("/org/users", reqSignedIn, Index)
|
2017-12-13 20:16:44 +08:00
|
|
|
r.Get("/org/users/new", reqSignedIn, Index)
|
|
|
|
r.Get("/org/users/invite", reqSignedIn, Index)
|
2017-12-21 04:20:12 +08:00
|
|
|
r.Get("/org/teams", reqSignedIn, Index)
|
|
|
|
r.Get("/org/teams/*", reqSignedIn, Index)
|
2015-02-25 21:27:34 +08:00
|
|
|
r.Get("/org/apikeys/", reqSignedIn, Index)
|
|
|
|
r.Get("/dashboard/import/", reqSignedIn, Index)
|
2017-08-15 20:49:12 +08:00
|
|
|
r.Get("/configuration", reqGrafanaAdmin, Index)
|
2016-03-04 06:05:08 +08:00
|
|
|
r.Get("/admin", reqGrafanaAdmin, Index)
|
2015-02-12 22:46:14 +08:00
|
|
|
r.Get("/admin/settings", reqGrafanaAdmin, Index)
|
2015-01-28 18:33:50 +08:00
|
|
|
r.Get("/admin/users", reqGrafanaAdmin, Index)
|
2015-02-10 23:26:23 +08:00
|
|
|
r.Get("/admin/users/create", reqGrafanaAdmin, Index)
|
|
|
|
r.Get("/admin/users/edit/:id", reqGrafanaAdmin, Index)
|
2015-08-11 21:20:50 +08:00
|
|
|
r.Get("/admin/orgs", reqGrafanaAdmin, Index)
|
|
|
|
r.Get("/admin/orgs/edit/:id", reqGrafanaAdmin, Index)
|
2016-01-25 13:18:17 +08:00
|
|
|
r.Get("/admin/stats", reqGrafanaAdmin, Index)
|
2015-10-20 22:02:56 +08:00
|
|
|
|
2016-03-28 19:10:42 +08:00
|
|
|
r.Get("/styleguide", reqSignedIn, Index)
|
|
|
|
|
2016-02-29 19:54:36 +08:00
|
|
|
r.Get("/plugins", reqSignedIn, Index)
|
|
|
|
r.Get("/plugins/:id/edit", reqSignedIn, Index)
|
|
|
|
r.Get("/plugins/:id/page/:page", reqSignedIn, Index)
|
2015-12-04 19:21:33 +08:00
|
|
|
|
2015-01-16 18:54:19 +08:00
|
|
|
r.Get("/dashboard/*", reqSignedIn, Index)
|
2016-09-22 17:46:58 +08:00
|
|
|
r.Get("/dashboard-solo/snapshot/*", Index)
|
2015-10-20 22:02:56 +08:00
|
|
|
r.Get("/dashboard-solo/*", reqSignedIn, Index)
|
2016-05-03 22:40:21 +08:00
|
|
|
r.Get("/import/dashboard", reqSignedIn, Index)
|
2017-12-14 18:30:57 +08:00
|
|
|
r.Get("/dashboards/", reqSignedIn, Index)
|
2016-05-19 17:03:10 +08:00
|
|
|
r.Get("/dashboards/*", reqSignedIn, Index)
|
2014-12-16 19:04:08 +08:00
|
|
|
|
2015-12-22 18:07:15 +08:00
|
|
|
r.Get("/playlists/", reqSignedIn, Index)
|
|
|
|
r.Get("/playlists/*", reqSignedIn, Index)
|
2016-06-06 15:17:29 +08:00
|
|
|
r.Get("/alerting/", reqSignedIn, Index)
|
2016-06-15 16:48:04 +08:00
|
|
|
r.Get("/alerting/*", reqSignedIn, Index)
|
2015-12-22 18:07:15 +08:00
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// sign up
|
2015-07-19 18:34:03 +08:00
|
|
|
r.Get("/signup", Index)
|
2015-08-31 17:35:07 +08:00
|
|
|
r.Get("/api/user/signup/options", wrap(GetSignUpOptions))
|
2015-09-11 23:17:10 +08:00
|
|
|
r.Post("/api/user/signup", quota("user"), bind(dtos.SignUpForm{}), wrap(SignUp))
|
2015-08-28 15:24:30 +08:00
|
|
|
r.Post("/api/user/signup/step2", bind(dtos.SignUpStep2Form{}), wrap(SignUpStep2))
|
2014-12-16 04:25:02 +08:00
|
|
|
|
2015-07-20 21:52:49 +08:00
|
|
|
// invited
|
|
|
|
r.Get("/api/user/invite/:code", wrap(GetInviteInfoByCode))
|
2015-07-20 23:46:48 +08:00
|
|
|
r.Post("/api/user/invite/complete", bind(dtos.CompleteInviteForm{}), wrap(CompleteInvite))
|
2015-07-20 21:52:49 +08:00
|
|
|
|
2015-06-08 16:57:01 +08:00
|
|
|
// reset password
|
|
|
|
r.Get("/user/password/send-reset-email", Index)
|
|
|
|
r.Get("/user/password/reset", Index)
|
|
|
|
|
|
|
|
r.Post("/api/user/password/send-reset-email", bind(dtos.SendResetPasswordEmailForm{}), wrap(SendResetPasswordEmail))
|
2015-06-08 19:39:02 +08:00
|
|
|
r.Post("/api/user/password/reset", bind(dtos.ResetUserPasswordForm{}), wrap(ResetPassword))
|
2014-12-16 04:25:02 +08:00
|
|
|
|
2015-03-21 20:53:16 +08:00
|
|
|
// dashboard snapshots
|
2015-04-15 16:39:03 +08:00
|
|
|
r.Get("/dashboard/snapshot/*", Index)
|
2016-01-19 21:05:24 +08:00
|
|
|
r.Get("/dashboard/snapshots/", reqSignedIn, Index)
|
2015-03-27 03:34:58 +08:00
|
|
|
|
2016-01-19 21:05:24 +08:00
|
|
|
// api for dashboard snapshots
|
|
|
|
r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot)
|
2015-10-14 22:39:57 +08:00
|
|
|
r.Get("/api/snapshot/shared-options/", GetSharingOptions)
|
2015-03-21 20:53:16 +08:00
|
|
|
r.Get("/api/snapshots/:key", GetDashboardSnapshot)
|
2016-03-18 16:13:34 +08:00
|
|
|
r.Get("/api/snapshots-delete/:key", reqEditorRole, DeleteDashboardSnapshot)
|
2015-03-21 20:53:16 +08:00
|
|
|
|
2015-04-07 15:25:00 +08:00
|
|
|
// api renew session based on remember cookie
|
2015-09-11 23:17:10 +08:00
|
|
|
r.Get("/api/login/ping", quota("session"), LoginApiPing)
|
2015-04-07 15:25:00 +08:00
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// authed api
|
2017-09-13 20:53:38 +08:00
|
|
|
r.Group("/api", func(apiRoute RouteRegister) {
|
2015-05-19 17:47:14 +08:00
|
|
|
|
|
|
|
// user (signed in)
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/user", func(userRoute RouteRegister) {
|
|
|
|
userRoute.Get("/", wrap(GetSignedInUser))
|
|
|
|
userRoute.Put("/", bind(m.UpdateUserCommand{}), wrap(UpdateSignedInUser))
|
|
|
|
userRoute.Post("/using/:id", wrap(UserSetUsingOrg))
|
|
|
|
userRoute.Get("/orgs", wrap(GetSignedInUserOrgList))
|
|
|
|
|
|
|
|
userRoute.Post("/stars/dashboard/:id", wrap(StarDashboard))
|
|
|
|
userRoute.Delete("/stars/dashboard/:id", wrap(UnstarDashboard))
|
|
|
|
|
|
|
|
userRoute.Put("/password", bind(m.ChangeUserPasswordCommand{}), wrap(ChangeUserPassword))
|
|
|
|
userRoute.Get("/quotas", wrap(GetUserQuotas))
|
|
|
|
userRoute.Put("/helpflags/:id", wrap(SetHelpFlag))
|
2016-11-09 17:41:39 +08:00
|
|
|
// For dev purpose
|
2017-09-13 20:53:38 +08:00
|
|
|
userRoute.Get("/helpflags/clear", wrap(ClearHelpFlags))
|
2016-04-03 04:54:06 +08:00
|
|
|
|
2017-09-13 20:53:38 +08:00
|
|
|
userRoute.Get("/preferences", wrap(GetUserPreferences))
|
|
|
|
userRoute.Put("/preferences", bind(dtos.UpdatePrefsCmd{}), wrap(UpdateUserPreferences))
|
2015-01-20 01:01:04 +08:00
|
|
|
})
|
|
|
|
|
2015-05-19 17:47:14 +08:00
|
|
|
// users (admin permission required)
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/users", func(usersRoute RouteRegister) {
|
|
|
|
usersRoute.Get("/", wrap(SearchUsers))
|
|
|
|
usersRoute.Get("/search", wrap(SearchUsersWithPaging))
|
|
|
|
usersRoute.Get("/:id", wrap(GetUserById))
|
|
|
|
usersRoute.Get("/:id/orgs", wrap(GetUserOrgList))
|
2017-01-31 13:25:55 +08:00
|
|
|
// query parameters /users/lookup?loginOrEmail=admin@example.com
|
2017-09-13 20:53:38 +08:00
|
|
|
usersRoute.Get("/lookup", wrap(GetUserByLoginOrEmail))
|
|
|
|
usersRoute.Put("/:id", bind(m.UpdateUserCommand{}), wrap(UpdateUser))
|
|
|
|
usersRoute.Post("/:id/using/:orgId", wrap(UpdateUserActiveOrg))
|
2015-05-18 23:28:15 +08:00
|
|
|
}, reqGrafanaAdmin)
|
|
|
|
|
2017-12-08 23:25:45 +08:00
|
|
|
// team (admin permission required)
|
|
|
|
apiRoute.Group("/teams", func(teamsRoute RouteRegister) {
|
|
|
|
teamsRoute.Get("/:teamId", wrap(GetTeamById))
|
|
|
|
teamsRoute.Get("/search", wrap(SearchTeams))
|
|
|
|
teamsRoute.Post("/", quota("teams"), bind(m.CreateTeamCommand{}), wrap(CreateTeam))
|
|
|
|
teamsRoute.Put("/:teamId", bind(m.UpdateTeamCommand{}), wrap(UpdateTeam))
|
|
|
|
teamsRoute.Delete("/:teamId", wrap(DeleteTeamById))
|
|
|
|
teamsRoute.Get("/:teamId/members", wrap(GetTeamMembers))
|
|
|
|
teamsRoute.Post("/:teamId/members", quota("teams"), bind(m.AddTeamMemberCommand{}), wrap(AddTeamMember))
|
|
|
|
teamsRoute.Delete("/:teamId/members/:userId", wrap(RemoveTeamMember))
|
2017-04-19 21:35:11 +08:00
|
|
|
}, reqOrgAdmin)
|
2017-04-10 07:24:16 +08:00
|
|
|
|
2015-09-11 01:18:36 +08:00
|
|
|
// org information available to all users.
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/org", func(orgRoute RouteRegister) {
|
|
|
|
orgRoute.Get("/", wrap(GetOrgCurrent))
|
|
|
|
orgRoute.Get("/quotas", wrap(GetOrgQuotas))
|
2015-09-11 01:18:36 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// current org
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/org", func(orgRoute RouteRegister) {
|
|
|
|
orgRoute.Put("/", bind(dtos.UpdateOrgForm{}), wrap(UpdateOrgCurrent))
|
|
|
|
orgRoute.Put("/address", bind(dtos.UpdateOrgAddressForm{}), wrap(UpdateOrgAddressCurrent))
|
|
|
|
orgRoute.Post("/users", quota("user"), bind(m.AddOrgUserCommand{}), wrap(AddOrgUserToCurrentOrg))
|
|
|
|
orgRoute.Get("/users", wrap(GetOrgUsersForCurrentOrg))
|
|
|
|
orgRoute.Patch("/users/:userId", bind(m.UpdateOrgUserCommand{}), wrap(UpdateOrgUserForCurrentOrg))
|
|
|
|
orgRoute.Delete("/users/:userId", wrap(RemoveOrgUserForCurrentOrg))
|
2015-07-17 15:51:34 +08:00
|
|
|
|
|
|
|
// invites
|
2017-09-13 20:53:38 +08:00
|
|
|
orgRoute.Get("/invites", wrap(GetPendingOrgInvites))
|
|
|
|
orgRoute.Post("/invites", quota("user"), bind(dtos.AddInviteForm{}), wrap(AddOrgInvite))
|
|
|
|
orgRoute.Patch("/invites/:code/revoke", wrap(RevokeInvite))
|
2015-12-18 13:46:40 +08:00
|
|
|
|
2016-04-03 04:54:06 +08:00
|
|
|
// prefs
|
2017-09-13 20:53:38 +08:00
|
|
|
orgRoute.Get("/preferences", wrap(GetOrgPreferences))
|
|
|
|
orgRoute.Put("/preferences", bind(dtos.UpdatePrefsCmd{}), wrap(UpdateOrgPreferences))
|
2015-12-03 23:43:55 +08:00
|
|
|
}, reqOrgAdmin)
|
2015-05-19 16:16:32 +08:00
|
|
|
|
|
|
|
// create new org
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Post("/orgs", quota("org"), bind(m.CreateOrgCommand{}), wrap(CreateOrg))
|
2015-05-19 16:16:32 +08:00
|
|
|
|
2015-05-19 17:47:14 +08:00
|
|
|
// search all orgs
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Get("/orgs", reqGrafanaAdmin, wrap(SearchOrgs))
|
2015-05-19 17:47:14 +08:00
|
|
|
|
2015-05-19 16:16:32 +08:00
|
|
|
// orgs (admin routes)
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/orgs/:orgId", func(orgsRoute RouteRegister) {
|
|
|
|
orgsRoute.Get("/", wrap(GetOrgById))
|
|
|
|
orgsRoute.Put("/", bind(dtos.UpdateOrgForm{}), wrap(UpdateOrg))
|
|
|
|
orgsRoute.Put("/address", bind(dtos.UpdateOrgAddressForm{}), wrap(UpdateOrgAddress))
|
|
|
|
orgsRoute.Delete("/", wrap(DeleteOrgById))
|
|
|
|
orgsRoute.Get("/users", wrap(GetOrgUsers))
|
|
|
|
orgsRoute.Post("/users", bind(m.AddOrgUserCommand{}), wrap(AddOrgUser))
|
|
|
|
orgsRoute.Patch("/users/:userId", bind(m.UpdateOrgUserCommand{}), wrap(UpdateOrgUser))
|
|
|
|
orgsRoute.Delete("/users/:userId", wrap(RemoveOrgUser))
|
|
|
|
orgsRoute.Get("/quotas", wrap(GetOrgQuotas))
|
|
|
|
orgsRoute.Put("/quotas/:target", bind(m.UpdateOrgQuotaCmd{}), wrap(UpdateOrgQuota))
|
2015-05-19 16:16:32 +08:00
|
|
|
}, reqGrafanaAdmin)
|
2015-01-27 03:26:17 +08:00
|
|
|
|
2016-01-13 05:50:56 +08:00
|
|
|
// orgs (admin routes)
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/orgs/name/:name", func(orgsRoute RouteRegister) {
|
|
|
|
orgsRoute.Get("/", wrap(GetOrgByName))
|
2016-01-13 05:50:56 +08:00
|
|
|
}, reqGrafanaAdmin)
|
|
|
|
|
2015-01-27 15:26:11 +08:00
|
|
|
// auth api keys
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/auth/keys", func(keysRoute RouteRegister) {
|
|
|
|
keysRoute.Get("/", wrap(GetApiKeys))
|
|
|
|
keysRoute.Post("/", quota("api_key"), bind(m.AddApiKeyCommand{}), wrap(AddApiKey))
|
|
|
|
keysRoute.Delete("/:id", wrap(DeleteApiKey))
|
2015-12-03 23:43:55 +08:00
|
|
|
}, reqOrgAdmin)
|
2015-01-27 03:26:17 +08:00
|
|
|
|
2016-03-17 14:35:06 +08:00
|
|
|
// Preferences
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/preferences", func(prefRoute RouteRegister) {
|
|
|
|
prefRoute.Post("/set-home-dash", bind(m.SavePreferencesCommand{}), wrap(SetHomeDashboard))
|
2016-03-17 14:35:06 +08:00
|
|
|
})
|
2016-03-11 22:30:05 +08:00
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// Data sources
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/datasources", func(datasourceRoute RouteRegister) {
|
|
|
|
datasourceRoute.Get("/", wrap(GetDataSources))
|
2017-11-16 23:29:05 +08:00
|
|
|
datasourceRoute.Post("/", quota("data_source"), bind(m.AddDataSourceCommand{}), wrap(AddDataSource))
|
2017-09-13 20:53:38 +08:00
|
|
|
datasourceRoute.Put("/:id", bind(m.UpdateDataSourceCommand{}), wrap(UpdateDataSource))
|
2017-11-16 23:29:05 +08:00
|
|
|
datasourceRoute.Delete("/:id", wrap(DeleteDataSourceById))
|
|
|
|
datasourceRoute.Delete("/name/:name", wrap(DeleteDataSourceByName))
|
2017-09-13 20:53:38 +08:00
|
|
|
datasourceRoute.Get("/:id", wrap(GetDataSourceById))
|
|
|
|
datasourceRoute.Get("/name/:name", wrap(GetDataSourceByName))
|
2015-12-03 23:43:55 +08:00
|
|
|
}, reqOrgAdmin)
|
|
|
|
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Get("/datasources/id/:name", wrap(GetDataSourceIdByName), reqSignedIn)
|
2016-03-08 04:25:26 +08:00
|
|
|
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Get("/plugins", wrap(GetPluginList))
|
|
|
|
apiRoute.Get("/plugins/:pluginId/settings", wrap(GetPluginSettingById))
|
|
|
|
apiRoute.Get("/plugins/:pluginId/markdown/:name", wrap(GetPluginMarkdown))
|
2016-03-11 16:57:20 +08:00
|
|
|
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/plugins", func(pluginRoute RouteRegister) {
|
|
|
|
pluginRoute.Get("/:pluginId/dashboards/", wrap(GetPluginDashboards))
|
|
|
|
pluginRoute.Post("/:pluginId/settings", bind(m.UpdatePluginSettingCmd{}), wrap(UpdatePluginSetting))
|
2015-12-03 23:43:55 +08:00
|
|
|
}, reqOrgAdmin)
|
|
|
|
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Get("/frontend/settings/", GetFrontendSettings)
|
|
|
|
apiRoute.Any("/datasources/proxy/:id/*", reqSignedIn, hs.ProxyDataSourceRequest)
|
|
|
|
apiRoute.Any("/datasources/proxy/:id", reqSignedIn, hs.ProxyDataSourceRequest)
|
2015-02-10 17:19:43 +08:00
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// Dashboard
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/dashboards", func(dashboardRoute RouteRegister) {
|
2017-09-18 18:32:29 +08:00
|
|
|
dashboardRoute.Get("/db/:slug", wrap(GetDashboard))
|
2017-12-20 21:42:09 +08:00
|
|
|
dashboardRoute.Delete("/db/:slug", wrap(DeleteDashboard))
|
History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
version, along with a dashboard version model and structs to represent
the queries and commands necessary for the dashboard version API
methods.
- API endpoints were created to support working with dashboard
versions.
- Methods were added to create, update, read, and destroy dashboard
versions in the database.
- Logic was added to compute the diff between two versions, and
display it to the user.
- The dashboard migration logic was updated to save a "Version
1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
are `version`, `created`, `created_by`, `message`. Default
is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
"dashboardId": <int>,
"version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
was added and vendored.
2017-05-25 07:14:39 +08:00
|
|
|
|
2017-09-13 20:53:38 +08:00
|
|
|
dashboardRoute.Post("/calculate-diff", bind(dtos.CalculateDiffOptions{}), wrap(CalculateDashboardDiff))
|
2017-06-07 17:50:09 +08:00
|
|
|
|
2017-12-20 21:42:09 +08:00
|
|
|
dashboardRoute.Post("/db", bind(m.SaveDashboardCommand{}), wrap(PostDashboard))
|
2017-09-13 20:53:38 +08:00
|
|
|
dashboardRoute.Get("/home", wrap(GetHomeDashboard))
|
|
|
|
dashboardRoute.Get("/tags", GetDashboardTags)
|
|
|
|
dashboardRoute.Post("/import", bind(dtos.ImportDashboardCommand{}), wrap(ImportDashboard))
|
2017-10-12 23:38:49 +08:00
|
|
|
|
|
|
|
dashboardRoute.Group("/id/:dashboardId", func(dashIdRoute RouteRegister) {
|
2017-10-16 20:09:52 +08:00
|
|
|
dashIdRoute.Get("/versions", wrap(GetDashboardVersions))
|
|
|
|
dashIdRoute.Get("/versions/:id", wrap(GetDashboardVersion))
|
2017-12-20 21:42:09 +08:00
|
|
|
dashIdRoute.Post("/restore", bind(dtos.RestoreDashboardVersionCommand{}), wrap(RestoreDashboardVersion))
|
2017-10-12 23:38:49 +08:00
|
|
|
|
|
|
|
dashIdRoute.Group("/acl", func(aclRoute RouteRegister) {
|
|
|
|
aclRoute.Get("/", wrap(GetDashboardAclList))
|
|
|
|
aclRoute.Post("/", bind(dtos.UpdateDashboardAclCommand{}), wrap(UpdateDashboardAcl))
|
|
|
|
aclRoute.Delete("/:aclId", wrap(DeleteDashboardAcl))
|
|
|
|
})
|
|
|
|
})
|
2015-01-14 21:25:12 +08:00
|
|
|
})
|
2015-01-27 03:26:17 +08:00
|
|
|
|
2016-01-19 21:05:24 +08:00
|
|
|
// Dashboard snapshots
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/dashboard/snapshots", func(dashboardRoute RouteRegister) {
|
|
|
|
dashboardRoute.Get("/", wrap(SearchDashboardSnapshots))
|
2016-01-19 21:05:24 +08:00
|
|
|
})
|
2016-01-19 17:37:36 +08:00
|
|
|
|
2015-12-22 18:07:15 +08:00
|
|
|
// Playlist
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/playlists", func(playlistRoute RouteRegister) {
|
|
|
|
playlistRoute.Get("/", wrap(SearchPlaylists))
|
|
|
|
playlistRoute.Get("/:id", ValidateOrgPlaylist, wrap(GetPlaylist))
|
|
|
|
playlistRoute.Get("/:id/items", ValidateOrgPlaylist, wrap(GetPlaylistItems))
|
|
|
|
playlistRoute.Get("/:id/dashboards", ValidateOrgPlaylist, wrap(GetPlaylistDashboards))
|
|
|
|
playlistRoute.Delete("/:id", reqEditorRole, ValidateOrgPlaylist, wrap(DeletePlaylist))
|
|
|
|
playlistRoute.Put("/:id", reqEditorRole, bind(m.UpdatePlaylistCommand{}), ValidateOrgPlaylist, wrap(UpdatePlaylist))
|
|
|
|
playlistRoute.Post("/", reqEditorRole, bind(m.CreatePlaylistCommand{}), wrap(CreatePlaylist))
|
2015-12-22 18:07:15 +08:00
|
|
|
})
|
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// Search
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Get("/search/", Search)
|
2015-01-27 03:26:17 +08:00
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// metrics
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Post("/tsdb/query", bind(dtos.MetricRequest{}), wrap(QueryMetrics))
|
|
|
|
apiRoute.Get("/tsdb/testdata/scenarios", wrap(GetTestDataScenarios))
|
|
|
|
apiRoute.Get("/tsdb/testdata/gensql", reqGrafanaAdmin, wrap(GenerateSqlTestData))
|
|
|
|
apiRoute.Get("/tsdb/testdata/random-walk", wrap(GetTestDataRandomWalk))
|
|
|
|
|
|
|
|
apiRoute.Group("/alerts", func(alertsRoute RouteRegister) {
|
|
|
|
alertsRoute.Post("/test", bind(dtos.AlertTestCommand{}), wrap(AlertTest))
|
2017-10-24 17:00:23 +08:00
|
|
|
alertsRoute.Post("/:alertId/pause", reqEditorRole, bind(dtos.PauseAlertCommand{}), wrap(PauseAlert))
|
2017-09-13 20:53:38 +08:00
|
|
|
alertsRoute.Get("/:alertId", ValidateOrgAlert, wrap(GetAlert))
|
|
|
|
alertsRoute.Get("/", wrap(GetAlerts))
|
|
|
|
alertsRoute.Get("/states-for-dashboard", wrap(GetAlertStatesForDashboard))
|
2016-04-26 23:36:50 +08:00
|
|
|
})
|
|
|
|
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Get("/alert-notifications", wrap(GetAlertNotifications))
|
|
|
|
apiRoute.Get("/alert-notifiers", wrap(GetAlertNotifiers))
|
2016-07-14 19:32:16 +08:00
|
|
|
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/alert-notifications", func(alertNotifications RouteRegister) {
|
|
|
|
alertNotifications.Post("/test", bind(dtos.NotificationTestCommand{}), wrap(NotificationTest))
|
|
|
|
alertNotifications.Post("/", bind(m.CreateAlertNotificationCommand{}), wrap(CreateAlertNotification))
|
|
|
|
alertNotifications.Put("/:notificationId", bind(m.UpdateAlertNotificationCommand{}), wrap(UpdateAlertNotification))
|
|
|
|
alertNotifications.Get("/:notificationId", wrap(GetAlertNotificationById))
|
|
|
|
alertNotifications.Delete("/:notificationId", wrap(DeleteAlertNotification))
|
2016-10-19 14:01:14 +08:00
|
|
|
}, reqEditorRole)
|
2016-07-14 19:32:16 +08:00
|
|
|
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Get("/annotations", wrap(GetAnnotations))
|
|
|
|
apiRoute.Post("/annotations/mass-delete", reqOrgAdmin, bind(dtos.DeleteAnnotationsCmd{}), wrap(DeleteAnnotations))
|
2017-04-12 21:46:41 +08:00
|
|
|
|
2017-09-13 20:53:38 +08:00
|
|
|
apiRoute.Group("/annotations", func(annotationsRoute RouteRegister) {
|
|
|
|
annotationsRoute.Post("/", bind(dtos.PostAnnotationsCmd{}), wrap(PostAnnotation))
|
2017-10-07 16:31:39 +08:00
|
|
|
annotationsRoute.Delete("/:annotationId", wrap(DeleteAnnotationById))
|
|
|
|
annotationsRoute.Put("/:annotationId", bind(dtos.UpdateAnnotationsCmd{}), wrap(UpdateAnnotation))
|
|
|
|
annotationsRoute.Delete("/region/:regionId", wrap(DeleteAnnotationRegion))
|
2017-10-12 16:12:15 +08:00
|
|
|
annotationsRoute.Post("/graphite", bind(dtos.PostGraphiteAnnotationsCmd{}), wrap(PostGraphiteAnnotation))
|
2017-04-12 21:46:41 +08:00
|
|
|
}, reqEditorRole)
|
2016-09-08 17:25:45 +08:00
|
|
|
|
2016-06-07 16:05:10 +08:00
|
|
|
// error test
|
|
|
|
r.Get("/metrics/error", wrap(GenerateError))
|
|
|
|
|
2015-01-15 19:16:54 +08:00
|
|
|
}, reqSignedIn)
|
|
|
|
|
|
|
|
// admin api
|
2017-09-13 20:53:38 +08:00
|
|
|
r.Group("/api/admin", func(adminRoute RouteRegister) {
|
|
|
|
adminRoute.Get("/settings", AdminGetSettings)
|
|
|
|
adminRoute.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser)
|
|
|
|
adminRoute.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword)
|
|
|
|
adminRoute.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), AdminUpdateUserPermissions)
|
|
|
|
adminRoute.Delete("/users/:id", AdminDeleteUser)
|
|
|
|
adminRoute.Get("/users/:id/quotas", wrap(GetUserQuotas))
|
|
|
|
adminRoute.Put("/users/:id/quotas/:target", bind(m.UpdateUserQuotaCmd{}), wrap(UpdateUserQuota))
|
|
|
|
adminRoute.Get("/stats", AdminGetStats)
|
|
|
|
adminRoute.Post("/pause-all-alerts", bind(dtos.PauseAllAlertsCommand{}), wrap(PauseAllAlerts))
|
2015-01-16 21:32:18 +08:00
|
|
|
}, reqGrafanaAdmin)
|
2014-12-16 04:25:02 +08:00
|
|
|
|
|
|
|
// rendering
|
2015-01-16 18:54:19 +08:00
|
|
|
r.Get("/render/*", reqSignedIn, RenderToPng)
|
2015-01-06 16:11:00 +08:00
|
|
|
|
2016-04-09 04:42:33 +08:00
|
|
|
// grafana.net proxy
|
|
|
|
r.Any("/api/gnet/*", reqSignedIn, ProxyGnetRequest)
|
|
|
|
|
2016-02-21 06:51:22 +08:00
|
|
|
// Gravatar service.
|
2017-11-17 20:13:58 +08:00
|
|
|
avatarCacheServer := avatar.NewCacheServer()
|
|
|
|
r.Get("/avatar/:hash", avatarCacheServer.Handler)
|
2016-02-21 06:51:22 +08:00
|
|
|
|
2016-03-04 06:05:08 +08:00
|
|
|
// Websocket
|
2016-12-21 21:36:32 +08:00
|
|
|
r.Any("/ws", hs.streamManager.Serve)
|
2016-03-04 06:05:08 +08:00
|
|
|
|
2016-03-14 23:18:07 +08:00
|
|
|
// streams
|
2016-12-21 21:36:32 +08:00
|
|
|
//r.Post("/api/streams/push", reqSignedIn, bind(dtos.StreamMessage{}), liveConn.PushToStream)
|
2016-03-14 23:18:07 +08:00
|
|
|
|
2017-09-13 20:53:38 +08:00
|
|
|
r.Register(macaronR)
|
|
|
|
|
|
|
|
InitAppPluginRoutes(macaronR)
|
2015-10-06 17:20:50 +08:00
|
|
|
|
2017-09-13 20:53:38 +08:00
|
|
|
macaronR.NotFound(NotFoundHandler)
|
2014-12-16 04:25:02 +08:00
|
|
|
}
|