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:
@@ -16,7 +16,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||
"github.com/grafana/grafana/pkg/services/cloudmigration"
|
||||
"github.com/grafana/grafana/pkg/services/cloudmigration/api"
|
||||
"github.com/grafana/grafana/pkg/services/cloudmigration/cmsclient"
|
||||
"github.com/grafana/grafana/pkg/services/cloudmigration/gmsclient"
|
||||
"github.com/grafana/grafana/pkg/services/contexthandler"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
@@ -38,7 +38,7 @@ type Service struct {
|
||||
cfg *setting.Cfg
|
||||
|
||||
features featuremgmt.FeatureToggles
|
||||
cmsClient cmsclient.Client
|
||||
gmsClient gmsclient.Client
|
||||
|
||||
dsService datasources.DataSourceService
|
||||
gcomService gcom.Service
|
||||
@@ -95,16 +95,16 @@ func ProvideService(
|
||||
s.api = api.RegisterApi(routeRegister, s, tracer)
|
||||
|
||||
if !cfg.CloudMigration.IsDeveloperMode {
|
||||
// get CMS path from the config
|
||||
// get GMS path from the config
|
||||
domain, err := s.parseCloudMigrationConfig()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("config parse error: %w", err)
|
||||
}
|
||||
s.cmsClient = cmsclient.NewCMSClient(domain)
|
||||
s.gmsClient = gmsclient.NewGMSClient(domain)
|
||||
|
||||
s.gcomService = gcom.New(gcom.Config{ApiURL: cfg.GrafanaComAPIURL, Token: cfg.CloudMigration.GcomAPIToken})
|
||||
} else {
|
||||
s.cmsClient = cmsclient.NewInMemoryClient()
|
||||
s.gmsClient = gmsclient.NewInMemoryClient()
|
||||
s.gcomService = &gcomStub{policies: map[string]gcom.AccessPolicy{}, token: nil}
|
||||
s.cfg.StackID = "12345"
|
||||
}
|
||||
@@ -242,7 +242,7 @@ func (s *Service) CreateToken(ctx context.Context) (cloudmigration.CreateAccessT
|
||||
Instance: cloudmigration.Base64HGInstance{
|
||||
StackID: instance.ID,
|
||||
RegionSlug: instance.RegionSlug,
|
||||
ClusterSlug: instance.ClusterSlug, // This should be used for routing to CMS
|
||||
ClusterSlug: instance.ClusterSlug, // This should be used for routing to GMS
|
||||
Slug: instance.Slug,
|
||||
},
|
||||
})
|
||||
@@ -275,11 +275,11 @@ func (s *Service) findAccessPolicyByName(ctx context.Context, regionSlug, access
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *Service) ValidateToken(ctx context.Context, cm cloudmigration.CloudMigration) error {
|
||||
func (s *Service) ValidateToken(ctx context.Context, cm cloudmigration.CloudMigrationSession) error {
|
||||
ctx, span := s.tracer.Start(ctx, "CloudMigrationService.ValidateToken")
|
||||
defer span.End()
|
||||
|
||||
if err := s.cmsClient.ValidateKey(ctx, cm); err != nil {
|
||||
if err := s.gmsClient.ValidateKey(ctx, cm); err != nil {
|
||||
return fmt.Errorf("validating key: %w", err)
|
||||
}
|
||||
|
||||
@@ -315,10 +315,10 @@ func (s *Service) DeleteToken(ctx context.Context, tokenID string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) GetMigration(ctx context.Context, uid string) (*cloudmigration.CloudMigration, error) {
|
||||
func (s *Service) GetSession(ctx context.Context, uid string) (*cloudmigration.CloudMigrationSession, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "CloudMigrationService.GetMigration")
|
||||
defer span.End()
|
||||
migration, err := s.store.GetMigrationByUID(ctx, uid)
|
||||
migration, err := s.store.GetMigrationSessionByUID(ctx, uid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -326,25 +326,25 @@ func (s *Service) GetMigration(ctx context.Context, uid string) (*cloudmigration
|
||||
return migration, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetMigrationList(ctx context.Context) (*cloudmigration.CloudMigrationListResponse, error) {
|
||||
values, err := s.store.GetAllCloudMigrations(ctx)
|
||||
func (s *Service) GetSessionList(ctx context.Context) (*cloudmigration.CloudMigrationSessionListResponse, error) {
|
||||
values, err := s.store.GetAllCloudMigrationSessions(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
migrations := make([]cloudmigration.CloudMigrationResponse, 0)
|
||||
migrations := make([]cloudmigration.CloudMigrationSessionResponse, 0)
|
||||
for _, v := range values {
|
||||
migrations = append(migrations, cloudmigration.CloudMigrationResponse{
|
||||
migrations = append(migrations, cloudmigration.CloudMigrationSessionResponse{
|
||||
UID: v.UID,
|
||||
Stack: v.Stack,
|
||||
Slug: v.Slug,
|
||||
Created: v.Created,
|
||||
Updated: v.Updated,
|
||||
})
|
||||
}
|
||||
return &cloudmigration.CloudMigrationListResponse{Migrations: migrations}, nil
|
||||
return &cloudmigration.CloudMigrationSessionListResponse{Sessions: migrations}, nil
|
||||
}
|
||||
|
||||
func (s *Service) CreateMigration(ctx context.Context, cmd cloudmigration.CloudMigrationRequest) (*cloudmigration.CloudMigrationResponse, error) {
|
||||
func (s *Service) CreateSession(ctx context.Context, cmd cloudmigration.CloudMigrationSessionRequest) (*cloudmigration.CloudMigrationSessionResponse, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "CloudMigrationService.createMigration")
|
||||
defer span.End()
|
||||
|
||||
@@ -359,32 +359,27 @@ func (s *Service) CreateMigration(ctx context.Context, cmd cloudmigration.CloudM
|
||||
}
|
||||
|
||||
migration := token.ToMigration()
|
||||
// validate token against cms before saving
|
||||
// validate token against GMS before saving
|
||||
if err := s.ValidateToken(ctx, migration); err != nil {
|
||||
return nil, fmt.Errorf("token validation: %w", err)
|
||||
}
|
||||
|
||||
cm, err := s.store.CreateMigration(ctx, migration)
|
||||
cm, err := s.store.CreateMigrationSession(ctx, migration)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating migration: %w", err)
|
||||
}
|
||||
|
||||
return &cloudmigration.CloudMigrationResponse{
|
||||
return &cloudmigration.CloudMigrationSessionResponse{
|
||||
UID: cm.UID,
|
||||
Stack: token.Instance.Slug,
|
||||
Slug: token.Instance.Slug,
|
||||
Created: cm.Created,
|
||||
Updated: cm.Updated,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) UpdateMigration(ctx context.Context, uid string, request cloudmigration.CloudMigrationRequest) (*cloudmigration.CloudMigrationResponse, error) {
|
||||
// TODO: Implement method
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *Service) RunMigration(ctx context.Context, uid string) (*cloudmigration.MigrateDataResponseDTO, error) {
|
||||
func (s *Service) RunMigration(ctx context.Context, uid string) (*cloudmigration.MigrateDataResponse, error) {
|
||||
// Get migration to read the auth token
|
||||
migration, err := s.GetMigration(ctx, uid)
|
||||
migration, err := s.GetSession(ctx, uid)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("migration get error: %w", err)
|
||||
}
|
||||
@@ -396,8 +391,8 @@ func (s *Service) RunMigration(ctx context.Context, uid string) (*cloudmigration
|
||||
return nil, fmt.Errorf("migration data get error: %w", err)
|
||||
}
|
||||
|
||||
// Call the cms service
|
||||
resp, err := s.cmsClient.MigrateData(ctx, *migration, *request)
|
||||
// Call the gms service
|
||||
resp, err := s.gmsClient.MigrateData(ctx, *migration, *request)
|
||||
if err != nil {
|
||||
s.log.Error("error migrating data: %w", err)
|
||||
return nil, fmt.Errorf("migrate data error: %w", err)
|
||||
@@ -411,9 +406,9 @@ func (s *Service) RunMigration(ctx context.Context, uid string) (*cloudmigration
|
||||
}
|
||||
|
||||
// save the result of the migration
|
||||
runUID, err := s.createMigrationRun(ctx, cloudmigration.CloudMigrationRun{
|
||||
CloudMigrationUID: migration.UID,
|
||||
Result: respData,
|
||||
runUID, err := s.createMigrationRun(ctx, cloudmigration.CloudMigrationSnapshot{
|
||||
SessionUID: migration.UID,
|
||||
Result: respData,
|
||||
})
|
||||
if err != nil {
|
||||
response.Error(http.StatusInternalServerError, "migration run save error", err)
|
||||
@@ -424,8 +419,8 @@ func (s *Service) RunMigration(ctx context.Context, uid string) (*cloudmigration
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *Service) getMigrationDataJSON(ctx context.Context) (*cloudmigration.MigrateDataRequestDTO, error) {
|
||||
var migrationDataSlice []cloudmigration.MigrateDataRequestItemDTO
|
||||
func (s *Service) getMigrationDataJSON(ctx context.Context) (*cloudmigration.MigrateDataRequest, error) {
|
||||
var migrationDataSlice []cloudmigration.MigrateDataRequestItem
|
||||
// Data sources
|
||||
dataSources, err := s.getDataSources(ctx)
|
||||
if err != nil {
|
||||
@@ -433,7 +428,7 @@ func (s *Service) getMigrationDataJSON(ctx context.Context) (*cloudmigration.Mig
|
||||
return nil, err
|
||||
}
|
||||
for _, ds := range dataSources {
|
||||
migrationDataSlice = append(migrationDataSlice, cloudmigration.MigrateDataRequestItemDTO{
|
||||
migrationDataSlice = append(migrationDataSlice, cloudmigration.MigrateDataRequestItem{
|
||||
Type: cloudmigration.DatasourceDataType,
|
||||
RefID: ds.UID,
|
||||
Name: ds.Name,
|
||||
@@ -450,7 +445,7 @@ func (s *Service) getMigrationDataJSON(ctx context.Context) (*cloudmigration.Mig
|
||||
|
||||
for _, dashboard := range dashboards {
|
||||
dashboard.Data.Del("id")
|
||||
migrationDataSlice = append(migrationDataSlice, cloudmigration.MigrateDataRequestItemDTO{
|
||||
migrationDataSlice = append(migrationDataSlice, cloudmigration.MigrateDataRequestItem{
|
||||
Type: cloudmigration.DashboardDataType,
|
||||
RefID: dashboard.UID,
|
||||
Name: dashboard.Title,
|
||||
@@ -466,14 +461,14 @@ func (s *Service) getMigrationDataJSON(ctx context.Context) (*cloudmigration.Mig
|
||||
}
|
||||
|
||||
for _, f := range folders {
|
||||
migrationDataSlice = append(migrationDataSlice, cloudmigration.MigrateDataRequestItemDTO{
|
||||
migrationDataSlice = append(migrationDataSlice, cloudmigration.MigrateDataRequestItem{
|
||||
Type: cloudmigration.FolderDataType,
|
||||
RefID: f.UID,
|
||||
Name: f.Title,
|
||||
Data: f,
|
||||
})
|
||||
}
|
||||
migrationData := &cloudmigration.MigrateDataRequestDTO{
|
||||
migrationData := &cloudmigration.MigrateDataRequest{
|
||||
Items: migrationDataSlice,
|
||||
}
|
||||
|
||||
@@ -547,7 +542,7 @@ func (s *Service) getDashboards(ctx context.Context) ([]dashboards.Dashboard, er
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *Service) createMigrationRun(ctx context.Context, cmr cloudmigration.CloudMigrationRun) (string, error) {
|
||||
func (s *Service) createMigrationRun(ctx context.Context, cmr cloudmigration.CloudMigrationSnapshot) (string, error) {
|
||||
uid, err := s.store.CreateMigrationRun(ctx, cmr)
|
||||
if err != nil {
|
||||
s.log.Error("Failed to save migration run", "err", err)
|
||||
@@ -556,7 +551,7 @@ func (s *Service) createMigrationRun(ctx context.Context, cmr cloudmigration.Clo
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetMigrationStatus(ctx context.Context, runUID string) (*cloudmigration.CloudMigrationRun, error) {
|
||||
func (s *Service) GetMigrationStatus(ctx context.Context, runUID string) (*cloudmigration.CloudMigrationSnapshot, error) {
|
||||
cmr, err := s.store.GetMigrationStatus(ctx, runUID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("retrieving migration status from db: %w", err)
|
||||
@@ -564,15 +559,15 @@ func (s *Service) GetMigrationStatus(ctx context.Context, runUID string) (*cloud
|
||||
return cmr, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetMigrationRunList(ctx context.Context, migUID string) (*cloudmigration.CloudMigrationRunList, error) {
|
||||
func (s *Service) GetMigrationRunList(ctx context.Context, migUID string) (*cloudmigration.SnapshotList, error) {
|
||||
runs, err := s.store.GetMigrationStatusList(ctx, migUID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("retrieving migration statuses from db: %w", err)
|
||||
}
|
||||
|
||||
runList := &cloudmigration.CloudMigrationRunList{Runs: []cloudmigration.MigrateDataResponseListDTO{}}
|
||||
runList := &cloudmigration.SnapshotList{Runs: []cloudmigration.MigrateDataResponseList{}}
|
||||
for _, s := range runs {
|
||||
runList.Runs = append(runList.Runs, cloudmigration.MigrateDataResponseListDTO{
|
||||
runList.Runs = append(runList.Runs, cloudmigration.MigrateDataResponseList{
|
||||
RunUID: s.UID,
|
||||
})
|
||||
}
|
||||
@@ -580,8 +575,8 @@ func (s *Service) GetMigrationRunList(ctx context.Context, migUID string) (*clou
|
||||
return runList, nil
|
||||
}
|
||||
|
||||
func (s *Service) DeleteMigration(ctx context.Context, uid string) (*cloudmigration.CloudMigration, error) {
|
||||
c, err := s.store.DeleteMigration(ctx, uid)
|
||||
func (s *Service) DeleteSession(ctx context.Context, uid string) (*cloudmigration.CloudMigrationSession, error) {
|
||||
c, err := s.store.DeleteMigrationSessionByUID(ctx, uid)
|
||||
if err != nil {
|
||||
return c, fmt.Errorf("deleting migration from db: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user