@ -1,9 +1,7 @@
package api
import (
"fmt"
"net/http"
"strconv"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/api/routing"
@ -12,6 +10,7 @@ import (
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/services/cloudmigration"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/web"
)
@ -37,17 +36,17 @@ func RegisterApi(
return api
}
// RegisterAPI Endpoints Registers Endpoints on Grafana Router
// register Endpoints Registers Endpoints on Grafana Router
func ( cma * CloudMigrationAPI ) registerEndpoints ( ) {
cma . routeRegister . Group ( "/api/cloudmigration" , func ( cloudMigrationRoute routing . RouteRegister ) {
// migration
cloudMigrationRoute . Get ( "/migration" , routing . Wrap ( cma . GetMigrationList ) )
cloudMigrationRoute . Post ( "/migration" , routing . Wrap ( cma . CreateMigration ) )
cloudMigrationRoute . Get ( "/migration/:id" , routing . Wrap ( cma . GetMigration ) )
cloudMigrationRoute . Delete ( "/migration/:id" , routing . Wrap ( cma . DeleteMigration ) )
cloudMigrationRoute . Post ( "/migration/:id/run" , routing . Wrap ( cma . RunMigration ) )
cloudMigrationRoute . Get ( "/migration/:id/run" , routing . Wrap ( cma . GetMigrationRunList ) )
cloudMigrationRoute . Get ( "/migration/:id/ run/:runID" , routing . Wrap ( cma . GetMigrationRun ) )
cloudMigrationRoute . Get ( "/migration/:u id" , routing . Wrap ( cma . GetMigration ) )
cloudMigrationRoute . Delete ( "/migration/:u id" , routing . Wrap ( cma . DeleteMigration ) )
cloudMigrationRoute . Post ( "/migration/:u id/run" , routing . Wrap ( cma . RunMigration ) )
cloudMigrationRoute . Get ( "/migration/:u id/run" , routing . Wrap ( cma . GetMigrationRunList ) )
cloudMigrationRoute . Get ( "/migration/run/:runU ID" , routing . Wrap ( cma . GetMigrationRun ) )
cloudMigrationRoute . Post ( "/token" , routing . Wrap ( cma . CreateToken ) )
} , middleware . ReqOrgAdmin )
}
@ -97,7 +96,7 @@ func (cma *CloudMigrationAPI) GetMigrationList(c *contextmodel.ReqContext) respo
return response . JSON ( http . StatusOK , cloudMigrations )
}
// swagger:route GET /cloudmigration/migration/{id} migrations getCloudMigration
// swagger:route GET /cloudmigration/migration/{u id} migrations getCloudMigration
//
// Get a cloud migration.
//
@ -112,11 +111,12 @@ func (cma *CloudMigrationAPI) GetMigration(c *contextmodel.ReqContext) response.
ctx , span := cma . tracer . Start ( c . Req . Context ( ) , "MigrationAPI.GetMigration" )
defer span . End ( )
id , err := strconv . ParseInt ( web . Params ( c . Req ) [ ":id" ] , 10 , 64 )
if err != nil {
return response . Error ( http . StatusBadRequest , "id is inval id" , err )
u id := web . Params ( c . Req ) [ ":u id" ]
if err := util . ValidateUID ( uid ) ; err != nil {
return response . Error ( http . StatusBadRequest , "invalid migration u id" , err )
}
cloudMigration , err := cma . cloudMigrationService . GetMigration ( ctx , id )
cloudMigration , err := cma . cloudMigrationService . GetMigration ( ctx , uid )
if err != nil {
return response . Error ( http . StatusNotFound , "migration not found" , err )
}
@ -125,10 +125,10 @@ func (cma *CloudMigrationAPI) GetMigration(c *contextmodel.ReqContext) response.
// swagger:parameters getCloudMigration
type GetCloudMigrationRequest struct {
// ID of an migration
// U ID of a migration
//
// in: path
ID int64 ` json:"id" `
UID string ` json:"u id" `
}
// swagger:route POST /cloudmigration/migration migrations createMigration
@ -155,7 +155,7 @@ func (cma *CloudMigrationAPI) CreateMigration(c *contextmodel.ReqContext) respon
return response . JSON ( http . StatusOK , cloudMigration )
}
// swagger:route POST /cloudmigration/migration/{id}/run migrations runCloudMigration
// swagger:route POST /cloudmigration/migration/{u id}/run migrations runCloudMigration
//
// Trigger the run of a migration to the Grafana Cloud.
//
@ -170,13 +170,12 @@ func (cma *CloudMigrationAPI) RunMigration(c *contextmodel.ReqContext) response.
ctx , span := cma . tracer . Start ( c . Req . Context ( ) , "MigrationAPI.RunMigration" )
defer span . End ( )
stringID := web . Params ( c . Req ) [ ":id" ]
id , err := strconv . ParseInt ( stringID , 10 , 64 )
if err != nil {
return response . Error ( http . StatusBadRequest , "id is invalid" , err )
uid := web . Params ( c . Req ) [ ":uid" ]
if err := util . ValidateUID ( uid ) ; err != nil {
return response . Error ( http . StatusBadRequest , "invalid migration uid" , err )
}
result , err := cma . cloudMigrationService . RunMigration ( ctx , id )
result , err := cma . cloudMigrationService . RunMigration ( ctx , u id)
if err != nil {
return response . Error ( http . StatusInternalServerError , "migration run error" , err )
}
@ -186,13 +185,13 @@ func (cma *CloudMigrationAPI) RunMigration(c *contextmodel.ReqContext) response.
// swagger:parameters runCloudMigration
type RunCloudMigrationRequest struct {
// ID of an migration
// U ID of a migration
//
// in: path
ID int64 ` json:"id" `
UID string ` json:"u id" `
}
// swagger:route GET /cloudmigration/migration/{id}/ run/{runID} migrations getCloudMigrationRun
// swagger:route GET /cloudmigration/migration/run/{runU ID} migrations getCloudMigrationRun
//
// Get the result of a single migration run.
//
@ -205,7 +204,12 @@ func (cma *CloudMigrationAPI) GetMigrationRun(c *contextmodel.ReqContext) respon
ctx , span := cma . tracer . Start ( c . Req . Context ( ) , "MigrationAPI.GetMigrationRun" )
defer span . End ( )
migrationStatus , err := cma . cloudMigrationService . GetMigrationStatus ( ctx , web . Params ( c . Req ) [ ":id" ] , web . Params ( c . Req ) [ ":runID" ] )
runUid := web . Params ( c . Req ) [ ":runUID" ]
if err := util . ValidateUID ( runUid ) ; err != nil {
return response . Error ( http . StatusBadRequest , "invalid runUID" , err )
}
migrationStatus , err := cma . cloudMigrationService . GetMigrationStatus ( ctx , runUid )
if err != nil {
return response . Error ( http . StatusInternalServerError , "migration status error" , err )
}
@ -221,18 +225,13 @@ func (cma *CloudMigrationAPI) GetMigrationRun(c *contextmodel.ReqContext) respon
// swagger:parameters getCloudMigrationRun
type GetMigrationRunParams struct {
// ID of an migration
// RunU ID of a migration ru n
//
// in: path
ID int64 ` json:"id" `
// Run ID of a migration run
//
// in: path
RunID int64 ` json:"runID" `
RunUID string ` json:"runUID" `
}
// swagger:route GET /cloudmigration/migration/{id}/run migrations getCloudMigrationRunList
// swagger:route GET /cloudmigration/migration/{uid}/run migrations getCloudMigrationRunList
//
// Get a list of migration runs for a migration.
//
@ -245,7 +244,12 @@ func (cma *CloudMigrationAPI) GetMigrationRunList(c *contextmodel.ReqContext) re
ctx , span := cma . tracer . Start ( c . Req . Context ( ) , "MigrationAPI.GetMigrationRunList" )
defer span . End ( )
runList , err := cma . cloudMigrationService . GetMigrationRunList ( ctx , web . Params ( c . Req ) [ ":id" ] )
uid := web . Params ( c . Req ) [ ":uid" ]
if err := util . ValidateUID ( uid ) ; err != nil {
return response . Error ( http . StatusBadRequest , "invalid migration uid" , err )
}
runList , err := cma . cloudMigrationService . GetMigrationRunList ( ctx , uid )
if err != nil {
return response . Error ( http . StatusInternalServerError , "list migration status error" , err )
}
@ -255,13 +259,13 @@ func (cma *CloudMigrationAPI) GetMigrationRunList(c *contextmodel.ReqContext) re
// swagger:parameters getCloudMigrationRunList
type GetCloudMigrationRunList struct {
// ID of an migration
// U ID of a migration
//
// in: path
ID int64 ` json:"id" `
UID string ` json:"u id" `
}
// swagger:route DELETE /cloudmigration/migration/{id} migrations deleteCloudMigration
// swagger:route DELETE /cloudmigration/migration/{u id} migrations deleteCloudMigration
//
// Delete a migration.
//
@ -274,15 +278,12 @@ func (cma *CloudMigrationAPI) DeleteMigration(c *contextmodel.ReqContext) respon
ctx , span := cma . tracer . Start ( c . Req . Context ( ) , "MigrationAPI.DeleteMigration" )
defer span . End ( )
idStr := web . Params ( c . Req ) [ ":id" ]
if idStr == "" {
return response . Error ( http . StatusBadRequest , "missing migration id" , fmt . Errorf ( "missing migration id" ) )
u id := web . Params ( c . Req ) [ ":u id" ]
if err := util . ValidateUID ( uid ) ; err != nil {
return response . Error ( http . StatusBadRequest , "invalid migration uid" , err )
}
id , err := strconv . ParseInt ( idStr , 10 , 64 )
if err != nil {
return response . Error ( http . StatusBadRequest , "migration id should be numeric" , fmt . Errorf ( "migration id should be numeric" ) )
}
_ , err = cma . cloudMigrationService . DeleteMigration ( ctx , id )
_ , err := cma . cloudMigrationService . DeleteMigration ( ctx , uid )
if err != nil {
return response . Error ( http . StatusInternalServerError , "migration delete error" , err )
}
@ -291,10 +292,10 @@ func (cma *CloudMigrationAPI) DeleteMigration(c *contextmodel.ReqContext) respon
// swagger:parameters deleteCloudMigration
type DeleteMigrationRequest struct {
// ID of an migration
// U ID of a migration
//
// in: path
ID int64 ` json:"id" `
UID string ` json:"u id" `
}
// swagger:response cloudMigrationRunResponse