CloudMigrations: remove unused code from sync migration approach (#93430)
This commit is contained in:
@@ -6,14 +6,12 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/infra/kvstore"
|
||||
@@ -401,74 +399,6 @@ func (s *Service) CreateSession(ctx context.Context, cmd cloudmigration.CloudMig
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) RunMigration(ctx context.Context, uid string) (*cloudmigration.MigrateDataResponse, error) {
|
||||
// Get migration to read the auth token
|
||||
migration, err := s.GetSession(ctx, uid)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("migration get error: %w", err)
|
||||
}
|
||||
|
||||
// Get migration data JSON
|
||||
request, err := s.getMigrationDataJSON(ctx, &user.SignedInUser{})
|
||||
if err != nil {
|
||||
s.log.Error("error getting the json request body for migration run", "err", err.Error())
|
||||
return nil, fmt.Errorf("migration data get error: %w", err)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// save the result of the migration
|
||||
runUID, err := s.createMigrationRun(ctx, cloudmigration.CloudMigrationSnapshot{
|
||||
SessionUID: migration.UID,
|
||||
Resources: resp.Items,
|
||||
})
|
||||
if err != nil {
|
||||
response.Error(http.StatusInternalServerError, "migration run save error", err)
|
||||
}
|
||||
|
||||
resp.RunUID = runUID
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
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)
|
||||
return "", err
|
||||
}
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
return cmr, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetMigrationRunList(ctx context.Context, migUID string) (*cloudmigration.CloudMigrationRunList, 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.MigrateDataResponseList{}}
|
||||
for _, s := range runs {
|
||||
runList.Runs = append(runList.Runs, cloudmigration.MigrateDataResponseList{
|
||||
RunUID: s.UID,
|
||||
})
|
||||
}
|
||||
|
||||
return runList, nil
|
||||
}
|
||||
|
||||
func (s *Service) DeleteSession(ctx context.Context, sessionUID string) (*cloudmigration.CloudMigrationSession, error) {
|
||||
session, snapshots, err := s.store.DeleteMigrationSessionByUID(ctx, sessionUID)
|
||||
if err != nil {
|
||||
|
||||
@@ -41,26 +41,10 @@ func (s *NoopServiceImpl) CreateSession(ctx context.Context, cm cloudmigration.C
|
||||
return nil, cloudmigration.ErrFeatureDisabledError
|
||||
}
|
||||
|
||||
func (s *NoopServiceImpl) GetMigrationStatus(ctx context.Context, runUID string) (*cloudmigration.CloudMigrationSnapshot, error) {
|
||||
return nil, cloudmigration.ErrFeatureDisabledError
|
||||
}
|
||||
|
||||
func (s *NoopServiceImpl) GetMigrationRunList(ctx context.Context, uid string) (*cloudmigration.CloudMigrationRunList, error) {
|
||||
return nil, cloudmigration.ErrFeatureDisabledError
|
||||
}
|
||||
|
||||
func (s *NoopServiceImpl) DeleteSession(ctx context.Context, uid string) (*cloudmigration.CloudMigrationSession, error) {
|
||||
return nil, cloudmigration.ErrFeatureDisabledError
|
||||
}
|
||||
|
||||
func (s *NoopServiceImpl) CreateMigrationRun(context.Context, cloudmigration.CloudMigrationSnapshot) (string, error) {
|
||||
return "", cloudmigration.ErrInternalNotImplementedError
|
||||
}
|
||||
|
||||
func (s *NoopServiceImpl) RunMigration(context.Context, string) (*cloudmigration.MigrateDataResponse, error) {
|
||||
return nil, cloudmigration.ErrFeatureDisabledError
|
||||
}
|
||||
|
||||
func (s *NoopServiceImpl) CreateSnapshot(ctx context.Context, user *user.SignedInUser, sessionUid string) (*cloudmigration.CloudMigrationSnapshot, error) {
|
||||
return nil, cloudmigration.ErrFeatureDisabledError
|
||||
}
|
||||
|
||||
@@ -65,71 +65,6 @@ func Test_CreateGetAndDeleteToken(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func Test_CreateGetRunMigrationsAndRuns(t *testing.T) {
|
||||
s := setUpServiceTest(t, true)
|
||||
|
||||
createTokenResp, err := s.CreateToken(context.Background())
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createTokenResp.Token)
|
||||
|
||||
cmd := cloudmigration.CloudMigrationSessionRequest{
|
||||
AuthToken: createTokenResp.Token,
|
||||
}
|
||||
|
||||
createResp, err := s.CreateSession(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, createResp.UID)
|
||||
require.NotEmpty(t, createResp.Slug)
|
||||
|
||||
getMigResp, err := s.GetSession(context.Background(), createResp.UID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, getMigResp)
|
||||
require.Equal(t, createResp.UID, getMigResp.UID)
|
||||
require.Equal(t, createResp.Slug, getMigResp.Slug)
|
||||
|
||||
listResp, err := s.GetSessionList(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, listResp)
|
||||
require.Equal(t, 1, len(listResp.Sessions))
|
||||
require.Equal(t, createResp.UID, listResp.Sessions[0].UID)
|
||||
require.Equal(t, createResp.Slug, listResp.Sessions[0].Slug)
|
||||
|
||||
runResp, err := s.RunMigration(ctxWithSignedInUser(), createResp.UID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, runResp)
|
||||
resultItemsByType := make(map[string]int)
|
||||
for _, item := range runResp.Items {
|
||||
resultItemsByType[string(item.Type)] = resultItemsByType[string(item.Type)] + 1
|
||||
}
|
||||
require.Equal(t, 1, resultItemsByType["DASHBOARD"])
|
||||
require.Equal(t, 2, resultItemsByType["DATASOURCE"])
|
||||
require.Equal(t, 2, len(resultItemsByType))
|
||||
|
||||
runStatusResp, err := s.GetMigrationStatus(context.Background(), runResp.RunUID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, runResp.RunUID, runStatusResp.UID)
|
||||
|
||||
listRunResp, err := s.GetMigrationRunList(context.Background(), createResp.UID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(listRunResp.Runs))
|
||||
require.Equal(t, runResp.RunUID, listRunResp.Runs[0].RunUID)
|
||||
|
||||
/**
|
||||
-- This is not working at the moment since it is a mix of old and new methods
|
||||
will be fixed later when we clean the old functions and stick to the new ones.
|
||||
|
||||
delMigResp, err := s.DeleteSession(context.Background(), createResp.UID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, createResp.UID, delMigResp.UID)
|
||||
|
||||
// after deleting the session, the snapshots and resources should not exist anymore.
|
||||
// we check the snapshot for now
|
||||
listRunResp2, err := s.GetMigrationRunList(context.Background(), createResp.UID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 0, len(listRunResp2.Runs))
|
||||
*/
|
||||
}
|
||||
|
||||
func Test_GetSnapshotStatusFromGMS(t *testing.T) {
|
||||
s := setUpServiceTest(t, false).(*Service)
|
||||
|
||||
|
||||
@@ -82,54 +82,6 @@ func (m FakeServiceImpl) GetSessionList(_ context.Context) (*cloudmigration.Clou
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m FakeServiceImpl) RunMigration(_ context.Context, _ string) (*cloudmigration.MigrateDataResponse, error) {
|
||||
if m.ReturnError {
|
||||
return nil, fmt.Errorf("mock error")
|
||||
}
|
||||
r := fakeMigrateDataResponseDTO()
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func fakeMigrateDataResponseDTO() cloudmigration.MigrateDataResponse {
|
||||
return cloudmigration.MigrateDataResponse{
|
||||
RunUID: "fake_uid",
|
||||
Items: []cloudmigration.CloudMigrationResource{
|
||||
{Type: "type", RefID: "make_refid", Status: "ok", Error: "none"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (m FakeServiceImpl) CreateMigrationRun(ctx context.Context, run cloudmigration.CloudMigrationSnapshot) (string, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (m FakeServiceImpl) GetMigrationStatus(_ context.Context, _ string) (*cloudmigration.CloudMigrationSnapshot, error) {
|
||||
if m.ReturnError {
|
||||
return nil, fmt.Errorf("mock error")
|
||||
}
|
||||
return &cloudmigration.CloudMigrationSnapshot{
|
||||
ID: 0,
|
||||
UID: "fake_uid",
|
||||
SessionUID: "fake_mig_uid",
|
||||
Resources: fakeMigrateDataResponseDTO().Items,
|
||||
Created: fixedDate,
|
||||
Updated: fixedDate,
|
||||
Finished: fixedDate,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m FakeServiceImpl) GetMigrationRunList(_ context.Context, _ string) (*cloudmigration.CloudMigrationRunList, error) {
|
||||
if m.ReturnError {
|
||||
return nil, fmt.Errorf("mock error")
|
||||
}
|
||||
return &cloudmigration.CloudMigrationRunList{
|
||||
Runs: []cloudmigration.MigrateDataResponseList{
|
||||
{RunUID: "fake_run_uid_1"},
|
||||
{RunUID: "fake_run_uid_2"},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m FakeServiceImpl) CreateSnapshot(ctx context.Context, user *user.SignedInUser, sessionUid string) (*cloudmigration.CloudMigrationSnapshot, error) {
|
||||
if m.ReturnError {
|
||||
return nil, fmt.Errorf("mock error")
|
||||
|
||||
@@ -14,11 +14,6 @@ type store interface {
|
||||
// the work is done in a transaction.
|
||||
DeleteMigrationSessionByUID(ctx context.Context, uid string) (*cloudmigration.CloudMigrationSession, []cloudmigration.CloudMigrationSnapshot, error)
|
||||
|
||||
CreateMigrationRun(ctx context.Context, cmr cloudmigration.CloudMigrationSnapshot) (string, error)
|
||||
GetMigrationStatus(ctx context.Context, cmrUID string) (*cloudmigration.CloudMigrationSnapshot, error)
|
||||
// GetMigrationStatusList Deprecated: true - use GetSnapshotList instead
|
||||
GetMigrationStatusList(ctx context.Context, migrationUID string) ([]*cloudmigration.CloudMigrationSnapshot, error)
|
||||
|
||||
CreateSnapshot(ctx context.Context, snapshot cloudmigration.CloudMigrationSnapshot) (string, error)
|
||||
UpdateSnapshot(ctx context.Context, snapshot cloudmigration.UpdateSnapshotCmd) error
|
||||
GetSnapshotByUID(ctx context.Context, sessUid, id string, resultPage int, resultLimit int) (*cloudmigration.CloudMigrationSnapshot, error)
|
||||
|
||||
@@ -51,22 +51,6 @@ func (ss *sqlStore) GetMigrationSessionByUID(ctx context.Context, uid string) (*
|
||||
return &cm, err
|
||||
}
|
||||
|
||||
func (ss *sqlStore) CreateMigrationRun(ctx context.Context, cmr cloudmigration.CloudMigrationSnapshot) (string, error) {
|
||||
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
cmr.Created = time.Now()
|
||||
cmr.Updated = time.Now()
|
||||
cmr.Finished = time.Now()
|
||||
cmr.UID = util.GenerateShortUID()
|
||||
|
||||
_, err := sess.Insert(&cmr)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return cmr.UID, nil
|
||||
}
|
||||
|
||||
func (ss *sqlStore) CreateMigrationSession(ctx context.Context, migration cloudmigration.CloudMigrationSession) (*cloudmigration.CloudMigrationSession, error) {
|
||||
if err := ss.encryptToken(ctx, &migration); err != nil {
|
||||
return nil, fmt.Errorf("encrypting token: %w", err)
|
||||
@@ -176,34 +160,6 @@ func (ss *sqlStore) DeleteMigrationSessionByUID(ctx context.Context, uid string)
|
||||
return &c, snapshots, nil
|
||||
}
|
||||
|
||||
func (ss *sqlStore) GetMigrationStatus(ctx context.Context, cmrUID string) (*cloudmigration.CloudMigrationSnapshot, error) {
|
||||
var c cloudmigration.CloudMigrationSnapshot
|
||||
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
exist, err := sess.Where("uid=?", cmrUID).Get(&c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exist {
|
||||
return cloudmigration.ErrMigrationRunNotFound
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return &c, err
|
||||
}
|
||||
|
||||
func (ss *sqlStore) GetMigrationStatusList(ctx context.Context, migrationUID string) ([]*cloudmigration.CloudMigrationSnapshot, error) {
|
||||
var runs = make([]*cloudmigration.CloudMigrationSnapshot, 0)
|
||||
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
return sess.Find(&runs, &cloudmigration.CloudMigrationSnapshot{
|
||||
SessionUID: migrationUID,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return runs, nil
|
||||
}
|
||||
|
||||
func (ss *sqlStore) CreateSnapshot(ctx context.Context, snapshot cloudmigration.CloudMigrationSnapshot) (string, error) {
|
||||
if snapshot.SessionUID == "" {
|
||||
return "", fmt.Errorf("sessionUID is required")
|
||||
|
||||
@@ -111,61 +111,6 @@ func Test_DeleteMigrationSession(t *testing.T) {
|
||||
}
|
||||
*/
|
||||
|
||||
func Test_CreateMigrationRun(t *testing.T) {
|
||||
_, s := setUpTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("creates a session run and retrieves it from db", func(t *testing.T) {
|
||||
cmr := cloudmigration.CloudMigrationSnapshot{
|
||||
SessionUID: "asdfg",
|
||||
Status: cloudmigration.SnapshotStatusFinished,
|
||||
}
|
||||
|
||||
createResp, err := s.CreateMigrationRun(ctx, cmr)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, createResp)
|
||||
|
||||
getMRResp, err := s.GetMigrationStatus(ctx, createResp)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, cmr.Status, getMRResp.Status)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetMigrationStatus(t *testing.T) {
|
||||
_, s := setUpTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("gets a migration status by uid", func(t *testing.T) {
|
||||
getMRResp, err := s.GetMigrationStatus(ctx, "poiuy")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "poiuy", getMRResp.UID)
|
||||
})
|
||||
|
||||
t.Run("returns error if migration run was not found", func(t *testing.T) {
|
||||
getMRResp, err := s.GetMigrationStatus(ctx, "fake_uid")
|
||||
require.ErrorIs(t, cloudmigration.ErrMigrationRunNotFound, err)
|
||||
require.Equal(t, int64(0), getMRResp.ID)
|
||||
require.Equal(t, "", getMRResp.UID)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetMigrationStatusList(t *testing.T) {
|
||||
_, s := setUpTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("gets migration status list from db", func(t *testing.T) {
|
||||
list, err := s.GetMigrationStatusList(ctx, "qwerty")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 2, len(list))
|
||||
})
|
||||
|
||||
t.Run("returns no error if migration was not found, just empty list", func(t *testing.T) {
|
||||
list, err := s.GetMigrationStatusList(ctx, "fake_migration")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 0, len(list))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SnapshotManagement(t *testing.T) {
|
||||
_, s := setUpTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
Reference in New Issue
Block a user