From e89aef57cb6de348dbcaad8881d7c9473c5af057 Mon Sep 17 00:00:00 2001 From: Matheus Macabu Date: Mon, 7 Oct 2024 12:53:14 +0200 Subject: [PATCH] CloudMigrations: wire ngalert to cloud migration service and add slicesext.Map helper (#94254) * CloudMigrations: add slicesext.Map function to simplify dto creation * CloudMigrations: wire ngalert to cloud migration service --- .../cloudmigrationimpl/cloudmigration.go | 4 +++ .../cloudmigrationimpl/cloudmigration_test.go | 30 ++++++++++++++++ .../cloudmigration/slicesext/slicesext.go | 11 ++++++ .../slicesext/slicesext_test.go | 36 +++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 pkg/services/cloudmigration/slicesext/slicesext.go create mode 100644 pkg/services/cloudmigration/slicesext/slicesext_test.go diff --git a/pkg/services/cloudmigration/cloudmigrationimpl/cloudmigration.go b/pkg/services/cloudmigration/cloudmigrationimpl/cloudmigration.go index 90ee06b6d60..bf689200719 100644 --- a/pkg/services/cloudmigration/cloudmigrationimpl/cloudmigration.go +++ b/pkg/services/cloudmigration/cloudmigrationimpl/cloudmigration.go @@ -28,6 +28,7 @@ import ( "github.com/grafana/grafana/pkg/services/folder" "github.com/grafana/grafana/pkg/services/gcom" "github.com/grafana/grafana/pkg/services/libraryelements" + "github.com/grafana/grafana/pkg/services/ngalert" "github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" "github.com/grafana/grafana/pkg/services/secrets" secretskv "github.com/grafana/grafana/pkg/services/secrets/kvstore" @@ -64,6 +65,7 @@ type Service struct { secretsService secrets.Service kvStore *kvstore.NamespacedKVStore libraryElementsService libraryelements.Service + ngAlert *ngalert.AlertNG api *api.CloudMigrationAPI tracer tracing.Tracer @@ -99,6 +101,7 @@ func ProvideService( pluginStore pluginstore.Store, kvStore kvstore.KVStore, libraryElementsService libraryelements.Service, + ngAlert *ngalert.AlertNG, ) (cloudmigration.Service, error) { if !features.IsEnabledGlobally(featuremgmt.FlagOnPremToCloudMigrations) { return &NoopServiceImpl{}, nil @@ -118,6 +121,7 @@ func ProvideService( pluginStore: pluginStore, kvStore: kvstore.WithNamespace(kvStore, 0, "cloudmigration"), libraryElementsService: libraryElementsService, + ngAlert: ngAlert, } s.api = api.RegisterApi(routeRegister, s, tracer) diff --git a/pkg/services/cloudmigration/cloudmigrationimpl/cloudmigration_test.go b/pkg/services/cloudmigration/cloudmigrationimpl/cloudmigration_test.go index 44affa5ae92..cdbd0f2c78d 100644 --- a/pkg/services/cloudmigration/cloudmigrationimpl/cloudmigration_test.go +++ b/pkg/services/cloudmigration/cloudmigrationimpl/cloudmigration_test.go @@ -12,11 +12,15 @@ import ( "github.com/google/uuid" "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" "github.com/grafana/grafana/pkg/api/routing" + "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/infra/db" + "github.com/grafana/grafana/pkg/infra/httpclient" "github.com/grafana/grafana/pkg/infra/kvstore" "github.com/grafana/grafana/pkg/infra/tracing" "github.com/grafana/grafana/pkg/plugins" + "github.com/grafana/grafana/pkg/services/accesscontrol/actest" + "github.com/grafana/grafana/pkg/services/annotations/annotationstest" "github.com/grafana/grafana/pkg/services/cloudmigration" "github.com/grafana/grafana/pkg/services/cloudmigration/gmsclient" "github.com/grafana/grafana/pkg/services/contexthandler/ctxkey" @@ -29,7 +33,12 @@ import ( "github.com/grafana/grafana/pkg/services/folder/foldertest" libraryelementsfake "github.com/grafana/grafana/pkg/services/libraryelements/fake" libraryelements "github.com/grafana/grafana/pkg/services/libraryelements/model" + "github.com/grafana/grafana/pkg/services/ngalert" + "github.com/grafana/grafana/pkg/services/ngalert/metrics" + ngalertstore "github.com/grafana/grafana/pkg/services/ngalert/store" + ngalertfakes "github.com/grafana/grafana/pkg/services/ngalert/tests/fakes" "github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" + "github.com/grafana/grafana/pkg/services/quota/quotatest" secretsfakes "github.com/grafana/grafana/pkg/services/secrets/fakes" secretskv "github.com/grafana/grafana/pkg/services/secrets/kvstore" "github.com/grafana/grafana/pkg/services/user" @@ -764,6 +773,26 @@ func setUpServiceTest(t *testing.T, withDashboardMock bool) cloudmigration.Servi }, } + featureToggles := featuremgmt.WithFeatures(featuremgmt.FlagOnPremToCloudMigrations, featuremgmt.FlagDashboardRestore) + + kvStore := kvstore.ProvideService(sqlStore) + + bus := bus.ProvideBus(tracer) + fakeAccessControl := actest.FakeAccessControl{} + fakeAccessControlService := actest.FakeService{} + alertMetrics := metrics.NewNGAlert(prometheus.NewRegistry()) + + ruleStore, err := ngalertstore.ProvideDBStore(cfg, featureToggles, sqlStore, mockFolder, dashboardService, fakeAccessControl) + require.NoError(t, err) + + ng, err := ngalert.ProvideService( + cfg, featureToggles, nil, nil, rr, sqlStore, kvStore, nil, nil, quotatest.New(false, nil), + secretsService, nil, alertMetrics, mockFolder, fakeAccessControl, dashboardService, nil, bus, fakeAccessControlService, + annotationstest.NewFakeAnnotationsRepo(), &pluginstore.FakePluginStore{}, tracer, ruleStore, + httpclient.NewProvider(), ngalertfakes.NewFakeReceiverPermissionsService(), + ) + require.NoError(t, err) + s, err := ProvideService( cfg, httpclient.NewProvider(), @@ -782,6 +811,7 @@ func setUpServiceTest(t *testing.T, withDashboardMock bool) cloudmigration.Servi &pluginstore.FakePluginStore{}, kvstore.ProvideService(sqlStore), &libraryelementsfake.LibraryElementService{}, + ng, ) require.NoError(t, err) diff --git a/pkg/services/cloudmigration/slicesext/slicesext.go b/pkg/services/cloudmigration/slicesext/slicesext.go new file mode 100644 index 00000000000..276858d5418 --- /dev/null +++ b/pkg/services/cloudmigration/slicesext/slicesext.go @@ -0,0 +1,11 @@ +package slicesext + +func Map[T any, U any](xs []T, f func(T) U) []U { + out := make([]U, 0, len(xs)) + + for _, x := range xs { + out = append(out, f(x)) + } + + return out +} diff --git a/pkg/services/cloudmigration/slicesext/slicesext_test.go b/pkg/services/cloudmigration/slicesext/slicesext_test.go new file mode 100644 index 00000000000..8e7080be9c1 --- /dev/null +++ b/pkg/services/cloudmigration/slicesext/slicesext_test.go @@ -0,0 +1,36 @@ +package slicesext_test + +import ( + "strconv" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/grafana/grafana/pkg/services/cloudmigration/slicesext" +) + +func TestMap(t *testing.T) { + t.Parallel() + + t.Run("mapping a nil slice does nothing and returns an empty slice", func(t *testing.T) { + t.Parallel() + + require.Empty(t, slicesext.Map[any, any](nil, nil)) + }) + + t.Run("mapping a non-nil slice with a nil function panics", func(t *testing.T) { + t.Parallel() + + require.Panics(t, func() { slicesext.Map[int, any]([]int{1, 2, 3}, nil) }) + }) + + t.Run("mapping a non-nil slice with a non-nil function returns the mapped slice", func(t *testing.T) { + t.Parallel() + + original := []int{1, 2, 3} + expected := []string{"1", "2", "3"} + fn := func(i int) string { return strconv.Itoa(i) } + + require.ElementsMatch(t, expected, slicesext.Map(original, fn)) + }) +}