* - 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 commitd654e4c530, reversing changes made to5fe0b483eb. * 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>
194 lines
5.1 KiB
Go
194 lines
5.1 KiB
Go
package cloudmigration
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
|
)
|
|
|
|
var (
|
|
ErrInternalNotImplementedError = errutil.Internal("cloudmigrations.notImplemented", errutil.WithPublicMessage("Internal server error"))
|
|
ErrFeatureDisabledError = errutil.Internal("cloudmigrations.disabled", errutil.WithPublicMessage("Cloud migrations are disabled on this instance"))
|
|
ErrMigrationNotFound = errutil.NotFound("cloudmigrations.migrationNotFound", errutil.WithPublicMessage("Migration not found"))
|
|
ErrMigrationRunNotFound = errutil.NotFound("cloudmigrations.migrationRunNotFound", errutil.WithPublicMessage("Migration run not found"))
|
|
ErrMigrationNotDeleted = errutil.Internal("cloudmigrations.migrationNotDeleted", errutil.WithPublicMessage("Migration not deleted"))
|
|
)
|
|
|
|
// CloudMigration api dtos
|
|
type CloudMigration struct {
|
|
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
|
|
UID string `json:"uid" xorm:"uid"`
|
|
AuthToken string `json:"-"`
|
|
Stack string `json:"stack"`
|
|
StackID int `json:"stackID" xorm:"stack_id"`
|
|
RegionSlug string `json:"regionSlug"`
|
|
ClusterSlug string `json:"clusterSlug"`
|
|
Created time.Time `json:"created"`
|
|
Updated time.Time `json:"updated"`
|
|
}
|
|
|
|
type MigratedResourceResult struct {
|
|
Status string `json:"status"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
type MigrationResult struct {
|
|
Status string `json:"status"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
type MigratedResource struct {
|
|
Type string `json:"type"`
|
|
ID string `json:"id"`
|
|
RefID string `json:"refID"`
|
|
Name string `json:"name"`
|
|
Result MigratedResourceResult `json:"result"`
|
|
}
|
|
|
|
type CloudMigrationRun struct {
|
|
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
|
|
UID string `json:"uid" xorm:"uid"`
|
|
CloudMigrationUID string `json:"migrationUid" xorm:"cloud_migration_uid"`
|
|
Result []byte `json:"result"` //store raw cms response body
|
|
Created time.Time `json:"created"`
|
|
Updated time.Time `json:"updated"`
|
|
Finished time.Time `json:"finished"`
|
|
}
|
|
|
|
func (r CloudMigrationRun) ToResponse() (*MigrateDataResponseDTO, error) {
|
|
var result MigrateDataResponseDTO
|
|
err := json.Unmarshal(r.Result, &result)
|
|
if err != nil {
|
|
return nil, errors.New("could not parse result of run")
|
|
}
|
|
result.RunUID = r.UID
|
|
return &result, nil
|
|
}
|
|
|
|
type CloudMigrationRunList struct {
|
|
Runs []MigrateDataResponseListDTO `json:"runs"`
|
|
}
|
|
|
|
// swagger:parameters createMigration
|
|
type CloudMigrationRequestParams struct {
|
|
// required: true
|
|
// in: body
|
|
Body CloudMigrationRequest `json:"body"`
|
|
}
|
|
|
|
type CloudMigrationRequest struct {
|
|
AuthToken string `json:"authToken"`
|
|
}
|
|
|
|
type CloudMigrationResponse struct {
|
|
UID string `json:"uid"`
|
|
Stack string `json:"stack"`
|
|
Created time.Time `json:"created"`
|
|
Updated time.Time `json:"updated"`
|
|
}
|
|
|
|
type CloudMigrationListResponse struct {
|
|
Migrations []CloudMigrationResponse `json:"migrations"`
|
|
}
|
|
|
|
type MigrateDatasourcesRequest struct {
|
|
MigrateToPDC bool
|
|
MigrateCredentials bool
|
|
}
|
|
|
|
type MigrateDatasourcesResponse struct {
|
|
DatasourcesMigrated int
|
|
}
|
|
|
|
type MigrateDatasourcesRequestDTO struct {
|
|
MigrateToPDC bool `json:"migrateToPDC"`
|
|
MigrateCredentials bool `json:"migrateCredentials"`
|
|
}
|
|
|
|
type MigrateDatasourcesResponseDTO struct {
|
|
DatasourcesMigrated int `json:"datasourcesMigrated"`
|
|
}
|
|
|
|
// access token
|
|
|
|
type CreateAccessTokenResponse struct {
|
|
Token string
|
|
}
|
|
|
|
type CreateAccessTokenResponseDTO struct {
|
|
Token string `json:"token"`
|
|
}
|
|
|
|
type Base64EncodedTokenPayload struct {
|
|
Token string
|
|
Instance Base64HGInstance
|
|
}
|
|
|
|
func (p Base64EncodedTokenPayload) ToMigration() CloudMigration {
|
|
return CloudMigration{
|
|
AuthToken: p.Token,
|
|
Stack: p.Instance.Slug,
|
|
StackID: p.Instance.StackID,
|
|
RegionSlug: p.Instance.RegionSlug,
|
|
ClusterSlug: p.Instance.ClusterSlug,
|
|
}
|
|
}
|
|
|
|
type Base64HGInstance struct {
|
|
StackID int
|
|
Slug string
|
|
RegionSlug string
|
|
ClusterSlug string
|
|
}
|
|
|
|
// dtos for cms api
|
|
|
|
// swagger:enum MigrateDataType
|
|
type MigrateDataType string
|
|
|
|
const (
|
|
DashboardDataType MigrateDataType = "DASHBOARD"
|
|
DatasourceDataType MigrateDataType = "DATASOURCE"
|
|
FolderDataType MigrateDataType = "FOLDER"
|
|
)
|
|
|
|
type MigrateDataRequestDTO struct {
|
|
Items []MigrateDataRequestItemDTO `json:"items"`
|
|
}
|
|
|
|
type MigrateDataRequestItemDTO struct {
|
|
Type MigrateDataType `json:"type"`
|
|
RefID string `json:"refId"`
|
|
Name string `json:"name"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
// swagger:enum ItemStatus
|
|
type ItemStatus string
|
|
|
|
const (
|
|
ItemStatusOK ItemStatus = "OK"
|
|
ItemStatusError ItemStatus = "ERROR"
|
|
)
|
|
|
|
type MigrateDataResponseDTO struct {
|
|
RunUID string `json:"uid"`
|
|
Items []MigrateDataResponseItemDTO `json:"items"`
|
|
}
|
|
|
|
type MigrateDataResponseListDTO struct {
|
|
RunUID string `json:"uid"`
|
|
}
|
|
|
|
type MigrateDataResponseItemDTO struct {
|
|
// required:true
|
|
Type MigrateDataType `json:"type"`
|
|
// required:true
|
|
RefID string `json:"refId"`
|
|
// required:true
|
|
Status ItemStatus `json:"status"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|