@ -2,6 +2,7 @@ package api
import (
"net/http"
"strconv"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/api/routing"
@ -12,7 +13,7 @@ import (
"github.com/grafana/grafana/pkg/web"
)
type MigrationAPI struct {
type Cloud MigrationAPI struct {
cloudMigrationsService cloudmigration . Service
routeRegister routing . RouteRegister
log log . Logger
@ -21,8 +22,8 @@ type MigrationAPI struct {
func RegisterApi (
rr routing . RouteRegister ,
cms cloudmigration . Service ,
) * MigrationAPI {
api := & MigrationAPI {
) * Cloud MigrationAPI {
api := & Cloud MigrationAPI{
log : log . New ( "cloudmigrations.api" ) ,
routeRegister : rr ,
cloudMigrationsService : cms ,
@ -32,25 +33,87 @@ func RegisterApi(
}
// RegisterAPIEndpoints Registers Endpoints on Grafana Router
func ( api * MigrationAPI ) registerEndpoints ( ) {
api . routeRegister . Group ( "/api/cloudmigrations" , func ( apiRoute routing . RouteRegister ) {
apiRoute . Post (
"/migrate_datasources" ,
routing . Wrap ( api . MigrateDatasources ) ,
)
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 ) )
} , middleware . ReqGrafanaAdmin )
}
func ( api * MigrationAPI ) MigrateDatasources ( c * contextmodel . ReqContext ) response . Response {
var req cloudmigration . MigrateDatasourcesRequestDTO
if err := web . Bind ( c . Req , & req ) ; err != nil {
func ( cma * CloudMigrationAPI ) CreateToken ( c * contextmodel . ReqContext ) response . Response {
err := cma . cloudMigrationsService . CreateToken ( c . Req . Context ( ) )
if err != nil {
return response . Error ( http . StatusInternalServerError , "token creation error" , err )
}
return response . Success ( "Token created" )
}
func ( cma * CloudMigrationAPI ) GetMigrationList ( c * contextmodel . ReqContext ) response . Response {
cloudMigrations , err := cma . cloudMigrationsService . GetMigrationList ( c . Req . Context ( ) )
if err != nil {
return response . Error ( http . StatusInternalServerError , "migration list error" , err )
}
return response . JSON ( http . StatusOK , cloudMigrations )
}
func ( cma * CloudMigrationAPI ) GetMigration ( c * contextmodel . ReqContext ) response . Response {
id , err := strconv . ParseInt ( web . Params ( c . Req ) [ ":id" ] , 10 , 64 )
if err != nil {
return response . Error ( http . StatusBadRequest , "id is invalid" , err )
}
cloudMigration , err := cma . cloudMigrationsService . GetMigration ( c . Req . Context ( ) , id )
if err != nil {
return response . Error ( http . StatusNotFound , "migration not found" , err )
}
return response . JSON ( http . StatusOK , cloudMigration )
}
func ( cma * CloudMigrationAPI ) CreateMigration ( c * contextmodel . ReqContext ) response . Response {
cmd := cloudmigration . CloudMigrationRequest { }
if err := web . Bind ( c . Req , & cmd ) ; err != nil {
return response . Error ( http . StatusBadRequest , "bad request data" , err )
}
cloudMigration , err := cma . cloudMigrationsService . CreateMigration ( c . Req . Context ( ) , cmd )
if err != nil {
return response . Error ( http . StatusInternalServerError , "migration creation error" , err )
}
return response . JSON ( http . StatusOK , cloudMigration )
}
func ( cma * CloudMigrationAPI ) RunMigration ( c * contextmodel . ReqContext ) response . Response {
cloudMigrationRun , err := cma . cloudMigrationsService . RunMigration ( c . Req . Context ( ) , web . Params ( c . Req ) [ ":id" ] )
if err != nil {
return response . Error ( http . StatusInternalServerError , "migration run error" , err )
}
return response . JSON ( http . StatusOK , cloudMigrationRun )
}
resp , err := api . cloudMigrationsService . MigrateDatasources ( c . Req . Context ( ) , & cloudmigration . MigrateDatasourcesRequest { MigrateToPDC : req . MigrateToPDC , MigrateCredentials : req . MigrateCredentials } )
func ( cma * CloudMigrationAPI ) GetMigrationRun ( c * contextmodel . ReqContext ) response . Response {
migrationStatus , err := cma . cloudMigrationsService . GetMigrationStatus ( c . Req . Context ( ) , web . Params ( c . Req ) [ ":id" ] , web . Params ( c . Req ) [ ":runID" ] )
if err != nil {
return response . Error ( http . StatusInternalServerError , "data source migrations error" , err )
return response . Error ( http . StatusInternalServerError , "migration statu s error" , err )
}
return response . JSON ( http . StatusOK , migrationStatus )
}
return response . JSON ( http . StatusOK , cloudmigration . MigrateDatasourcesResponseDTO { DatasourcesMigrated : resp . DatasourcesMigrated } )
func ( cma * CloudMigrationAPI ) GetMigrationRunList ( c * contextmodel . ReqContext ) response . Response {
migrationStatus , err := cma . cloudMigrationsService . GetMigrationStatusList ( c . Req . Context ( ) , web . Params ( c . Req ) [ ":id" ] )
if err != nil {
return response . Error ( http . StatusInternalServerError , "migration status error" , err )
}
return response . JSON ( http . StatusOK , migrationStatus )
}
func ( cma * CloudMigrationAPI ) DeleteMigration ( c * contextmodel . ReqContext ) response . Response {
err := cma . cloudMigrationsService . DeleteMigration ( c . Req . Context ( ) , web . Params ( c . Req ) [ ":id" ] )
if err != nil {
return response . Error ( http . StatusInternalServerError , "migration delete error" , err )
}
return response . Empty ( http . StatusOK )
}