mirror of https://github.com/grafana/grafana
Restructure cloudmigration service (#83211)
* Restructure cloudmigation service * Adjust codewoners and wire * Comment out unused metricsdatasources/base-class
parent
648abdbd0e
commit
80447dd0cc
@ -0,0 +1,16 @@ |
|||||||
|
package cloudmigrationimpl |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/services/cloudmigration" |
||||||
|
) |
||||||
|
|
||||||
|
// CloudMigrationsServiceImpl Define the Service Implementation.
|
||||||
|
type NoopServiceImpl struct{} |
||||||
|
|
||||||
|
var _ cloudmigration.Service = (*NoopServiceImpl)(nil) |
||||||
|
|
||||||
|
func (s *NoopServiceImpl) MigrateDatasources(ctx context.Context, request *cloudmigration.MigrateDatasourcesRequest) (*cloudmigration.MigrateDatasourcesResponse, error) { |
||||||
|
return nil, cloudmigration.ErrFeatureDisabledError |
||||||
|
} |
@ -0,0 +1,15 @@ |
|||||||
|
package cloudmigrationimpl |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
"testing" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/services/cloudmigration" |
||||||
|
"github.com/stretchr/testify/assert" |
||||||
|
) |
||||||
|
|
||||||
|
func Test_NoopServiceDoesNothing(t *testing.T) { |
||||||
|
s := &NoopServiceImpl{} |
||||||
|
_, e := s.MigrateDatasources(context.Background(), &cloudmigration.MigrateDatasourcesRequest{}) |
||||||
|
assert.ErrorIs(t, e, cloudmigration.ErrFeatureDisabledError) |
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
package cloudmigrationimpl |
||||||
|
|
||||||
|
import ( |
||||||
|
"errors" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/services/cloudmigration" |
||||||
|
"github.com/prometheus/client_golang/prometheus" |
||||||
|
) |
||||||
|
|
||||||
|
// type Metrics struct {
|
||||||
|
// log log.Logger
|
||||||
|
// }
|
||||||
|
|
||||||
|
func (s *Service) registerMetrics(prom prometheus.Registerer) error { |
||||||
|
for _, m := range cloudmigration.PromMetrics { |
||||||
|
if err := prom.Register(m); err != nil { |
||||||
|
var alreadyRegisterErr prometheus.AlreadyRegisteredError |
||||||
|
if errors.As(err, &alreadyRegisterErr) { |
||||||
|
s.log.Warn("metric already registered", "metric", m) |
||||||
|
continue |
||||||
|
} |
||||||
|
return err |
||||||
|
} |
||||||
|
} |
||||||
|
return nil |
||||||
|
} |
@ -0,0 +1,11 @@ |
|||||||
|
package cloudmigrationimpl |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/services/cloudmigration" |
||||||
|
) |
||||||
|
|
||||||
|
type store interface { |
||||||
|
MigrateDatasources(context.Context, *cloudmigration.MigrateDatasourcesRequest) (*cloudmigration.MigrateDatasourcesResponse, error) |
||||||
|
} |
@ -0,0 +1,16 @@ |
|||||||
|
package cloudmigrationimpl |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/infra/db" |
||||||
|
"github.com/grafana/grafana/pkg/services/cloudmigration" |
||||||
|
) |
||||||
|
|
||||||
|
type sqlStore struct { |
||||||
|
db db.DB |
||||||
|
} |
||||||
|
|
||||||
|
func (ss *sqlStore) MigrateDatasources(ctx context.Context, request *cloudmigration.MigrateDatasourcesRequest) (*cloudmigration.MigrateDatasourcesResponse, error) { |
||||||
|
return nil, cloudmigration.ErrInternalNotImplementedError |
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
package cloudmigrationimpl |
||||||
|
|
||||||
|
import ( |
||||||
|
"testing" |
||||||
|
) |
||||||
|
|
||||||
|
func TestMigrateDatasources(t *testing.T) { |
||||||
|
// TODO: Write this test
|
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
package cloudmigration |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
) |
||||||
|
|
||||||
|
type Service interface { |
||||||
|
MigrateDatasources(context.Context, *MigrateDatasourcesRequest) (*MigrateDatasourcesResponse, error) |
||||||
|
} |
@ -0,0 +1,15 @@ |
|||||||
|
package cloudmigrationtest |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/services/cloudmigration" |
||||||
|
) |
||||||
|
|
||||||
|
type Service struct { |
||||||
|
ExpectedError error |
||||||
|
} |
||||||
|
|
||||||
|
func (s *Service) MigrateDatasources(ctx context.Context, request *cloudmigration.MigrateDatasourcesRequest) (*cloudmigration.MigrateDatasourcesResponse, error) { |
||||||
|
return nil, cloudmigration.ErrInternalNotImplementedError |
||||||
|
} |
@ -0,0 +1,43 @@ |
|||||||
|
package cloudmigration |
||||||
|
|
||||||
|
import ( |
||||||
|
"github.com/grafana/grafana/pkg/util/errutil" |
||||||
|
"github.com/prometheus/client_golang/prometheus" |
||||||
|
) |
||||||
|
|
||||||
|
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")) |
||||||
|
) |
||||||
|
|
||||||
|
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"` |
||||||
|
} |
||||||
|
|
||||||
|
const ( |
||||||
|
namespace = "grafana" |
||||||
|
subsystem = "cloudmigrations" |
||||||
|
) |
||||||
|
|
||||||
|
var PromMetrics = []prometheus.Collector{ |
||||||
|
prometheus.NewCounterVec(prometheus.CounterOpts{ |
||||||
|
Namespace: namespace, |
||||||
|
Subsystem: subsystem, |
||||||
|
Name: "datasources_migrated", |
||||||
|
Help: "Total amount of data sources migrated", |
||||||
|
}, []string{"pdc_converted"}), |
||||||
|
} |
@ -1,10 +0,0 @@ |
|||||||
package api |
|
||||||
|
|
||||||
type migrateDatasourcesRequestDTO struct { |
|
||||||
MigrateToPDC bool `json:"migrateToPDC"` |
|
||||||
MigrateCredentials bool `json:"migrateCredentials"` |
|
||||||
} |
|
||||||
|
|
||||||
type migrateDatasourcesResponseDTO struct { |
|
||||||
DatasourcesMigrated int `json:"datasourcesMigrated"` |
|
||||||
} |
|
@ -1,11 +0,0 @@ |
|||||||
package cloudmigrations |
|
||||||
|
|
||||||
import ( |
|
||||||
"context" |
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/services/cloudmigrations/models" |
|
||||||
) |
|
||||||
|
|
||||||
type CloudMigrationService interface { |
|
||||||
MigrateDatasources(ctx context.Context, request *models.MigrateDatasourcesRequest) (*models.MigrateDatasourcesResponse, error) |
|
||||||
} |
|
@ -1,40 +0,0 @@ |
|||||||
package metrics |
|
||||||
|
|
||||||
import ( |
|
||||||
"errors" |
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log" |
|
||||||
"github.com/prometheus/client_golang/prometheus" |
|
||||||
) |
|
||||||
|
|
||||||
type Metrics struct { |
|
||||||
log log.Logger |
|
||||||
} |
|
||||||
|
|
||||||
func RegisterMetrics( |
|
||||||
prom prometheus.Registerer, |
|
||||||
) (*Metrics, error) { |
|
||||||
s := &Metrics{ |
|
||||||
log: log.New("cloudmigrations.metrics"), |
|
||||||
} |
|
||||||
|
|
||||||
if err := s.registerMetrics(prom); err != nil { |
|
||||||
return nil, err |
|
||||||
} |
|
||||||
|
|
||||||
return s, nil |
|
||||||
} |
|
||||||
|
|
||||||
func (s *Metrics) registerMetrics(prom prometheus.Registerer) error { |
|
||||||
for _, m := range promMetrics { |
|
||||||
if err := prom.Register(m); err != nil { |
|
||||||
var alreadyRegisterErr prometheus.AlreadyRegisteredError |
|
||||||
if errors.As(err, &alreadyRegisterErr) { |
|
||||||
s.log.Warn("metric already registered", "metric", m) |
|
||||||
continue |
|
||||||
} |
|
||||||
return err |
|
||||||
} |
|
||||||
} |
|
||||||
return nil |
|
||||||
} |
|
@ -1,19 +0,0 @@ |
|||||||
package metrics |
|
||||||
|
|
||||||
import ( |
|
||||||
"github.com/prometheus/client_golang/prometheus" |
|
||||||
) |
|
||||||
|
|
||||||
const ( |
|
||||||
namespace = "grafana" |
|
||||||
subsystem = "cloudmigrations" |
|
||||||
) |
|
||||||
|
|
||||||
var promMetrics = []prometheus.Collector{ |
|
||||||
prometheus.NewCounterVec(prometheus.CounterOpts{ |
|
||||||
Namespace: namespace, |
|
||||||
Subsystem: subsystem, |
|
||||||
Name: "datasources_migrated", |
|
||||||
Help: "Total amount of data sources migrated", |
|
||||||
}, []string{"pdc_converted"}), |
|
||||||
} |
|
@ -1,8 +0,0 @@ |
|||||||
package models |
|
||||||
|
|
||||||
import "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")) |
|
||||||
) |
|
@ -1,10 +0,0 @@ |
|||||||
package models |
|
||||||
|
|
||||||
type MigrateDatasourcesRequest struct { |
|
||||||
MigrateToPDC bool |
|
||||||
MigrateCredentials bool |
|
||||||
} |
|
||||||
|
|
||||||
type MigrateDatasourcesResponse struct { |
|
||||||
DatasourcesMigrated int |
|
||||||
} |
|
@ -1,17 +0,0 @@ |
|||||||
package service |
|
||||||
|
|
||||||
import ( |
|
||||||
"context" |
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/services/cloudmigrations" |
|
||||||
"github.com/grafana/grafana/pkg/services/cloudmigrations/models" |
|
||||||
) |
|
||||||
|
|
||||||
// CloudMigrationsServiceImpl Define the Service Implementation.
|
|
||||||
type NoopServiceImpl struct{} |
|
||||||
|
|
||||||
var _ cloudmigrations.CloudMigrationService = (*NoopServiceImpl)(nil) |
|
||||||
|
|
||||||
func (cm *NoopServiceImpl) MigrateDatasources(ctx context.Context, request *models.MigrateDatasourcesRequest) (*models.MigrateDatasourcesResponse, error) { |
|
||||||
return nil, models.ErrFeatureDisabledError |
|
||||||
} |
|
@ -1,15 +0,0 @@ |
|||||||
package service |
|
||||||
|
|
||||||
import ( |
|
||||||
"context" |
|
||||||
"testing" |
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/services/cloudmigrations/models" |
|
||||||
"github.com/stretchr/testify/assert" |
|
||||||
) |
|
||||||
|
|
||||||
func Test_NoopServiceDoesNothing(t *testing.T) { |
|
||||||
s := &NoopServiceImpl{} |
|
||||||
_, e := s.MigrateDatasources(context.Background(), &models.MigrateDatasourcesRequest{}) |
|
||||||
assert.ErrorIs(t, e, models.ErrFeatureDisabledError) |
|
||||||
} |
|
Loading…
Reference in new issue