2014-12-15 14:25:02 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-01-13 08:38:54 -06:00
|
|
|
"github.com/go-macaron/binding"
|
2016-02-20 16:51:22 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/avatar"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2014-12-15 14:25:02 -06:00
|
|
|
)
|
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// Register adds http routes
|
2016-12-21 07:36:32 -06:00
|
|
|
func (hs *HttpServer) registerRoutes() {
|
2017-09-13 07:53:38 -05:00
|
|
|
macaronR := hs.macaron
|
2015-01-15 05:16:54 -06:00
|
|
|
reqSignedIn := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})
|
2015-01-16 07:32:18 -06:00
|
|
|
reqGrafanaAdmin := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})
|
2015-01-19 11:01:04 -06:00
|
|
|
reqEditorRole := middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)
|
2015-12-03 09:43:55 -06:00
|
|
|
reqOrgAdmin := middleware.RoleAuth(m.ROLE_ADMIN)
|
2015-09-11 10:17:10 -05:00
|
|
|
quota := middleware.Quota
|
2015-01-16 04:54:19 -06:00
|
|
|
bind := binding.Bind
|
2014-12-15 14:25:02 -06:00
|
|
|
|
2016-08-29 07:45:28 -05:00
|
|
|
// automatically set HEAD for every GET
|
2017-09-13 07:53:38 -05:00
|
|
|
macaronR.SetAutoHead(true)
|
|
|
|
|
2017-09-14 08:05:49 -05:00
|
|
|
r := newRouteRegister(middleware.RequestMetrics, middleware.RequestTracing)
|
2016-08-29 07:45:28 -05:00
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// not logged in views
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Get("/", reqSignedIn, Index)
|
2015-01-29 08:46:54 -06:00
|
|
|
r.Get("/logout", Logout)
|
2015-09-11 10:17:10 -05:00
|
|
|
r.Post("/login", quota("session"), bind(dtos.LoginCommand{}), wrap(LoginPost))
|
|
|
|
r.Get("/login/:name", quota("session"), OAuthLogin)
|
2015-01-27 03:09:54 -06:00
|
|
|
r.Get("/login", LoginView)
|
2015-08-10 06:46:59 -05:00
|
|
|
r.Get("/invite/:code", Index)
|
2014-12-15 14:25:02 -06:00
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// authed views
|
2015-01-19 12:10:29 -06:00
|
|
|
r.Get("/profile/", reqSignedIn, Index)
|
2016-04-04 12:52:41 -05:00
|
|
|
r.Get("/profile/password", reqSignedIn, Index)
|
2016-04-09 12:27:06 -05:00
|
|
|
r.Get("/profile/switch-org/:id", reqSignedIn, ChangeActiveOrgAndRedirectToHome)
|
2015-02-25 07:27:34 -06:00
|
|
|
r.Get("/org/", reqSignedIn, Index)
|
2015-06-11 01:16:09 -05:00
|
|
|
r.Get("/org/new", reqSignedIn, Index)
|
2015-02-25 07:27:34 -06:00
|
|
|
r.Get("/datasources/", reqSignedIn, Index)
|
2016-06-17 06:35:29 -05:00
|
|
|
r.Get("/datasources/new", reqSignedIn, Index)
|
2015-02-28 01:25:13 -06:00
|
|
|
r.Get("/datasources/edit/*", reqSignedIn, Index)
|
2015-02-25 07:27:34 -06:00
|
|
|
r.Get("/org/users/", reqSignedIn, Index)
|
|
|
|
r.Get("/org/apikeys/", reqSignedIn, Index)
|
|
|
|
r.Get("/dashboard/import/", reqSignedIn, Index)
|
2016-03-03 16:05:08 -06:00
|
|
|
r.Get("/admin", reqGrafanaAdmin, Index)
|
2015-02-12 08:46:14 -06:00
|
|
|
r.Get("/admin/settings", reqGrafanaAdmin, Index)
|
2015-01-28 04:33:50 -06:00
|
|
|
r.Get("/admin/users", reqGrafanaAdmin, Index)
|
2015-02-10 09:26:23 -06:00
|
|
|
r.Get("/admin/users/create", reqGrafanaAdmin, Index)
|
|
|
|
r.Get("/admin/users/edit/:id", reqGrafanaAdmin, Index)
|
2015-08-11 08:20:50 -05:00
|
|
|
r.Get("/admin/orgs", reqGrafanaAdmin, Index)
|
|
|
|
r.Get("/admin/orgs/edit/:id", reqGrafanaAdmin, Index)
|
2016-01-24 23:18:17 -06:00
|
|
|
r.Get("/admin/stats", reqGrafanaAdmin, Index)
|
2015-10-20 09:02:56 -05:00
|
|
|
|
2016-03-28 06:10:42 -05:00
|
|
|
r.Get("/styleguide", reqSignedIn, Index)
|
|
|
|
|
2016-02-29 05:54:36 -06:00
|
|
|
r.Get("/plugins", reqSignedIn, Index)
|
|
|
|
r.Get("/plugins/:id/edit", reqSignedIn, Index)
|
|
|
|
r.Get("/plugins/:id/page/:page", reqSignedIn, Index)
|
2015-12-04 05:21:33 -06:00
|
|
|
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Get("/dashboard/*", reqSignedIn, Index)
|
2016-09-22 04:46:58 -05:00
|
|
|
r.Get("/dashboard-solo/snapshot/*", Index)
|
2015-10-20 09:02:56 -05:00
|
|
|
r.Get("/dashboard-solo/*", reqSignedIn, Index)
|
2016-05-03 09:40:21 -05:00
|
|
|
r.Get("/import/dashboard", reqSignedIn, Index)
|
2016-05-19 04:03:10 -05:00
|
|
|
r.Get("/dashboards/*", reqSignedIn, Index)
|
2014-12-16 05:04:08 -06:00
|
|
|
|
2015-12-22 04:07:15 -06:00
|
|
|
r.Get("/playlists/", reqSignedIn, Index)
|
|
|
|
r.Get("/playlists/*", reqSignedIn, Index)
|
2016-06-06 02:17:29 -05:00
|
|
|
r.Get("/alerting/", reqSignedIn, Index)
|
2016-06-15 03:48:04 -05:00
|
|
|
r.Get("/alerting/*", reqSignedIn, Index)
|
2015-12-22 04:07:15 -06:00
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// sign up
|
2015-07-19 05:34:03 -05:00
|
|
|
r.Get("/signup", Index)
|
2015-08-31 04:35:07 -05:00
|
|
|
r.Get("/api/user/signup/options", wrap(GetSignUpOptions))
|
2015-09-11 10:17:10 -05:00
|
|
|
r.Post("/api/user/signup", quota("user"), bind(dtos.SignUpForm{}), wrap(SignUp))
|
2015-08-28 02:24:30 -05:00
|
|
|
r.Post("/api/user/signup/step2", bind(dtos.SignUpStep2Form{}), wrap(SignUpStep2))
|
2014-12-15 14:25:02 -06:00
|
|
|
|
2015-07-20 08:52:49 -05:00
|
|
|
// invited
|
|
|
|
r.Get("/api/user/invite/:code", wrap(GetInviteInfoByCode))
|
2015-07-20 10:46:48 -05:00
|
|
|
r.Post("/api/user/invite/complete", bind(dtos.CompleteInviteForm{}), wrap(CompleteInvite))
|
2015-07-20 08:52:49 -05:00
|
|
|
|
2015-06-08 03:57:01 -05: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 06:39:02 -05:00
|
|
|
r.Post("/api/user/password/reset", bind(dtos.ResetUserPasswordForm{}), wrap(ResetPassword))
|
2014-12-15 14:25:02 -06:00
|
|
|
|
2015-03-21 07:53:16 -05:00
|
|
|
// dashboard snapshots
|
2015-04-15 03:39:03 -05:00
|
|
|
r.Get("/dashboard/snapshot/*", Index)
|
2016-01-19 07:05:24 -06:00
|
|
|
r.Get("/dashboard/snapshots/", reqSignedIn, Index)
|
2015-03-26 14:34:58 -05:00
|
|
|
|
2016-01-19 07:05:24 -06:00
|
|
|
// api for dashboard snapshots
|
|
|
|
r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot)
|
2015-10-14 09:39:57 -05:00
|
|
|
r.Get("/api/snapshot/shared-options/", GetSharingOptions)
|
2015-03-21 07:53:16 -05:00
|
|
|
r.Get("/api/snapshots/:key", GetDashboardSnapshot)
|
2016-03-18 03:13:34 -05:00
|
|
|
r.Get("/api/snapshots-delete/:key", reqEditorRole, DeleteDashboardSnapshot)
|
2015-03-21 07:53:16 -05:00
|
|
|
|
2015-04-07 02:25:00 -05:00
|
|
|
// api renew session based on remember cookie
|
2015-09-11 10:17:10 -05:00
|
|
|
r.Get("/api/login/ping", quota("session"), LoginApiPing)
|
2015-04-07 02:25:00 -05:00
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// authed api
|
2017-09-13 07:53:38 -05:00
|
|
|
r.Group("/api", func(apiRoute RouteRegister) {
|
2015-05-19 04:47:14 -05:00
|
|
|
|
|
|
|
// user (signed in)
|
2017-09-13 07:53:38 -05: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 03:41:39 -06:00
|
|
|
// For dev purpose
|
2017-09-13 07:53:38 -05:00
|
|
|
userRoute.Get("/helpflags/clear", wrap(ClearHelpFlags))
|
2016-04-02 15:54:06 -05:00
|
|
|
|
2017-09-13 07:53:38 -05:00
|
|
|
userRoute.Get("/preferences", wrap(GetUserPreferences))
|
|
|
|
userRoute.Put("/preferences", bind(dtos.UpdatePrefsCmd{}), wrap(UpdateUserPreferences))
|
2015-01-19 11:01:04 -06:00
|
|
|
})
|
|
|
|
|
2015-05-19 04:47:14 -05:00
|
|
|
// users (admin permission required)
|
2017-09-13 07:53:38 -05: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-30 23:25:55 -06:00
|
|
|
// query parameters /users/lookup?loginOrEmail=admin@example.com
|
2017-09-13 07:53:38 -05:00
|
|
|
usersRoute.Get("/lookup", wrap(GetUserByLoginOrEmail))
|
|
|
|
usersRoute.Put("/:id", bind(m.UpdateUserCommand{}), wrap(UpdateUser))
|
|
|
|
usersRoute.Post("/:id/using/:orgId", wrap(UpdateUserActiveOrg))
|
2015-05-18 10:28:15 -05:00
|
|
|
}, reqGrafanaAdmin)
|
|
|
|
|
2015-09-10 12:18:36 -05:00
|
|
|
// org information available to all users.
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Group("/org", func(orgRoute RouteRegister) {
|
|
|
|
orgRoute.Get("/", wrap(GetOrgCurrent))
|
|
|
|
orgRoute.Get("/quotas", wrap(GetOrgQuotas))
|
2015-09-10 12:18:36 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
// current org
|
2017-09-13 07:53:38 -05: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 02:51:34 -05:00
|
|
|
|
|
|
|
// invites
|
2017-09-13 07:53:38 -05: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-17 23:46:40 -06:00
|
|
|
|
2016-04-02 15:54:06 -05:00
|
|
|
// prefs
|
2017-09-13 07:53:38 -05:00
|
|
|
orgRoute.Get("/preferences", wrap(GetOrgPreferences))
|
|
|
|
orgRoute.Put("/preferences", bind(dtos.UpdatePrefsCmd{}), wrap(UpdateOrgPreferences))
|
2015-12-03 09:43:55 -06:00
|
|
|
}, reqOrgAdmin)
|
2015-05-19 03:16:32 -05:00
|
|
|
|
|
|
|
// create new org
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Post("/orgs", quota("org"), bind(m.CreateOrgCommand{}), wrap(CreateOrg))
|
2015-05-19 03:16:32 -05:00
|
|
|
|
2015-05-19 04:47:14 -05:00
|
|
|
// search all orgs
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Get("/orgs", reqGrafanaAdmin, wrap(SearchOrgs))
|
2015-05-19 04:47:14 -05:00
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
// orgs (admin routes)
|
2017-09-13 07:53:38 -05: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 03:16:32 -05:00
|
|
|
}, reqGrafanaAdmin)
|
2015-01-26 13:26:17 -06:00
|
|
|
|
2016-01-12 15:50:56 -06:00
|
|
|
// orgs (admin routes)
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Group("/orgs/name/:name", func(orgsRoute RouteRegister) {
|
|
|
|
orgsRoute.Get("/", wrap(GetOrgByName))
|
2016-01-12 15:50:56 -06:00
|
|
|
}, reqGrafanaAdmin)
|
|
|
|
|
2015-01-27 01:26:11 -06:00
|
|
|
// auth api keys
|
2017-09-13 07:53:38 -05: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 09:43:55 -06:00
|
|
|
}, reqOrgAdmin)
|
2015-01-26 13:26:17 -06:00
|
|
|
|
2016-03-17 01:35:06 -05:00
|
|
|
// Preferences
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Group("/preferences", func(prefRoute RouteRegister) {
|
|
|
|
prefRoute.Post("/set-home-dash", bind(m.SavePreferencesCommand{}), wrap(SetHomeDashboard))
|
2016-03-17 01:35:06 -05:00
|
|
|
})
|
2016-03-11 08:30:05 -06:00
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// Data sources
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Group("/datasources", func(datasourceRoute RouteRegister) {
|
|
|
|
datasourceRoute.Get("/", wrap(GetDataSources))
|
2017-11-16 09:29:05 -06:00
|
|
|
datasourceRoute.Post("/", quota("data_source"), bind(m.AddDataSourceCommand{}), wrap(AddDataSource))
|
2017-09-13 07:53:38 -05:00
|
|
|
datasourceRoute.Put("/:id", bind(m.UpdateDataSourceCommand{}), wrap(UpdateDataSource))
|
2017-11-16 09:29:05 -06:00
|
|
|
datasourceRoute.Delete("/:id", wrap(DeleteDataSourceById))
|
|
|
|
datasourceRoute.Delete("/name/:name", wrap(DeleteDataSourceByName))
|
2017-09-13 07:53:38 -05:00
|
|
|
datasourceRoute.Get("/:id", wrap(GetDataSourceById))
|
|
|
|
datasourceRoute.Get("/name/:name", wrap(GetDataSourceByName))
|
2015-12-03 09:43:55 -06:00
|
|
|
}, reqOrgAdmin)
|
|
|
|
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Get("/datasources/id/:name", wrap(GetDataSourceIdByName), reqSignedIn)
|
2016-03-07 14:25:26 -06:00
|
|
|
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Get("/plugins", wrap(GetPluginList))
|
|
|
|
apiRoute.Get("/plugins/:pluginId/settings", wrap(GetPluginSettingById))
|
|
|
|
apiRoute.Get("/plugins/:pluginId/markdown/:name", wrap(GetPluginMarkdown))
|
2016-03-11 02:57:20 -06:00
|
|
|
|
2017-09-13 07:53:38 -05: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 09:43:55 -06:00
|
|
|
}, reqOrgAdmin)
|
|
|
|
|
2017-09-13 07:53:38 -05: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 03:19:43 -06:00
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// Dashboard
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Group("/dashboards", func(dashboardRoute RouteRegister) {
|
|
|
|
dashboardRoute.Get("/db/:slug", GetDashboard)
|
|
|
|
dashboardRoute.Delete("/db/:slug", reqEditorRole, 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-24 18:14:39 -05:00
|
|
|
|
2017-09-13 07:53:38 -05:00
|
|
|
dashboardRoute.Get("/id/:dashboardId/versions", wrap(GetDashboardVersions))
|
|
|
|
dashboardRoute.Get("/id/:dashboardId/versions/:id", wrap(GetDashboardVersion))
|
|
|
|
dashboardRoute.Post("/id/:dashboardId/restore", reqEditorRole, bind(dtos.RestoreDashboardVersionCommand{}), wrap(RestoreDashboardVersion))
|
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-24 18:14:39 -05:00
|
|
|
|
2017-09-13 07:53:38 -05:00
|
|
|
dashboardRoute.Post("/calculate-diff", bind(dtos.CalculateDiffOptions{}), wrap(CalculateDashboardDiff))
|
2017-06-07 04:50:09 -05:00
|
|
|
|
2017-09-13 07:53:38 -05:00
|
|
|
dashboardRoute.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), wrap(PostDashboard))
|
|
|
|
dashboardRoute.Get("/file/:file", GetDashboardFromJsonFile)
|
|
|
|
dashboardRoute.Get("/home", wrap(GetHomeDashboard))
|
|
|
|
dashboardRoute.Get("/tags", GetDashboardTags)
|
|
|
|
dashboardRoute.Post("/import", bind(dtos.ImportDashboardCommand{}), wrap(ImportDashboard))
|
2015-01-14 07:25:12 -06:00
|
|
|
})
|
2015-01-26 13:26:17 -06:00
|
|
|
|
2016-01-19 07:05:24 -06:00
|
|
|
// Dashboard snapshots
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Group("/dashboard/snapshots", func(dashboardRoute RouteRegister) {
|
|
|
|
dashboardRoute.Get("/", wrap(SearchDashboardSnapshots))
|
2016-01-19 07:05:24 -06:00
|
|
|
})
|
2016-01-19 03:37:36 -06:00
|
|
|
|
2015-12-22 04:07:15 -06:00
|
|
|
// Playlist
|
2017-09-13 07:53:38 -05: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 04:07:15 -06:00
|
|
|
})
|
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// Search
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Get("/search/", Search)
|
2015-01-26 13:26:17 -06:00
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// metrics
|
2017-09-13 07:53:38 -05: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 04:00:23 -05:00
|
|
|
alertsRoute.Post("/:alertId/pause", reqEditorRole, bind(dtos.PauseAlertCommand{}), wrap(PauseAlert))
|
2017-09-13 07:53:38 -05:00
|
|
|
alertsRoute.Get("/:alertId", ValidateOrgAlert, wrap(GetAlert))
|
|
|
|
alertsRoute.Get("/", wrap(GetAlerts))
|
|
|
|
alertsRoute.Get("/states-for-dashboard", wrap(GetAlertStatesForDashboard))
|
2016-04-26 10:36:50 -05:00
|
|
|
})
|
|
|
|
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Get("/alert-notifications", wrap(GetAlertNotifications))
|
|
|
|
apiRoute.Get("/alert-notifiers", wrap(GetAlertNotifiers))
|
2016-07-14 06:32:16 -05:00
|
|
|
|
2017-09-13 07:53:38 -05: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 01:01:14 -05:00
|
|
|
}, reqEditorRole)
|
2016-07-14 06:32:16 -05:00
|
|
|
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Get("/annotations", wrap(GetAnnotations))
|
|
|
|
apiRoute.Post("/annotations/mass-delete", reqOrgAdmin, bind(dtos.DeleteAnnotationsCmd{}), wrap(DeleteAnnotations))
|
2017-04-12 08:46:41 -05:00
|
|
|
|
2017-09-13 07:53:38 -05:00
|
|
|
apiRoute.Group("/annotations", func(annotationsRoute RouteRegister) {
|
2017-11-16 09:18:03 -06:00
|
|
|
annotationsRoute.Post("/", bind(dtos.PostAnnotationsCmd{}), wrap(PostAnnotation))
|
2017-10-07 03:31:39 -05:00
|
|
|
annotationsRoute.Delete("/:annotationId", wrap(DeleteAnnotationById))
|
|
|
|
annotationsRoute.Put("/:annotationId", bind(dtos.UpdateAnnotationsCmd{}), wrap(UpdateAnnotation))
|
|
|
|
annotationsRoute.Delete("/region/:regionId", wrap(DeleteAnnotationRegion))
|
2017-11-16 09:18:03 -06:00
|
|
|
annotationsRoute.Post("/graphite", bind(dtos.PostGraphiteAnnotationsCmd{}), wrap(PostGraphiteAnnotation))
|
2017-04-12 08:46:41 -05:00
|
|
|
}, reqEditorRole)
|
2016-09-08 04:25:45 -05:00
|
|
|
|
2016-06-07 03:05:10 -05:00
|
|
|
// error test
|
|
|
|
r.Get("/metrics/error", wrap(GenerateError))
|
|
|
|
|
2015-01-15 05:16:54 -06:00
|
|
|
}, reqSignedIn)
|
|
|
|
|
|
|
|
// admin api
|
2017-09-13 07:53:38 -05: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 07:32:18 -06:00
|
|
|
}, reqGrafanaAdmin)
|
2014-12-15 14:25:02 -06:00
|
|
|
|
|
|
|
// rendering
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Get("/render/*", reqSignedIn, RenderToPng)
|
2015-01-06 02:11:00 -06:00
|
|
|
|
2016-04-08 15:42:33 -05:00
|
|
|
// grafana.net proxy
|
|
|
|
r.Any("/api/gnet/*", reqSignedIn, ProxyGnetRequest)
|
|
|
|
|
2016-02-20 16:51:22 -06:00
|
|
|
// Gravatar service.
|
2017-11-17 06:13:58 -06:00
|
|
|
avatarCacheServer := avatar.NewCacheServer()
|
|
|
|
r.Get("/avatar/:hash", avatarCacheServer.Handler)
|
2016-02-20 16:51:22 -06:00
|
|
|
|
2016-03-03 16:05:08 -06:00
|
|
|
// Websocket
|
2016-12-21 07:36:32 -06:00
|
|
|
r.Any("/ws", hs.streamManager.Serve)
|
2016-03-03 16:05:08 -06:00
|
|
|
|
2016-03-14 10:18:07 -05:00
|
|
|
// streams
|
2016-12-21 07:36:32 -06:00
|
|
|
//r.Post("/api/streams/push", reqSignedIn, bind(dtos.StreamMessage{}), liveConn.PushToStream)
|
2016-03-14 10:18:07 -05:00
|
|
|
|
2017-09-13 07:53:38 -05:00
|
|
|
r.Register(macaronR)
|
|
|
|
|
|
|
|
InitAppPluginRoutes(macaronR)
|
2015-10-06 04:20:50 -05:00
|
|
|
|
2017-09-13 07:53:38 -05:00
|
|
|
macaronR.NotFound(NotFoundHandler)
|
2014-12-15 14:25:02 -06:00
|
|
|
}
|