CloudMigration: Add service to list all migrations (#85308)

This commit is contained in:
lean.dev 2024-03-28 08:50:31 -03:00 committed by GitHub
parent a44aa342a7
commit 5b147d0847
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 83 additions and 7 deletions

View File

@ -10,7 +10,7 @@ type Service interface {
SaveEncryptedToken(context.Context, string) error
// migration
GetMigration(context.Context, int64) (*CloudMigrationResponse, error)
GetMigrationList(context.Context) ([]CloudMigrationResponse, error)
GetMigrationList(context.Context) (*CloudMigrationListResponse, error)
CreateMigration(context.Context, CloudMigrationRequest) (*CloudMigrationResponse, error)
UpdateMigration(context.Context, int64, CloudMigrationRequest) (*CloudMigrationResponse, error)
RunMigration(context.Context, string) (*CloudMigrationRun, error)

View File

@ -20,7 +20,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
)
// CloudMigrationsServiceImpl Define the Service Implementation.
// Service Define the cloudmigration.Service Implementation.
type Service struct {
store store
@ -192,13 +192,29 @@ func (s *Service) SaveEncryptedToken(ctx context.Context, token string) error {
}
func (s *Service) GetMigration(ctx context.Context, id int64) (*cloudmigration.CloudMigrationResponse, error) {
// TODO: Implement method
// commenting to fix linter, uncomment when this function is implemented
// ctx, span := s.tracer.Start(ctx, "CloudMigrationService.GetMigration")
// defer span.End()
return nil, nil
}
func (s *Service) GetMigrationList(ctx context.Context) ([]cloudmigration.CloudMigrationResponse, error) {
// TODO: Implement method
return nil, nil
func (s *Service) GetMigrationList(ctx context.Context) (*cloudmigration.CloudMigrationListResponse, error) {
values, err := s.store.GetAllCloudMigrations(ctx)
if err != nil {
return nil, err
}
migrations := make([]cloudmigration.CloudMigrationResponse, 0)
for _, v := range values {
migrations = append(migrations, cloudmigration.CloudMigrationResponse{
ID: v.ID,
Stack: v.Stack,
Created: v.Created,
Updated: v.Updated,
})
}
return &cloudmigration.CloudMigrationListResponse{Migrations: migrations}, nil
}
func (s *Service) CreateMigration(ctx context.Context, cm cloudmigration.CloudMigrationRequest) (*cloudmigration.CloudMigrationResponse, error) {

View File

@ -30,7 +30,7 @@ func (s *NoopServiceImpl) GetMigration(ctx context.Context, id int64) (*cloudmig
return nil, cloudmigration.ErrFeatureDisabledError
}
func (s *NoopServiceImpl) GetMigrationList(ctx context.Context) ([]cloudmigration.CloudMigrationResponse, error) {
func (s *NoopServiceImpl) GetMigrationList(ctx context.Context) (*cloudmigration.CloudMigrationListResponse, error) {
return nil, cloudmigration.ErrFeatureDisabledError
}

View File

@ -8,4 +8,5 @@ import (
type store interface {
MigrateDatasources(context.Context, *cloudmigration.MigrateDatasourcesRequest) (*cloudmigration.MigrateDatasourcesResponse, error)
GetAllCloudMigrations(ctx context.Context) ([]*cloudmigration.CloudMigration, error)
}

View File

@ -14,3 +14,12 @@ type sqlStore struct {
func (ss *sqlStore) MigrateDatasources(ctx context.Context, request *cloudmigration.MigrateDatasourcesRequest) (*cloudmigration.MigrateDatasourcesResponse, error) {
return nil, cloudmigration.ErrInternalNotImplementedError
}
func (ss *sqlStore) GetAllCloudMigrations(ctx context.Context) ([]*cloudmigration.CloudMigration, error) {
var migrations = make([]*cloudmigration.CloudMigration, 0)
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error { return sess.Find(&migrations) })
if err != nil {
return nil, err
}
return migrations, nil
}

View File

@ -1,9 +1,55 @@
package cloudmigrationimpl
import (
"context"
"strconv"
"testing"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/tests/testsuite"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
testsuite.Run(m)
}
func TestMigrateDatasources(t *testing.T) {
// TODO: Write this test
}
func TestGetAllCloudMigrations(t *testing.T) {
testDB := db.InitTestDB(t)
s := &sqlStore{db: testDB}
ctx := context.Background()
t.Run("get all cloud_migrations", func(t *testing.T) {
// replace this with proper method when created
_, err := testDB.GetSqlxSession().Exec(ctx, `
INSERT INTO cloud_migration (id, auth_token, stack, created, updated)
VALUES (1, '12345', 'stack1', '2024-03-25 15:30:36.000', '2024-03-27 15:30:43.000'),
(2, '6789', 'stack2', '2024-03-25 15:30:36.000', '2024-03-27 15:30:43.000'),
(3, '777', 'stack3', '2024-03-25 15:30:36.000', '2024-03-27 15:30:43.000');
`)
require.NoError(t, err)
value, err := s.GetAllCloudMigrations(ctx)
require.NoError(t, err)
require.Equal(t, 3, len(value))
for _, m := range value {
switch m.ID {
case 1:
require.Equal(t, "stack1", m.Stack)
require.Equal(t, "12345", m.AuthToken)
case 2:
require.Equal(t, "stack2", m.Stack)
require.Equal(t, "6789", m.AuthToken)
case 3:
require.Equal(t, "stack3", m.Stack)
require.Equal(t, "777", m.AuthToken)
default:
require.Fail(t, "ID value not expected: "+strconv.FormatInt(m.ID, 10))
}
}
})
}

View File

@ -58,6 +58,10 @@ type CloudMigrationResponse struct {
Updated time.Time `json:"updated"`
}
type CloudMigrationListResponse struct {
Migrations []CloudMigrationResponse `json:"migrations"`
}
type MigrateDatasourcesRequest struct {
MigrateToPDC bool
MigrateCredentials bool