api.go 15.8 KB
Newer Older
1 2 3
package api

import (
4
	"github.com/go-macaron/binding"
5
	"github.com/grafana/grafana/pkg/api/avatar"
6 7 8
	"github.com/grafana/grafana/pkg/api/dtos"
	"github.com/grafana/grafana/pkg/middleware"
	m "github.com/grafana/grafana/pkg/models"
9 10
)

11
// Register adds http routes
12
func (hs *HttpServer) registerRoutes() {
13
	macaronR := hs.macaron
14
	reqSignedIn := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})
15
	reqGrafanaAdmin := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})
16
	reqEditorRole := middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)
17
	reqOrgAdmin := middleware.RoleAuth(m.ROLE_ADMIN)
woodsaj committed
18
	quota := middleware.Quota
19
	bind := binding.Bind
20

21
	// automatically set HEAD for every GET
22 23
	macaronR.SetAutoHead(true)

bergquist committed
24
	r := newRouteRegister(middleware.RequestMetrics, middleware.RequestTracing)
25

26
	// not logged in views
27
	r.Get("/", reqSignedIn, Index)
28
	r.Get("/logout", Logout)
woodsaj committed
29 30
	r.Post("/login", quota("session"), bind(dtos.LoginCommand{}), wrap(LoginPost))
	r.Get("/login/:name", quota("session"), OAuthLogin)
31
	r.Get("/login", LoginView)
32
	r.Get("/invite/:code", Index)
33

34
	// authed views
Torkel Ödegaard committed
35
	r.Get("/profile/", reqSignedIn, Index)
36
	r.Get("/profile/password", reqSignedIn, Index)
37
	r.Get("/profile/switch-org/:id", reqSignedIn, ChangeActiveOrgAndRedirectToHome)
38
	r.Get("/org/", reqSignedIn, Index)
39
	r.Get("/org/new", reqSignedIn, Index)
40
	r.Get("/datasources/", reqSignedIn, Index)
41
	r.Get("/datasources/new", reqSignedIn, Index)
42
	r.Get("/datasources/edit/*", reqSignedIn, Index)
43
	r.Get("/org/users", reqSignedIn, Index)
44 45
	r.Get("/org/users/new", reqSignedIn, Index)
	r.Get("/org/users/invite", reqSignedIn, Index)
46 47
	r.Get("/org/teams", reqSignedIn, Index)
	r.Get("/org/teams/*", reqSignedIn, Index)
48 49
	r.Get("/org/apikeys/", reqSignedIn, Index)
	r.Get("/dashboard/import/", reqSignedIn, Index)
50
	r.Get("/configuration", reqGrafanaAdmin, Index)
51
	r.Get("/admin", reqGrafanaAdmin, Index)
52
	r.Get("/admin/settings", reqGrafanaAdmin, Index)
53
	r.Get("/admin/users", reqGrafanaAdmin, Index)
54 55
	r.Get("/admin/users/create", reqGrafanaAdmin, Index)
	r.Get("/admin/users/edit/:id", reqGrafanaAdmin, Index)
56 57
	r.Get("/admin/orgs", reqGrafanaAdmin, Index)
	r.Get("/admin/orgs/edit/:id", reqGrafanaAdmin, Index)
58
	r.Get("/admin/stats", reqGrafanaAdmin, Index)
59

60 61
	r.Get("/styleguide", reqSignedIn, Index)

62 63 64
	r.Get("/plugins", reqSignedIn, Index)
	r.Get("/plugins/:id/edit", reqSignedIn, Index)
	r.Get("/plugins/:id/page/:page", reqSignedIn, Index)
woodsaj committed
65

66
	r.Get("/dashboard/*", reqSignedIn, Index)
67
	r.Get("/dashboard-solo/snapshot/*", Index)
68
	r.Get("/dashboard-solo/*", reqSignedIn, Index)
69
	r.Get("/import/dashboard", reqSignedIn, Index)
70
	r.Get("/dashboards/", reqSignedIn, Index)
71
	r.Get("/dashboards/*", reqSignedIn, Index)
72

73 74
	r.Get("/playlists/", reqSignedIn, Index)
	r.Get("/playlists/*", reqSignedIn, Index)
75
	r.Get("/alerting/", reqSignedIn, Index)
76
	r.Get("/alerting/*", reqSignedIn, Index)
77

78
	// sign up
79
	r.Get("/signup", Index)
80
	r.Get("/api/user/signup/options", wrap(GetSignUpOptions))
woodsaj committed
81
	r.Post("/api/user/signup", quota("user"), bind(dtos.SignUpForm{}), wrap(SignUp))
82
	r.Post("/api/user/signup/step2", bind(dtos.SignUpStep2Form{}), wrap(SignUpStep2))
83

84 85
	// invited
	r.Get("/api/user/invite/:code", wrap(GetInviteInfoByCode))
86
	r.Post("/api/user/invite/complete", bind(dtos.CompleteInviteForm{}), wrap(CompleteInvite))
87

88 89 90 91 92
	// 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))
93
	r.Post("/api/user/password/reset", bind(dtos.ResetUserPasswordForm{}), wrap(ResetPassword))
94

95
	// dashboard snapshots
96
	r.Get("/dashboard/snapshot/*", Index)
utkarshcmu committed
97
	r.Get("/dashboard/snapshots/", reqSignedIn, Index)
98

utkarshcmu committed
99 100
	// api for dashboard snapshots
	r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot)
101
	r.Get("/api/snapshot/shared-options/", GetSharingOptions)
102
	r.Get("/api/snapshots/:key", GetDashboardSnapshot)
103
	r.Get("/api/snapshots-delete/:key", reqEditorRole, DeleteDashboardSnapshot)
104

105
	// api renew session based on remember cookie
woodsaj committed
106
	r.Get("/api/login/ping", quota("session"), LoginApiPing)
107

108
	// authed api
109
	r.Group("/api", func(apiRoute RouteRegister) {
110 111

		// user (signed in)
112 113 114 115 116 117 118 119 120 121 122 123
		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))
124
			// For dev purpose
125
			userRoute.Get("/helpflags/clear", wrap(ClearHelpFlags))
126

127 128
			userRoute.Get("/preferences", wrap(GetUserPreferences))
			userRoute.Put("/preferences", bind(dtos.UpdatePrefsCmd{}), wrap(UpdateUserPreferences))
129 130
		})

131
		// users (admin permission required)
132 133 134 135 136
		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))
137
			// query parameters /users/lookup?loginOrEmail=admin@example.com
138 139 140
			usersRoute.Get("/lookup", wrap(GetUserByLoginOrEmail))
			usersRoute.Put("/:id", bind(m.UpdateUserCommand{}), wrap(UpdateUser))
			usersRoute.Post("/:id/using/:orgId", wrap(UpdateUserActiveOrg))
141 142
		}, reqGrafanaAdmin)

143 144 145 146 147 148 149 150 151 152
		// 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))
153
		}, reqOrgAdmin)
154

155
		// org information available to all users.
156 157 158
		apiRoute.Group("/org", func(orgRoute RouteRegister) {
			orgRoute.Get("/", wrap(GetOrgCurrent))
			orgRoute.Get("/quotas", wrap(GetOrgQuotas))
159 160 161
		})

		// current org
162 163 164 165 166 167 168
		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))
169 170

			// invites
171 172 173
			orgRoute.Get("/invites", wrap(GetPendingOrgInvites))
			orgRoute.Post("/invites", quota("user"), bind(dtos.AddInviteForm{}), wrap(AddOrgInvite))
			orgRoute.Patch("/invites/:code/revoke", wrap(RevokeInvite))
174

175
			// prefs
176 177
			orgRoute.Get("/preferences", wrap(GetOrgPreferences))
			orgRoute.Put("/preferences", bind(dtos.UpdatePrefsCmd{}), wrap(UpdateOrgPreferences))
178
		}, reqOrgAdmin)
179 180

		// create new org
181
		apiRoute.Post("/orgs", quota("org"), bind(m.CreateOrgCommand{}), wrap(CreateOrg))
182

183
		// search all orgs
184
		apiRoute.Get("/orgs", reqGrafanaAdmin, wrap(SearchOrgs))
185

186
		// orgs (admin routes)
187 188 189 190 191 192 193 194 195 196 197
		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))
198
		}, reqGrafanaAdmin)
199

200
		// orgs (admin routes)
201 202
		apiRoute.Group("/orgs/name/:name", func(orgsRoute RouteRegister) {
			orgsRoute.Get("/", wrap(GetOrgByName))
203 204
		}, reqGrafanaAdmin)

205
		// auth api keys
206 207 208 209
		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))
210
		}, reqOrgAdmin)
211

utkarshcmu committed
212
		// Preferences
213 214
		apiRoute.Group("/preferences", func(prefRoute RouteRegister) {
			prefRoute.Post("/set-home-dash", bind(m.SavePreferencesCommand{}), wrap(SetHomeDashboard))
utkarshcmu committed
215
		})
utkarshcmu committed
216

217
		// Data sources
218 219
		apiRoute.Group("/datasources", func(datasourceRoute RouteRegister) {
			datasourceRoute.Get("/", wrap(GetDataSources))
220
			datasourceRoute.Post("/", quota("data_source"), bind(m.AddDataSourceCommand{}), wrap(AddDataSource))
221
			datasourceRoute.Put("/:id", bind(m.UpdateDataSourceCommand{}), wrap(UpdateDataSource))
222 223
			datasourceRoute.Delete("/:id", wrap(DeleteDataSourceById))
			datasourceRoute.Delete("/name/:name", wrap(DeleteDataSourceByName))
224 225
			datasourceRoute.Get("/:id", wrap(GetDataSourceById))
			datasourceRoute.Get("/name/:name", wrap(GetDataSourceByName))
226 227
		}, reqOrgAdmin)

228
		apiRoute.Get("/datasources/id/:name", wrap(GetDataSourceIdByName), reqSignedIn)
229

230 231 232
		apiRoute.Get("/plugins", wrap(GetPluginList))
		apiRoute.Get("/plugins/:pluginId/settings", wrap(GetPluginSettingById))
		apiRoute.Get("/plugins/:pluginId/markdown/:name", wrap(GetPluginMarkdown))
233

234 235 236
		apiRoute.Group("/plugins", func(pluginRoute RouteRegister) {
			pluginRoute.Get("/:pluginId/dashboards/", wrap(GetPluginDashboards))
			pluginRoute.Post("/:pluginId/settings", bind(m.UpdatePluginSettingCmd{}), wrap(UpdatePluginSetting))
237 238
		}, reqOrgAdmin)

239 240 241
		apiRoute.Get("/frontend/settings/", GetFrontendSettings)
		apiRoute.Any("/datasources/proxy/:id/*", reqSignedIn, hs.ProxyDataSourceRequest)
		apiRoute.Any("/datasources/proxy/:id", reqSignedIn, hs.ProxyDataSourceRequest)
242

243
		// Dashboard
244
		apiRoute.Group("/dashboards", func(dashboardRoute RouteRegister) {
245 246
			dashboardRoute.Get("/db/:slug", wrap(GetDashboard))
			dashboardRoute.Delete("/db/:slug", reqEditorRole, wrap(DeleteDashboard))
247

248
			dashboardRoute.Post("/calculate-diff", bind(dtos.CalculateDiffOptions{}), wrap(CalculateDashboardDiff))
249

250 251 252 253
			dashboardRoute.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), wrap(PostDashboard))
			dashboardRoute.Get("/home", wrap(GetHomeDashboard))
			dashboardRoute.Get("/tags", GetDashboardTags)
			dashboardRoute.Post("/import", bind(dtos.ImportDashboardCommand{}), wrap(ImportDashboard))
254 255

			dashboardRoute.Group("/id/:dashboardId", func(dashIdRoute RouteRegister) {
256 257 258
				dashIdRoute.Get("/versions", wrap(GetDashboardVersions))
				dashIdRoute.Get("/versions/:id", wrap(GetDashboardVersion))
				dashIdRoute.Post("/restore", reqEditorRole, bind(dtos.RestoreDashboardVersionCommand{}), wrap(RestoreDashboardVersion))
259 260 261 262 263 264 265

				dashIdRoute.Group("/acl", func(aclRoute RouteRegister) {
					aclRoute.Get("/", wrap(GetDashboardAclList))
					aclRoute.Post("/", bind(dtos.UpdateDashboardAclCommand{}), wrap(UpdateDashboardAcl))
					aclRoute.Delete("/:aclId", wrap(DeleteDashboardAcl))
				})
			})
266
		})
267

utkarshcmu committed
268
		// Dashboard snapshots
269 270
		apiRoute.Group("/dashboard/snapshots", func(dashboardRoute RouteRegister) {
			dashboardRoute.Get("/", wrap(SearchDashboardSnapshots))
utkarshcmu committed
271
		})
272

273
		// Playlist
274 275 276 277 278 279 280 281
		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))
282 283
		})

284
		// Search
285
		apiRoute.Get("/search/", Search)
286

287
		// metrics
288 289 290 291 292 293 294
		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))
295
			alertsRoute.Post("/:alertId/pause", reqEditorRole, bind(dtos.PauseAlertCommand{}), wrap(PauseAlert))
296 297 298
			alertsRoute.Get("/:alertId", ValidateOrgAlert, wrap(GetAlert))
			alertsRoute.Get("/", wrap(GetAlerts))
			alertsRoute.Get("/states-for-dashboard", wrap(GetAlertStatesForDashboard))
299 300
		})

301 302
		apiRoute.Get("/alert-notifications", wrap(GetAlertNotifications))
		apiRoute.Get("/alert-notifiers", wrap(GetAlertNotifiers))
303

304 305 306 307 308 309
		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))
310
		}, reqEditorRole)
311

312 313
		apiRoute.Get("/annotations", wrap(GetAnnotations))
		apiRoute.Post("/annotations/mass-delete", reqOrgAdmin, bind(dtos.DeleteAnnotationsCmd{}), wrap(DeleteAnnotations))
314

315 316
		apiRoute.Group("/annotations", func(annotationsRoute RouteRegister) {
			annotationsRoute.Post("/", bind(dtos.PostAnnotationsCmd{}), wrap(PostAnnotation))
317 318 319
			annotationsRoute.Delete("/:annotationId", wrap(DeleteAnnotationById))
			annotationsRoute.Put("/:annotationId", bind(dtos.UpdateAnnotationsCmd{}), wrap(UpdateAnnotation))
			annotationsRoute.Delete("/region/:regionId", wrap(DeleteAnnotationRegion))
320
			annotationsRoute.Post("/graphite", bind(dtos.PostGraphiteAnnotationsCmd{}), wrap(PostGraphiteAnnotation))
321
		}, reqEditorRole)
322

323 324 325
		// error test
		r.Get("/metrics/error", wrap(GenerateError))

326 327 328
	}, reqSignedIn)

	// admin api
329 330 331 332 333 334 335 336 337 338
	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))
339
	}, reqGrafanaAdmin)
340 341

	// rendering
342
	r.Get("/render/*", reqSignedIn, RenderToPng)
343

344 345 346
	// grafana.net proxy
	r.Any("/api/gnet/*", reqSignedIn, ProxyGnetRequest)

347
	// Gravatar service.
348 349
	avatarCacheServer := avatar.NewCacheServer()
	r.Get("/avatar/:hash", avatarCacheServer.Handler)
350

351
	// Websocket
352
	r.Any("/ws", hs.streamManager.Serve)
353

354
	// streams
355
	//r.Post("/api/streams/push", reqSignedIn, bind(dtos.StreamMessage{}), liveConn.PushToStream)
356

357 358 359
	r.Register(macaronR)

	InitAppPluginRoutes(macaronR)
360

361
	macaronR.NotFound(NotFoundHandler)
362
}