| 
									
										
										
										
											2024-02-26 21:52:16 +08:00
										 |  |  | package cloudmigration | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-13 12:11:35 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/apimachinery/errutil" | 
					
						
							| 
									
										
										
										
											2024-02-26 21:52:16 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ( | 
					
						
							| 
									
										
										
										
											2024-05-13 12:22:46 +08:00
										 |  |  | 	ErrInternalNotImplementedError = errutil.Internal("cloudmigrations.notImplemented").Errorf("Internal server error") | 
					
						
							|  |  |  | 	ErrFeatureDisabledError        = errutil.Internal("cloudmigrations.disabled").Errorf("Cloud migrations are disabled on this instance") | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	ErrMigrationNotFound           = errutil.NotFound("cloudmigrations.sessionNotFound").Errorf("Session not found") | 
					
						
							| 
									
										
										
										
											2024-05-13 12:22:46 +08:00
										 |  |  | 	ErrMigrationRunNotFound        = errutil.NotFound("cloudmigrations.migrationRunNotFound").Errorf("Migration run not found") | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	ErrMigrationNotDeleted         = errutil.Internal("cloudmigrations.sessionNotDeleted").Errorf("Session not deleted") | 
					
						
							| 
									
										
										
										
											2024-05-31 20:39:10 +08:00
										 |  |  | 	ErrTokenNotFound               = errutil.NotFound("cloudmigrations.tokenNotFound").Errorf("Token not found") | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 	ErrSnapshotNotFound            = errutil.NotFound("cloudmigrations.snapshotNotFound").Errorf("Snapshot not found") | 
					
						
							| 
									
										
										
										
											2024-02-26 21:52:16 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | // CloudMigration domain structs
 | 
					
						
							| 
									
										
										
										
											2024-07-10 20:46:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // CloudMigrationSession represents a configured migration token
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | type CloudMigrationSession struct { | 
					
						
							|  |  |  | 	ID          int64  `xorm:"pk autoincr 'id'"` | 
					
						
							|  |  |  | 	UID         string `xorm:"uid"` | 
					
						
							|  |  |  | 	AuthToken   string | 
					
						
							|  |  |  | 	Slug        string | 
					
						
							|  |  |  | 	StackID     int `xorm:"stack_id"` | 
					
						
							|  |  |  | 	RegionSlug  string | 
					
						
							|  |  |  | 	ClusterSlug string | 
					
						
							|  |  |  | 	Created     time.Time | 
					
						
							|  |  |  | 	Updated     time.Time | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-10 20:46:38 +08:00
										 |  |  | // CloudMigrationSnapshot contains all of the metadata about a snapshot
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | type CloudMigrationSnapshot struct { | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 	ID             int64  `xorm:"pk autoincr 'id'"` | 
					
						
							|  |  |  | 	UID            string `xorm:"uid"` | 
					
						
							|  |  |  | 	SessionUID     string `xorm:"session_uid"` | 
					
						
							|  |  |  | 	Status         SnapshotStatus | 
					
						
							|  |  |  | 	EncryptionKey  string `xorm:"encryption_key"` // stored in the unified secrets table
 | 
					
						
							|  |  |  | 	LocalDir       string `xorm:"local_directory"` | 
					
						
							|  |  |  | 	GMSSnapshotUID string `xorm:"gms_snapshot_uid"` | 
					
						
							|  |  |  | 	ErrorString    string `xorm:"error_string"` | 
					
						
							|  |  |  | 	Created        time.Time | 
					
						
							|  |  |  | 	Updated        time.Time | 
					
						
							|  |  |  | 	Finished       time.Time | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | 	// Stored in the cloud_migration_resource table
 | 
					
						
							|  |  |  | 	Resources []CloudMigrationResource `xorm:"-"` | 
					
						
							| 
									
										
										
										
											2024-07-10 20:46:38 +08:00
										 |  |  | 	// Derived by querying the cloud_migration_resource table
 | 
					
						
							|  |  |  | 	StatsRollup SnapshotResourceStats `xorm:"-"` | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | type SnapshotStatus string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2024-07-15 21:22:57 +08:00
										 |  |  | 	SnapshotStatusCreating          SnapshotStatus = "creating" | 
					
						
							|  |  |  | 	SnapshotStatusPendingUpload     SnapshotStatus = "pending_upload" | 
					
						
							|  |  |  | 	SnapshotStatusUploading         SnapshotStatus = "uploading" | 
					
						
							|  |  |  | 	SnapshotStatusPendingProcessing SnapshotStatus = "pending_processing" | 
					
						
							|  |  |  | 	SnapshotStatusProcessing        SnapshotStatus = "processing" | 
					
						
							|  |  |  | 	SnapshotStatusFinished          SnapshotStatus = "finished" | 
					
						
							|  |  |  | 	SnapshotStatusCanceled          SnapshotStatus = "canceled" | 
					
						
							|  |  |  | 	SnapshotStatusError             SnapshotStatus = "error" | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | type CloudMigrationResource struct { | 
					
						
							|  |  |  | 	ID  int64  `xorm:"pk autoincr 'id'"` | 
					
						
							|  |  |  | 	UID string `xorm:"uid"` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-15 21:22:57 +08:00
										 |  |  | 	Type   MigrateDataType `xorm:"resource_type" json:"type"` | 
					
						
							|  |  |  | 	RefID  string          `xorm:"resource_uid" json:"refId"` | 
					
						
							|  |  |  | 	Status ItemStatus      `xorm:"status" json:"status"` | 
					
						
							|  |  |  | 	Error  string          `xorm:"error_string" json:"error"` | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	SnapshotUID string `xorm:"snapshot_uid"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-10 20:46:38 +08:00
										 |  |  | type MigrateDataType string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	DashboardDataType  MigrateDataType = "DASHBOARD" | 
					
						
							|  |  |  | 	DatasourceDataType MigrateDataType = "DATASOURCE" | 
					
						
							|  |  |  | 	FolderDataType     MigrateDataType = "FOLDER" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ItemStatus string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	ItemStatusOK      ItemStatus = "OK" | 
					
						
							|  |  |  | 	ItemStatusError   ItemStatus = "ERROR" | 
					
						
							|  |  |  | 	ItemStatusPending ItemStatus = "PENDING" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type SnapshotResourceStats struct { | 
					
						
							|  |  |  | 	CountsByType   map[MigrateDataType]int | 
					
						
							|  |  |  | 	CountsByStatus map[ItemStatus]int | 
					
						
							| 
									
										
										
										
											2024-07-16 21:04:21 +08:00
										 |  |  | 	Total          int | 
					
						
							| 
									
										
										
										
											2024-07-10 20:46:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | // Deprecated, use GetSnapshotResult for the async workflow
 | 
					
						
							|  |  |  | func (s CloudMigrationSnapshot) GetResult() (*MigrateDataResponse, error) { | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | 	result := MigrateDataResponse{ | 
					
						
							|  |  |  | 		RunUID: s.UID, | 
					
						
							|  |  |  | 		Items:  s.Resources, | 
					
						
							| 
									
										
										
										
											2024-04-04 00:47:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return &result, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | func (s CloudMigrationSnapshot) ShouldQueryGMS() bool { | 
					
						
							|  |  |  | 	return s.Status == SnapshotStatusPendingProcessing || s.Status == SnapshotStatusProcessing | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type CloudMigrationRunList struct { | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 	Runs []MigrateDataResponseList | 
					
						
							| 
									
										
										
										
											2024-04-02 19:57:42 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | type CloudMigrationSessionRequest struct { | 
					
						
							|  |  |  | 	AuthToken string | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | type CloudMigrationSessionResponse struct { | 
					
						
							|  |  |  | 	UID     string | 
					
						
							|  |  |  | 	Slug    string | 
					
						
							|  |  |  | 	Created time.Time | 
					
						
							|  |  |  | 	Updated time.Time | 
					
						
							| 
									
										
										
										
											2024-03-25 20:30:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | type CloudMigrationSessionListResponse struct { | 
					
						
							|  |  |  | 	Sessions []CloudMigrationSessionResponse | 
					
						
							| 
									
										
										
										
											2024-03-28 19:50:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | type GetSnapshotsQuery struct { | 
					
						
							|  |  |  | 	SnapshotUID string | 
					
						
							|  |  |  | 	SessionUID  string | 
					
						
							|  |  |  | 	ResultPage  int | 
					
						
							|  |  |  | 	ResultLimit int | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | type ListSnapshotsQuery struct { | 
					
						
							|  |  |  | 	SessionUID string | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | 	Page       int | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | 	Limit      int | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type UpdateSnapshotCmd struct { | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | 	UID       string | 
					
						
							|  |  |  | 	Status    SnapshotStatus | 
					
						
							|  |  |  | 	Resources []CloudMigrationResource | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | // access token
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-25 23:43:28 +08:00
										 |  |  | type CreateAccessTokenResponse struct { | 
					
						
							|  |  |  | 	Token string | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-02-26 21:52:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 03:02:54 +08:00
										 |  |  | type Base64EncodedTokenPayload struct { | 
					
						
							|  |  |  | 	Token    string | 
					
						
							|  |  |  | 	Instance Base64HGInstance | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | func (p Base64EncodedTokenPayload) ToMigration() CloudMigrationSession { | 
					
						
							|  |  |  | 	return CloudMigrationSession{ | 
					
						
							| 
									
										
										
										
											2024-03-29 08:55:27 +08:00
										 |  |  | 		AuthToken:   p.Token, | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | 		Slug:        p.Instance.Slug, | 
					
						
							| 
									
										
										
										
											2024-03-29 08:55:27 +08:00
										 |  |  | 		StackID:     p.Instance.StackID, | 
					
						
							|  |  |  | 		RegionSlug:  p.Instance.RegionSlug, | 
					
						
							|  |  |  | 		ClusterSlug: p.Instance.ClusterSlug, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 03:02:54 +08:00
										 |  |  | type Base64HGInstance struct { | 
					
						
							|  |  |  | 	StackID     int | 
					
						
							|  |  |  | 	Slug        string | 
					
						
							|  |  |  | 	RegionSlug  string | 
					
						
							|  |  |  | 	ClusterSlug string | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | // GMS domain structs
 | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | type MigrateDataRequest struct { | 
					
						
							|  |  |  | 	Items []MigrateDataRequestItem | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | type MigrateDataRequestItem struct { | 
					
						
							|  |  |  | 	Type  MigrateDataType | 
					
						
							|  |  |  | 	RefID string | 
					
						
							|  |  |  | 	Name  string | 
					
						
							|  |  |  | 	Data  interface{} | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | type MigrateDataResponse struct { | 
					
						
							|  |  |  | 	RunUID string | 
					
						
							| 
									
										
										
										
											2024-06-25 11:50:07 +08:00
										 |  |  | 	Items  []CloudMigrationResource | 
					
						
							| 
									
										
										
										
											2024-04-03 19:36:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:58:59 +08:00
										 |  |  | type MigrateDataResponseList struct { | 
					
						
							|  |  |  | 	RunUID string | 
					
						
							| 
									
										
										
										
											2024-05-01 02:44:35 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | type CreateSessionResponse struct { | 
					
						
							|  |  |  | 	SnapshotUid string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-03 21:38:26 +08:00
										 |  |  | type StartSnapshotResponse struct { | 
					
						
							| 
									
										
										
										
											2024-07-11 20:32:02 +08:00
										 |  |  | 	SnapshotID           string `json:"snapshotID"` | 
					
						
							|  |  |  | 	MaxItemsPerPartition uint32 `json:"maxItemsPerPartition"` | 
					
						
							|  |  |  | 	Algo                 string `json:"algo"` | 
					
						
							|  |  |  | 	EncryptionKey        string `json:"encryptionKey"` | 
					
						
							| 
									
										
										
										
											2024-07-22 21:11:57 +08:00
										 |  |  | 	Metadata             []byte `json:"metadata"` | 
					
						
							| 
									
										
										
										
											2024-06-19 21:20:52 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-07-15 21:22:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Based on Grafana Migration Service DTOs
 | 
					
						
							|  |  |  | type GetSnapshotStatusResponse struct { | 
					
						
							|  |  |  | 	State   SnapshotState            `json:"state"` | 
					
						
							|  |  |  | 	Results []CloudMigrationResource `json:"results"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type SnapshotState string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	SnapshotStateInitialized SnapshotState = "INITIALIZED" | 
					
						
							|  |  |  | 	SnapshotStateProcessing  SnapshotState = "PROCESSING" | 
					
						
							|  |  |  | 	SnapshotStateFinished    SnapshotState = "FINISHED" | 
					
						
							|  |  |  | 	SnapshotStateCanceled    SnapshotState = "CANCELED" | 
					
						
							|  |  |  | 	SnapshotStateError       SnapshotState = "ERROR" | 
					
						
							|  |  |  | 	SnapshotStateUnknown     SnapshotState = "UNKNOWN" | 
					
						
							|  |  |  | ) |