Cloud Migration: Remove ID migration in favor of UID (#86550)

* - Added migration to add a new collumn UID to both migration and migration_run tables.
- Added migration to set UID for records already existent in the database before adding a new column.
- Added UID field to CloudMigration and CloudMigrationRun dtos (model.go)

* Fix db migration

* Updating store and model

* Updating API to use UID

* fix typo

* update openapi specs and generated endpoints

* fix spec

* update openapi specs and generated endpoints

* Fixing db mapping

* Fix frontend

* fix migration

* remove migration uid from run fetch endpoint

* Revert "merge"

This reverts commit d654e4c530, reversing
changes made to 5fe0b483eb.

* manual merge

* rename some funcs for consistency

* make interfaces consistent

* validate uids

* update generated frontend api

* fix enterprise spec

* manually resolve api

* try again

* try yet again

* once more

---------

Co-authored-by: Josh Hunt <joshhunt@users.noreply.github.com>
Co-authored-by: Michael Mandrus <michael.mandrus@grafana.com>
Co-authored-by: joshhunt <josh@trtr.co>
This commit is contained in:
lean.dev
2024-05-01 13:29:25 -03:00
committed by GitHub
parent fcb40e601d
commit 0719f73f35
13 changed files with 316 additions and 315 deletions
+48 -47
View File
@@ -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
}
// RegisterAPIEndpoints Registers Endpoints on Grafana Router
// 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/: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/:uid", routing.Wrap(cma.GetMigration))
cloudMigrationRoute.Delete("/migration/:uid", routing.Wrap(cma.DeleteMigration))
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.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/{uid} 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 invalid", err)
uid := web.Params(c.Req)[":uid"]
if err := util.ValidateUID(uid); err != nil {
return response.Error(http.StatusBadRequest, "invalid migration uid", 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
// UID of a migration
//
// in: path
ID int64 `json:"id"`
UID string `json:"uid"`
}
// 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/{uid}/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, uid)
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
// UID of a migration
//
// in: path
ID int64 `json:"id"`
UID string `json:"uid"`
}
// swagger:route GET /cloudmigration/migration/{id}/run/{runID} migrations getCloudMigrationRun
// swagger:route GET /cloudmigration/migration/run/{runUID} 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
// RunUID of a migration run
//
// 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
// UID of a migration
//
// in: path
ID int64 `json:"id"`
UID string `json:"uid"`
}
// swagger:route DELETE /cloudmigration/migration/{id} migrations deleteCloudMigration
// swagger:route DELETE /cloudmigration/migration/{uid} 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"))
uid := web.Params(c.Req)[":uid"]
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
// UID of a migration
//
// in: path
ID int64 `json:"id"`
UID string `json:"uid"`
}
// swagger:response cloudMigrationRunResponse