Files
grafana/pkg/services/cloudmigration/gmsclient/inmemory_client.go
T
Michael Mandrus ee90cd3031 CloudMigrations: Send local events to gms during the migration process (#90637)
* add gms client function

* add timeout config for endpoint

* report events to gms

* fix lint error

* clean up report calls and make sure reports all have local ids

* extra validation

* improve error logging and fix url
2024-07-20 07:02:31 +03:00

101 lines
2.9 KiB
Go

package gmsclient
import (
"context"
"fmt"
"math/rand"
cryptoRand "crypto/rand"
"github.com/google/uuid"
"github.com/grafana/grafana/pkg/services/cloudmigration"
"golang.org/x/crypto/nacl/box"
)
// NewInMemoryClient returns an implementation of Client that returns canned responses
func NewInMemoryClient() Client {
return &memoryClientImpl{}
}
type memoryClientImpl struct {
snapshot *cloudmigration.StartSnapshotResponse
}
func (c *memoryClientImpl) ValidateKey(ctx context.Context, cm cloudmigration.CloudMigrationSession) error {
return nil
}
func (c *memoryClientImpl) MigrateData(
ctx context.Context,
cm cloudmigration.CloudMigrationSession,
request cloudmigration.MigrateDataRequest,
) (*cloudmigration.MigrateDataResponse, error) {
result := cloudmigration.MigrateDataResponse{
Items: make([]cloudmigration.CloudMigrationResource, len(request.Items)),
}
for i, v := range request.Items {
result.Items[i] = cloudmigration.CloudMigrationResource{
Type: v.Type,
RefID: v.RefID,
Status: cloudmigration.ItemStatusOK,
}
}
// simulate flakiness on one random item
i := rand.Intn(len(result.Items))
failedItem := result.Items[i]
failedItem.Status, failedItem.Error = cloudmigration.ItemStatusError, "simulated random error"
result.Items[i] = failedItem
return &result, nil
}
func (c *memoryClientImpl) StartSnapshot(context.Context, cloudmigration.CloudMigrationSession) (*cloudmigration.StartSnapshotResponse, error) {
publicKey, _, err := box.GenerateKey(cryptoRand.Reader)
if err != nil {
return nil, fmt.Errorf("nacl: generating public and private key: %w", err)
}
c.snapshot = &cloudmigration.StartSnapshotResponse{
EncryptionKey: fmt.Sprintf("%x", publicKey[:]),
SnapshotID: uuid.NewString(),
MaxItemsPerPartition: 10,
Algo: "nacl",
}
return c.snapshot, nil
}
func (c *memoryClientImpl) GetSnapshotStatus(ctx context.Context, session cloudmigration.CloudMigrationSession, snapshot cloudmigration.CloudMigrationSnapshot, offset int) (*cloudmigration.GetSnapshotStatusResponse, error) {
gmsResp := &cloudmigration.GetSnapshotStatusResponse{
State: cloudmigration.SnapshotStateFinished,
Results: []cloudmigration.CloudMigrationResource{
{
Type: cloudmigration.DashboardDataType,
RefID: "dash1",
Status: cloudmigration.ItemStatusOK,
},
{
Type: cloudmigration.DatasourceDataType,
RefID: "ds1",
Status: cloudmigration.ItemStatusError,
Error: "fake error",
},
{
Type: cloudmigration.FolderDataType,
RefID: "folder1",
Status: cloudmigration.ItemStatusOK,
},
},
}
return gmsResp, nil
}
func (c *memoryClientImpl) CreatePresignedUploadUrl(ctx context.Context, sess cloudmigration.CloudMigrationSession, snapshot cloudmigration.CloudMigrationSnapshot) (string, error) {
return "http://localhost:3000", nil
}
func (c *memoryClientImpl) ReportEvent(context.Context, cloudmigration.CloudMigrationSession, EventRequestDTO) {
}