mirror of
https://github.com/grafana/grafana.git
synced 2025-08-03 02:02:12 +08:00
Grafana: define the api for the grafana cloudmigration api (not the csm api) (#84430)
* Add cloud migration endpoints * Built auth into creating a migration. * Added more detail to the migration result model * goimports * Update pkg/services/cloudmigration/api/api.go Co-authored-by: lean.dev <34773040+leandro-deveikis@users.noreply.github.com> * Update pkg/services/cloudmigration/api/api.go Co-authored-by: lean.dev <34773040+leandro-deveikis@users.noreply.github.com> --------- Co-authored-by: Leonard Gram <leo@xlson.com> Co-authored-by: lean.dev <34773040+leandro-deveikis@users.noreply.github.com>
This commit is contained in:
@ -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 CloudMigrationAPI 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{
|
||||
) *CloudMigrationAPI {
|
||||
api := &CloudMigrationAPI{
|
||||
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)
|
||||
}
|
||||
|
||||
resp, err := api.cloudMigrationsService.MigrateDatasources(c.Req.Context(), &cloudmigration.MigrateDatasourcesRequest{MigrateToPDC: req.MigrateToPDC, MigrateCredentials: req.MigrateCredentials})
|
||||
cloudMigration, err := cma.cloudMigrationsService.CreateMigration(c.Req.Context(), cmd)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "data source migrations error", err)
|
||||
return response.Error(http.StatusInternalServerError, "migration creation error", err)
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, cloudmigration.MigrateDatasourcesResponseDTO{DatasourcesMigrated: resp.DatasourcesMigrated})
|
||||
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)
|
||||
}
|
||||
|
||||
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, "migration status error", err)
|
||||
}
|
||||
return response.JSON(http.StatusOK, migrationStatus)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user