CloudMigrations: Refactor API for async work (#89084)
* rename some stuff * more renaming * clean up api * rename more functions * rename cms -> gms * update comment * update swagger gen * update endpoints * overzealous * final touches * dont modify existing migrations * break structs into domain and dtos * add some conversion funcs * fix build * update frontend * try to make swagger happy
This commit is contained in:
@@ -40,14 +40,16 @@ func RegisterApi(
|
||||
// registerEndpoints 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/:uid", routing.Wrap(cma.GetMigration))
|
||||
cloudMigrationRoute.Delete("/migration/:uid", routing.Wrap(cma.DeleteMigration))
|
||||
cloudMigrationRoute.Get("/migration", routing.Wrap(cma.GetSessionList))
|
||||
cloudMigrationRoute.Post("/migration", routing.Wrap(cma.CreateSession))
|
||||
cloudMigrationRoute.Get("/migration/:uid", routing.Wrap(cma.GetSession))
|
||||
cloudMigrationRoute.Delete("/migration/:uid", routing.Wrap(cma.DeleteSession))
|
||||
|
||||
// TODO new APIs for snapshot management to replace these
|
||||
cloudMigrationRoute.Post("/migration/:uid/run", routing.Wrap(cma.RunMigration))
|
||||
cloudMigrationRoute.Get("/migration/:uid/run", routing.Wrap(cma.GetMigrationRunList))
|
||||
cloudMigrationRoute.Get("/migration/run/:runUID", routing.Wrap(cma.GetMigrationRun))
|
||||
|
||||
cloudMigrationRoute.Get("/token", routing.Wrap(cma.GetToken))
|
||||
cloudMigrationRoute.Post("/token", routing.Wrap(cma.CreateToken))
|
||||
cloudMigrationRoute.Delete("/token/:uid", routing.Wrap(cma.DeleteToken))
|
||||
@@ -141,84 +143,88 @@ func (cma *CloudMigrationAPI) DeleteToken(c *contextmodel.ReqContext) response.R
|
||||
return response.Empty(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// swagger:route GET /cloudmigration/migration migrations getMigrationList
|
||||
// swagger:route GET /cloudmigration/migration migrations getSessionList
|
||||
//
|
||||
// Get a list of all cloud migrations.
|
||||
// Get a list of all cloud migration sessions that have been created.
|
||||
//
|
||||
// Responses:
|
||||
// 200: cloudMigrationListResponse
|
||||
// 200: cloudMigrationSessionListResponse
|
||||
// 401: unauthorisedError
|
||||
// 403: forbiddenError
|
||||
// 500: internalServerError
|
||||
func (cma *CloudMigrationAPI) GetMigrationList(c *contextmodel.ReqContext) response.Response {
|
||||
ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.GetMigrationList")
|
||||
func (cma *CloudMigrationAPI) GetSessionList(c *contextmodel.ReqContext) response.Response {
|
||||
ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.GetSessionList")
|
||||
defer span.End()
|
||||
|
||||
cloudMigrations, err := cma.cloudMigrationService.GetMigrationList(ctx)
|
||||
sl, err := cma.cloudMigrationService.GetSessionList(ctx)
|
||||
if err != nil {
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "migration list error", err)
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "session list error", err)
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, cloudMigrations)
|
||||
return response.JSON(http.StatusOK, convertSessionListToDTO(*sl))
|
||||
}
|
||||
|
||||
// swagger:route GET /cloudmigration/migration/{uid} migrations getCloudMigration
|
||||
// swagger:route GET /cloudmigration/migration/{uid} migrations getSession
|
||||
//
|
||||
// Get a cloud migration.
|
||||
//
|
||||
// It returns migrations that has been created.
|
||||
// Get a cloud migration session by its uid.
|
||||
//
|
||||
// Responses:
|
||||
// 200: cloudMigrationResponse
|
||||
// 200: cloudMigrationSessionResponse
|
||||
// 401: unauthorisedError
|
||||
// 403: forbiddenError
|
||||
// 500: internalServerError
|
||||
func (cma *CloudMigrationAPI) GetMigration(c *contextmodel.ReqContext) response.Response {
|
||||
ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.GetMigration")
|
||||
func (cma *CloudMigrationAPI) GetSession(c *contextmodel.ReqContext) response.Response {
|
||||
ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.GetSession")
|
||||
defer span.End()
|
||||
|
||||
uid := web.Params(c.Req)[":uid"]
|
||||
if err := util.ValidateUID(uid); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "invalid migration uid", err)
|
||||
return response.Error(http.StatusBadRequest, "invalid session uid", err)
|
||||
}
|
||||
|
||||
cloudMigration, err := cma.cloudMigrationService.GetMigration(ctx, uid)
|
||||
s, err := cma.cloudMigrationService.GetSession(ctx, uid)
|
||||
if err != nil {
|
||||
return response.ErrOrFallback(http.StatusNotFound, "migration not found", err)
|
||||
return response.ErrOrFallback(http.StatusNotFound, "session not found", err)
|
||||
}
|
||||
return response.JSON(http.StatusOK, cloudMigration)
|
||||
|
||||
return response.JSON(http.StatusOK, CloudMigrationSessionResponseDTO{
|
||||
UID: s.UID,
|
||||
Slug: s.Slug,
|
||||
Created: s.Created,
|
||||
Updated: s.Updated,
|
||||
})
|
||||
}
|
||||
|
||||
// swagger:parameters getCloudMigration
|
||||
type GetCloudMigrationRequest struct {
|
||||
// UID of a migration
|
||||
//
|
||||
// in: path
|
||||
UID string `json:"uid"`
|
||||
}
|
||||
|
||||
// swagger:route POST /cloudmigration/migration migrations createMigration
|
||||
// swagger:route POST /cloudmigration/migration migrations createSession
|
||||
//
|
||||
// Create a migration.
|
||||
// Create a migration session.
|
||||
//
|
||||
// Responses:
|
||||
// 200: cloudMigrationResponse
|
||||
// 200: cloudMigrationSessionResponse
|
||||
// 401: unauthorisedError
|
||||
// 403: forbiddenError
|
||||
// 500: internalServerError
|
||||
func (cma *CloudMigrationAPI) CreateMigration(c *contextmodel.ReqContext) response.Response {
|
||||
ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.CreateMigration")
|
||||
func (cma *CloudMigrationAPI) CreateSession(c *contextmodel.ReqContext) response.Response {
|
||||
ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.CreateSession")
|
||||
defer span.End()
|
||||
|
||||
cmd := cloudmigration.CloudMigrationRequest{}
|
||||
cmd := CloudMigrationSessionRequestDTO{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.ErrOrFallback(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
cloudMigration, err := cma.cloudMigrationService.CreateMigration(ctx, cmd)
|
||||
s, err := cma.cloudMigrationService.CreateSession(ctx, cloudmigration.CloudMigrationSessionRequest{
|
||||
AuthToken: cmd.AuthToken,
|
||||
})
|
||||
if err != nil {
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "migration creation error", err)
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "session creation error", err)
|
||||
}
|
||||
return response.JSON(http.StatusOK, cloudMigration)
|
||||
|
||||
return response.JSON(http.StatusOK, CloudMigrationSessionResponseDTO{
|
||||
UID: s.UID,
|
||||
Slug: s.Slug,
|
||||
Created: s.Created,
|
||||
Updated: s.Updated,
|
||||
})
|
||||
}
|
||||
|
||||
// swagger:route POST /cloudmigration/migration/{uid}/run migrations runCloudMigration
|
||||
@@ -246,15 +252,7 @@ func (cma *CloudMigrationAPI) RunMigration(c *contextmodel.ReqContext) response.
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "migration run error", err)
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
// swagger:parameters runCloudMigration
|
||||
type RunCloudMigrationRequest struct {
|
||||
// UID of a migration
|
||||
//
|
||||
// in: path
|
||||
UID string `json:"uid"`
|
||||
return response.JSON(http.StatusOK, convertMigrateDataResponseToDTO(*result))
|
||||
}
|
||||
|
||||
// swagger:route GET /cloudmigration/migration/run/{runUID} migrations getCloudMigrationRun
|
||||
@@ -280,21 +278,13 @@ func (cma *CloudMigrationAPI) GetMigrationRun(c *contextmodel.ReqContext) respon
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "migration status error", err)
|
||||
}
|
||||
|
||||
runResponse, err := migrationStatus.ToResponse()
|
||||
result, err := migrationStatus.GetResult()
|
||||
if err != nil {
|
||||
cma.log.Error("could not return migration run", "err", err)
|
||||
return response.Error(http.StatusInternalServerError, "migration run get error", err)
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, runResponse)
|
||||
}
|
||||
|
||||
// swagger:parameters getCloudMigrationRun
|
||||
type GetMigrationRunParams struct {
|
||||
// RunUID of a migration run
|
||||
//
|
||||
// in: path
|
||||
RunUID string `json:"runUID"`
|
||||
return response.JSON(http.StatusOK, convertMigrateDataResponseToDTO(*result))
|
||||
}
|
||||
|
||||
// swagger:route GET /cloudmigration/migration/{uid}/run migrations getCloudMigrationRunList
|
||||
@@ -320,118 +310,36 @@ func (cma *CloudMigrationAPI) GetMigrationRunList(c *contextmodel.ReqContext) re
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "list migration status error", err)
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, runList)
|
||||
runs := make([]MigrateDataResponseListDTO, len(runList.Runs))
|
||||
for i := 0; i < len(runList.Runs); i++ {
|
||||
runs[i] = MigrateDataResponseListDTO{runList.Runs[i].RunUID}
|
||||
}
|
||||
return response.JSON(http.StatusOK, SnapshotListDTO{
|
||||
Runs: runs,
|
||||
})
|
||||
}
|
||||
|
||||
// swagger:parameters getCloudMigrationRunList
|
||||
type GetCloudMigrationRunList struct {
|
||||
// UID of a migration
|
||||
//
|
||||
// in: path
|
||||
UID string `json:"uid"`
|
||||
}
|
||||
|
||||
// swagger:route DELETE /cloudmigration/migration/{uid} migrations deleteCloudMigration
|
||||
// swagger:route DELETE /cloudmigration/migration/{uid} migrations deleteSession
|
||||
//
|
||||
// Delete a migration.
|
||||
// Delete a migration session by its uid.
|
||||
//
|
||||
// Responses:
|
||||
// 200
|
||||
// 401: unauthorisedError
|
||||
// 403: forbiddenError
|
||||
// 500: internalServerError
|
||||
func (cma *CloudMigrationAPI) DeleteMigration(c *contextmodel.ReqContext) response.Response {
|
||||
ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.DeleteMigration")
|
||||
func (cma *CloudMigrationAPI) DeleteSession(c *contextmodel.ReqContext) response.Response {
|
||||
ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.DeleteSession")
|
||||
defer span.End()
|
||||
|
||||
uid := web.Params(c.Req)[":uid"]
|
||||
if err := util.ValidateUID(uid); err != nil {
|
||||
return response.ErrOrFallback(http.StatusBadRequest, "invalid migration uid", err)
|
||||
return response.ErrOrFallback(http.StatusBadRequest, "invalid session uid", err)
|
||||
}
|
||||
|
||||
_, err := cma.cloudMigrationService.DeleteMigration(ctx, uid)
|
||||
_, err := cma.cloudMigrationService.DeleteSession(ctx, uid)
|
||||
if err != nil {
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "migration delete error", err)
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "session delete error", err)
|
||||
}
|
||||
return response.Empty(http.StatusOK)
|
||||
}
|
||||
|
||||
// swagger:parameters deleteCloudMigration
|
||||
type DeleteMigrationRequest struct {
|
||||
// UID of a migration
|
||||
//
|
||||
// in: path
|
||||
UID string `json:"uid"`
|
||||
}
|
||||
|
||||
// swagger:response cloudMigrationRunResponse
|
||||
type CloudMigrationRunResponse struct {
|
||||
// in: body
|
||||
Body cloudmigration.MigrateDataResponseDTO
|
||||
}
|
||||
|
||||
// swagger:response cloudMigrationListResponse
|
||||
type CloudMigrationListResponse struct {
|
||||
// in: body
|
||||
Body cloudmigration.CloudMigrationListResponse
|
||||
}
|
||||
|
||||
// swagger:parameters createMigration
|
||||
type CreateMigration struct {
|
||||
// in:body
|
||||
// required:true
|
||||
Body cloudmigration.CloudMigrationRequest
|
||||
}
|
||||
|
||||
// swagger:response cloudMigrationResponse
|
||||
type CloudMigrationResponse struct {
|
||||
// in: body
|
||||
Body cloudmigration.CloudMigrationResponse
|
||||
}
|
||||
|
||||
// swagger:response cloudMigrationRunListResponse
|
||||
type CloudMigrationRunListResponse struct {
|
||||
// in: body
|
||||
Body cloudmigration.CloudMigrationRunList
|
||||
}
|
||||
|
||||
// swagger:parameters getCloudMigrationToken
|
||||
type GetCloudMigrationToken struct {
|
||||
}
|
||||
|
||||
// swagger:response cloudMigrationGetTokenResponse
|
||||
type CloudMigrationGetTokenResponse struct {
|
||||
// in: body
|
||||
Body GetAccessTokenResponseDTO
|
||||
}
|
||||
|
||||
type GetAccessTokenResponseDTO struct {
|
||||
ID string `json:"id"`
|
||||
DisplayName string `json:"displayName"`
|
||||
ExpiresAt string `json:"expiresAt"`
|
||||
FirstUsedAt string `json:"firstUsedAt"`
|
||||
LastUsedAt string `json:"lastUsedAt"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
}
|
||||
|
||||
// swagger:response cloudMigrationCreateTokenResponse
|
||||
type CloudMigrationCreateTokenResponse struct {
|
||||
// in: body
|
||||
Body CreateAccessTokenResponseDTO
|
||||
}
|
||||
|
||||
type CreateAccessTokenResponseDTO struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// swagger:parameters deleteCloudMigrationToken
|
||||
type DeleteCloudMigrationToken struct {
|
||||
// UID of a cloud migration token
|
||||
//
|
||||
// in: path
|
||||
UID string `json:"uid"`
|
||||
}
|
||||
|
||||
// swagger:response cloudMigrationDeleteTokenResponse
|
||||
type CloudMigrationDeleteTokenResponse struct {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user