| 
									
										
										
										
											2024-01-23 00:09:08 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-05-31 20:39:10 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2024-01-23 00:09:08 +08:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/api/routing" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							| 
									
										
										
										
											2024-03-25 23:43:28 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/tracing" | 
					
						
							| 
									
										
										
										
											2024-01-23 00:09:08 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/middleware" | 
					
						
							| 
									
										
										
										
											2024-02-26 21:52:16 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/cloudmigration" | 
					
						
							| 
									
										
										
										
											2024-01-23 00:09:08 +08:00
										 |  |  | 	contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2024-01-23 00:09:08 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | type CloudMigrationAPI struct { | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	cloudMigrationService cloudmigration.Service | 
					
						
							|  |  |  | 	routeRegister         routing.RouteRegister | 
					
						
							|  |  |  | 	log                   log.Logger | 
					
						
							|  |  |  | 	tracer                tracing.Tracer | 
					
						
							| 
									
										
										
										
											2024-01-23 00:09:08 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func RegisterApi( | 
					
						
							|  |  |  | 	rr routing.RouteRegister, | 
					
						
							| 
									
										
										
										
											2024-02-26 21:52:16 +08:00
										 |  |  | 	cms cloudmigration.Service, | 
					
						
							| 
									
										
										
										
											2024-03-25 23:43:28 +08:00
										 |  |  | 	tracer tracing.Tracer, | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | ) *CloudMigrationAPI { | 
					
						
							|  |  |  | 	api := &CloudMigrationAPI{ | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 		log:                   log.New("cloudmigrations.api"), | 
					
						
							|  |  |  | 		routeRegister:         rr, | 
					
						
							|  |  |  | 		cloudMigrationService: cms, | 
					
						
							|  |  |  | 		tracer:                tracer, | 
					
						
							| 
									
										
										
										
											2024-01-23 00:09:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	api.registerEndpoints() | 
					
						
							|  |  |  | 	return api | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | // registerEndpoints Registers Endpoints on Grafana Router
 | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | func (cma *CloudMigrationAPI) registerEndpoints() { | 
					
						
							|  |  |  | 	cma.routeRegister.Group("/api/cloudmigration", func(cloudMigrationRoute routing.RouteRegister) { | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 		// destination instance endpoints for token management
 | 
					
						
							|  |  |  | 		cloudMigrationRoute.Get("/token", routing.Wrap(cma.GetToken)) | 
					
						
							|  |  |  | 		cloudMigrationRoute.Post("/token", routing.Wrap(cma.CreateToken)) | 
					
						
							|  |  |  | 		cloudMigrationRoute.Delete("/token/:uid", routing.Wrap(cma.DeleteToken)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// on-prem instance endpoints for managing GMS sessions
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 		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)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 		// sync approach to data migration
 | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | 		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)) | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 		// async approach to data migration using snapshots
 | 
					
						
							|  |  |  | 		cloudMigrationRoute.Post("/migration/:uid/snapshot", routing.Wrap(cma.CreateSnapshot)) | 
					
						
							|  |  |  | 		cloudMigrationRoute.Get("/migration/:uid/snapshot/:snapshotUid", routing.Wrap(cma.GetSnapshot)) | 
					
						
							|  |  |  | 		cloudMigrationRoute.Get("/migration/:uid/snapshots", routing.Wrap(cma.GetSnapshotList)) | 
					
						
							|  |  |  | 		cloudMigrationRoute.Post("/migration/:uid/snapshot/:snapshotUid/upload", routing.Wrap(cma.UploadSnapshot)) | 
					
						
							|  |  |  | 		cloudMigrationRoute.Post("/migration/:uid/snapshot/:snapshotUid/cancel", routing.Wrap(cma.CancelSnapshot)) | 
					
						
							| 
									
										
										
										
											2024-04-04 03:43:48 +08:00
										 |  |  | 	}, middleware.ReqOrgAdmin) | 
					
						
							| 
									
										
										
										
											2024-01-23 00:09:08 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-31 20:39:10 +08:00
										 |  |  | // swagger:route GET /cloudmigration/token migrations getCloudMigrationToken
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Fetch the cloud migration token if it exists.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: cloudMigrationGetTokenResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 404: notFoundError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							|  |  |  | func (cma *CloudMigrationAPI) GetToken(c *contextmodel.ReqContext) response.Response { | 
					
						
							|  |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.GetToken") | 
					
						
							|  |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	logger := cma.log.FromContext(ctx) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	token, err := cma.cloudMigrationService.GetToken(ctx) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		if !errors.Is(err, cloudmigration.ErrTokenNotFound) { | 
					
						
							|  |  |  | 			logger.Error("fetching cloud migration access token", "err", err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "fetching cloud migration access token", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.JSON(http.StatusOK, GetAccessTokenResponseDTO{ | 
					
						
							|  |  |  | 		ID:          token.ID, | 
					
						
							|  |  |  | 		DisplayName: token.DisplayName, | 
					
						
							|  |  |  | 		ExpiresAt:   token.ExpiresAt, | 
					
						
							|  |  |  | 		FirstUsedAt: token.FirstUsedAt, | 
					
						
							|  |  |  | 		LastUsedAt:  token.LastUsedAt, | 
					
						
							|  |  |  | 		CreatedAt:   token.CreatedAt, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | // swagger:route POST /cloudmigration/token migrations createCloudMigrationToken
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Create gcom access token.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: cloudMigrationCreateTokenResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | func (cma *CloudMigrationAPI) CreateToken(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2024-03-25 23:43:28 +08:00
										 |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.CreateAccessToken") | 
					
						
							|  |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	logger := cma.log.FromContext(ctx) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	resp, err := cma.cloudMigrationService.CreateToken(ctx) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-03-25 23:43:28 +08:00
										 |  |  | 		logger.Error("creating gcom access token", "err", err.Error()) | 
					
						
							| 
									
										
										
										
											2024-05-13 12:22:46 +08:00
										 |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "creating gcom access token", err) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-03-25 23:43:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-13 12:22:19 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, CreateAccessTokenResponseDTO(resp)) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-31 21:03:43 +08:00
										 |  |  | // swagger:route DELETE /cloudmigration/token/{uid} migrations deleteCloudMigrationToken
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Deletes a cloud migration token.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 204: cloudMigrationDeleteTokenResponse
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | // 400: badRequestError
 | 
					
						
							| 
									
										
										
										
											2024-05-31 21:03:43 +08:00
										 |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							|  |  |  | func (cma *CloudMigrationAPI) DeleteToken(c *contextmodel.ReqContext) response.Response { | 
					
						
							|  |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.DeleteToken") | 
					
						
							|  |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	logger := cma.log.FromContext(ctx) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	uid := web.Params(c.Req)[":uid"] | 
					
						
							|  |  |  | 	if err := util.ValidateUID(uid); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "invalid migration uid", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := cma.cloudMigrationService.DeleteToken(ctx, uid); err != nil { | 
					
						
							|  |  |  | 		logger.Error("deleting cloud migration token", "err", err.Error()) | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "deleting cloud migration token", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.Empty(http.StatusNoContent) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | // swagger:route GET /cloudmigration/migration migrations getSessionList
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | // Get a list of all cloud migration sessions that have been created.
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | // 200: cloudMigrationSessionListResponse
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | func (cma *CloudMigrationAPI) GetSessionList(c *contextmodel.ReqContext) response.Response { | 
					
						
							|  |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.GetSessionList") | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	sl, err := cma.cloudMigrationService.GetSessionList(ctx) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "session list error", err) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, convertSessionListToDTO(*sl)) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | // swagger:route GET /cloudmigration/migration/{uid} migrations getSession
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | // Get a cloud migration session by its uid.
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | // 200: cloudMigrationSessionResponse
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | // 400: badRequestError
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | func (cma *CloudMigrationAPI) GetSession(c *contextmodel.ReqContext) response.Response { | 
					
						
							|  |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.GetSession") | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | 	uid := web.Params(c.Req)[":uid"] | 
					
						
							|  |  |  | 	if err := util.ValidateUID(uid); err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 		return response.Error(http.StatusBadRequest, "invalid session uid", err) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	s, err := cma.cloudMigrationService.GetSession(ctx, uid) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 		return response.ErrOrFallback(http.StatusNotFound, "session not found", err) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, CloudMigrationSessionResponseDTO{ | 
					
						
							|  |  |  | 		UID:     s.UID, | 
					
						
							|  |  |  | 		Slug:    s.Slug, | 
					
						
							|  |  |  | 		Created: s.Created, | 
					
						
							|  |  |  | 		Updated: s.Updated, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | // swagger:route POST /cloudmigration/migration migrations createSession
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | // Create a migration session.
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | // 200: cloudMigrationSessionResponse
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | // 400: badRequestError
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | func (cma *CloudMigrationAPI) CreateSession(c *contextmodel.ReqContext) response.Response { | 
					
						
							|  |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.CreateSession") | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	cmd := CloudMigrationSessionRequestDTO{} | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	if err := web.Bind(c.Req, &cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2024-05-13 12:22:46 +08:00
										 |  |  | 		return response.ErrOrFallback(http.StatusBadRequest, "bad request data", err) | 
					
						
							| 
									
										
										
										
											2024-01-23 00:09:08 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	s, err := cma.cloudMigrationService.CreateSession(ctx, cloudmigration.CloudMigrationSessionRequest{ | 
					
						
							|  |  |  | 		AuthToken: cmd.AuthToken, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "session creation error", err) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return response.JSON(http.StatusOK, CloudMigrationSessionResponseDTO{ | 
					
						
							|  |  |  | 		UID:     s.UID, | 
					
						
							|  |  |  | 		Slug:    s.Slug, | 
					
						
							|  |  |  | 		Created: s.Created, | 
					
						
							|  |  |  | 		Updated: s.Updated, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | // swagger:route POST /cloudmigration/migration/{uid}/run migrations runCloudMigration
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Trigger the run of a migration to the Grafana Cloud.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // It returns migrations that has been created.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: cloudMigrationRunResponse
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | // 400: badRequestError
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | func (cma *CloudMigrationAPI) RunMigration(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.RunMigration") | 
					
						
							|  |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | 	uid := web.Params(c.Req)[":uid"] | 
					
						
							|  |  |  | 	if err := util.ValidateUID(uid); err != nil { | 
					
						
							| 
									
										
										
										
											2024-05-13 12:22:46 +08:00
										 |  |  | 		return response.ErrOrFallback(http.StatusBadRequest, "invalid migration uid", err) | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | 	result, err := cma.cloudMigrationService.RunMigration(ctx, uid) | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-05-13 12:22:46 +08:00
										 |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "migration run error", err) | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, convertMigrateDataResponseToDTO(*result)) | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | // swagger:route GET /cloudmigration/migration/run/{runUID} migrations getCloudMigrationRun
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Get the result of a single migration run.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: cloudMigrationRunResponse
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | // 400: badRequestError
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | func (cma *CloudMigrationAPI) GetMigrationRun(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.GetMigrationRun") | 
					
						
							|  |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | 	runUid := web.Params(c.Req)[":runUID"] | 
					
						
							|  |  |  | 	if err := util.ValidateUID(runUid); err != nil { | 
					
						
							| 
									
										
										
										
											2024-05-13 12:22:46 +08:00
										 |  |  | 		return response.ErrOrFallback(http.StatusBadRequest, "invalid runUID", err) | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	migrationStatus, err := cma.cloudMigrationService.GetMigrationStatus(ctx, runUid) | 
					
						
							| 
									
										
										
										
											2024-01-23 00:09:08 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-05-13 12:22:46 +08:00
										 |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "migration status error", err) | 
					
						
							| 
									
										
										
										
											2024-01-23 00:09:08 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-04-04 00:47:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	result, err := migrationStatus.GetResult() | 
					
						
							| 
									
										
										
										
											2024-04-04 00:47:49 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		cma.log.Error("could not return migration run", "err", err) | 
					
						
							|  |  |  | 		return response.Error(http.StatusInternalServerError, "migration run get error", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, convertMigrateDataResponseToDTO(*result)) | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | // swagger:route GET /cloudmigration/migration/{uid}/run migrations getCloudMigrationRunList
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Get a list of migration runs for a migration.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: cloudMigrationRunListResponse
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | // 400: badRequestError
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | func (cma *CloudMigrationAPI) GetMigrationRunList(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.GetMigrationRunList") | 
					
						
							|  |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | 	uid := web.Params(c.Req)[":uid"] | 
					
						
							|  |  |  | 	if err := util.ValidateUID(uid); err != nil { | 
					
						
							| 
									
										
										
										
											2024-05-13 12:22:46 +08:00
										 |  |  | 		return response.ErrOrFallback(http.StatusBadRequest, "invalid migration uid", err) | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	runList, err := cma.cloudMigrationService.GetMigrationRunList(ctx, uid) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-05-13 12:22:46 +08:00
										 |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "list migration status error", err) | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	runs := make([]MigrateDataResponseListDTO, len(runList.Runs)) | 
					
						
							|  |  |  | 	for i := 0; i < len(runList.Runs); i++ { | 
					
						
							|  |  |  | 		runs[i] = MigrateDataResponseListDTO{runList.Runs[i].RunUID} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, CloudMigrationRunListDTO{ | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 		Runs: runs, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | // swagger:route DELETE /cloudmigration/migration/{uid} migrations deleteSession
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | // Delete a migration session by its uid.
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | // 400: badRequestError
 | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | func (cma *CloudMigrationAPI) DeleteSession(c *contextmodel.ReqContext) response.Response { | 
					
						
							|  |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.DeleteSession") | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | 	uid := web.Params(c.Req)[":uid"] | 
					
						
							|  |  |  | 	if err := util.ValidateUID(uid); err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 		return response.ErrOrFallback(http.StatusBadRequest, "invalid session uid", err) | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-05-02 00:29:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	_, err := cma.cloudMigrationService.DeleteSession(ctx, uid) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "session delete error", err) | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return response.Empty(http.StatusOK) | 
					
						
							| 
									
										
										
										
											2024-01-23 00:09:08 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // swagger:route POST /cloudmigration/migration/{uid}/snapshot migrations createSnapshot
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Trigger the creation of an instance snapshot associated with the provided session.
 | 
					
						
							|  |  |  | // If the snapshot initialization is successful, the snapshot uid is returned.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: createSnapshotResponse
 | 
					
						
							|  |  |  | // 400: badRequestError
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							|  |  |  | func (cma *CloudMigrationAPI) CreateSnapshot(c *contextmodel.ReqContext) response.Response { | 
					
						
							|  |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.CreateSnapshot") | 
					
						
							|  |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	uid := web.Params(c.Req)[":uid"] | 
					
						
							| 
									
										
										
										
											2024-07-03 21:38:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 	if err := util.ValidateUID(uid); err != nil { | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusBadRequest, "invalid session uid", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-03 21:38:26 +08:00
										 |  |  | 	ss, err := cma.cloudMigrationService.CreateSnapshot(ctx, c.SignedInUser, uid) | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "error creating snapshot", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.JSON(http.StatusOK, CreateSnapshotResponseDTO{ | 
					
						
							|  |  |  | 		SnapshotUID: ss.UID, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:route GET /cloudmigration/migration/{uid}/snapshot/{snapshotUid} migrations getSnapshot
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Get metadata about a snapshot, including where it is in its processing and final results.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: getSnapshotResponse
 | 
					
						
							|  |  |  | // 400: badRequestError
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							|  |  |  | func (cma *CloudMigrationAPI) GetSnapshot(c *contextmodel.ReqContext) response.Response { | 
					
						
							|  |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.GetSnapshot") | 
					
						
							|  |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sessUid, snapshotUid := web.Params(c.Req)[":uid"], web.Params(c.Req)[":snapshotUid"] | 
					
						
							|  |  |  | 	if err := util.ValidateUID(sessUid); err != nil { | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusBadRequest, "invalid session uid", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err := util.ValidateUID(snapshotUid); err != nil { | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusBadRequest, "invalid snapshot uid", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | 	q := cloudmigration.GetSnapshotsQuery{ | 
					
						
							|  |  |  | 		SnapshotUID: snapshotUid, | 
					
						
							|  |  |  | 		SessionUID:  sessUid, | 
					
						
							|  |  |  | 		ResultPage:  c.QueryInt("resultPage"), | 
					
						
							|  |  |  | 		ResultLimit: c.QueryInt("resultLimit"), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if q.ResultLimit == 0 { | 
					
						
							|  |  |  | 		q.ResultLimit = 100 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if q.ResultPage < 1 { | 
					
						
							|  |  |  | 		q.ResultPage = 1 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	snapshot, err := cma.cloudMigrationService.GetSnapshot(ctx, q) | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "error retrieving snapshot", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | 	results := snapshot.Resources | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | 	dtoResults := make([]MigrateDataResponseItemDTO, len(results)) | 
					
						
							|  |  |  | 	for i := 0; i < len(results); i++ { | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 		dtoResults[i] = MigrateDataResponseItemDTO{ | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | 			Type:   MigrateDataType(results[i].Type), | 
					
						
							|  |  |  | 			RefID:  results[i].RefID, | 
					
						
							|  |  |  | 			Status: ItemStatus(results[i].Status), | 
					
						
							|  |  |  | 			Error:  results[i].Error, | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-10 20:46:38 +08:00
										 |  |  | 	dtoStats := SnapshotResourceStats{ | 
					
						
							|  |  |  | 		Types:    make(map[MigrateDataType]int, len(snapshot.StatsRollup.CountsByStatus)), | 
					
						
							|  |  |  | 		Statuses: make(map[ItemStatus]int, len(snapshot.StatsRollup.CountsByType)), | 
					
						
							| 
									
										
										
										
											2024-07-25 11:34:25 +08:00
										 |  |  | 		Total:    snapshot.StatsRollup.Total, | 
					
						
							| 
									
										
										
										
											2024-07-10 20:46:38 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	for s, c := range snapshot.StatsRollup.CountsByStatus { | 
					
						
							|  |  |  | 		dtoStats.Statuses[ItemStatus(s)] = c | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for s, c := range snapshot.StatsRollup.CountsByType { | 
					
						
							|  |  |  | 		dtoStats.Types[MigrateDataType(s)] = c | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 	respDto := GetSnapshotResponseDTO{ | 
					
						
							|  |  |  | 		SnapshotDTO: SnapshotDTO{ | 
					
						
							|  |  |  | 			SnapshotUID: snapshot.UID, | 
					
						
							|  |  |  | 			Status:      fromSnapshotStatus(snapshot.Status), | 
					
						
							|  |  |  | 			SessionUID:  sessUid, | 
					
						
							|  |  |  | 			Created:     snapshot.Created, | 
					
						
							|  |  |  | 			Finished:    snapshot.Finished, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2024-07-10 20:46:38 +08:00
										 |  |  | 		Results:     dtoResults, | 
					
						
							|  |  |  | 		StatsRollup: dtoStats, | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.JSON(http.StatusOK, respDto) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:route GET /cloudmigration/migration/{uid}/snapshots migrations getShapshotList
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Get a list of snapshots for a session.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: snapshotListResponse
 | 
					
						
							|  |  |  | // 400: badRequestError
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							|  |  |  | func (cma *CloudMigrationAPI) GetSnapshotList(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2024-07-31 21:24:40 +08:00
										 |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.GetSnapshotList") | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	uid := web.Params(c.Req)[":uid"] | 
					
						
							|  |  |  | 	if err := util.ValidateUID(uid); err != nil { | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusBadRequest, "invalid session uid", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	q := cloudmigration.ListSnapshotsQuery{ | 
					
						
							|  |  |  | 		SessionUID: uid, | 
					
						
							|  |  |  | 		Limit:      c.QueryInt("limit"), | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | 		Page:       c.QueryInt("page"), | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if q.Limit == 0 { | 
					
						
							|  |  |  | 		q.Limit = 100 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | 	if q.Page < 1 { | 
					
						
							|  |  |  | 		q.Page = 1 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	snapshotList, err := cma.cloudMigrationService.GetSnapshotList(ctx, q) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "error retrieving snapshot list", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dtos := make([]SnapshotDTO, len(snapshotList)) | 
					
						
							|  |  |  | 	for i := 0; i < len(snapshotList); i++ { | 
					
						
							|  |  |  | 		dtos[i] = SnapshotDTO{ | 
					
						
							|  |  |  | 			SnapshotUID: snapshotList[i].UID, | 
					
						
							|  |  |  | 			Status:      fromSnapshotStatus(snapshotList[i].Status), | 
					
						
							|  |  |  | 			SessionUID:  uid, | 
					
						
							|  |  |  | 			Created:     snapshotList[i].Created, | 
					
						
							|  |  |  | 			Finished:    snapshotList[i].Finished, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.JSON(http.StatusOK, SnapshotListResponseDTO{ | 
					
						
							|  |  |  | 		Snapshots: dtos, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:route POST /cloudmigration/migration/{uid}/snapshot/{snapshotUid}/upload migrations uploadSnapshot
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Upload a snapshot to the Grafana Migration Service for processing.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200:
 | 
					
						
							|  |  |  | // 400: badRequestError
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							|  |  |  | func (cma *CloudMigrationAPI) UploadSnapshot(c *contextmodel.ReqContext) response.Response { | 
					
						
							|  |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.UploadSnapshot") | 
					
						
							|  |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sessUid, snapshotUid := web.Params(c.Req)[":uid"], web.Params(c.Req)[":snapshotUid"] | 
					
						
							|  |  |  | 	if err := util.ValidateUID(sessUid); err != nil { | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusBadRequest, "invalid session uid", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err := util.ValidateUID(snapshotUid); err != nil { | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusBadRequest, "invalid snapshot uid", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := cma.cloudMigrationService.UploadSnapshot(ctx, sessUid, snapshotUid); err != nil { | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "error uploading snapshot", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.JSON(http.StatusOK, nil) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:route POST /cloudmigration/migration/{uid}/snapshot/{snapshotUid}/cancel migrations cancelSnapshot
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Cancel a snapshot, wherever it is in its processing chain.
 | 
					
						
							|  |  |  | // TODO: Implement
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200:
 | 
					
						
							|  |  |  | // 400: badRequestError
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							|  |  |  | func (cma *CloudMigrationAPI) CancelSnapshot(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2024-06-21 21:35:15 +08:00
										 |  |  | 	ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.CancelSnapshot") | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 	defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sessUid, snapshotUid := web.Params(c.Req)[":uid"], web.Params(c.Req)[":snapshotUid"] | 
					
						
							|  |  |  | 	if err := util.ValidateUID(sessUid); err != nil { | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusBadRequest, "invalid session uid", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err := util.ValidateUID(snapshotUid); err != nil { | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusBadRequest, "invalid snapshot uid", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-21 21:35:15 +08:00
										 |  |  | 	if err := cma.cloudMigrationService.CancelSnapshot(ctx, sessUid, snapshotUid); err != nil { | 
					
						
							|  |  |  | 		return response.ErrOrFallback(http.StatusInternalServerError, "error canceling snapshot", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return response.JSON(http.StatusOK, nil) | 
					
						
							|  |  |  | } |