diff --git a/authorizer/authorize_find.go b/authorizer/authorize_find.go index ae5eac7a93b..f21b8ce9476 100644 --- a/authorizer/authorize_find.go +++ b/authorizer/authorize_find.go @@ -9,7 +9,7 @@ import ( ) // AuthorizeFindDBRPs takes the given items and returns only the ones that the user is authorized to access. -func AuthorizeFindDBRPs(ctx context.Context, rs []*influxdb.DBRPMappingV2) ([]*influxdb.DBRPMappingV2, int, error) { +func AuthorizeFindDBRPs(ctx context.Context, rs []*influxdb.DBRPMapping) ([]*influxdb.DBRPMapping, int, error) { // This filters without allocating // https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating rrs := rs[:0] diff --git a/cmd/influxd/launcher/launcher.go b/cmd/influxd/launcher/launcher.go index 304cdc75d01..faf031c0366 100644 --- a/cmd/influxd/launcher/launcher.go +++ b/cmd/influxd/launcher/launcher.go @@ -1257,7 +1257,7 @@ func (m *Launcher) CheckService() platform.CheckService { return m.apibackend.CheckService } -func (m *Launcher) DBRPMappingServiceV2() platform.DBRPMappingServiceV2 { +func (m *Launcher) DBRPMappingService() platform.DBRPMappingService { return m.apibackend.DBRPService } diff --git a/cmd/influxd/upgrade/database.go b/cmd/influxd/upgrade/database.go index 8f9fd38640c..d500fdc26d0 100644 --- a/cmd/influxd/upgrade/database.go +++ b/cmd/influxd/upgrade/database.go @@ -90,7 +90,7 @@ func upgradeDatabases(ctx context.Context, cli clients.CLI, v1 *influxDBv1, v2 * return nil, fmt.Errorf("error creating database %s: %w", bucket.ID.String(), err) } - mapping := &influxdb.DBRPMappingV2{ + mapping := &influxdb.DBRPMapping{ Database: db.Name, RetentionPolicy: rp.Name, Default: db.DefaultRetentionPolicy == rp.Name, diff --git a/cmd/influxd/upgrade/upgrade.go b/cmd/influxd/upgrade/upgrade.go index 085b87ea85c..3b1fef465e2 100644 --- a/cmd/influxd/upgrade/upgrade.go +++ b/cmd/influxd/upgrade/upgrade.go @@ -301,7 +301,7 @@ type influxDBv2 struct { kvStore kv.SchemaStore tenantStore *tenant.Store ts *tenant.Service - dbrpSvc influxdb.DBRPMappingServiceV2 + dbrpSvc influxdb.DBRPMappingService bucketSvc influxdb.BucketService onboardSvc influxdb.OnboardingService authSvc *authv1.Service diff --git a/cmd/influxd/upgrade/v2_dump_meta.go b/cmd/influxd/upgrade/v2_dump_meta.go index b735d43cea0..9ce34ba5d73 100644 --- a/cmd/influxd/upgrade/v2_dump_meta.go +++ b/cmd/influxd/upgrade/v2_dump_meta.go @@ -118,7 +118,7 @@ var v2DumpMetaCommand = &cobra.Command{ fmt.Fprintln(os.Stdout, "Mappings") fmt.Fprintln(os.Stdout, "---------") fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\n", "Database", "RP", "Org", "Bucket", "Default") - mappings, _, err := svc.dbrpSvc.FindMany(ctx, influxdb.DBRPMappingFilterV2{}) + mappings, _, err := svc.dbrpSvc.FindMany(ctx, influxdb.DBRPMappingFilter{}) if err != nil { return err } diff --git a/dbrp/bucket_service.go b/dbrp/bucket_service.go index 7ec97c8f0b2..6cee68ebb52 100644 --- a/dbrp/bucket_service.go +++ b/dbrp/bucket_service.go @@ -12,10 +12,10 @@ import ( type BucketService struct { influxdb.BucketService Logger *zap.Logger - DBRPMappingService influxdb.DBRPMappingServiceV2 + DBRPMappingService influxdb.DBRPMappingService } -func NewBucketService(logger *zap.Logger, bucketService influxdb.BucketService, dbrpService influxdb.DBRPMappingServiceV2) *BucketService { +func NewBucketService(logger *zap.Logger, bucketService influxdb.BucketService, dbrpService influxdb.DBRPMappingService) *BucketService { return &BucketService{ Logger: logger, BucketService: bucketService, @@ -33,7 +33,7 @@ func (s *BucketService) DeleteBucket(ctx context.Context, id platform.ID) error } logger := s.Logger.With(zap.String("bucket_id", id.String())) - mappings, _, err := s.DBRPMappingService.FindMany(ctx, influxdb.DBRPMappingFilterV2{ + mappings, _, err := s.DBRPMappingService.FindMany(ctx, influxdb.DBRPMappingFilter{ OrgID: &bucket.OrgID, BucketID: &bucket.ID, }) diff --git a/dbrp/bucket_service_test.go b/dbrp/bucket_service_test.go index 5b94b80fb25..99ab3f7d5a6 100644 --- a/dbrp/bucket_service_test.go +++ b/dbrp/bucket_service_test.go @@ -26,7 +26,7 @@ func TestBucketService(t *testing.T) { logger = zap.NewNop() bucketServiceMock = mocks.NewMockBucketService(ctrl) - dbrpService = mocks.NewMockDBRPMappingServiceV2(ctrl) + dbrpService = mocks.NewMockDBRPMappingService(ctrl) bucket = &influxdb.Bucket{ ID: bucketID, @@ -42,10 +42,10 @@ func TestBucketService(t *testing.T) { Return(nil) findMapping := dbrpService.EXPECT(). - FindMany(gomock.Any(), influxdb.DBRPMappingFilterV2{ + FindMany(gomock.Any(), influxdb.DBRPMappingFilter{ BucketID: &bucketID, OrgID: &orgID, - }).Return([]*influxdb.DBRPMappingV2{ + }).Return([]*influxdb.DBRPMapping{ {ID: mappingID}, }, 1, nil) deleteMapping := dbrpService.EXPECT(). diff --git a/dbrp/http_client_dbrp.go b/dbrp/http_client_dbrp.go index a198603c0c3..3b1a8635c5f 100644 --- a/dbrp/http_client_dbrp.go +++ b/dbrp/http_client_dbrp.go @@ -13,7 +13,7 @@ import ( "github.com/influxdata/influxdb/v2/pkg/httpc" ) -var _ influxdb.DBRPMappingServiceV2 = (*Client)(nil) +var _ influxdb.DBRPMappingService = (*Client)(nil) // Client connects to Influx via HTTP using tokens to manage DBRPs. type Client struct { @@ -32,7 +32,7 @@ func (c *Client) dbrpURL(id platform.ID) string { return path.Join(c.Prefix, id.String()) } -func (c *Client) FindByID(ctx context.Context, orgID, id platform.ID) (*influxdb.DBRPMappingV2, error) { +func (c *Client) FindByID(ctx context.Context, orgID, id platform.ID) (*influxdb.DBRPMapping, error) { span, _ := tracing.StartSpanFromContext(ctx) defer span.Finish() @@ -47,7 +47,7 @@ func (c *Client) FindByID(ctx context.Context, orgID, id platform.ID) (*influxdb return resp.Content, nil } -func (c *Client) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilterV2, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMappingV2, int, error) { +func (c *Client) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilter, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) { span, _ := tracing.StartSpanFromContext(ctx) defer span.Finish() @@ -84,11 +84,11 @@ func (c *Client) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilter return resp.Content, len(resp.Content), nil } -func (c *Client) Create(ctx context.Context, dbrp *influxdb.DBRPMappingV2) error { +func (c *Client) Create(ctx context.Context, dbrp *influxdb.DBRPMapping) error { span, _ := tracing.StartSpanFromContext(ctx) defer span.Finish() - var newDBRP influxdb.DBRPMappingV2 + var newDBRP influxdb.DBRPMapping if err := c.Client. PostJSON(createDBRPRequest{ Database: dbrp.Database, @@ -105,7 +105,7 @@ func (c *Client) Create(ctx context.Context, dbrp *influxdb.DBRPMappingV2) error return nil } -func (c *Client) Update(ctx context.Context, dbrp *influxdb.DBRPMappingV2) error { +func (c *Client) Update(ctx context.Context, dbrp *influxdb.DBRPMapping) error { span, _ := tracing.StartSpanFromContext(ctx) defer span.Finish() @@ -113,7 +113,7 @@ func (c *Client) Update(ctx context.Context, dbrp *influxdb.DBRPMappingV2) error return err } - var newDBRP influxdb.DBRPMappingV2 + var newDBRP influxdb.DBRPMapping if err := c.Client. PatchJSON(dbrp, c.dbrpURL(dbrp.ID)). QueryParams([2]string{"orgID", dbrp.OrganizationID.String()}). diff --git a/dbrp/http_client_dbrp_test.go b/dbrp/http_client_dbrp_test.go index c17179fc93d..611420cbe39 100644 --- a/dbrp/http_client_dbrp_test.go +++ b/dbrp/http_client_dbrp_test.go @@ -16,13 +16,13 @@ import ( func setup(t *testing.T) (*dbrp.Client, func()) { t.Helper() - dbrpSvc := &mock.DBRPMappingServiceV2{ - CreateFn: func(ctx context.Context, dbrp *influxdb.DBRPMappingV2) error { + dbrpSvc := &mock.DBRPMappingService{ + CreateFn: func(ctx context.Context, dbrp *influxdb.DBRPMapping) error { dbrp.ID = 1 return nil }, - FindByIDFn: func(ctx context.Context, orgID, id platform.ID) (*influxdb.DBRPMappingV2, error) { - return &influxdb.DBRPMappingV2{ + FindByIDFn: func(ctx context.Context, orgID, id platform.ID) (*influxdb.DBRPMapping, error) { + return &influxdb.DBRPMapping{ ID: id, Database: "db", RetentionPolicy: "rp", @@ -31,8 +31,8 @@ func setup(t *testing.T) (*dbrp.Client, func()) { BucketID: 1, }, nil }, - FindManyFn: func(ctx context.Context, dbrp influxdb.DBRPMappingFilterV2, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMappingV2, int, error) { - return []*influxdb.DBRPMappingV2{}, 0, nil + FindManyFn: func(ctx context.Context, dbrp influxdb.DBRPMappingFilter, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) { + return []*influxdb.DBRPMapping{}, 0, nil }, } orgSvc := &mock.OrganizationService{ @@ -60,7 +60,7 @@ func TestClient(t *testing.T) { client, shutdown := setup(t) defer shutdown() - if err := client.Create(context.Background(), &influxdb.DBRPMappingV2{ + if err := client.Create(context.Background(), &influxdb.DBRPMapping{ Database: "db", RetentionPolicy: "rp", Default: false, @@ -79,7 +79,7 @@ func TestClient(t *testing.T) { t.Error(err) } oid := platform.ID(1) - if _, _, err := client.FindMany(context.Background(), influxdb.DBRPMappingFilterV2{OrgID: &oid}); err != nil { + if _, _, err := client.FindMany(context.Background(), influxdb.DBRPMappingFilter{OrgID: &oid}); err != nil { t.Error(err) } }) @@ -88,7 +88,7 @@ func TestClient(t *testing.T) { client, shutdown := setup(t) defer shutdown() - if err := client.Update(context.Background(), &influxdb.DBRPMappingV2{ + if err := client.Update(context.Background(), &influxdb.DBRPMapping{ ID: 1, Database: "db", RetentionPolicy: "rp", diff --git a/dbrp/http_server_dbrp.go b/dbrp/http_server_dbrp.go index 89549cb2ed9..d7de8d02dd9 100644 --- a/dbrp/http_server_dbrp.go +++ b/dbrp/http_server_dbrp.go @@ -23,12 +23,12 @@ type Handler struct { chi.Router api *kithttp.API log *zap.Logger - dbrpSvc influxdb.DBRPMappingServiceV2 + dbrpSvc influxdb.DBRPMappingService orgSvc influxdb.OrganizationService } // NewHTTPHandler constructs a new http server. -func NewHTTPHandler(log *zap.Logger, dbrpSvc influxdb.DBRPMappingServiceV2, orgSvc influxdb.OrganizationService) *Handler { +func NewHTTPHandler(log *zap.Logger, dbrpSvc influxdb.DBRPMappingService, orgSvc influxdb.OrganizationService) *Handler { h := &Handler{ api: kithttp.NewAPI(kithttp.WithLog(log)), log: log, @@ -114,7 +114,7 @@ func (h *Handler) handlePostDBRP(w http.ResponseWriter, r *http.Request) { return } - dbrp := &influxdb.DBRPMappingV2{ + dbrp := &influxdb.DBRPMapping{ Database: req.Database, RetentionPolicy: req.RetentionPolicy, Default: req.Default, @@ -129,7 +129,7 @@ func (h *Handler) handlePostDBRP(w http.ResponseWriter, r *http.Request) { } type getDBRPsResponse struct { - Content []*influxdb.DBRPMappingV2 `json:"content"` + Content []*influxdb.DBRPMapping `json:"content"` } func (h *Handler) handleGetDBRPs(w http.ResponseWriter, r *http.Request) { @@ -150,7 +150,7 @@ func (h *Handler) handleGetDBRPs(w http.ResponseWriter, r *http.Request) { } type getDBRPResponse struct { - Content *influxdb.DBRPMappingV2 `json:"content"` + Content *influxdb.DBRPMapping `json:"content"` } func (h *Handler) handleGetDBRP(w http.ResponseWriter, r *http.Request) { @@ -244,7 +244,7 @@ func (h *Handler) handlePatchDBRP(w http.ResponseWriter, r *http.Request) { } h.api.Respond(w, r, http.StatusOK, struct { - Content *influxdb.DBRPMappingV2 `json:"content"` + Content *influxdb.DBRPMapping `json:"content"` }{ Content: dbrp, }) @@ -281,7 +281,7 @@ func (h *Handler) handleDeleteDBRP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) } -func (h *Handler) getFilterFromHTTPRequest(r *http.Request) (f influxdb.DBRPMappingFilterV2, err error) { +func (h *Handler) getFilterFromHTTPRequest(r *http.Request) (f influxdb.DBRPMappingFilter, err error) { // Always provide OrgID. f.OrgID, err = h.mustGetOrgIDFromHTTPRequest(r) if err != nil { diff --git a/dbrp/http_server_dbrp_test.go b/dbrp/http_server_dbrp_test.go index b5ad43ed5ed..6c81941d261 100644 --- a/dbrp/http_server_dbrp_test.go +++ b/dbrp/http_server_dbrp_test.go @@ -22,7 +22,7 @@ import ( "go.uber.org/zap/zaptest" ) -func initHttpService(t *testing.T) (influxdb.DBRPMappingServiceV2, *httptest.Server, func()) { +func initHttpService(t *testing.T) (influxdb.DBRPMappingService, *httptest.Server, func()) { t.Helper() ctx := context.Background() bucketSvc := mock.NewBucketService() @@ -56,7 +56,7 @@ func Test_handlePostDBRP(t *testing.T) { table := []struct { Name string ExpectedErr error - ExpectedDBRP *influxdb.DBRPMappingV2 + ExpectedDBRP *influxdb.DBRPMapping Input io.Reader }{ { @@ -68,7 +68,7 @@ func Test_handlePostDBRP(t *testing.T) { "retention_policy": "autogen", "default": false }`), - ExpectedDBRP: &influxdb.DBRPMappingV2{ + ExpectedDBRP: &influxdb.DBRPMapping{ OrganizationID: influxdbtesting.MustIDBase16("059af7ed2a034000"), }, }, @@ -81,7 +81,7 @@ func Test_handlePostDBRP(t *testing.T) { "retention_policy": "autogen", "default": false }`), - ExpectedDBRP: &influxdb.DBRPMappingV2{ + ExpectedDBRP: &influxdb.DBRPMapping{ OrganizationID: influxdbtesting.MustIDBase16("059af7ed2a034000"), }, }, @@ -158,7 +158,7 @@ func Test_handlePostDBRP(t *testing.T) { assert.Equal(t, tt.ExpectedErr.Error(), actualErr.Error()) return } - dbrp := &influxdb.DBRPMappingV2{} + dbrp := &influxdb.DBRPMapping{} if err := json.NewDecoder(resp.Body).Decode(&dbrp); err != nil { t.Fatal(err) } @@ -183,12 +183,12 @@ func Test_handleGetDBRPs(t *testing.T) { Name string QueryParams string ExpectedErr error - ExpectedDBRPs []influxdb.DBRPMappingV2 + ExpectedDBRPs []influxdb.DBRPMapping }{ { Name: "ok", QueryParams: "orgID=059af7ed2a034000", - ExpectedDBRPs: []influxdb.DBRPMappingV2{ + ExpectedDBRPs: []influxdb.DBRPMapping{ { ID: influxdbtesting.MustIDBase16("1111111111111111"), BucketID: influxdbtesting.MustIDBase16("5555f7ed2a035555"), @@ -225,12 +225,12 @@ func Test_handleGetDBRPs(t *testing.T) { { Name: "no match", QueryParams: "orgID=059af7ed2a034000&default=false", - ExpectedDBRPs: []influxdb.DBRPMappingV2{}, + ExpectedDBRPs: []influxdb.DBRPMapping{}, }, { Name: "all match", QueryParams: "orgID=059af7ed2a034000&default=true&rp=autogen&db=mydb&bucketID=5555f7ed2a035555&id=1111111111111111", - ExpectedDBRPs: []influxdb.DBRPMappingV2{ + ExpectedDBRPs: []influxdb.DBRPMapping{ { ID: influxdbtesting.MustIDBase16("1111111111111111"), BucketID: influxdbtesting.MustIDBase16("5555f7ed2a035555"), @@ -244,7 +244,7 @@ func Test_handleGetDBRPs(t *testing.T) { { Name: "org name", QueryParams: "org=org", - ExpectedDBRPs: []influxdb.DBRPMappingV2{ + ExpectedDBRPs: []influxdb.DBRPMapping{ { ID: influxdbtesting.MustIDBase16("1111111111111111"), BucketID: influxdbtesting.MustIDBase16("5555f7ed2a035555"), @@ -269,7 +269,7 @@ func Test_handleGetDBRPs(t *testing.T) { if svc, ok := svc.(*dbrp.Service); ok { svc.IDGen = mock.NewIDGenerator("1111111111111111", t) } - db := &influxdb.DBRPMappingV2{ + db := &influxdb.DBRPMapping{ BucketID: influxdbtesting.MustIDBase16("5555f7ed2a035555"), OrganizationID: influxdbtesting.MustIDBase16("059af7ed2a034000"), Database: "mydb", @@ -301,7 +301,7 @@ func Test_handleGetDBRPs(t *testing.T) { return } dbrps := struct { - Content []influxdb.DBRPMappingV2 `json:"content"` + Content []influxdb.DBRPMapping `json:"content"` }{} if err := json.NewDecoder(resp.Body).Decode(&dbrps); err != nil { t.Fatal(err) @@ -323,7 +323,7 @@ func Test_handlePatchDBRP(t *testing.T) { table := []struct { Name string ExpectedErr error - ExpectedDBRP *influxdb.DBRPMappingV2 + ExpectedDBRP *influxdb.DBRPMapping URLSuffix string Input io.Reader }{ @@ -334,7 +334,7 @@ func Test_handlePatchDBRP(t *testing.T) { "retention_policy": "updaterp", "database": "wont_change" }`), - ExpectedDBRP: &influxdb.DBRPMappingV2{ + ExpectedDBRP: &influxdb.DBRPMapping{ ID: influxdbtesting.MustIDBase16("1111111111111111"), BucketID: influxdbtesting.MustIDBase16("5555f7ed2a035555"), OrganizationID: influxdbtesting.MustIDBase16("059af7ed2a034000"), @@ -350,7 +350,7 @@ func Test_handlePatchDBRP(t *testing.T) { "retention_policy": "updaterp", "database": "wont_change" }`), - ExpectedDBRP: &influxdb.DBRPMappingV2{ + ExpectedDBRP: &influxdb.DBRPMapping{ ID: influxdbtesting.MustIDBase16("1111111111111111"), BucketID: influxdbtesting.MustIDBase16("5555f7ed2a035555"), OrganizationID: influxdbtesting.MustIDBase16("059af7ed2a034000"), @@ -397,7 +397,7 @@ func Test_handlePatchDBRP(t *testing.T) { svc.IDGen = mock.NewIDGenerator("1111111111111111", t) } - dbrp := &influxdb.DBRPMappingV2{ + dbrp := &influxdb.DBRPMapping{ BucketID: influxdbtesting.MustIDBase16("5555f7ed2a035555"), OrganizationID: influxdbtesting.MustIDBase16("059af7ed2a034000"), Database: "mydb", @@ -429,7 +429,7 @@ func Test_handlePatchDBRP(t *testing.T) { return } dbrpResponse := struct { - Content *influxdb.DBRPMappingV2 `json:"content"` + Content *influxdb.DBRPMapping `json:"content"` }{} if err := json.NewDecoder(resp.Body).Decode(&dbrpResponse); err != nil { @@ -489,7 +489,7 @@ func Test_handleDeleteDBRP(t *testing.T) { defer shutdown() client := server.Client() - d := &influxdb.DBRPMappingV2{ + d := &influxdb.DBRPMapping{ ID: influxdbtesting.MustIDBase16("1111111111111111"), BucketID: influxdbtesting.MustIDBase16("5555f7ed2a035555"), OrganizationID: influxdbtesting.MustIDBase16("059af7ed2a034000"), diff --git a/dbrp/index.go b/dbrp/index.go index 8d688fb54b0..765d2248ce2 100644 --- a/dbrp/index.go +++ b/dbrp/index.go @@ -9,7 +9,7 @@ import ( var ( ByOrgIDIndexMapping = kv.NewIndexMapping(bucket, byOrgIDIndexBucket, func(v []byte) ([]byte, error) { - var dbrp influxdb.DBRPMappingV2 + var dbrp influxdb.DBRPMapping if err := json.Unmarshal(v, &dbrp); err != nil { return nil, err } diff --git a/dbrp/middleware_auth.go b/dbrp/middleware_auth.go index cc98a124c60..41411d3d869 100644 --- a/dbrp/middleware_auth.go +++ b/dbrp/middleware_auth.go @@ -9,18 +9,18 @@ import ( "github.com/influxdata/influxdb/v2/authorizer" ) -var _ influxdb.DBRPMappingServiceV2 = (*AuthorizedService)(nil) +var _ influxdb.DBRPMappingService = (*AuthorizedService)(nil) type AuthorizedService struct { - influxdb.DBRPMappingServiceV2 + influxdb.DBRPMappingService } -func NewAuthorizedService(s influxdb.DBRPMappingServiceV2) *AuthorizedService { - return &AuthorizedService{DBRPMappingServiceV2: s} +func NewAuthorizedService(s influxdb.DBRPMappingService) *AuthorizedService { + return &AuthorizedService{DBRPMappingService: s} } -func (svc AuthorizedService) FindByID(ctx context.Context, orgID, id platform.ID) (*influxdb.DBRPMappingV2, error) { - mapping, err := svc.DBRPMappingServiceV2.FindByID(ctx, orgID, id) +func (svc AuthorizedService) FindByID(ctx context.Context, orgID, id platform.ID) (*influxdb.DBRPMapping, error) { + mapping, err := svc.DBRPMappingService.FindByID(ctx, orgID, id) if err != nil { return nil, err } @@ -30,35 +30,35 @@ func (svc AuthorizedService) FindByID(ctx context.Context, orgID, id platform.ID return mapping, nil } -func (svc AuthorizedService) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilterV2, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMappingV2, int, error) { - dbrps, _, err := svc.DBRPMappingServiceV2.FindMany(ctx, filter, opts...) +func (svc AuthorizedService) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilter, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) { + dbrps, _, err := svc.DBRPMappingService.FindMany(ctx, filter, opts...) if err != nil { return nil, 0, err } return authorizer.AuthorizeFindDBRPs(ctx, dbrps) } -func (svc AuthorizedService) Create(ctx context.Context, t *influxdb.DBRPMappingV2) error { +func (svc AuthorizedService) Create(ctx context.Context, t *influxdb.DBRPMapping) error { if _, _, err := authorizer.AuthorizeWrite(ctx, influxdb.BucketsResourceType, t.BucketID, t.OrganizationID); err != nil { return err } - return svc.DBRPMappingServiceV2.Create(ctx, t) + return svc.DBRPMappingService.Create(ctx, t) } -func (svc AuthorizedService) Update(ctx context.Context, u *influxdb.DBRPMappingV2) error { +func (svc AuthorizedService) Update(ctx context.Context, u *influxdb.DBRPMapping) error { if _, _, err := authorizer.AuthorizeWrite(ctx, influxdb.BucketsResourceType, u.BucketID, u.OrganizationID); err != nil { return err } - return svc.DBRPMappingServiceV2.Update(ctx, u) + return svc.DBRPMappingService.Update(ctx, u) } func (svc AuthorizedService) Delete(ctx context.Context, orgID, id platform.ID) error { - mapping, err := svc.DBRPMappingServiceV2.FindByID(ctx, orgID, id) + mapping, err := svc.DBRPMappingService.FindByID(ctx, orgID, id) if err != nil { return err } if _, _, err := authorizer.AuthorizeWrite(ctx, influxdb.BucketsResourceType, mapping.BucketID, orgID); err != nil { return err } - return svc.DBRPMappingServiceV2.Delete(ctx, orgID, id) + return svc.DBRPMappingService.Delete(ctx, orgID, id) } diff --git a/dbrp/middleware_auth_test.go b/dbrp/middleware_auth_test.go index 23fc0d0cbee..2fe472cf74c 100644 --- a/dbrp/middleware_auth_test.go +++ b/dbrp/middleware_auth_test.go @@ -16,7 +16,7 @@ import ( func TestAuth_FindByID(t *testing.T) { type fields struct { - service influxdb.DBRPMappingServiceV2 + service influxdb.DBRPMappingService } type args struct { orgID platform.ID @@ -36,9 +36,9 @@ func TestAuth_FindByID(t *testing.T) { { name: "authorized to access id by org id", fields: fields{ - service: &mock.DBRPMappingServiceV2{ - FindByIDFn: func(_ context.Context, _, _ platform.ID) (*influxdb.DBRPMappingV2, error) { - return &influxdb.DBRPMappingV2{ + service: &mock.DBRPMappingService{ + FindByIDFn: func(_ context.Context, _, _ platform.ID) (*influxdb.DBRPMapping, error) { + return &influxdb.DBRPMapping{ OrganizationID: platform.ID(1), BucketID: platform.ID(1), }, nil @@ -63,9 +63,9 @@ func TestAuth_FindByID(t *testing.T) { { name: "authorized to access id by id", fields: fields{ - service: &mock.DBRPMappingServiceV2{ - FindByIDFn: func(_ context.Context, _, _ platform.ID) (*influxdb.DBRPMappingV2, error) { - return &influxdb.DBRPMappingV2{ + service: &mock.DBRPMappingService{ + FindByIDFn: func(_ context.Context, _, _ platform.ID) (*influxdb.DBRPMapping, error) { + return &influxdb.DBRPMapping{ OrganizationID: platform.ID(1), BucketID: platform.ID(1), }, nil @@ -90,9 +90,9 @@ func TestAuth_FindByID(t *testing.T) { { name: "unauthorized to access id by org id", fields: fields{ - service: &mock.DBRPMappingServiceV2{ - FindByIDFn: func(_ context.Context, _, _ platform.ID) (*influxdb.DBRPMappingV2, error) { - return &influxdb.DBRPMappingV2{ + service: &mock.DBRPMappingService{ + FindByIDFn: func(_ context.Context, _, _ platform.ID) (*influxdb.DBRPMapping, error) { + return &influxdb.DBRPMapping{ OrganizationID: platform.ID(2), BucketID: platform.ID(1), }, nil @@ -120,9 +120,9 @@ func TestAuth_FindByID(t *testing.T) { { name: "unauthorized to access id by id", fields: fields{ - service: &mock.DBRPMappingServiceV2{ - FindByIDFn: func(_ context.Context, _, _ platform.ID) (*influxdb.DBRPMappingV2, error) { - return &influxdb.DBRPMappingV2{ + service: &mock.DBRPMappingService{ + FindByIDFn: func(_ context.Context, _, _ platform.ID) (*influxdb.DBRPMapping, error) { + return &influxdb.DBRPMapping{ OrganizationID: platform.ID(1), BucketID: platform.ID(1), }, nil @@ -164,15 +164,15 @@ func TestAuth_FindByID(t *testing.T) { func TestAuth_FindMany(t *testing.T) { type fields struct { - service influxdb.DBRPMappingServiceV2 + service influxdb.DBRPMappingService } type args struct { - filter influxdb.DBRPMappingFilterV2 + filter influxdb.DBRPMappingFilter permissions []influxdb.Permission } type wants struct { err error - ms []*influxdb.DBRPMappingV2 + ms []*influxdb.DBRPMapping } tests := []struct { @@ -184,9 +184,9 @@ func TestAuth_FindMany(t *testing.T) { { name: "no result", fields: fields{ - service: &mock.DBRPMappingServiceV2{ - FindManyFn: func(ctx context.Context, dbrp influxdb.DBRPMappingFilterV2, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMappingV2, int, error) { - return []*influxdb.DBRPMappingV2{ + service: &mock.DBRPMappingService{ + FindManyFn: func(ctx context.Context, dbrp influxdb.DBRPMappingFilter, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) { + return []*influxdb.DBRPMapping{ { ID: 1, OrganizationID: 1, @@ -219,19 +219,19 @@ func TestAuth_FindMany(t *testing.T) { OrgID: influxdbtesting.IDPtr(42), }, }}, - filter: influxdb.DBRPMappingFilterV2{}, + filter: influxdb.DBRPMappingFilter{}, }, wants: wants{ err: nil, - ms: []*influxdb.DBRPMappingV2{}, + ms: []*influxdb.DBRPMapping{}, }, }, { name: "partial", fields: fields{ - service: &mock.DBRPMappingServiceV2{ - FindManyFn: func(ctx context.Context, dbrp influxdb.DBRPMappingFilterV2, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMappingV2, int, error) { - return []*influxdb.DBRPMappingV2{ + service: &mock.DBRPMappingService{ + FindManyFn: func(ctx context.Context, dbrp influxdb.DBRPMappingFilter, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) { + return []*influxdb.DBRPMapping{ { ID: 1, OrganizationID: 1, @@ -264,11 +264,11 @@ func TestAuth_FindMany(t *testing.T) { OrgID: influxdbtesting.IDPtr(1), }, }}, - filter: influxdb.DBRPMappingFilterV2{}, + filter: influxdb.DBRPMappingFilter{}, }, wants: wants{ err: nil, - ms: []*influxdb.DBRPMappingV2{ + ms: []*influxdb.DBRPMapping{ { ID: 1, OrganizationID: 1, @@ -285,9 +285,9 @@ func TestAuth_FindMany(t *testing.T) { { name: "all", fields: fields{ - service: &mock.DBRPMappingServiceV2{ - FindManyFn: func(ctx context.Context, dbrp influxdb.DBRPMappingFilterV2, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMappingV2, int, error) { - return []*influxdb.DBRPMappingV2{ + service: &mock.DBRPMappingService{ + FindManyFn: func(ctx context.Context, dbrp influxdb.DBRPMappingFilter, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) { + return []*influxdb.DBRPMapping{ { ID: 1, OrganizationID: 1, @@ -336,11 +336,11 @@ func TestAuth_FindMany(t *testing.T) { }, }, }, - filter: influxdb.DBRPMappingFilterV2{}, + filter: influxdb.DBRPMappingFilter{}, }, wants: wants{ err: nil, - ms: []*influxdb.DBRPMappingV2{ + ms: []*influxdb.DBRPMapping{ { ID: 1, OrganizationID: 1, @@ -378,7 +378,7 @@ func TestAuth_FindMany(t *testing.T) { t.Errorf("got wrong number back") } influxdbtesting.ErrorsEqual(t, err, tt.wants.err) - if diff := cmp.Diff(tt.wants.ms, gots, influxdbtesting.DBRPMappingCmpOptionsV2...); diff != "" { + if diff := cmp.Diff(tt.wants.ms, gots, influxdbtesting.DBRPMappingCmpOptions...); diff != "" { t.Errorf("unexpected result -want/+got:\n\t%s", diff) } }) @@ -387,10 +387,10 @@ func TestAuth_FindMany(t *testing.T) { func TestAuth_Create(t *testing.T) { type fields struct { - service influxdb.DBRPMappingServiceV2 + service influxdb.DBRPMappingService } type args struct { - m influxdb.DBRPMappingV2 + m influxdb.DBRPMapping permission influxdb.Permission } type wants struct { @@ -406,10 +406,10 @@ func TestAuth_Create(t *testing.T) { { name: "authorized", fields: fields{ - service: &mock.DBRPMappingServiceV2{}, + service: &mock.DBRPMappingService{}, }, args: args{ - m: influxdb.DBRPMappingV2{ + m: influxdb.DBRPMapping{ ID: 1, OrganizationID: 1, BucketID: 2, @@ -430,10 +430,10 @@ func TestAuth_Create(t *testing.T) { { name: "unauthorized", fields: fields{ - service: &mock.DBRPMappingServiceV2{}, + service: &mock.DBRPMappingService{}, }, args: args{ - m: influxdb.DBRPMappingV2{ + m: influxdb.DBRPMapping{ ID: 1, OrganizationID: 1, BucketID: 2, @@ -471,7 +471,7 @@ func TestAuth_Create(t *testing.T) { func TestAuth_Update(t *testing.T) { type fields struct { - service influxdb.DBRPMappingServiceV2 + service influxdb.DBRPMappingService } type args struct { orgID platform.ID @@ -491,7 +491,7 @@ func TestAuth_Update(t *testing.T) { { name: "authorized", fields: fields{ - service: &mock.DBRPMappingServiceV2{}, + service: &mock.DBRPMappingService{}, }, args: args{ permission: influxdb.Permission{ @@ -511,7 +511,7 @@ func TestAuth_Update(t *testing.T) { { name: "unauthorized", fields: fields{ - service: &mock.DBRPMappingServiceV2{}, + service: &mock.DBRPMappingService{}, }, args: args{ permission: influxdb.Permission{ @@ -541,7 +541,7 @@ func TestAuth_Update(t *testing.T) { ctx = influxdbcontext.SetAuthorizer(ctx, mock.NewMockAuthorizer(false, []influxdb.Permission{tt.args.permission})) // Does not matter how we update, we only need to check auth. - err := s.Update(ctx, &influxdb.DBRPMappingV2{ID: tt.args.id, OrganizationID: tt.args.orgID, BucketID: 1}) + err := s.Update(ctx, &influxdb.DBRPMapping{ID: tt.args.id, OrganizationID: tt.args.orgID, BucketID: 1}) influxdbtesting.ErrorsEqual(t, err, tt.wants.err) }) } @@ -549,7 +549,7 @@ func TestAuth_Update(t *testing.T) { func TestAuth_Delete(t *testing.T) { type fields struct { - service influxdb.DBRPMappingServiceV2 + service influxdb.DBRPMappingService } type args struct { orgID platform.ID @@ -569,9 +569,9 @@ func TestAuth_Delete(t *testing.T) { { name: "authorized", fields: fields{ - service: &mock.DBRPMappingServiceV2{ - FindByIDFn: func(_ context.Context, _, _ platform.ID) (*influxdb.DBRPMappingV2, error) { - return &influxdb.DBRPMappingV2{ + service: &mock.DBRPMappingService{ + FindByIDFn: func(_ context.Context, _, _ platform.ID) (*influxdb.DBRPMapping, error) { + return &influxdb.DBRPMapping{ OrganizationID: platform.ID(1), BucketID: platform.ID(1), }, nil @@ -596,9 +596,9 @@ func TestAuth_Delete(t *testing.T) { { name: "unauthorized", fields: fields{ - service: &mock.DBRPMappingServiceV2{ - FindByIDFn: func(_ context.Context, _, _ platform.ID) (*influxdb.DBRPMappingV2, error) { - return &influxdb.DBRPMappingV2{ + service: &mock.DBRPMappingService{ + FindByIDFn: func(_ context.Context, _, _ platform.ID) (*influxdb.DBRPMapping, error) { + return &influxdb.DBRPMapping{ OrganizationID: platform.ID(1), BucketID: platform.ID(1), }, nil diff --git a/dbrp/mocks/dbrp_mapping_service.go b/dbrp/mocks/dbrp_mapping_service.go new file mode 100644 index 00000000000..085790844ee --- /dev/null +++ b/dbrp/mocks/dbrp_mapping_service.go @@ -0,0 +1,114 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/influxdata/influxdb/v2 (interfaces: DBRPMappingService) + +// Package mocks is a generated GoMock package. +package mocks + +import ( + context "context" + gomock "github.com/golang/mock/gomock" + influxdb "github.com/influxdata/influxdb/v2" + "github.com/influxdata/influxdb/v2/kit/platform" + reflect "reflect" +) + +// MockDBRPMappingService is a mock of DBRPMappingService interface +type MockDBRPMappingService struct { + ctrl *gomock.Controller + recorder *MockDBRPMappingServiceMockRecorder +} + +// MockDBRPMappingServiceMockRecorder is the mock recorder for MockDBRPMappingService +type MockDBRPMappingServiceMockRecorder struct { + mock *MockDBRPMappingService +} + +// NewMockDBRPMappingService creates a new mock instance +func NewMockDBRPMappingService(ctrl *gomock.Controller) *MockDBRPMappingService { + mock := &MockDBRPMappingService{ctrl: ctrl} + mock.recorder = &MockDBRPMappingServiceMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockDBRPMappingService) EXPECT() *MockDBRPMappingServiceMockRecorder { + return m.recorder +} + +// Create mocks base method +func (m *MockDBRPMappingService) Create(arg0 context.Context, arg1 *influxdb.DBRPMapping) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Create", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// Create indicates an expected call of Create +func (mr *MockDBRPMappingServiceMockRecorder) Create(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*MockDBRPMappingService)(nil).Create), arg0, arg1) +} + +// Delete mocks base method +func (m *MockDBRPMappingService) Delete(arg0 context.Context, arg1, arg2 platform.ID) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Delete", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 +} + +// Delete indicates an expected call of Delete +func (mr *MockDBRPMappingServiceMockRecorder) Delete(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockDBRPMappingService)(nil).Delete), arg0, arg1, arg2) +} + +// FindByID mocks base method +func (m *MockDBRPMappingService) FindByID(arg0 context.Context, arg1, arg2 platform.ID) (*influxdb.DBRPMapping, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "FindByID", arg0, arg1, arg2) + ret0, _ := ret[0].(*influxdb.DBRPMapping) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// FindByID indicates an expected call of FindByID +func (mr *MockDBRPMappingServiceMockRecorder) FindByID(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindByID", reflect.TypeOf((*MockDBRPMappingService)(nil).FindByID), arg0, arg1, arg2) +} + +// FindMany mocks base method +func (m *MockDBRPMappingService) FindMany(arg0 context.Context, arg1 influxdb.DBRPMappingFilter, arg2 ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) { + m.ctrl.T.Helper() + varargs := []interface{}{arg0, arg1} + for _, a := range arg2 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "FindMany", varargs...) + ret0, _ := ret[0].([]*influxdb.DBRPMapping) + ret1, _ := ret[1].(int) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// FindMany indicates an expected call of FindMany +func (mr *MockDBRPMappingServiceMockRecorder) FindMany(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{arg0, arg1}, arg2...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindMany", reflect.TypeOf((*MockDBRPMappingService)(nil).FindMany), varargs...) +} + +// Update mocks base method +func (m *MockDBRPMappingService) Update(arg0 context.Context, arg1 *influxdb.DBRPMapping) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Update", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// Update indicates an expected call of Update +func (mr *MockDBRPMappingServiceMockRecorder) Update(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockDBRPMappingService)(nil).Update), arg0, arg1) +} diff --git a/dbrp/mocks/dbrp_mapping_service_v2.go b/dbrp/mocks/dbrp_mapping_service_v2.go deleted file mode 100644 index 5e3f1e92d7f..00000000000 --- a/dbrp/mocks/dbrp_mapping_service_v2.go +++ /dev/null @@ -1,114 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: github.com/influxdata/influxdb/v2 (interfaces: DBRPMappingServiceV2) - -// Package mocks is a generated GoMock package. -package mocks - -import ( - context "context" - gomock "github.com/golang/mock/gomock" - influxdb "github.com/influxdata/influxdb/v2" - "github.com/influxdata/influxdb/v2/kit/platform" - reflect "reflect" -) - -// MockDBRPMappingServiceV2 is a mock of DBRPMappingServiceV2 interface -type MockDBRPMappingServiceV2 struct { - ctrl *gomock.Controller - recorder *MockDBRPMappingServiceV2MockRecorder -} - -// MockDBRPMappingServiceV2MockRecorder is the mock recorder for MockDBRPMappingServiceV2 -type MockDBRPMappingServiceV2MockRecorder struct { - mock *MockDBRPMappingServiceV2 -} - -// NewMockDBRPMappingServiceV2 creates a new mock instance -func NewMockDBRPMappingServiceV2(ctrl *gomock.Controller) *MockDBRPMappingServiceV2 { - mock := &MockDBRPMappingServiceV2{ctrl: ctrl} - mock.recorder = &MockDBRPMappingServiceV2MockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use -func (m *MockDBRPMappingServiceV2) EXPECT() *MockDBRPMappingServiceV2MockRecorder { - return m.recorder -} - -// Create mocks base method -func (m *MockDBRPMappingServiceV2) Create(arg0 context.Context, arg1 *influxdb.DBRPMappingV2) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Create", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// Create indicates an expected call of Create -func (mr *MockDBRPMappingServiceV2MockRecorder) Create(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*MockDBRPMappingServiceV2)(nil).Create), arg0, arg1) -} - -// Delete mocks base method -func (m *MockDBRPMappingServiceV2) Delete(arg0 context.Context, arg1, arg2 platform.ID) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Delete", arg0, arg1, arg2) - ret0, _ := ret[0].(error) - return ret0 -} - -// Delete indicates an expected call of Delete -func (mr *MockDBRPMappingServiceV2MockRecorder) Delete(arg0, arg1, arg2 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockDBRPMappingServiceV2)(nil).Delete), arg0, arg1, arg2) -} - -// FindByID mocks base method -func (m *MockDBRPMappingServiceV2) FindByID(arg0 context.Context, arg1, arg2 platform.ID) (*influxdb.DBRPMappingV2, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "FindByID", arg0, arg1, arg2) - ret0, _ := ret[0].(*influxdb.DBRPMappingV2) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// FindByID indicates an expected call of FindByID -func (mr *MockDBRPMappingServiceV2MockRecorder) FindByID(arg0, arg1, arg2 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindByID", reflect.TypeOf((*MockDBRPMappingServiceV2)(nil).FindByID), arg0, arg1, arg2) -} - -// FindMany mocks base method -func (m *MockDBRPMappingServiceV2) FindMany(arg0 context.Context, arg1 influxdb.DBRPMappingFilterV2, arg2 ...influxdb.FindOptions) ([]*influxdb.DBRPMappingV2, int, error) { - m.ctrl.T.Helper() - varargs := []interface{}{arg0, arg1} - for _, a := range arg2 { - varargs = append(varargs, a) - } - ret := m.ctrl.Call(m, "FindMany", varargs...) - ret0, _ := ret[0].([]*influxdb.DBRPMappingV2) - ret1, _ := ret[1].(int) - ret2, _ := ret[2].(error) - return ret0, ret1, ret2 -} - -// FindMany indicates an expected call of FindMany -func (mr *MockDBRPMappingServiceV2MockRecorder) FindMany(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{arg0, arg1}, arg2...) - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindMany", reflect.TypeOf((*MockDBRPMappingServiceV2)(nil).FindMany), varargs...) -} - -// Update mocks base method -func (m *MockDBRPMappingServiceV2) Update(arg0 context.Context, arg1 *influxdb.DBRPMappingV2) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Update", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// Update indicates an expected call of Update -func (mr *MockDBRPMappingServiceV2MockRecorder) Update(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockDBRPMappingServiceV2)(nil).Update), arg0, arg1) -} diff --git a/dbrp/service.go b/dbrp/service.go index 2ed92d84d5b..42cc5185eb7 100644 --- a/dbrp/service.go +++ b/dbrp/service.go @@ -43,7 +43,7 @@ var ( defaultBucket = []byte("dbrpdefaultv1") ) -var _ influxdb.DBRPMappingServiceV2 = (*AuthorizedService)(nil) +var _ influxdb.DBRPMappingService = (*AuthorizedService)(nil) type Service struct { store kv.Store @@ -54,7 +54,7 @@ type Service struct { byOrg *kv.Index } -func indexForeignKey(dbrp influxdb.DBRPMappingV2) []byte { +func indexForeignKey(dbrp influxdb.DBRPMapping) []byte { return composeForeignKey(dbrp.OrganizationID, dbrp.Database) } @@ -66,13 +66,13 @@ func composeForeignKey(orgID platform.ID, db string) []byte { return key } -func NewService(ctx context.Context, bucketSvc influxdb.BucketService, st kv.Store) influxdb.DBRPMappingServiceV2 { +func NewService(ctx context.Context, bucketSvc influxdb.BucketService, st kv.Store) influxdb.DBRPMappingService { return &Service{ store: st, IDGen: snowflake.NewDefaultIDGenerator(), bucketSvc: bucketSvc, byOrgAndDatabase: kv.NewIndex(kv.NewIndexMapping(bucket, indexBucket, func(v []byte) ([]byte, error) { - var dbrp influxdb.DBRPMappingV2 + var dbrp influxdb.DBRPMapping if err := json.Unmarshal(v, &dbrp); err != nil { return nil, err } @@ -178,10 +178,10 @@ func (s *Service) getFirstBut(tx kv.Tx, compKey []byte, skipID []byte) (next []b } // isDBRPUnique verifies if the triple orgID-database-retention-policy is unique. -func (s *Service) isDBRPUnique(ctx context.Context, m influxdb.DBRPMappingV2) error { +func (s *Service) isDBRPUnique(ctx context.Context, m influxdb.DBRPMapping) error { return s.store.View(ctx, func(tx kv.Tx) error { return s.byOrgAndDatabase.Walk(ctx, tx, composeForeignKey(m.OrganizationID, m.Database), func(k, v []byte) (bool, error) { - dbrp := &influxdb.DBRPMappingV2{} + dbrp := &influxdb.DBRPMapping{} if err := json.Unmarshal(v, dbrp); err != nil { return false, ErrInternalService(err) } @@ -202,13 +202,13 @@ func (s *Service) isDBRPUnique(ctx context.Context, m influxdb.DBRPMappingV2) er } // FindBy returns the mapping for the given ID. -func (s *Service) FindByID(ctx context.Context, orgID, id platform.ID) (*influxdb.DBRPMappingV2, error) { +func (s *Service) FindByID(ctx context.Context, orgID, id platform.ID) (*influxdb.DBRPMapping, error) { encodedID, err := id.Encode() if err != nil { return nil, ErrInvalidDBRPID(id.String(), err) } - m := &influxdb.DBRPMappingV2{} + m := &influxdb.DBRPMapping{} if err := s.store.View(ctx, func(tx kv.Tx) error { bucket, err := tx.Bucket(bucket) if err != nil { @@ -239,7 +239,7 @@ func (s *Service) FindByID(ctx context.Context, orgID, id platform.ID) (*influxd // FindMany returns a list of mappings that match filter and the total count of matching dbrp mappings. // TODO(affo): find a smart way to apply FindOptions to a list of items. -func (s *Service) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilterV2, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMappingV2, int, error) { +func (s *Service) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilter, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) { // Memoize default IDs. defs := make(map[string]*platform.ID) get := func(tx kv.Tx, orgID platform.ID, db string) (*platform.ID, error) { @@ -258,10 +258,10 @@ func (s *Service) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilte return defs[k], nil } - ms := []*influxdb.DBRPMappingV2{} + ms := []*influxdb.DBRPMapping{} add := func(tx kv.Tx) func(k, v []byte) (bool, error) { return func(k, v []byte) (bool, error) { - m := influxdb.DBRPMappingV2{} + m := influxdb.DBRPMapping{} if err := json.Unmarshal(v, &m); err != nil { return false, ErrInternalService(err) } @@ -342,7 +342,7 @@ func (s *Service) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilte // Create creates a new mapping. // If another mapping with same organization ID, database, and retention policy exists, an error is returned. // If the mapping already contains a valid ID, that one is used for storing the mapping. -func (s *Service) Create(ctx context.Context, dbrp *influxdb.DBRPMappingV2) error { +func (s *Service) Create(ctx context.Context, dbrp *influxdb.DBRPMapping) error { if !dbrp.ID.Valid() { dbrp.ID = s.IDGen.ID() } @@ -415,7 +415,7 @@ func (s *Service) Create(ctx context.Context, dbrp *influxdb.DBRPMappingV2) erro // Updates a mapping. // If another mapping with same organization ID, database, and retention policy exists, an error is returned. // Un-setting `Default` for a mapping will cause the first one to become the default. -func (s *Service) Update(ctx context.Context, dbrp *influxdb.DBRPMappingV2) error { +func (s *Service) Update(ctx context.Context, dbrp *influxdb.DBRPMapping) error { if err := dbrp.Validate(); err != nil { return ErrInvalidDBRP(err) } @@ -524,7 +524,7 @@ func (s *Service) Delete(ctx context.Context, orgID, id platform.ID) error { // filterFunc is capable to validate if the dbrp is valid from a given filter. // it runs true if the filtering data are contained in the dbrp. -func filterFunc(dbrp *influxdb.DBRPMappingV2, filter influxdb.DBRPMappingFilterV2) bool { +func filterFunc(dbrp *influxdb.DBRPMapping, filter influxdb.DBRPMappingFilter) bool { return (filter.ID == nil || (*filter.ID) == dbrp.ID) && (filter.OrgID == nil || (*filter.OrgID) == dbrp.OrganizationID) && (filter.BucketID == nil || (*filter.BucketID) == dbrp.BucketID) && diff --git a/dbrp/service_test.go b/dbrp/service_test.go index cffe5840f2d..3ff539718b0 100644 --- a/dbrp/service_test.go +++ b/dbrp/service_test.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "github.com/influxdata/influxdb/v2/kit/platform" "io/ioutil" "os" "testing" @@ -12,6 +11,7 @@ import ( "github.com/influxdata/influxdb/v2" "github.com/influxdata/influxdb/v2/bolt" "github.com/influxdata/influxdb/v2/dbrp" + "github.com/influxdata/influxdb/v2/kit/platform" "github.com/influxdata/influxdb/v2/kv" "github.com/influxdata/influxdb/v2/kv/migration/all" "github.com/influxdata/influxdb/v2/mock" @@ -48,7 +48,7 @@ func NewTestBoltStore(t *testing.T) (kv.Store, func(), error) { return s, close, nil } -func initDBRPMappingService(f itesting.DBRPMappingFieldsV2, t *testing.T) (influxdb.DBRPMappingServiceV2, func()) { +func initDBRPMappingService(f itesting.DBRPMappingFields, t *testing.T) (influxdb.DBRPMappingService, func()) { s, closeStore, err := NewTestBoltStore(t) if err != nil { t.Fatalf("failed to create new bolt kv store: %v", err) @@ -79,7 +79,7 @@ func initDBRPMappingService(f itesting.DBRPMappingFieldsV2, t *testing.T) (influ } } -func TestBoltDBRPMappingServiceV2(t *testing.T) { +func TestBoltDBRPMappingService(t *testing.T) { t.Parallel() - itesting.DBRPMappingServiceV2(initDBRPMappingService, t) + itesting.DBRPMappingService(initDBRPMappingService, t) } diff --git a/dbrp_mapping.go b/dbrp_mapping.go index e7b045d161d..de21d7fd53c 100644 --- a/dbrp_mapping.go +++ b/dbrp_mapping.go @@ -10,25 +10,25 @@ import ( "github.com/influxdata/influxdb/v2/kit/platform/errors" ) -// DBRPMappingServiceV2 provides CRUD to DBRPMappingV2s. -type DBRPMappingServiceV2 interface { +// DBRPMappingService provides CRUD to DBRPMappingV2s. +type DBRPMappingService interface { // FindBy returns the dbrp mapping for the specified ID. // Requires orgID because every resource will be org-scoped. - FindByID(ctx context.Context, orgID, id platform.ID) (*DBRPMappingV2, error) + FindByID(ctx context.Context, orgID, id platform.ID) (*DBRPMapping, error) // FindMany returns a list of dbrp mappings that match filter and the total count of matching dbrp mappings. - FindMany(ctx context.Context, dbrp DBRPMappingFilterV2, opts ...FindOptions) ([]*DBRPMappingV2, int, error) + FindMany(ctx context.Context, dbrp DBRPMappingFilter, opts ...FindOptions) ([]*DBRPMapping, int, error) // Create creates a new dbrp mapping, if a different mapping exists an error is returned. - Create(ctx context.Context, dbrp *DBRPMappingV2) error + Create(ctx context.Context, dbrp *DBRPMapping) error // Update a new dbrp mapping - Update(ctx context.Context, dbrp *DBRPMappingV2) error + Update(ctx context.Context, dbrp *DBRPMapping) error // Delete removes a dbrp mapping. // Deleting a mapping that does not exists is not an error. // Requires orgID because every resource will be org-scoped. Delete(ctx context.Context, orgID, id platform.ID) error } -// DBRPMappingV2 represents a mapping of a database and retention policy to an organization ID and bucket ID. -type DBRPMappingV2 struct { +// DBRPMapping represents a mapping of a database and retention policy to an organization ID and bucket ID. +type DBRPMapping struct { ID platform.ID `json:"id"` Database string `json:"database"` RetentionPolicy string `json:"retention_policy"` @@ -41,7 +41,7 @@ type DBRPMappingV2 struct { } // Validate reports any validation errors for the mapping. -func (m DBRPMappingV2) Validate() error { +func (m DBRPMapping) Validate() error { if !validName(m.Database) { return &errors.Error{ Code: errors.EInvalid, @@ -70,7 +70,7 @@ func (m DBRPMappingV2) Validate() error { } // Equal checks if the two mappings are identical. -func (m *DBRPMappingV2) Equal(o *DBRPMappingV2) bool { +func (m *DBRPMapping) Equal(o *DBRPMapping) bool { if m == o { return true } @@ -90,8 +90,8 @@ func (m *DBRPMappingV2) Equal(o *DBRPMappingV2) bool { m.BucketID == o.BucketID } -// DBRPMappingFilterV2 represents a set of filters that restrict the returned results. -type DBRPMappingFilterV2 struct { +// DBRPMappingFilter represents a set of filters that restrict the returned results. +type DBRPMappingFilter struct { ID *platform.ID OrgID *platform.ID BucketID *platform.ID @@ -101,7 +101,7 @@ type DBRPMappingFilterV2 struct { Default *bool } -func (f DBRPMappingFilterV2) String() string { +func (f DBRPMappingFilter) String() string { var s strings.Builder s.WriteString("{ id:") @@ -149,69 +149,6 @@ func (f DBRPMappingFilterV2) String() string { return s.String() } -// DBRPMappingService provides a mapping of cluster, database and retention policy to an organization ID and bucket ID. -type DBRPMappingService interface { - // FindBy returns the dbrp mapping the for cluster, db and rp. - FindBy(ctx context.Context, cluster, db, rp string) (*DBRPMapping, error) - // Find returns the first dbrp mapping the matches the filter. - Find(ctx context.Context, filter DBRPMappingFilter) (*DBRPMapping, error) - // FindMany returns a list of dbrp mappings that match filter and the total count of matching dbrp mappings. - FindMany(ctx context.Context, filter DBRPMappingFilter, opt ...FindOptions) ([]*DBRPMapping, int, error) - // Create creates a new dbrp mapping, if a different mapping exists an error is returned. - Create(ctx context.Context, dbrpMap *DBRPMapping) error - // Delete removes a dbrp mapping. - // Deleting a mapping that does not exists is not an error. - Delete(ctx context.Context, cluster, db, rp string) error -} - -// DBRPMapping represents a mapping of a cluster, database and retention policy to an organization ID and bucket ID. -type DBRPMapping struct { - Cluster string `json:"cluster"` - Database string `json:"database"` - RetentionPolicy string `json:"retention_policy"` - - // Default indicates if this mapping is the default for the cluster and database. - Default bool `json:"default"` - - OrganizationID platform.ID `json:"organization_id"` - BucketID platform.ID `json:"bucket_id"` -} - -// Validate reports any validation errors for the mapping. -func (m DBRPMapping) Validate() error { - if !validName(m.Cluster) { - return &errors.Error{ - Code: errors.EInvalid, - Msg: "cluster must contain at least one character and only be letters, numbers, '_', '-', and '.'", - } - } - if !validName(m.Database) { - return &errors.Error{ - Code: errors.EInvalid, - Msg: "database must contain at least one character and only be letters, numbers, '_', '-', and '.'", - } - } - if !validName(m.RetentionPolicy) { - return &errors.Error{ - Code: errors.EInvalid, - Msg: "retentionPolicy must contain at least one character and only be letters, numbers, '_', '-', and '.'", - } - } - if !m.OrganizationID.Valid() { - return &errors.Error{ - Code: errors.EInvalid, - Msg: "organizationID is required", - } - } - if !m.BucketID.Valid() { - return &errors.Error{ - Code: errors.EInvalid, - Msg: "bucketID is required", - } - } - return nil -} - // validName checks to see if the given name can would be valid for DB/RP name func validName(name string) bool { for _, r := range name { @@ -224,65 +161,3 @@ func validName(name string) bool { name != ".." && !strings.ContainsAny(name, `/\`) } - -// Equal checks if the two mappings are identical. -func (m *DBRPMapping) Equal(o *DBRPMapping) bool { - if m == o { - return true - } - if m == nil || o == nil { - return false - } - return m.Cluster == o.Cluster && - m.Database == o.Database && - m.RetentionPolicy == o.RetentionPolicy && - m.Default == o.Default && - m.OrganizationID.Valid() && - o.OrganizationID.Valid() && - m.BucketID.Valid() && - o.BucketID.Valid() && - m.OrganizationID == o.OrganizationID && - m.BucketID == o.BucketID -} - -// DBRPMappingFilter represents a set of filters that restrict the returned results by cluster, database and retention policy. -type DBRPMappingFilter struct { - Cluster *string - Database *string - RetentionPolicy *string - Default *bool -} - -func (f DBRPMappingFilter) String() string { - var s strings.Builder - s.WriteString("{") - - s.WriteString("cluster:") - if f.Cluster != nil { - s.WriteString(*f.Cluster) - } else { - s.WriteString("") - } - s.WriteString(" db:") - if f.Database != nil { - s.WriteString(*f.Database) - } else { - s.WriteString("") - } - - s.WriteString(" rp:") - if f.RetentionPolicy != nil { - s.WriteString(*f.RetentionPolicy) - } else { - s.WriteString("") - } - - s.WriteString(" default:") - if f.Default != nil { - s.WriteString(strconv.FormatBool(*f.Default)) - } else { - s.WriteString("") - } - s.WriteString("}") - return s.String() -} diff --git a/dbrp_mapping_test.go b/dbrp_mapping_test.go index a5a882a1a4b..20c3dc9a914 100644 --- a/dbrp_mapping_test.go +++ b/dbrp_mapping_test.go @@ -1,16 +1,15 @@ package influxdb_test import ( - platform2 "github.com/influxdata/influxdb/v2/kit/platform" "testing" platform "github.com/influxdata/influxdb/v2" + platform2 "github.com/influxdata/influxdb/v2/kit/platform" platformtesting "github.com/influxdata/influxdb/v2/testing" ) func TestDBRPMapping_Validate(t *testing.T) { type fields struct { - Cluster string Database string RetentionPolicy string Default bool @@ -22,17 +21,9 @@ func TestDBRPMapping_Validate(t *testing.T) { fields fields wantErr bool }{ - { - name: "mapping requires a cluster", - fields: fields{ - Cluster: "", - }, - wantErr: true, - }, { name: "mapping requires a database", fields: fields{ - Cluster: "abc", Database: "", }, wantErr: true, @@ -40,7 +31,6 @@ func TestDBRPMapping_Validate(t *testing.T) { { name: "mapping requires an rp", fields: fields{ - Cluster: "abc", Database: "telegraf", RetentionPolicy: "", }, @@ -49,7 +39,6 @@ func TestDBRPMapping_Validate(t *testing.T) { { name: "mapping requires an orgid", fields: fields{ - Cluster: "abc", Database: "telegraf", RetentionPolicy: "autogen", }, @@ -58,24 +47,15 @@ func TestDBRPMapping_Validate(t *testing.T) { { name: "mapping requires a bucket id", fields: fields{ - Cluster: "abc", Database: "telegraf", RetentionPolicy: "autogen", OrganizationID: platformtesting.MustIDBase16("debac1e0deadbeef"), }, wantErr: true, }, - { - name: "cluster name cannot have non-printable characters.", - fields: fields{ - Cluster: string([]byte{0x0D}), - }, - wantErr: true, - }, { name: "db cannot have non-letters/numbers/_/./-", fields: fields{ - Cluster: "12345_.", Database: string([]byte{0x0D}), }, wantErr: true, @@ -83,7 +63,6 @@ func TestDBRPMapping_Validate(t *testing.T) { { name: "rp cannot have non-printable characters", fields: fields{ - Cluster: "12345", Database: "telegraf", RetentionPolicy: string([]byte{0x0D}), }, @@ -92,7 +71,6 @@ func TestDBRPMapping_Validate(t *testing.T) { { name: "dash accepted as valid database", fields: fields{ - Cluster: "12345_.", Database: "howdy-doody", RetentionPolicy: "autogen", OrganizationID: platformtesting.MustIDBase16("debac1e0deadbeef"), @@ -103,7 +81,6 @@ func TestDBRPMapping_Validate(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { m := platform.DBRPMapping{ - Cluster: tt.fields.Cluster, Database: tt.fields.Database, RetentionPolicy: tt.fields.RetentionPolicy, Default: tt.fields.Default, diff --git a/http/api_handler.go b/http/api_handler.go index 4fb73baca98..ff3e4f474e9 100644 --- a/http/api_handler.go +++ b/http/api_handler.go @@ -73,7 +73,7 @@ type APIBackend struct { PasswordV1Service influxdb.PasswordsService AuthorizerV1 influxdb.AuthorizerV1 OnboardingService influxdb.OnboardingService - DBRPService influxdb.DBRPMappingServiceV2 + DBRPService influxdb.DBRPMappingService BucketService influxdb.BucketService SessionService influxdb.SessionService UserService influxdb.UserService diff --git a/http/client.go b/http/client.go index c8bdbe58d4e..bf59135ce6d 100644 --- a/http/client.go +++ b/http/client.go @@ -50,7 +50,7 @@ type Service struct { *NotificationEndpointService *TelegrafService *LabelService - DBRPMappingServiceV2 *dbrp.Client + DBRPMappingService *dbrp.Client } // NewService returns a service that is an HTTP client to a remote. @@ -80,7 +80,7 @@ func NewService(httpClient *httpc.Client, addr, token string) (*Service, error) NotificationEndpointService: &NotificationEndpointService{Client: httpClient}, TelegrafService: NewTelegrafService(httpClient), LabelService: &LabelService{Client: httpClient}, - DBRPMappingServiceV2: dbrp.NewClient(httpClient), + DBRPMappingService: dbrp.NewClient(httpClient), }, nil } diff --git a/http/legacy.go b/http/legacy.go index d12fe7c5516..458845b3019 100644 --- a/http/legacy.go +++ b/http/legacy.go @@ -15,7 +15,7 @@ func newLegacyBackend(b *APIBackend) *legacy.Backend { OrganizationService: b.OrganizationService, BucketService: b.BucketService, PointsWriter: b.PointsWriter, - DBRPMappingServiceV2: b.DBRPService, + DBRPMappingService: b.DBRPService, ProxyQueryService: b.InfluxQLService, InfluxqldQueryService: b.InfluxqldService, WriteEventRecorder: b.WriteEventRecorder, diff --git a/http/legacy/backend.go b/http/legacy/backend.go index 287e2fa4431..f8d6ef2c076 100644 --- a/http/legacy/backend.go +++ b/http/legacy/backend.go @@ -33,7 +33,7 @@ type Backend struct { OrganizationService influxdb.OrganizationService BucketService influxdb.BucketService PointsWriter storage.PointsWriter - DBRPMappingServiceV2 influxdb.DBRPMappingServiceV2 + DBRPMappingService influxdb.DBRPMappingService ProxyQueryService query.ProxyQueryService InfluxqldQueryService influxql.ProxyQueryService } diff --git a/http/legacy/write_handler.go b/http/legacy/write_handler.go index b0281f80de7..ed81d7eff0c 100644 --- a/http/legacy/write_handler.go +++ b/http/legacy/write_handler.go @@ -34,7 +34,7 @@ type PointsWriterBackend struct { EventRecorder metric.EventRecorder BucketService influxdb.BucketService PointsWriter storage.PointsWriter - DBRPMappingService influxdb.DBRPMappingServiceV2 + DBRPMappingService influxdb.DBRPMappingService } // NewPointsWriterBackend creates a new backend for legacy work. @@ -45,7 +45,7 @@ func NewPointsWriterBackend(b *Backend) *PointsWriterBackend { EventRecorder: b.WriteEventRecorder, BucketService: b.BucketService, PointsWriter: b.PointsWriter, - DBRPMappingService: b.DBRPMappingServiceV2, + DBRPMappingService: b.DBRPMappingService, } } @@ -55,7 +55,7 @@ type WriteHandler struct { EventRecorder metric.EventRecorder BucketService influxdb.BucketService PointsWriter storage.PointsWriter - DBRPMappingService influxdb.DBRPMappingServiceV2 + DBRPMappingService influxdb.DBRPMappingService router *httprouter.Router logger *zap.Logger @@ -201,10 +201,10 @@ func checkBucketWritePermissions(auth influxdb.Authorizer, orgID, bucketID platf return nil } -// findMapping finds a DBRPMappingV2 for the database and retention policy +// findMapping finds a DBRPMapping for the database and retention policy // combination. -func (h *WriteHandler) findMapping(ctx context.Context, orgID platform.ID, db, rp string) (*influxdb.DBRPMappingV2, error) { - filter := influxdb.DBRPMappingFilterV2{ +func (h *WriteHandler) findMapping(ctx context.Context, orgID platform.ID, db, rp string) (*influxdb.DBRPMapping, error) { + filter := influxdb.DBRPMappingFilter{ OrgID: &orgID, Database: &db, } diff --git a/http/legacy/write_handler_test.go b/http/legacy/write_handler_test.go index 973d73642ba..ebe071ceec1 100644 --- a/http/legacy/write_handler_test.go +++ b/http/legacy/write_handler_test.go @@ -35,7 +35,7 @@ func TestWriteHandler_BucketAndMappingExistsDefaultRP(t *testing.T) { var ( // Mocked Services eventRecorder = mocks.NewMockEventRecorder(ctrl) - dbrpMappingSvc = mocks.NewMockDBRPMappingServiceV2(ctrl) + dbrpMappingSvc = mocks.NewMockDBRPMappingService(ctrl) bucketService = mocks.NewMockBucketService(ctrl) pointsWriter = mocks.NewMockPointsWriter(ctrl) @@ -48,7 +48,7 @@ func TestWriteHandler_BucketAndMappingExistsDefaultRP(t *testing.T) { RetentionPolicyName: "autogen", RetentionPeriod: 72 * time.Hour, } - mapping = &influxdb.DBRPMappingV2{ + mapping = &influxdb.DBRPMapping{ OrganizationID: orgID, BucketID: bucket.ID, Database: "mydb", @@ -61,11 +61,11 @@ func TestWriteHandler_BucketAndMappingExistsDefaultRP(t *testing.T) { findAutogenMapping := dbrpMappingSvc. EXPECT(). - FindMany(gomock.Any(), influxdb.DBRPMappingFilterV2{ + FindMany(gomock.Any(), influxdb.DBRPMappingFilter{ OrgID: &mapping.OrganizationID, Database: &mapping.Database, Default: &mapping.Default, - }).Return([]*influxdb.DBRPMappingV2{mapping}, 1, nil) + }).Return([]*influxdb.DBRPMapping{mapping}, 1, nil) findBucketByID := bucketService. EXPECT(). @@ -116,7 +116,7 @@ func TestWriteHandler_BucketAndMappingExistsSpecificRP(t *testing.T) { var ( // Mocked Services eventRecorder = mocks.NewMockEventRecorder(ctrl) - dbrpMappingSvc = mocks.NewMockDBRPMappingServiceV2(ctrl) + dbrpMappingSvc = mocks.NewMockDBRPMappingService(ctrl) bucketService = mocks.NewMockBucketService(ctrl) pointsWriter = mocks.NewMockPointsWriter(ctrl) @@ -129,7 +129,7 @@ func TestWriteHandler_BucketAndMappingExistsSpecificRP(t *testing.T) { RetentionPolicyName: "autogen", RetentionPeriod: 72 * time.Hour, } - mapping = &influxdb.DBRPMappingV2{ + mapping = &influxdb.DBRPMapping{ OrganizationID: orgID, BucketID: bucket.ID, Database: "mydb", @@ -142,11 +142,11 @@ func TestWriteHandler_BucketAndMappingExistsSpecificRP(t *testing.T) { findAutogenMapping := dbrpMappingSvc. EXPECT(). - FindMany(gomock.Any(), influxdb.DBRPMappingFilterV2{ + FindMany(gomock.Any(), influxdb.DBRPMappingFilter{ OrgID: &mapping.OrganizationID, Database: &mapping.Database, RetentionPolicy: &mapping.RetentionPolicy, - }).Return([]*influxdb.DBRPMappingV2{mapping}, 1, nil) + }).Return([]*influxdb.DBRPMapping{mapping}, 1, nil) findBucketByID := bucketService. EXPECT(). @@ -197,7 +197,7 @@ func TestWriteHandler_PartialWrite(t *testing.T) { var ( // Mocked Services eventRecorder = mocks.NewMockEventRecorder(ctrl) - dbrpMappingSvc = mocks.NewMockDBRPMappingServiceV2(ctrl) + dbrpMappingSvc = mocks.NewMockDBRPMappingService(ctrl) bucketService = mocks.NewMockBucketService(ctrl) pointsWriter = mocks.NewMockPointsWriter(ctrl) @@ -210,7 +210,7 @@ func TestWriteHandler_PartialWrite(t *testing.T) { RetentionPolicyName: "autogen", RetentionPeriod: 72 * time.Hour, } - mapping = &influxdb.DBRPMappingV2{ + mapping = &influxdb.DBRPMapping{ OrganizationID: orgID, BucketID: bucket.ID, Database: "mydb", @@ -223,11 +223,11 @@ func TestWriteHandler_PartialWrite(t *testing.T) { findAutogenMapping := dbrpMappingSvc. EXPECT(). - FindMany(gomock.Any(), influxdb.DBRPMappingFilterV2{ + FindMany(gomock.Any(), influxdb.DBRPMappingFilter{ OrgID: &mapping.OrganizationID, Database: &mapping.Database, RetentionPolicy: &mapping.RetentionPolicy, - }).Return([]*influxdb.DBRPMappingV2{mapping}, 1, nil) + }).Return([]*influxdb.DBRPMapping{mapping}, 1, nil) findBucketByID := bucketService. EXPECT(). @@ -279,7 +279,7 @@ func TestWriteHandler_BucketAndMappingExistsNoPermissions(t *testing.T) { var ( // Mocked Services eventRecorder = mocks.NewMockEventRecorder(ctrl) - dbrpMappingSvc = mocks.NewMockDBRPMappingServiceV2(ctrl) + dbrpMappingSvc = mocks.NewMockDBRPMappingService(ctrl) bucketService = mocks.NewMockBucketService(ctrl) pointsWriter = mocks.NewMockPointsWriter(ctrl) @@ -292,7 +292,7 @@ func TestWriteHandler_BucketAndMappingExistsNoPermissions(t *testing.T) { RetentionPolicyName: "autogen", RetentionPeriod: 72 * time.Hour, } - mapping = &influxdb.DBRPMappingV2{ + mapping = &influxdb.DBRPMapping{ OrganizationID: orgID, BucketID: bucket.ID, Database: "mydb", @@ -305,11 +305,11 @@ func TestWriteHandler_BucketAndMappingExistsNoPermissions(t *testing.T) { findAutogenMapping := dbrpMappingSvc. EXPECT(). - FindMany(gomock.Any(), influxdb.DBRPMappingFilterV2{ + FindMany(gomock.Any(), influxdb.DBRPMappingFilter{ OrgID: &mapping.OrganizationID, Database: &mapping.Database, Default: &mapping.Default, - }).Return([]*influxdb.DBRPMappingV2{mapping}, 1, nil) + }).Return([]*influxdb.DBRPMapping{mapping}, 1, nil) findBucketByID := bucketService. EXPECT(). @@ -354,7 +354,7 @@ func TestWriteHandler_MappingNotExists(t *testing.T) { var ( // Mocked Services eventRecorder = mocks.NewMockEventRecorder(ctrl) - dbrpMappingSvc = mocks.NewMockDBRPMappingServiceV2(ctrl) + dbrpMappingSvc = mocks.NewMockDBRPMappingService(ctrl) bucketService = mocks.NewMockBucketService(ctrl) pointsWriter = mocks.NewMockPointsWriter(ctrl) @@ -367,7 +367,7 @@ func TestWriteHandler_MappingNotExists(t *testing.T) { RetentionPolicyName: "autogen", RetentionPeriod: 72 * time.Hour, } - mapping = &influxdb.DBRPMappingV2{ + mapping = &influxdb.DBRPMapping{ OrganizationID: orgID, BucketID: bucket.ID, Database: "mydb", @@ -380,7 +380,7 @@ func TestWriteHandler_MappingNotExists(t *testing.T) { findAutogenMapping := dbrpMappingSvc. EXPECT(). - FindMany(gomock.Any(), influxdb.DBRPMappingFilterV2{ + FindMany(gomock.Any(), influxdb.DBRPMappingFilter{ OrgID: &mapping.OrganizationID, Database: &mapping.Database, RetentionPolicy: &badRp, diff --git a/http/mocks/dbrp_mapping_service.go b/http/mocks/dbrp_mapping_service.go index 87007cd7167..cacadceb607 100644 --- a/http/mocks/dbrp_mapping_service.go +++ b/http/mocks/dbrp_mapping_service.go @@ -6,6 +6,7 @@ package mocks import ( context "context" + "github.com/influxdata/influxdb/v2/kit/platform" reflect "reflect" gomock "github.com/golang/mock/gomock" @@ -50,47 +51,32 @@ func (mr *MockDBRPMappingServiceMockRecorder) Create(arg0, arg1 interface{}) *go } // Delete mocks base method -func (m *MockDBRPMappingService) Delete(arg0 context.Context, arg1, arg2, arg3 string) error { +func (m *MockDBRPMappingService) Delete(arg0 context.Context, arg1, arg2 platform.ID) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Delete", arg0, arg1, arg2, arg3) + ret := m.ctrl.Call(m, "Delete", arg0, arg1, arg2) ret0, _ := ret[0].(error) return ret0 } // Delete indicates an expected call of Delete -func (mr *MockDBRPMappingServiceMockRecorder) Delete(arg0, arg1, arg2, arg3 interface{}) *gomock.Call { +func (mr *MockDBRPMappingServiceMockRecorder) Delete(arg0, arg1, arg2 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockDBRPMappingService)(nil).Delete), arg0, arg1, arg2, arg3) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockDBRPMappingService)(nil).Delete), arg0, arg1, arg2) } -// Find mocks base method -func (m *MockDBRPMappingService) Find(arg0 context.Context, arg1 influxdb.DBRPMappingFilter) (*influxdb.DBRPMapping, error) { +// FindByID mocks base method +func (m *MockDBRPMappingService) FindByID(arg0 context.Context, arg1, arg2 platform.ID) (*influxdb.DBRPMapping, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Find", arg0, arg1) + ret := m.ctrl.Call(m, "FindByID", arg0, arg1, arg2) ret0, _ := ret[0].(*influxdb.DBRPMapping) ret1, _ := ret[1].(error) return ret0, ret1 } -// Find indicates an expected call of Find -func (mr *MockDBRPMappingServiceMockRecorder) Find(arg0, arg1 interface{}) *gomock.Call { +// FindByID indicates an expected call of FindByID +func (mr *MockDBRPMappingServiceMockRecorder) FindByID(arg0, arg1, arg2 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Find", reflect.TypeOf((*MockDBRPMappingService)(nil).Find), arg0, arg1) -} - -// FindBy mocks base method -func (m *MockDBRPMappingService) FindBy(arg0 context.Context, arg1, arg2, arg3 string) (*influxdb.DBRPMapping, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "FindBy", arg0, arg1, arg2, arg3) - ret0, _ := ret[0].(*influxdb.DBRPMapping) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// FindBy indicates an expected call of FindBy -func (mr *MockDBRPMappingServiceMockRecorder) FindBy(arg0, arg1, arg2, arg3 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindBy", reflect.TypeOf((*MockDBRPMappingService)(nil).FindBy), arg0, arg1, arg2, arg3) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindByID", reflect.TypeOf((*MockDBRPMappingService)(nil).FindByID), arg0, arg1, arg2) } // FindMany mocks base method @@ -113,3 +99,17 @@ func (mr *MockDBRPMappingServiceMockRecorder) FindMany(arg0, arg1 interface{}, a varargs := append([]interface{}{arg0, arg1}, arg2...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindMany", reflect.TypeOf((*MockDBRPMappingService)(nil).FindMany), varargs...) } + +// Update mocks base method +func (m *MockDBRPMappingService) Update(arg0 context.Context, arg1 *influxdb.DBRPMapping) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Update", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// Update indicates an expected call of Update +func (mr *MockDBRPMappingServiceMockRecorder) Update(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockDBRPMappingService)(nil).Update), arg0, arg1) +} diff --git a/http/mocks/dbrp_mapping_service_v2.go b/http/mocks/dbrp_mapping_service_v2.go deleted file mode 100644 index 07af63edb4b..00000000000 --- a/http/mocks/dbrp_mapping_service_v2.go +++ /dev/null @@ -1,115 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: github.com/influxdata/influxdb/v2 (interfaces: DBRPMappingServiceV2) - -// Package mocks is a generated GoMock package. -package mocks - -import ( - context "context" - "github.com/influxdata/influxdb/v2/kit/platform" - reflect "reflect" - - gomock "github.com/golang/mock/gomock" - influxdb "github.com/influxdata/influxdb/v2" -) - -// MockDBRPMappingServiceV2 is a mock of DBRPMappingServiceV2 interface -type MockDBRPMappingServiceV2 struct { - ctrl *gomock.Controller - recorder *MockDBRPMappingServiceV2MockRecorder -} - -// MockDBRPMappingServiceV2MockRecorder is the mock recorder for MockDBRPMappingServiceV2 -type MockDBRPMappingServiceV2MockRecorder struct { - mock *MockDBRPMappingServiceV2 -} - -// NewMockDBRPMappingServiceV2 creates a new mock instance -func NewMockDBRPMappingServiceV2(ctrl *gomock.Controller) *MockDBRPMappingServiceV2 { - mock := &MockDBRPMappingServiceV2{ctrl: ctrl} - mock.recorder = &MockDBRPMappingServiceV2MockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use -func (m *MockDBRPMappingServiceV2) EXPECT() *MockDBRPMappingServiceV2MockRecorder { - return m.recorder -} - -// Create mocks base method -func (m *MockDBRPMappingServiceV2) Create(arg0 context.Context, arg1 *influxdb.DBRPMappingV2) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Create", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// Create indicates an expected call of Create -func (mr *MockDBRPMappingServiceV2MockRecorder) Create(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*MockDBRPMappingServiceV2)(nil).Create), arg0, arg1) -} - -// Delete mocks base method -func (m *MockDBRPMappingServiceV2) Delete(arg0 context.Context, arg1, arg2 platform.ID) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Delete", arg0, arg1, arg2) - ret0, _ := ret[0].(error) - return ret0 -} - -// Delete indicates an expected call of Delete -func (mr *MockDBRPMappingServiceV2MockRecorder) Delete(arg0, arg1, arg2 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockDBRPMappingServiceV2)(nil).Delete), arg0, arg1, arg2) -} - -// FindByID mocks base method -func (m *MockDBRPMappingServiceV2) FindByID(arg0 context.Context, arg1, arg2 platform.ID) (*influxdb.DBRPMappingV2, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "FindByID", arg0, arg1, arg2) - ret0, _ := ret[0].(*influxdb.DBRPMappingV2) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// FindByID indicates an expected call of FindByID -func (mr *MockDBRPMappingServiceV2MockRecorder) FindByID(arg0, arg1, arg2 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindByID", reflect.TypeOf((*MockDBRPMappingServiceV2)(nil).FindByID), arg0, arg1, arg2) -} - -// FindMany mocks base method -func (m *MockDBRPMappingServiceV2) FindMany(arg0 context.Context, arg1 influxdb.DBRPMappingFilterV2, arg2 ...influxdb.FindOptions) ([]*influxdb.DBRPMappingV2, int, error) { - m.ctrl.T.Helper() - varargs := []interface{}{arg0, arg1} - for _, a := range arg2 { - varargs = append(varargs, a) - } - ret := m.ctrl.Call(m, "FindMany", varargs...) - ret0, _ := ret[0].([]*influxdb.DBRPMappingV2) - ret1, _ := ret[1].(int) - ret2, _ := ret[2].(error) - return ret0, ret1, ret2 -} - -// FindMany indicates an expected call of FindMany -func (mr *MockDBRPMappingServiceV2MockRecorder) FindMany(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{arg0, arg1}, arg2...) - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindMany", reflect.TypeOf((*MockDBRPMappingServiceV2)(nil).FindMany), varargs...) -} - -// Update mocks base method -func (m *MockDBRPMappingServiceV2) Update(arg0 context.Context, arg1 *influxdb.DBRPMappingV2) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Update", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// Update indicates an expected call of Update -func (mr *MockDBRPMappingServiceV2MockRecorder) Update(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockDBRPMappingServiceV2)(nil).Update), arg0, arg1) -} diff --git a/influxql/v1tests/query_test.go b/influxql/v1tests/query_test.go index 34fb9c064bd..eafc811a0a9 100644 --- a/influxql/v1tests/query_test.go +++ b/influxql/v1tests/query_test.go @@ -161,8 +161,8 @@ func TestServer_Query_ShowDatabases(t *testing.T) { require.NoError(t, err) err = s.Launcher. - DBRPMappingServiceV2(). - Create(ctx, &influxdb.DBRPMappingV2{ + DBRPMappingService(). + Create(ctx, &influxdb.DBRPMapping{ Database: bi.db, RetentionPolicy: bi.rp, Default: true, diff --git a/influxql/v1tests/server_helpers.go b/influxql/v1tests/server_helpers.go index f11ee9d114d..6c38caabca1 100644 --- a/influxql/v1tests/server_helpers.go +++ b/influxql/v1tests/server_helpers.go @@ -173,8 +173,8 @@ func (qt *Test) init(ctx context.Context, t *testing.T, p *tests.DefaultPipeline if !qt.noDefaultMapping { ctx = icontext.SetAuthorizer(ctx, auth) err := p.Launcher. - DBRPMappingServiceV2(). - Create(ctx, &influxdb.DBRPMappingV2{ + DBRPMappingService(). + Create(ctx, &influxdb.DBRPMapping{ Database: qt.db, RetentionPolicy: qt.rp, Default: true, diff --git a/influxql/v1validation/validation_test.go b/influxql/v1validation/validation_test.go index 0dd5723a582..0abcd9868ff 100644 --- a/influxql/v1validation/validation_test.go +++ b/influxql/v1validation/validation_test.go @@ -154,7 +154,7 @@ func validate(t *testing.T, gf *TestSuite) { ctx = icontext.SetAuthorizer(ctx, tests.MakeAuthorization(p.DefaultOrgID, p.DefaultUserID, influxdb.OperPermissions())) - if err := p.Launcher.DBRPMappingServiceV2().Create(ctx, &influxdb.DBRPMappingV2{ + if err := p.Launcher.DBRPMappingService().Create(ctx, &influxdb.DBRPMapping{ Database: "mydb", RetentionPolicy: "autogen", Default: true, diff --git a/inmem/dbrp_mapping_service.go b/inmem/dbrp_mapping_service.go deleted file mode 100644 index c951e646332..00000000000 --- a/inmem/dbrp_mapping_service.go +++ /dev/null @@ -1,160 +0,0 @@ -package inmem - -import ( - "context" - "fmt" - "path" - - "github.com/influxdata/influxdb/v2/kit/platform/errors" - - "github.com/influxdata/influxdb/v2" -) - -var ( - errDBRPMappingNotFound = &errors.Error{ - Code: errors.ENotFound, - Msg: "dbrp mapping not found", - } -) - -func encodeDBRPMappingKey(cluster, db, rp string) string { - return path.Join(cluster, db, rp) -} - -func (s *Service) loadDBRPMapping(ctx context.Context, cluster, db, rp string) (*influxdb.DBRPMapping, error) { - i, ok := s.dbrpMappingKV.Load(encodeDBRPMappingKey(cluster, db, rp)) - if !ok { - return nil, errDBRPMappingNotFound - } - - m, ok := i.(influxdb.DBRPMapping) - if !ok { - return nil, fmt.Errorf("type %T is not a dbrp mapping", i) - } - - return &m, nil -} - -// FindBy returns a single dbrp mapping by cluster, db and rp. -func (s *Service) FindBy(ctx context.Context, cluster, db, rp string) (*influxdb.DBRPMapping, error) { - return s.loadDBRPMapping(ctx, cluster, db, rp) -} - -func (s *Service) forEachDBRPMapping(ctx context.Context, fn func(m *influxdb.DBRPMapping) bool) error { - var err error - s.dbrpMappingKV.Range(func(k, v interface{}) bool { - m, ok := v.(influxdb.DBRPMapping) - if !ok { - err = fmt.Errorf("type %T is not a dbrp mapping", v) - return false - } - return fn(&m) - }) - - return err -} - -func (s *Service) filterDBRPMappings(ctx context.Context, fn func(m *influxdb.DBRPMapping) bool) ([]*influxdb.DBRPMapping, error) { - mappings := []*influxdb.DBRPMapping{} - err := s.forEachDBRPMapping(ctx, func(m *influxdb.DBRPMapping) bool { - if fn(m) { - mappings = append(mappings, m) - } - return true - }) - - if err != nil { - return nil, err - } - - return mappings, nil -} - -// Find returns the first dbrp mapping that matches filter. -func (s *Service) Find(ctx context.Context, filter influxdb.DBRPMappingFilter) (*influxdb.DBRPMapping, error) { - if filter.Cluster == nil && filter.Database == nil && filter.RetentionPolicy == nil { - return nil, &errors.Error{ - Code: errors.EInvalid, - Msg: "no filter parameters provided", - } - } - - // filter by dbrpMapping id - if filter.Cluster != nil && filter.Database != nil && filter.RetentionPolicy != nil { - return s.FindBy(ctx, *filter.Cluster, *filter.Database, *filter.RetentionPolicy) - } - - mappings, n, err := s.FindMany(ctx, filter) - if err != nil { - return nil, err - } - - if n < 1 { - return nil, errDBRPMappingNotFound - } - - return mappings[0], nil -} - -// FindMany returns a list of dbrpMappings that match filter and the total count of matching dbrp mappings. -// Additional options provide pagination & sorting. -func (s *Service) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilter, opt ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) { - // filter by dbrpMapping id - if filter.Cluster != nil && filter.Database != nil && filter.RetentionPolicy != nil { - m, err := s.FindBy(ctx, *filter.Cluster, *filter.Database, *filter.RetentionPolicy) - if err != nil { - return nil, 0, err - } - return []*influxdb.DBRPMapping{m}, 1, nil - } - - filterFunc := func(mapping *influxdb.DBRPMapping) bool { - return (filter.Cluster == nil || (*filter.Cluster) == mapping.Cluster) && - (filter.Database == nil || (*filter.Database) == mapping.Database) && - (filter.RetentionPolicy == nil || (*filter.RetentionPolicy) == mapping.RetentionPolicy) && - (filter.Default == nil || (*filter.Default) == mapping.Default) - } - - mappings, err := s.filterDBRPMappings(ctx, filterFunc) - if err != nil { - return nil, 0, err - } - - return mappings, len(mappings), nil -} - -// Create creates a new dbrp mapping. -func (s *Service) Create(ctx context.Context, m *influxdb.DBRPMapping) error { - if err := m.Validate(); err != nil { - return nil - } - existing, err := s.loadDBRPMapping(ctx, m.Cluster, m.Database, m.RetentionPolicy) - if err != nil { - if err == errDBRPMappingNotFound { - return s.PutDBRPMapping(ctx, m) - } - return err - } - - if !existing.Equal(m) { - return &errors.Error{ - Code: errors.EConflict, - Msg: "dbrp mapping already exists", - } - } - - return s.PutDBRPMapping(ctx, m) -} - -// PutDBRPMapping sets dbrpMapping with the current ID. -func (s *Service) PutDBRPMapping(ctx context.Context, m *influxdb.DBRPMapping) error { - k := encodeDBRPMappingKey(m.Cluster, m.Database, m.RetentionPolicy) - s.dbrpMappingKV.Store(k, *m) - return nil -} - -// Delete removes a dbrp mapping -func (s *Service) Delete(ctx context.Context, cluster, db, rp string) error { - s.dbrpMappingKV.Delete(encodeDBRPMappingKey(cluster, db, rp)) - return nil -} diff --git a/inmem/dbrp_mapping_test.go b/inmem/dbrp_mapping_test.go deleted file mode 100644 index e9449661a7a..00000000000 --- a/inmem/dbrp_mapping_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package inmem - -import ( - "context" - "testing" - - platform "github.com/influxdata/influxdb/v2" - platformtesting "github.com/influxdata/influxdb/v2/testing" -) - -func initDBRPMappingService(f platformtesting.DBRPMappingFields, t *testing.T) (platform.DBRPMappingService, func()) { - s := NewService() - ctx := context.TODO() - if err := f.Populate(ctx, s); err != nil { - t.Fatal(err) - } - return s, func() {} -} - -func TestDBRPMappingService_CreateDBRPMapping(t *testing.T) { - t.Parallel() - platformtesting.CreateDBRPMapping(initDBRPMappingService, t) -} - -func TestDBRPMappingService_FindDBRPMappingByKey(t *testing.T) { - t.Parallel() - platformtesting.FindDBRPMappingByKey(initDBRPMappingService, t) -} - -func TestDBRPMappingService_FindDBRPMappings(t *testing.T) { - t.Parallel() - platformtesting.FindDBRPMappings(initDBRPMappingService, t) -} - -func TestDBRPMappingService_DeleteDBRPMapping(t *testing.T) { - t.Parallel() - platformtesting.DeleteDBRPMapping(initDBRPMappingService, t) -} - -func TestDBRPMappingService_FindDBRPMapping(t *testing.T) { - t.Parallel() - platformtesting.FindDBRPMapping(initDBRPMappingService, t) -} diff --git a/mock/dbrp_mapping.go b/mock/dbrp_mapping.go index 031a4318aa0..d86a044d611 100644 --- a/mock/dbrp_mapping.go +++ b/mock/dbrp_mapping.go @@ -8,91 +8,47 @@ import ( "github.com/influxdata/influxdb/v2" ) -var _ influxdb.DBRPMappingServiceV2 = (*DBRPMappingServiceV2)(nil) +var _ influxdb.DBRPMappingService = (*DBRPMappingService)(nil) -type DBRPMappingServiceV2 struct { - FindByIDFn func(ctx context.Context, orgID, id platform.ID) (*influxdb.DBRPMappingV2, error) - FindManyFn func(ctx context.Context, dbrp influxdb.DBRPMappingFilterV2, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMappingV2, int, error) - CreateFn func(ctx context.Context, dbrp *influxdb.DBRPMappingV2) error - UpdateFn func(ctx context.Context, dbrp *influxdb.DBRPMappingV2) error +type DBRPMappingService struct { + FindByIDFn func(ctx context.Context, orgID, id platform.ID) (*influxdb.DBRPMapping, error) + FindManyFn func(ctx context.Context, dbrp influxdb.DBRPMappingFilter, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) + CreateFn func(ctx context.Context, dbrp *influxdb.DBRPMapping) error + UpdateFn func(ctx context.Context, dbrp *influxdb.DBRPMapping) error DeleteFn func(ctx context.Context, orgID, id platform.ID) error } -func (s *DBRPMappingServiceV2) FindByID(ctx context.Context, orgID, id platform.ID) (*influxdb.DBRPMappingV2, error) { +func (s *DBRPMappingService) FindByID(ctx context.Context, orgID, id platform.ID) (*influxdb.DBRPMapping, error) { if s.FindByIDFn == nil { return nil, nil } return s.FindByIDFn(ctx, orgID, id) } -func (s *DBRPMappingServiceV2) FindMany(ctx context.Context, dbrp influxdb.DBRPMappingFilterV2, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMappingV2, int, error) { +func (s *DBRPMappingService) FindMany(ctx context.Context, dbrp influxdb.DBRPMappingFilter, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) { if s.FindManyFn == nil { return nil, 0, nil } return s.FindManyFn(ctx, dbrp, opts...) } -func (s *DBRPMappingServiceV2) Create(ctx context.Context, dbrp *influxdb.DBRPMappingV2) error { +func (s *DBRPMappingService) Create(ctx context.Context, dbrp *influxdb.DBRPMapping) error { if s.CreateFn == nil { return nil } return s.CreateFn(ctx, dbrp) } -func (s *DBRPMappingServiceV2) Update(ctx context.Context, dbrp *influxdb.DBRPMappingV2) error { +func (s *DBRPMappingService) Update(ctx context.Context, dbrp *influxdb.DBRPMapping) error { if s.UpdateFn == nil { return nil } return s.UpdateFn(ctx, dbrp) } -func (s *DBRPMappingServiceV2) Delete(ctx context.Context, orgID, id platform.ID) error { +func (s *DBRPMappingService) Delete(ctx context.Context, orgID, id platform.ID) error { if s.DeleteFn == nil { return nil } return s.DeleteFn(ctx, orgID, id) } - -type DBRPMappingService struct { - FindByFn func(ctx context.Context, cluster string, db string, rp string) (*influxdb.DBRPMapping, error) - FindFn func(ctx context.Context, filter influxdb.DBRPMappingFilter) (*influxdb.DBRPMapping, error) - FindManyFn func(ctx context.Context, filter influxdb.DBRPMappingFilter, opt ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) - CreateFn func(ctx context.Context, dbrpMap *influxdb.DBRPMapping) error - DeleteFn func(ctx context.Context, cluster string, db string, rp string) error -} - -func NewDBRPMappingService() *DBRPMappingService { - return &DBRPMappingService{ - FindByFn: func(ctx context.Context, cluster string, db string, rp string) (*influxdb.DBRPMapping, error) { - return nil, nil - }, - FindFn: func(ctx context.Context, filter influxdb.DBRPMappingFilter) (*influxdb.DBRPMapping, error) { - return nil, nil - }, - FindManyFn: func(ctx context.Context, filter influxdb.DBRPMappingFilter, opt ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) { - return nil, 0, nil - }, - CreateFn: func(ctx context.Context, dbrpMap *influxdb.DBRPMapping) error { return nil }, - DeleteFn: func(ctx context.Context, cluster string, db string, rp string) error { return nil }, - } -} - -func (s *DBRPMappingService) FindBy(ctx context.Context, cluster string, db string, rp string) (*influxdb.DBRPMapping, error) { - return s.FindByFn(ctx, cluster, db, rp) -} - -func (s *DBRPMappingService) Find(ctx context.Context, filter influxdb.DBRPMappingFilter) (*influxdb.DBRPMapping, error) { - return s.FindFn(ctx, filter) -} - -func (s *DBRPMappingService) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilter, opt ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) { - return s.FindManyFn(ctx, filter, opt...) -} - -func (s *DBRPMappingService) Create(ctx context.Context, dbrpMap *influxdb.DBRPMapping) error { - return s.CreateFn(ctx, dbrpMap) -} - -func (s *DBRPMappingService) Delete(ctx context.Context, cluster string, db string, rp string) error { - return s.DeleteFn(ctx, cluster, db, rp) -} diff --git a/query/influxql/compiler.go b/query/influxql/compiler.go index 1031e873653..87478238c7b 100644 --- a/query/influxql/compiler.go +++ b/query/influxql/compiler.go @@ -14,7 +14,7 @@ import ( const CompilerType = "influxql" // AddCompilerMappings adds the influxql specific compiler mappings. -func AddCompilerMappings(mappings flux.CompilerMappings, dbrpMappingSvc platform.DBRPMappingServiceV2) error { +func AddCompilerMappings(mappings flux.CompilerMappings, dbrpMappingSvc platform.DBRPMappingService) error { return mappings.Add(CompilerType, func() flux.Compiler { return NewCompiler(dbrpMappingSvc) }) @@ -31,12 +31,12 @@ type Compiler struct { logicalPlannerOptions []plan.LogicalOption - dbrpMappingSvc platform.DBRPMappingServiceV2 + dbrpMappingSvc platform.DBRPMappingService } var _ flux.Compiler = &Compiler{} -func NewCompiler(dbrpMappingSvc platform.DBRPMappingServiceV2) *Compiler { +func NewCompiler(dbrpMappingSvc platform.DBRPMappingService) *Compiler { return &Compiler{ dbrpMappingSvc: dbrpMappingSvc, } diff --git a/query/influxql/end_to_end_test.go b/query/influxql/end_to_end_test.go index 3d20b1d1d8e..f51fa322705 100644 --- a/query/influxql/end_to_end_test.go +++ b/query/influxql/end_to_end_test.go @@ -28,21 +28,21 @@ import ( const generatedInfluxQLDataDir = "testdata" -var dbrpMappingSvcE2E = &mock.DBRPMappingServiceV2{} +var dbrpMappingSvcE2E = &mock.DBRPMappingService{} func init() { - mapping := platform.DBRPMappingV2{ + mapping := platform.DBRPMapping{ Database: "db0", RetentionPolicy: "autogen", Default: true, OrganizationID: platformtesting.MustIDBase16("cadecadecadecade"), BucketID: platformtesting.MustIDBase16("da7aba5e5eedca5e"), } - dbrpMappingSvcE2E.FindByIDFn = func(ctx context.Context, orgID, id platform2.ID) (*platform.DBRPMappingV2, error) { + dbrpMappingSvcE2E.FindByIDFn = func(ctx context.Context, orgID, id platform2.ID) (*platform.DBRPMapping, error) { return &mapping, nil } - dbrpMappingSvcE2E.FindManyFn = func(ctx context.Context, filter platform.DBRPMappingFilterV2, opt ...platform.FindOptions) ([]*platform.DBRPMappingV2, int, error) { - return []*platform.DBRPMappingV2{&mapping}, 1, nil + dbrpMappingSvcE2E.FindManyFn = func(ctx context.Context, filter platform.DBRPMappingFilter, opt ...platform.FindOptions) ([]*platform.DBRPMapping, int, error) { + return []*platform.DBRPMapping{&mapping}, 1, nil } } diff --git a/query/influxql/spectests/testing.go b/query/influxql/spectests/testing.go index 3e915b0bc08..77be45c8373 100644 --- a/query/influxql/spectests/testing.go +++ b/query/influxql/spectests/testing.go @@ -20,35 +20,35 @@ import ( platformtesting "github.com/influxdata/influxdb/v2/testing" ) -var dbrpMappingSvc = &mock.DBRPMappingServiceV2{} +var dbrpMappingSvc = &mock.DBRPMappingService{} var organizationID platform2.ID var bucketID platform2.ID var altBucketID platform2.ID func init() { - mapping := platform.DBRPMappingV2{ + mapping := platform.DBRPMapping{ Database: "db0", RetentionPolicy: "autogen", Default: true, OrganizationID: organizationID, BucketID: bucketID, } - altMapping := platform.DBRPMappingV2{ + altMapping := platform.DBRPMapping{ Database: "db0", RetentionPolicy: "autogen", Default: true, OrganizationID: organizationID, BucketID: altBucketID, } - dbrpMappingSvc.FindByIDFn = func(ctx context.Context, orgID, id platform2.ID) (*platform.DBRPMappingV2, error) { + dbrpMappingSvc.FindByIDFn = func(ctx context.Context, orgID, id platform2.ID) (*platform.DBRPMapping, error) { return &mapping, nil } - dbrpMappingSvc.FindManyFn = func(ctx context.Context, filter platform.DBRPMappingFilterV2, opt ...platform.FindOptions) ([]*platform.DBRPMappingV2, int, error) { + dbrpMappingSvc.FindManyFn = func(ctx context.Context, filter platform.DBRPMappingFilter, opt ...platform.FindOptions) ([]*platform.DBRPMapping, int, error) { m := &mapping if filter.RetentionPolicy != nil && *filter.RetentionPolicy == "alternate" { m = &altMapping } - return []*platform.DBRPMappingV2{m}, 1, nil + return []*platform.DBRPMapping{m}, 1, nil } } diff --git a/query/influxql/transpiler.go b/query/influxql/transpiler.go index ce6458a2c7e..5e617f7d6bb 100644 --- a/query/influxql/transpiler.go +++ b/query/influxql/transpiler.go @@ -18,14 +18,14 @@ import ( // Transpiler converts InfluxQL queries into a query spec. type Transpiler struct { Config *Config - dbrpMappingSvc influxdb.DBRPMappingServiceV2 + dbrpMappingSvc influxdb.DBRPMappingService } -func NewTranspiler(dbrpMappingSvc influxdb.DBRPMappingServiceV2) *Transpiler { +func NewTranspiler(dbrpMappingSvc influxdb.DBRPMappingService) *Transpiler { return NewTranspilerWithConfig(dbrpMappingSvc, Config{}) } -func NewTranspilerWithConfig(dbrpMappingSvc influxdb.DBRPMappingServiceV2, cfg Config) *Transpiler { +func NewTranspilerWithConfig(dbrpMappingSvc influxdb.DBRPMappingService, cfg Config) *Transpiler { return &Transpiler{ Config: &cfg, dbrpMappingSvc: dbrpMappingSvc, @@ -58,10 +58,10 @@ type transpilerState struct { config Config file *ast.File assignments map[string]ast.Expression - dbrpMappingSvc influxdb.DBRPMappingServiceV2 + dbrpMappingSvc influxdb.DBRPMappingService } -func newTranspilerState(dbrpMappingSvc influxdb.DBRPMappingServiceV2, config *Config) *transpilerState { +func newTranspilerState(dbrpMappingSvc influxdb.DBRPMappingService, config *Config) *transpilerState { state := &transpilerState{ file: &ast.File{ Package: &ast.PackageClause{ @@ -697,7 +697,7 @@ func (t *transpilerState) from(m *influxql.Measurement) (ast.Expression, error) } } - var filter influxdb.DBRPMappingFilterV2 + var filter influxdb.DBRPMappingFilter if db != "" { filter.Database = &db } diff --git a/query/influxql/transpiler_test.go b/query/influxql/transpiler_test.go index 32398385a26..a584f094dcb 100644 --- a/query/influxql/transpiler_test.go +++ b/query/influxql/transpiler_test.go @@ -14,21 +14,21 @@ import ( "github.com/pkg/errors" ) -var dbrpMappingSvc = &mock.DBRPMappingServiceV2{} +var dbrpMappingSvc = &mock.DBRPMappingService{} func init() { - mapping := platform.DBRPMappingV2{ + mapping := platform.DBRPMapping{ Database: "db0", RetentionPolicy: "autogen", Default: true, OrganizationID: platformtesting.MustIDBase16("aaaaaaaaaaaaaaaa"), BucketID: platformtesting.MustIDBase16("bbbbbbbbbbbbbbbb"), } - dbrpMappingSvc.FindByIDFn = func(ctx context.Context, orgID, id platform2.ID) (*platform.DBRPMappingV2, error) { + dbrpMappingSvc.FindByIDFn = func(ctx context.Context, orgID, id platform2.ID) (*platform.DBRPMapping, error) { return &mapping, nil } - dbrpMappingSvc.FindManyFn = func(ctx context.Context, filter platform.DBRPMappingFilterV2, opt ...platform.FindOptions) ([]*platform.DBRPMappingV2, int, error) { - return []*platform.DBRPMappingV2{&mapping}, 1, nil + dbrpMappingSvc.FindManyFn = func(ctx context.Context, filter platform.DBRPMappingFilter, opt ...platform.FindOptions) ([]*platform.DBRPMapping, int, error) { + return []*platform.DBRPMapping{&mapping}, 1, nil } } diff --git a/query/stdlib/influxdata/influxdb/v1/databases.go b/query/stdlib/influxdata/influxdb/v1/databases.go index bdb1e97f0f2..b8aa9c35ecf 100644 --- a/query/stdlib/influxdata/influxdb/v1/databases.go +++ b/query/stdlib/influxdata/influxdb/v1/databases.go @@ -43,7 +43,7 @@ func (s *LocalDatabasesProcedureSpec) Copy() plan.ProcedureSpec { type DatabasesDecoder struct { orgID platform2.ID deps *DatabasesDependencies - databases []*platform.DBRPMappingV2 + databases []*platform.DBRPMapping alloc *memory.Allocator } @@ -52,7 +52,7 @@ func (bd *DatabasesDecoder) Connect(ctx context.Context) error { } func (bd *DatabasesDecoder) Fetch(ctx context.Context) (bool, error) { - b, _, err := bd.deps.DBRP.FindMany(ctx, platform.DBRPMappingFilterV2{}) + b, _, err := bd.deps.DBRP.FindMany(ctx, platform.DBRPMappingFilter{}) if err != nil { return false, err } @@ -62,7 +62,7 @@ func (bd *DatabasesDecoder) Fetch(ctx context.Context) (bool, error) { func (bd *DatabasesDecoder) Decode(ctx context.Context) (flux.Table, error) { type databaseInfo struct { - *platform.DBRPMappingV2 + *platform.DBRPMapping RetentionPeriod time.Duration } @@ -77,7 +77,7 @@ func (bd *DatabasesDecoder) Decode(ctx context.Context) (flux.Table, error) { return nil, err } databases = append(databases, databaseInfo{ - DBRPMappingV2: db, + DBRPMapping: db, RetentionPeriod: bucket.RetentionPeriod, }) } @@ -172,7 +172,7 @@ type key int const dependenciesKey key = iota type DatabasesDependencies struct { - DBRP platform.DBRPMappingServiceV2 + DBRP platform.DBRPMappingService BucketLookup platform.BucketService } diff --git a/testing/dbrp_mapping.go b/testing/dbrp_mapping.go index b605cd18dae..7e7f3c9f0ab 100644 --- a/testing/dbrp_mapping.go +++ b/testing/dbrp_mapping.go @@ -3,13 +3,17 @@ package testing import ( "bytes" "context" + "fmt" "sort" "testing" + "github.com/influxdata/influxdb/v2/kit/platform" errors2 "github.com/influxdata/influxdb/v2/kit/platform/errors" "github.com/google/go-cmp/cmp" - platform "github.com/influxdata/influxdb/v2" + "github.com/influxdata/influxdb/v2" + "github.com/influxdata/influxdb/v2/dbrp" + "github.com/influxdata/influxdb/v2/mock" "github.com/pkg/errors" ) @@ -23,34 +27,28 @@ const ( dbrpBucketBID = "b1077edb1077eded" ) -var dbrpMappingCmpOptions = cmp.Options{ +var DBRPMappingCmpOptions = cmp.Options{ cmp.Comparer(func(x, y []byte) bool { return bytes.Equal(x, y) }), - cmp.Transformer("Sort", func(in []*platform.DBRPMapping) []*platform.DBRPMapping { - out := make([]*platform.DBRPMapping, len(in)) + cmp.Transformer("Sort", func(in []*influxdb.DBRPMapping) []*influxdb.DBRPMapping { + out := make([]*influxdb.DBRPMapping, len(in)) copy(out, in) // Copy input slice to avoid mutating it sort.Slice(out, func(i, j int) bool { - if out[i].Cluster != out[j].Cluster { - return out[i].Cluster < out[j].Cluster - } - if out[i].Database != out[j].Database { - return out[i].Database < out[j].Database - } - return out[i].RetentionPolicy < out[j].RetentionPolicy + return out[i].ID < out[j].ID }) return out }), } -// DBRPMappingFields will include the dbrpMappings type DBRPMappingFields struct { - DBRPMappings []*platform.DBRPMapping + BucketSvc influxdb.BucketService + DBRPMappingsV2 []*influxdb.DBRPMapping } -// Populate creates all entities in DBRPMappingFields -func (f DBRPMappingFields) Populate(ctx context.Context, s platform.DBRPMappingService) error { - for _, m := range f.DBRPMappings { +// Populate creates all entities in DBRPMappingFields. +func (f DBRPMappingFields) Populate(ctx context.Context, s influxdb.DBRPMappingService) error { + for _, m := range f.DBRPMappingsV2 { if err := s.Create(ctx, m); err != nil { return errors.Wrap(err, "failed to populate dbrp mappings") } @@ -58,32 +56,75 @@ func (f DBRPMappingFields) Populate(ctx context.Context, s platform.DBRPMappingS return nil } -// CleanupDBRPMappings finds and removes all dbrp mappings -func CleanupDBRPMappings(ctx context.Context, s platform.DBRPMappingService) error { - mappings, _, err := s.FindMany(ctx, platform.DBRPMappingFilter{}) +// DBRPMappingService tests all the service functions. +func DBRPMappingService( + init func(DBRPMappingFields, *testing.T) (influxdb.DBRPMappingService, func()), + t *testing.T, +) { + tests := []struct { + name string + fn func(init func(DBRPMappingFields, *testing.T) (influxdb.DBRPMappingService, func()), + t *testing.T) + }{ + { + name: "create", + fn: CreateDBRPMappingV2, + }, + { + name: "find by ID", + fn: FindDBRPMappingByIDV2, + }, + { + name: "find", + fn: FindManyDBRPMappingsV2, + }, + { + name: "update", + fn: UpdateDBRPMappingV2, + }, + { + name: "delete", + fn: DeleteDBRPMappingV2, + }, + { + name: "miscellaneous", + fn: MiscDBRPMappingV2, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt := tt + t.Parallel() + tt.fn(init, t) + }) + } +} + +// CleanupDBRPMappingsV2 finds and removes all dbrp mappings. +func CleanupDBRPMappingsV2(ctx context.Context, s influxdb.DBRPMappingService) error { + mappings, _, err := s.FindMany(ctx, influxdb.DBRPMappingFilter{}) if err != nil { return errors.Wrap(err, "failed to retrieve all dbrp mappings") } for _, m := range mappings { - if err := s.Delete(ctx, m.Cluster, m.Database, m.RetentionPolicy); err != nil { - return errors.Wrapf(err, "failed to remove dbrp mapping %s/%s/%s", m.Cluster, m.Database, m.RetentionPolicy) + if err := s.Delete(ctx, m.OrganizationID, m.ID); err != nil { + return errors.Wrapf(err, "failed to remove dbrp mapping %v", m.ID) } } return nil } -// CreateDBRPMapping testing -func CreateDBRPMapping( - init func(DBRPMappingFields, *testing.T) (platform.DBRPMappingService, func()), +func CreateDBRPMappingV2( + init func(DBRPMappingFields, *testing.T) (influxdb.DBRPMappingService, func()), t *testing.T, ) { type args struct { - dbrpMapping *platform.DBRPMapping + dbrpMapping *influxdb.DBRPMapping } type wants struct { err error - dbrpMappings []*platform.DBRPMapping + dbrpMappings []*influxdb.DBRPMapping } tests := []struct { @@ -93,13 +134,13 @@ func CreateDBRPMapping( wants wants }{ { - name: "create dbrpMappings with empty set", + name: "basic create dbrp", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{}, + DBRPMappingsV2: []*influxdb.DBRPMapping{}, }, args: args{ - dbrpMapping: &platform.DBRPMapping{ - Cluster: "cluster1", + dbrpMapping: &influxdb.DBRPMapping{ + ID: 100, Database: "database1", RetentionPolicy: "retention_policy1", Default: false, @@ -108,42 +149,90 @@ func CreateDBRPMapping( }, }, wants: wants{ - dbrpMappings: []*platform.DBRPMapping{{ - Cluster: "cluster1", + dbrpMappings: []*influxdb.DBRPMapping{{ + ID: 100, Database: "database1", RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), + // If there is only one mapping for a database, that is the default one. + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), }}, }, }, { - name: "basic create dbrpMapping", + name: "create mapping for same db does not change default", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{{ - Cluster: "cluster1", + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + }, + }, + args: args{ + dbrpMapping: &influxdb.DBRPMapping{ + ID: 200, Database: "database1", - RetentionPolicy: "retention_policy1", + RetentionPolicy: "retention_policy2", Default: false, OrganizationID: MustIDBase16(dbrpOrg1ID), BucketID: MustIDBase16(dbrpBucket1ID), - }}, + }, + }, + wants: wants{ + dbrpMappings: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 200, + Database: "database1", + RetentionPolicy: "retention_policy2", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + }, + }, + }, + { + name: "create mapping for same db changes default", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + }, }, args: args{ - dbrpMapping: &platform.DBRPMapping{ - Cluster: "cluster2", - Database: "database2", + dbrpMapping: &influxdb.DBRPMapping{ + ID: 200, + Database: "database1", RetentionPolicy: "retention_policy2", Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket2ID), + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), }, }, wants: wants{ - dbrpMappings: []*platform.DBRPMapping{ + dbrpMappings: []*influxdb.DBRPMapping{ { - Cluster: "cluster1", + ID: 100, Database: "database1", RetentionPolicy: "retention_policy1", Default: false, @@ -151,56 +240,101 @@ func CreateDBRPMapping( BucketID: MustIDBase16(dbrpBucket1ID), }, { - Cluster: "cluster2", - Database: "database2", + ID: 200, + Database: "database1", RetentionPolicy: "retention_policy2", Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket2ID), + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), }, }, }, }, { - name: "idempotent create dbrpMapping", + name: "error on create existing dbrp with same ID", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{{ - Cluster: "cluster1", + DBRPMappingsV2: []*influxdb.DBRPMapping{{ + OrganizationID: MustIDBase16(dbrpOrg1ID), + ID: 100, Database: "database1", RetentionPolicy: "retention_policy1", Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), BucketID: MustIDBase16(dbrpBucket1ID), }}, }, args: args{ - dbrpMapping: &platform.DBRPMapping{ - Cluster: "cluster1", + dbrpMapping: &influxdb.DBRPMapping{ + // NOTE(affo): in the "same ID" concept, orgID must match too! + OrganizationID: MustIDBase16(dbrpOrg1ID), + ID: 100, + Database: "database2", + RetentionPolicy: "retention_policy2", + Default: false, + BucketID: MustIDBase16(dbrpBucket2ID), + }, + }, + wants: wants{ + err: dbrp.ErrDBRPAlreadyExists("dbrp already exist for this particular ID. If you are trying an update use the right function .Update"), + dbrpMappings: []*influxdb.DBRPMapping{{ + ID: 100, Database: "database1", RetentionPolicy: "retention_policy1", - Default: false, + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }}, + }, + }, + { + name: "error on create dbrp with same orgID, db and rp", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{{ OrganizationID: MustIDBase16(dbrpOrg1ID), + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: false, BucketID: MustIDBase16(dbrpBucket1ID), + }}, + }, + args: args{ + dbrpMapping: &influxdb.DBRPMapping{ + OrganizationID: MustIDBase16(dbrpOrg1ID), + ID: 200, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: false, + BucketID: MustIDBase16(dbrpBucket2ID), }, }, wants: wants{ - dbrpMappings: []*platform.DBRPMapping{ - { - Cluster: "cluster1", - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, + err: dbrp.ErrDBRPAlreadyExists("another DBRP mapping with same orgID, db, and rp exists"), + dbrpMappings: []*influxdb.DBRPMapping{{ + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }}, }, }, { - name: "error on create existing dbrpMapping", + name: "error bucket does not exist", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{{ - Cluster: "cluster1", + BucketSvc: &mock.BucketService{ + FindBucketByIDFn: func(ctx context.Context, id platform.ID) (*influxdb.Bucket, error) { + if id == MustIDBase16(dbrpBucket2ID) { + return nil, &errors2.Error{ + Code: errors2.ENotFound, + Msg: "bucket not found", + } + } + return nil, nil + }, + }, + DBRPMappingsV2: []*influxdb.DBRPMapping{{ + ID: 100, Database: "database1", RetentionPolicy: "retention_policy1", Default: false, @@ -209,30 +343,28 @@ func CreateDBRPMapping( }}, }, args: args{ - dbrpMapping: &platform.DBRPMapping{ - Cluster: "cluster1", + dbrpMapping: &influxdb.DBRPMapping{ + ID: 200, Database: "database1", - RetentionPolicy: "retention_policy1", + RetentionPolicy: "retention_policy2", Default: true, OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), + BucketID: MustIDBase16(dbrpBucket2ID), }, }, wants: wants{ err: &errors2.Error{ - Code: errors2.EConflict, - Msg: "dbrp mapping already exists", - }, - dbrpMappings: []*platform.DBRPMapping{ - { - Cluster: "cluster1", - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, + Code: errors2.ENotFound, + Msg: "bucket not found", }, + dbrpMappings: []*influxdb.DBRPMapping{{ + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }}, }, }, } @@ -244,37 +376,39 @@ func CreateDBRPMapping( ctx := context.Background() err := s.Create(ctx, tt.args.dbrpMapping) if (err != nil) != (tt.wants.err != nil) { - t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err) + t.Errorf("expected error '%v' got '%v'", tt.wants.err, err) } if err != nil && tt.wants.err != nil { if err.Error() != tt.wants.err.Error() { - t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error()) + t.Errorf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error()) } } - dbrpMappings, _, err := s.FindMany(ctx, platform.DBRPMappingFilter{}) + dbrpMappings, n, err := s.FindMany(ctx, influxdb.DBRPMappingFilter{}) if err != nil { - t.Fatalf("failed to retrieve dbrpMappings: %v", err) + t.Fatalf("failed to retrieve dbrps: %v", err) } - if diff := cmp.Diff(dbrpMappings, tt.wants.dbrpMappings, dbrpMappingCmpOptions...); diff != "" { - t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) + if n != len(tt.wants.dbrpMappings) { + t.Errorf("want dbrpMappings count of %d, got %d", len(tt.wants.dbrpMappings), n) + } + if diff := cmp.Diff(tt.wants.dbrpMappings, dbrpMappings, DBRPMappingCmpOptions...); diff != "" { + t.Errorf("dbrpMappings are different -want/+got\ndiff %s", diff) } }) } } -// FindDBRPMappings testing -func FindDBRPMappings( - init func(DBRPMappingFields, *testing.T) (platform.DBRPMappingService, func()), +func FindManyDBRPMappingsV2( + init func(DBRPMappingFields, *testing.T) (influxdb.DBRPMappingService, func()), t *testing.T, ) { type args struct { - filter platform.DBRPMappingFilter + filter influxdb.DBRPMappingFilter } type wants struct { - dbrpMappings []*platform.DBRPMapping + dbrpMappings []*influxdb.DBRPMapping err error } tests := []struct { @@ -284,11 +418,11 @@ func FindDBRPMappings( wants wants }{ { - name: "find all dbrpMappings", + name: "find all dbrps", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ { - Cluster: "cluster1", + ID: 100, Database: "database1", RetentionPolicy: "retention_policy1", Default: false, @@ -296,7 +430,7 @@ func FindDBRPMappings( BucketID: MustIDBase16(dbrpBucket1ID), }, { - Cluster: "cluster2", + ID: 200, Database: "database2", RetentionPolicy: "retention_policy2", Default: true, @@ -306,20 +440,20 @@ func FindDBRPMappings( }, }, args: args{ - filter: platform.DBRPMappingFilter{}, + filter: influxdb.DBRPMappingFilter{}, }, wants: wants{ - dbrpMappings: []*platform.DBRPMapping{ + dbrpMappings: []*influxdb.DBRPMapping{ { - Cluster: "cluster1", + ID: 100, Database: "database1", RetentionPolicy: "retention_policy1", - Default: false, + Default: true, OrganizationID: MustIDBase16(dbrpOrg1ID), BucketID: MustIDBase16(dbrpBucket1ID), }, { - Cluster: "cluster2", + ID: 200, Database: "database2", RetentionPolicy: "retention_policy2", Default: true, @@ -330,60 +464,60 @@ func FindDBRPMappings( }, }, { - name: "find dbrpMappings by cluster", + name: "find by ID", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ { - Cluster: "cluster1", + ID: MustIDBase16("1111111111111111"), Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), + RetentionPolicy: "retention_policyA", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), }, { - Cluster: "cluster2", + ID: MustIDBase16("2222222222222222"), Database: "database2", - RetentionPolicy: "retention_policy2", + RetentionPolicy: "retention_policyB", Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket2ID), + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), }, }, }, args: args{ - filter: platform.DBRPMappingFilter{ - Cluster: strPtr("cluster2"), + filter: influxdb.DBRPMappingFilter{ + ID: MustIDBase16Ptr("1111111111111111"), }, }, wants: wants{ - dbrpMappings: []*platform.DBRPMapping{ + dbrpMappings: []*influxdb.DBRPMapping{ { - Cluster: "cluster2", - Database: "database2", - RetentionPolicy: "retention_policy2", + ID: MustIDBase16("1111111111111111"), + Database: "database1", + RetentionPolicy: "retention_policyA", Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket2ID), + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), }, }, }, }, { - name: "find default rp from dbrpMappings", + name: "find by bucket ID", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ { - Cluster: "cluster", - Database: "database", + ID: 100, + Database: "database1", RetentionPolicy: "retention_policyA", Default: false, OrganizationID: MustIDBase16(dbrpOrg3ID), BucketID: MustIDBase16(dbrpBucketAID), }, { - Cluster: "cluster", - Database: "database", + ID: 200, + Database: "database2", RetentionPolicy: "retention_policyB", Default: true, OrganizationID: MustIDBase16(dbrpOrg3ID), @@ -392,17 +526,15 @@ func FindDBRPMappings( }, }, args: args{ - filter: platform.DBRPMappingFilter{ - Cluster: strPtr("cluster"), - Database: strPtr("database"), - Default: boolPtr(true), + filter: influxdb.DBRPMappingFilter{ + BucketID: MustIDBase16Ptr(dbrpBucketBID), }, }, wants: wants{ - dbrpMappings: []*platform.DBRPMapping{ + dbrpMappings: []*influxdb.DBRPMapping{ { - Cluster: "cluster", - Database: "database", + ID: 200, + Database: "database2", RetentionPolicy: "retention_policyB", Default: true, OrganizationID: MustIDBase16(dbrpOrg3ID), @@ -411,117 +543,358 @@ func FindDBRPMappings( }, }, }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s, done := init(tt.fields, t) - defer done() - ctx := context.Background() - - dbrpMappings, _, err := s.FindMany(ctx, tt.args.filter) - if (err != nil) != (tt.wants.err != nil) { - t.Fatalf("expected errors to be equal '%v' got '%v'", tt.wants.err, err) - } - - if err != nil && tt.wants.err != nil { - if err.Error() != tt.wants.err.Error() { - t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err) - } - } - - if diff := cmp.Diff(dbrpMappings, tt.wants.dbrpMappings, dbrpMappingCmpOptions...); diff != "" { - t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) - } - }) - } -} - -// FindDBRPMappingByKey testing -func FindDBRPMappingByKey( - init func(DBRPMappingFields, *testing.T) (platform.DBRPMappingService, func()), - t *testing.T, -) { - type args struct { - Cluster, - Database, - RetentionPolicy string - } - - type wants struct { - dbrpMapping *platform.DBRPMapping - err error - } - - tests := []struct { - name string - fields DBRPMappingFields - args args - wants wants - }{ { - name: "find dbrpMappings by cluster db and rp", + name: "find by orgID", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ { - Cluster: "cluster", + ID: 100, Database: "database", RetentionPolicy: "retention_policyA", Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), + OrganizationID: MustIDBase16(dbrpOrg2ID), BucketID: MustIDBase16(dbrpBucketAID), }, { - Cluster: "cluster", + ID: 200, Database: "database", RetentionPolicy: "retention_policyB", - Default: false, + Default: true, + OrganizationID: MustIDBase16(dbrpOrg2ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + { + ID: 300, + Database: "database1", + RetentionPolicy: "retention_policyA", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + { + ID: 400, + Database: "database1", + RetentionPolicy: "retention_policyB", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + { + ID: 500, + Database: "database2", + RetentionPolicy: "retention_policyA", + Default: true, OrganizationID: MustIDBase16(dbrpOrg3ID), BucketID: MustIDBase16(dbrpBucketBID), }, }, }, args: args{ - Cluster: "cluster", - Database: "database", - RetentionPolicy: "retention_policyB", - }, - wants: wants{ - dbrpMapping: &platform.DBRPMapping{ - Cluster: "cluster", - Database: "database", - RetentionPolicy: "retention_policyB", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), + filter: influxdb.DBRPMappingFilter{ + OrgID: MustIDBase16Ptr(dbrpOrg3ID), }, }, - }, - { - name: "find non existing dbrpMapping", + wants: wants{ + dbrpMappings: []*influxdb.DBRPMapping{ + { + ID: 300, + Database: "database1", + RetentionPolicy: "retention_policyA", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + { + ID: 400, + Database: "database1", + RetentionPolicy: "retention_policyB", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + { + ID: 500, + Database: "database2", + RetentionPolicy: "retention_policyA", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + }, + }, + }, + { + name: "find by db", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policyA", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + { + ID: 200, + Database: "database2", + RetentionPolicy: "retention_policyB", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + }, + }, + args: args{ + filter: influxdb.DBRPMappingFilter{ + Database: stringPtr("database1"), + }, + }, + wants: wants{ + dbrpMappings: []*influxdb.DBRPMapping{ { - Cluster: "cluster", + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policyA", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + }, + }, + }, + { + name: "find by rp", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + ID: 100, Database: "database", RetentionPolicy: "retention_policyA", Default: false, OrganizationID: MustIDBase16(dbrpOrg3ID), BucketID: MustIDBase16(dbrpBucketAID), }, + { + ID: 200, + Database: "database", + RetentionPolicy: "retention_policyB", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, }, }, args: args{ - Cluster: "clusterX", - Database: "database", - RetentionPolicy: "retention_policyA", + filter: influxdb.DBRPMappingFilter{ + RetentionPolicy: stringPtr("retention_policyB"), + }, }, wants: wants{ - err: &errors2.Error{ - Code: errors2.ENotFound, - Msg: "dbrp mapping not found", + dbrpMappings: []*influxdb.DBRPMapping{ + { + ID: 200, + Database: "database", + RetentionPolicy: "retention_policyB", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + }, + }, + }, + { + name: "find by default", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database", + RetentionPolicy: "retention_policyA", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + { + ID: 200, + Database: "database", + RetentionPolicy: "retention_policyB", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + }, + }, + args: args{ + filter: influxdb.DBRPMappingFilter{ + Default: boolPtr(true), + }, + }, + wants: wants{ + dbrpMappings: []*influxdb.DBRPMapping{ + { + ID: 200, + Database: "database", + RetentionPolicy: "retention_policyB", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + }, + }, + }, + { + name: "find default", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + ID: MustIDBase16("0000000000000100"), + Database: "database", + RetentionPolicy: "retention_policyA", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + { + ID: MustIDBase16("0000000000000200"), + Database: "database", + RetentionPolicy: "retention_policyB", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + { + ID: MustIDBase16("0000000000000300"), + Database: "database", + RetentionPolicy: "retention_policyB", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg2ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + }, + }, + args: args{ + filter: influxdb.DBRPMappingFilter{ + OrgID: MustIDBase16Ptr(dbrpOrg3ID), + Database: stringPtr("database"), + Default: boolPtr(true), + }, + }, + wants: wants{ + dbrpMappings: []*influxdb.DBRPMapping{ + { + ID: MustIDBase16("0000000000000200"), + Database: "database", + RetentionPolicy: "retention_policyB", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + }, + }, + }, + { + name: "mixed", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policyA", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + { + ID: 200, + Database: "database2", + RetentionPolicy: "retention_policyA", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + { + ID: 300, + Database: "database1", + RetentionPolicy: "retention_policyA", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg2ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + // This one will substitute 200 as default for "database2". + { + ID: 400, + Database: "database2", + RetentionPolicy: "retention_policyB", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + }, + }, + args: args{ + filter: influxdb.DBRPMappingFilter{ + RetentionPolicy: stringPtr("retention_policyA"), + Default: boolPtr(true), + OrgID: MustIDBase16Ptr(dbrpOrg3ID), + }, + }, + wants: wants{ + dbrpMappings: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policyA", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + }, + }, + }, + { + name: "not found", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + Database: "database1", + RetentionPolicy: "retention_policyA", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + { + Database: "database1", + RetentionPolicy: "retention_policyB", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + { + Database: "database1", + RetentionPolicy: "retention_policyA", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg2ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + { + Database: "database2", + RetentionPolicy: "retention_policyB", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketBID), + }, + }, + }, + args: args{ + filter: influxdb.DBRPMappingFilter{ + Database: stringPtr("database1"), + RetentionPolicy: stringPtr("retention_policyC"), }, }, + wants: wants{ + dbrpMappings: []*influxdb.DBRPMapping{}, + }, }, } @@ -531,35 +904,35 @@ func FindDBRPMappingByKey( defer done() ctx := context.Background() - dbrpMapping, err := s.FindBy(ctx, tt.args.Cluster, tt.args.Database, tt.args.RetentionPolicy) + dbrpMappings, _, err := s.FindMany(ctx, tt.args.filter) if (err != nil) != (tt.wants.err != nil) { - t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err) + t.Fatalf("expected errors to be equal '%v' got '%v'", tt.wants.err, err) } if err != nil && tt.wants.err != nil { if err.Error() != tt.wants.err.Error() { - t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error()) + t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err) } } - if diff := cmp.Diff(dbrpMapping, tt.wants.dbrpMapping, dbrpMappingCmpOptions...); diff != "" { + if diff := cmp.Diff(dbrpMappings, tt.wants.dbrpMappings, DBRPMappingCmpOptions...); diff != "" { t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) } }) } } -// FindDBRPMapping testing -func FindDBRPMapping( - init func(DBRPMappingFields, *testing.T) (platform.DBRPMappingService, func()), +func FindDBRPMappingByIDV2( + init func(DBRPMappingFields, *testing.T) (influxdb.DBRPMappingService, func()), t *testing.T, ) { type args struct { - filter platform.DBRPMappingFilter + OrgID platform.ID + ID platform.ID } type wants struct { - dbrpMapping *platform.DBRPMapping + dbrpMapping *influxdb.DBRPMapping err error } @@ -570,11 +943,11 @@ func FindDBRPMapping( wants wants }{ { - name: "find dbrpMappings by cluster db and rp", + name: "find existing dbrp", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ { - Cluster: "cluster", + ID: 100, Database: "database", RetentionPolicy: "retention_policyA", Default: false, @@ -582,102 +955,483 @@ func FindDBRPMapping( BucketID: MustIDBase16(dbrpBucketAID), }, { - Cluster: "cluster", + ID: 200, Database: "database", RetentionPolicy: "retention_policyB", Default: false, OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + { + ID: 300, + Database: "database", + RetentionPolicy: "retention_policyC", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + }, + }, + args: args{ + OrgID: MustIDBase16(dbrpOrg3ID), + ID: 200, + }, + wants: wants{ + dbrpMapping: &influxdb.DBRPMapping{ + ID: 200, + Database: "database", + RetentionPolicy: "retention_policyB", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + }, + }, + { + name: "find non existing dbrp", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database", + RetentionPolicy: "retention_policyA", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + }, + }, + args: args{ + OrgID: MustIDBase16(dbrpOrg3ID), + ID: 200, + }, + wants: wants{ + err: dbrp.ErrDBRPNotFound, + }, + }, + { + name: "find existing dbrp but wrong orgID", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database", + RetentionPolicy: "retention_policyA", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg3ID), + BucketID: MustIDBase16(dbrpBucketAID), + }, + }, + }, + args: args{ + OrgID: MustIDBase16(dbrpOrg2ID), + ID: 100, + }, + wants: wants{ + err: dbrp.ErrDBRPNotFound, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s, done := init(tt.fields, t) + defer done() + ctx := context.Background() + + dbrpMapping, err := s.FindByID(ctx, tt.args.OrgID, tt.args.ID) + if (err != nil) != (tt.wants.err != nil) { + t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err) + } + + if err != nil && tt.wants.err != nil { + if err.Error() != tt.wants.err.Error() { + t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error()) + } + } + + if diff := cmp.Diff(dbrpMapping, tt.wants.dbrpMapping, DBRPMappingCmpOptions...); diff != "" { + t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) + } + }) + } +} + +func UpdateDBRPMappingV2( + init func(DBRPMappingFields, *testing.T) (influxdb.DBRPMappingService, func()), + t *testing.T, +) { + type args struct { + dbrpMapping *influxdb.DBRPMapping + } + type wants struct { + err error + dbrpMappings []*influxdb.DBRPMapping + } + + tests := []struct { + name string + fields DBRPMappingFields + args args + wants wants + }{ + { + name: "basic update", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + }, + }, + args: args{ + dbrpMapping: &influxdb.DBRPMapping{ + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy2", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + }, + wants: wants{ + dbrpMappings: []*influxdb.DBRPMapping{{ + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy2", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }}, + }, + }, + { + name: "update invalid dbrp", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{{ + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }}, + }, + args: args{ + dbrpMapping: &influxdb.DBRPMapping{ + ID: 100, + Database: "./", // invalid db name. + RetentionPolicy: "retention_policy2", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket2ID), + }, + }, + wants: wants{ + err: dbrp.ErrInvalidDBRP(fmt.Errorf("database must contain at least one character and only be letters, numbers, '_', '-', and '.'")), + dbrpMappings: []*influxdb.DBRPMapping{{ + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }}, + }, + }, + { + name: "error dbrp not found", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{{ + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }}, + }, + args: args{ + dbrpMapping: &influxdb.DBRPMapping{ + ID: 200, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket2ID), + }, + }, + wants: wants{ + err: dbrp.ErrDBRPNotFound, + dbrpMappings: []*influxdb.DBRPMapping{{ + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }}, + }, + }, + { + name: "update unchangeable fields", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{{ + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }}, + }, + args: args{ + dbrpMapping: &influxdb.DBRPMapping{ + ID: 100, + Database: "wont_change", + RetentionPolicy: "retention_policy1", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket2ID), + }, + }, + wants: wants{ + dbrpMappings: []*influxdb.DBRPMapping{{ + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }}, + }, + }, + { + name: "update to same orgID, db, and rp", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 200, + Database: "database1", + RetentionPolicy: "retention_policy2", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), }, }, }, args: args{ - filter: platform.DBRPMappingFilter{ - Cluster: strPtr("cluster"), - Database: strPtr("database"), - RetentionPolicy: strPtr("retention_policyB"), + dbrpMapping: &influxdb.DBRPMapping{ + ID: 200, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), }, }, wants: wants{ - dbrpMapping: &platform.DBRPMapping{ - Cluster: "cluster", - Database: "database", - RetentionPolicy: "retention_policyB", + err: dbrp.ErrDBRPAlreadyExists("another DBRP mapping with same orgID, db, and rp exists"), + dbrpMappings: []*influxdb.DBRPMapping{{ + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 200, + Database: "database1", + RetentionPolicy: "retention_policy2", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + }, + }, + }, + { + name: "update default when only one dbrp is present", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{{ + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }}, + }, + args: args{ + dbrpMapping: &influxdb.DBRPMapping{ + ID: 100, + Database: "wont_change", + RetentionPolicy: "retention_policy1", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket2ID), }, }, + wants: wants{ + dbrpMappings: []*influxdb.DBRPMapping{{ + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }}, + }, }, { - name: "find default rp from dbrpMappings", + name: "set default when more dbrps are present", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ { - Cluster: "cluster", - Database: "database", - RetentionPolicy: "retention_policyA", + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 200, + Database: "database1", + RetentionPolicy: "retention_policy2", Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), }, { - Cluster: "cluster", - Database: "database", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), + ID: 300, + Database: "database1", + RetentionPolicy: "retention_policy3", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), }, }, }, args: args{ - filter: platform.DBRPMappingFilter{ - Cluster: strPtr("cluster"), - Database: strPtr("database"), - Default: boolPtr(true), + dbrpMapping: &influxdb.DBRPMapping{ + ID: 200, + Database: "database1", + RetentionPolicy: "retention_policy2", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), }, }, wants: wants{ - dbrpMapping: &platform.DBRPMapping{ - Cluster: "cluster", - Database: "database", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), + dbrpMappings: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 200, + Database: "database1", + RetentionPolicy: "retention_policy2", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 300, + Database: "database1", + RetentionPolicy: "retention_policy3", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, }, }, }, { - name: "find dbrpMapping with invalid filter", + name: "unset default when more dbrps are present", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ { - Cluster: "cluster", - Database: "database", - RetentionPolicy: "retention_policyA", + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), }, { - Cluster: "cluster", - Database: "database", - RetentionPolicy: "retention_policyB", + ID: 200, + Database: "database1", + RetentionPolicy: "retention_policy2", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 300, + Database: "database1", + RetentionPolicy: "retention_policy3", Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), }, }, }, args: args{ - filter: platform.DBRPMappingFilter{}, + dbrpMapping: &influxdb.DBRPMapping{ + ID: 300, + Database: "database1", + RetentionPolicy: "retention_policy3", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, }, wants: wants{ - err: &errors2.Error{ - Code: errors2.EInvalid, - Msg: "no filter parameters provided", + dbrpMappings: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 200, + Database: "database1", + RetentionPolicy: "retention_policy2", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 300, + Database: "database1", + RetentionPolicy: "retention_policy3", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, }, }, }, @@ -688,36 +1442,39 @@ func FindDBRPMapping( s, done := init(tt.fields, t) defer done() ctx := context.Background() - - dbrpMapping, err := s.Find(ctx, tt.args.filter) + err := s.Update(ctx, tt.args.dbrpMapping) if (err != nil) != (tt.wants.err != nil) { - t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err) + t.Errorf("expected error '%v' got '%v'", tt.wants.err, err) } if err != nil && tt.wants.err != nil { if err.Error() != tt.wants.err.Error() { - t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error()) + t.Errorf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error()) } } - if diff := cmp.Diff(dbrpMapping, tt.wants.dbrpMapping, dbrpMappingCmpOptions...); diff != "" { + dbrpMappings, _, err := s.FindMany(ctx, influxdb.DBRPMappingFilter{}) + if err != nil { + t.Fatalf("failed to retrieve dbrps: %v", err) + } + if diff := cmp.Diff(dbrpMappings, tt.wants.dbrpMappings, DBRPMappingCmpOptions...); diff != "" { t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) } }) } } -// DeleteDBRPMapping testing -func DeleteDBRPMapping( - init func(DBRPMappingFields, *testing.T) (platform.DBRPMappingService, func()), +func DeleteDBRPMappingV2( + init func(DBRPMappingFields, *testing.T) (influxdb.DBRPMappingService, func()), t *testing.T, ) { type args struct { - Cluster, Database, RetentionPolicy string + OrgID platform.ID + ID platform.ID } type wants struct { err error - dbrpMappings []*platform.DBRPMapping + dbrpMappings []*influxdb.DBRPMapping } tests := []struct { @@ -727,11 +1484,11 @@ func DeleteDBRPMapping( wants wants }{ { - name: "delete existing dbrpMapping", + name: "delete existing dbrp", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ { - Cluster: "cluster1", + ID: 100, Database: "database1", RetentionPolicy: "retention_policy1", Default: false, @@ -739,7 +1496,7 @@ func DeleteDBRPMapping( BucketID: MustIDBase16(dbrpBucket1ID), }, { - Cluster: "cluster2", + ID: 200, Database: "database2", RetentionPolicy: "retention_policy2", Default: true, @@ -749,13 +1506,12 @@ func DeleteDBRPMapping( }, }, args: args{ - Cluster: "cluster1", - Database: "database1", - RetentionPolicy: "retention_policy1", + OrgID: MustIDBase16(dbrpOrg1ID), + ID: 100, }, wants: wants{ - dbrpMappings: []*platform.DBRPMapping{{ - Cluster: "cluster2", + dbrpMappings: []*influxdb.DBRPMapping{{ + ID: 200, Database: "database2", RetentionPolicy: "retention_policy2", Default: true, @@ -765,11 +1521,99 @@ func DeleteDBRPMapping( }, }, { - name: "delete dbrpMappings using key that does not exist", + name: "delete default dbrp", + fields: DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 200, + Database: "database1", + RetentionPolicy: "retention_policy2", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 300, + Database: "database1", + RetentionPolicy: "retention_policy3", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 400, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg2ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 500, + Database: "database1", + RetentionPolicy: "retention_policy2", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg2ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + }, + }, + args: args{ + OrgID: MustIDBase16(dbrpOrg1ID), + ID: 200, + }, + wants: wants{ + dbrpMappings: []*influxdb.DBRPMapping{ + // The first one becomes the default one. + { + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 300, + Database: "database1", + RetentionPolicy: "retention_policy3", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 400, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg2ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 500, + Database: "database1", + RetentionPolicy: "retention_policy2", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg2ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + }, + }, + }, + { + name: "delete non-existing dbrp", fields: DBRPMappingFields{ - DBRPMappings: []*platform.DBRPMapping{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ { - Cluster: "cluster1", + ID: 100, Database: "database1", RetentionPolicy: "retention_policy1", Default: false, @@ -777,7 +1621,7 @@ func DeleteDBRPMapping( BucketID: MustIDBase16(dbrpBucket1ID), }, { - Cluster: "cluster2", + ID: 200, Database: "database2", RetentionPolicy: "retention_policy2", Default: true, @@ -787,22 +1631,21 @@ func DeleteDBRPMapping( }, }, args: args{ - Cluster: "cluster3", - Database: "db", - RetentionPolicy: "rp", + OrgID: MustIDBase16(dbrpOrg2ID), + ID: 100, }, wants: wants{ - dbrpMappings: []*platform.DBRPMapping{ + dbrpMappings: []*influxdb.DBRPMapping{ { - Cluster: "cluster1", + ID: 100, Database: "database1", RetentionPolicy: "retention_policy1", - Default: false, + Default: true, OrganizationID: MustIDBase16(dbrpOrg1ID), BucketID: MustIDBase16(dbrpBucket1ID), }, { - Cluster: "cluster2", + ID: 200, Database: "database2", RetentionPolicy: "retention_policy2", Default: true, @@ -819,7 +1662,7 @@ func DeleteDBRPMapping( s, done := init(tt.fields, t) defer done() ctx := context.Background() - err := s.Delete(ctx, tt.args.Cluster, tt.args.Database, tt.args.RetentionPolicy) + err := s.Delete(ctx, tt.args.OrgID, tt.args.ID) if (err != nil) != (tt.wants.err != nil) { t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err) } @@ -830,21 +1673,106 @@ func DeleteDBRPMapping( } } - filter := platform.DBRPMappingFilter{} + filter := influxdb.DBRPMappingFilter{} dbrpMappings, _, err := s.FindMany(ctx, filter) if err != nil { - t.Fatalf("failed to retrieve dbrpMappings: %v", err) + t.Fatalf("failed to retrieve dbrps: %v", err) } - if diff := cmp.Diff(dbrpMappings, tt.wants.dbrpMappings, dbrpMappingCmpOptions...); diff != "" { + if diff := cmp.Diff(dbrpMappings, tt.wants.dbrpMappings, DBRPMappingCmpOptions...); diff != "" { t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) } }) } } -func strPtr(s string) *string { - return &s -} -func boolPtr(b bool) *bool { - return &b +func MiscDBRPMappingV2( + init func(DBRPMappingFields, *testing.T) (influxdb.DBRPMappingService, func()), + t *testing.T, +) { + fields := DBRPMappingFields{ + DBRPMappingsV2: []*influxdb.DBRPMapping{ + { + ID: 100, + Database: "database1", + RetentionPolicy: "retention_policy1", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg1ID), + BucketID: MustIDBase16(dbrpBucket1ID), + }, + { + ID: 200, + Database: "database2", + RetentionPolicy: "retention_policy2", + Default: true, + OrganizationID: MustIDBase16(dbrpOrg2ID), + BucketID: MustIDBase16(dbrpBucket2ID), + }, + }, + } + s, done := init(fields, t) + defer done() + ctx := context.Background() + + t.Run("defaults are ok", func(t *testing.T) { + if !fields.DBRPMappingsV2[0].Default || !fields.DBRPMappingsV2[1].Default { + t.Errorf("should be default") + } + }) + + t.Run("what is inited is present", func(t *testing.T) { + filter := influxdb.DBRPMappingFilter{} + dbrpMappings, _, err := s.FindMany(ctx, filter) + if err != nil { + t.Fatalf("failed to retrieve dbrps: %v", err) + } + if diff := cmp.Diff(dbrpMappings, fields.DBRPMappingsV2, DBRPMappingCmpOptions...); diff != "" { + t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) + } + }) + + t.Run("delete works", func(t *testing.T) { + err := s.Delete(ctx, fields.DBRPMappingsV2[0].OrganizationID, fields.DBRPMappingsV2[0].ID) + if err != nil { + t.Fatalf("failed to delete: %v", err) + } + err = s.Delete(ctx, fields.DBRPMappingsV2[1].OrganizationID, fields.DBRPMappingsV2[1].ID) + if err != nil { + t.Fatalf("failed to delete: %v", err) + } + }) + + t.Run("nothing left", func(t *testing.T) { + filter := influxdb.DBRPMappingFilter{} + dbrpMappings, _, err := s.FindMany(ctx, filter) + if err != nil { + t.Fatalf("failed to retrieve dbrps: %v", err) + } + if diff := cmp.Diff(dbrpMappings, []*influxdb.DBRPMapping{}, DBRPMappingCmpOptions...); diff != "" { + t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) + } + }) + + t.Run("new one is still ok", func(t *testing.T) { + m := &influxdb.DBRPMapping{ + ID: 300, + Database: "database2", + RetentionPolicy: "retention_policy2", + Default: false, + OrganizationID: MustIDBase16(dbrpOrg2ID), + BucketID: MustIDBase16(dbrpBucket2ID), + } + if err := s.Create(ctx, m); err != nil { + t.Fatalf("failed to create: %v", err) + } + got, err := s.FindByID(ctx, m.OrganizationID, m.ID) + if err != nil { + t.Fatalf("failed to retrieve dbrp: %v", err) + } + if diff := cmp.Diff(m, got, DBRPMappingCmpOptions...); diff != "" { + t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) + } + if !m.Default { + t.Errorf("should be default") + } + }) } diff --git a/testing/dbrp_mapping_v2.go b/testing/dbrp_mapping_v2.go deleted file mode 100644 index 04cce4d6175..00000000000 --- a/testing/dbrp_mapping_v2.go +++ /dev/null @@ -1,1768 +0,0 @@ -package testing - -import ( - "bytes" - "context" - "fmt" - "sort" - "testing" - - "github.com/influxdata/influxdb/v2/kit/platform" - errors2 "github.com/influxdata/influxdb/v2/kit/platform/errors" - - "github.com/google/go-cmp/cmp" - "github.com/influxdata/influxdb/v2" - "github.com/influxdata/influxdb/v2/dbrp" - "github.com/influxdata/influxdb/v2/mock" - "github.com/pkg/errors" -) - -var DBRPMappingCmpOptionsV2 = cmp.Options{ - cmp.Comparer(func(x, y []byte) bool { - return bytes.Equal(x, y) - }), - cmp.Transformer("Sort", func(in []*influxdb.DBRPMappingV2) []*influxdb.DBRPMappingV2 { - out := make([]*influxdb.DBRPMappingV2, len(in)) - copy(out, in) // Copy input slice to avoid mutating it - sort.Slice(out, func(i, j int) bool { - return out[i].ID < out[j].ID - }) - return out - }), -} - -type DBRPMappingFieldsV2 struct { - BucketSvc influxdb.BucketService - DBRPMappingsV2 []*influxdb.DBRPMappingV2 -} - -// Populate creates all entities in DBRPMappingFieldsV2. -func (f DBRPMappingFieldsV2) Populate(ctx context.Context, s influxdb.DBRPMappingServiceV2) error { - for _, m := range f.DBRPMappingsV2 { - if err := s.Create(ctx, m); err != nil { - return errors.Wrap(err, "failed to populate dbrp mappings") - } - } - return nil -} - -// DBRPMappingServiceV2 tests all the service functions. -func DBRPMappingServiceV2( - init func(DBRPMappingFieldsV2, *testing.T) (influxdb.DBRPMappingServiceV2, func()), - t *testing.T, -) { - tests := []struct { - name string - fn func(init func(DBRPMappingFieldsV2, *testing.T) (influxdb.DBRPMappingServiceV2, func()), - t *testing.T) - }{ - { - name: "create", - fn: CreateDBRPMappingV2, - }, - { - name: "find by ID", - fn: FindDBRPMappingByIDV2, - }, - { - name: "find", - fn: FindManyDBRPMappingsV2, - }, - { - name: "update", - fn: UpdateDBRPMappingV2, - }, - { - name: "delete", - fn: DeleteDBRPMappingV2, - }, - { - name: "miscellaneous", - fn: MiscDBRPMappingV2, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - tt := tt - t.Parallel() - tt.fn(init, t) - }) - } -} - -// CleanupDBRPMappingsV2 finds and removes all dbrp mappings. -func CleanupDBRPMappingsV2(ctx context.Context, s influxdb.DBRPMappingServiceV2) error { - mappings, _, err := s.FindMany(ctx, influxdb.DBRPMappingFilterV2{}) - if err != nil { - return errors.Wrap(err, "failed to retrieve all dbrp mappings") - } - - for _, m := range mappings { - if err := s.Delete(ctx, m.OrganizationID, m.ID); err != nil { - return errors.Wrapf(err, "failed to remove dbrp mapping %v", m.ID) - } - } - return nil -} - -func CreateDBRPMappingV2( - init func(DBRPMappingFieldsV2, *testing.T) (influxdb.DBRPMappingServiceV2, func()), - t *testing.T, -) { - type args struct { - dbrpMapping *influxdb.DBRPMappingV2 - } - type wants struct { - err error - dbrpMappings []*influxdb.DBRPMappingV2 - } - - tests := []struct { - name string - fields DBRPMappingFieldsV2 - args args - wants wants - }{ - { - name: "basic create dbrp", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{}, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - // If there is only one mapping for a database, that is the default one. - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - }, - { - name: "create mapping for same db does not change default", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - }, - }, - { - name: "create mapping for same db changes default", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - }, - }, - { - name: "error on create existing dbrp with same ID", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{{ - OrganizationID: MustIDBase16(dbrpOrg1ID), - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - // NOTE(affo): in the "same ID" concept, orgID must match too! - OrganizationID: MustIDBase16(dbrpOrg1ID), - ID: 100, - Database: "database2", - RetentionPolicy: "retention_policy2", - Default: false, - BucketID: MustIDBase16(dbrpBucket2ID), - }, - }, - wants: wants{ - err: dbrp.ErrDBRPAlreadyExists("dbrp already exist for this particular ID. If you are trying an update use the right function .Update"), - dbrpMappings: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - }, - { - name: "error on create dbrp with same orgID, db and rp", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{{ - OrganizationID: MustIDBase16(dbrpOrg1ID), - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - OrganizationID: MustIDBase16(dbrpOrg1ID), - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - BucketID: MustIDBase16(dbrpBucket2ID), - }, - }, - wants: wants{ - err: dbrp.ErrDBRPAlreadyExists("another DBRP mapping with same orgID, db, and rp exists"), - dbrpMappings: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - }, - { - name: "error bucket does not exist", - fields: DBRPMappingFieldsV2{ - BucketSvc: &mock.BucketService{ - FindBucketByIDFn: func(ctx context.Context, id platform.ID) (*influxdb.Bucket, error) { - if id == MustIDBase16(dbrpBucket2ID) { - return nil, &errors2.Error{ - Code: errors2.ENotFound, - Msg: "bucket not found", - } - } - return nil, nil - }, - }, - DBRPMappingsV2: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket2ID), - }, - }, - wants: wants{ - err: &errors2.Error{ - Code: errors2.ENotFound, - Msg: "bucket not found", - }, - dbrpMappings: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s, done := init(tt.fields, t) - defer done() - ctx := context.Background() - err := s.Create(ctx, tt.args.dbrpMapping) - if (err != nil) != (tt.wants.err != nil) { - t.Errorf("expected error '%v' got '%v'", tt.wants.err, err) - } - - if err != nil && tt.wants.err != nil { - if err.Error() != tt.wants.err.Error() { - t.Errorf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error()) - } - } - - dbrpMappings, n, err := s.FindMany(ctx, influxdb.DBRPMappingFilterV2{}) - if err != nil { - t.Fatalf("failed to retrieve dbrps: %v", err) - } - if n != len(tt.wants.dbrpMappings) { - t.Errorf("want dbrpMappings count of %d, got %d", len(tt.wants.dbrpMappings), n) - } - if diff := cmp.Diff(tt.wants.dbrpMappings, dbrpMappings, DBRPMappingCmpOptionsV2...); diff != "" { - t.Errorf("dbrpMappings are different -want/+got\ndiff %s", diff) - } - }) - } -} - -func FindManyDBRPMappingsV2( - init func(DBRPMappingFieldsV2, *testing.T) (influxdb.DBRPMappingServiceV2, func()), - t *testing.T, -) { - type args struct { - filter influxdb.DBRPMappingFilterV2 - } - - type wants struct { - dbrpMappings []*influxdb.DBRPMappingV2 - err error - } - tests := []struct { - name string - fields DBRPMappingFieldsV2 - args args - wants wants - }{ - { - name: "find all dbrps", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database2", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket2ID), - }, - }, - }, - args: args{ - filter: influxdb.DBRPMappingFilterV2{}, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database2", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket2ID), - }, - }, - }, - }, - { - name: "find by ID", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: MustIDBase16("1111111111111111"), - Database: "database1", - RetentionPolicy: "retention_policyA", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - ID: MustIDBase16("2222222222222222"), - Database: "database2", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - args: args{ - filter: influxdb.DBRPMappingFilterV2{ - ID: MustIDBase16Ptr("1111111111111111"), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: MustIDBase16("1111111111111111"), - Database: "database1", - RetentionPolicy: "retention_policyA", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - }, - }, - }, - { - name: "find by bucket ID", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policyA", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - ID: 200, - Database: "database2", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - args: args{ - filter: influxdb.DBRPMappingFilterV2{ - BucketID: MustIDBase16Ptr(dbrpBucketBID), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: 200, - Database: "database2", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - }, - { - name: "find by orgID", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database", - RetentionPolicy: "retention_policyA", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - ID: 200, - Database: "database", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - { - ID: 300, - Database: "database1", - RetentionPolicy: "retention_policyA", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - { - ID: 400, - Database: "database1", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - { - ID: 500, - Database: "database2", - RetentionPolicy: "retention_policyA", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - args: args{ - filter: influxdb.DBRPMappingFilterV2{ - OrgID: MustIDBase16Ptr(dbrpOrg3ID), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: 300, - Database: "database1", - RetentionPolicy: "retention_policyA", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - { - ID: 400, - Database: "database1", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - { - ID: 500, - Database: "database2", - RetentionPolicy: "retention_policyA", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - }, - { - name: "find by db", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policyA", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - ID: 200, - Database: "database2", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - args: args{ - filter: influxdb.DBRPMappingFilterV2{ - Database: stringPtr("database1"), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policyA", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - }, - }, - }, - { - name: "find by rp", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database", - RetentionPolicy: "retention_policyA", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - ID: 200, - Database: "database", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - args: args{ - filter: influxdb.DBRPMappingFilterV2{ - RetentionPolicy: stringPtr("retention_policyB"), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: 200, - Database: "database", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - }, - { - name: "find by default", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database", - RetentionPolicy: "retention_policyA", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - ID: 200, - Database: "database", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - args: args{ - filter: influxdb.DBRPMappingFilterV2{ - Default: boolPtr(true), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: 200, - Database: "database", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - }, - { - name: "find default", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: MustIDBase16("0000000000000100"), - Database: "database", - RetentionPolicy: "retention_policyA", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - ID: MustIDBase16("0000000000000200"), - Database: "database", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - { - ID: MustIDBase16("0000000000000300"), - Database: "database", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - args: args{ - filter: influxdb.DBRPMappingFilterV2{ - OrgID: MustIDBase16Ptr(dbrpOrg3ID), - Database: stringPtr("database"), - Default: boolPtr(true), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: MustIDBase16("0000000000000200"), - Database: "database", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - }, - { - name: "mixed", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policyA", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - ID: 200, - Database: "database2", - RetentionPolicy: "retention_policyA", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - ID: 300, - Database: "database1", - RetentionPolicy: "retention_policyA", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - // This one will substitute 200 as default for "database2". - { - ID: 400, - Database: "database2", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - args: args{ - filter: influxdb.DBRPMappingFilterV2{ - RetentionPolicy: stringPtr("retention_policyA"), - Default: boolPtr(true), - OrgID: MustIDBase16Ptr(dbrpOrg3ID), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policyA", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - }, - }, - }, - { - name: "not found", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - Database: "database1", - RetentionPolicy: "retention_policyA", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - Database: "database1", - RetentionPolicy: "retention_policyB", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - Database: "database1", - RetentionPolicy: "retention_policyA", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - Database: "database2", - RetentionPolicy: "retention_policyB", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketBID), - }, - }, - }, - args: args{ - filter: influxdb.DBRPMappingFilterV2{ - Database: stringPtr("database1"), - RetentionPolicy: stringPtr("retention_policyC"), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{}, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s, done := init(tt.fields, t) - defer done() - ctx := context.Background() - - dbrpMappings, _, err := s.FindMany(ctx, tt.args.filter) - if (err != nil) != (tt.wants.err != nil) { - t.Fatalf("expected errors to be equal '%v' got '%v'", tt.wants.err, err) - } - - if err != nil && tt.wants.err != nil { - if err.Error() != tt.wants.err.Error() { - t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err) - } - } - - if diff := cmp.Diff(dbrpMappings, tt.wants.dbrpMappings, DBRPMappingCmpOptionsV2...); diff != "" { - t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) - } - }) - } -} - -func FindDBRPMappingByIDV2( - init func(DBRPMappingFieldsV2, *testing.T) (influxdb.DBRPMappingServiceV2, func()), - t *testing.T, -) { - type args struct { - OrgID platform.ID - ID platform.ID - } - - type wants struct { - dbrpMapping *influxdb.DBRPMappingV2 - err error - } - - tests := []struct { - name string - fields DBRPMappingFieldsV2 - args args - wants wants - }{ - { - name: "find existing dbrp", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database", - RetentionPolicy: "retention_policyA", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - ID: 200, - Database: "database", - RetentionPolicy: "retention_policyB", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - { - ID: 300, - Database: "database", - RetentionPolicy: "retention_policyC", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - }, - }, - args: args{ - OrgID: MustIDBase16(dbrpOrg3ID), - ID: 200, - }, - wants: wants{ - dbrpMapping: &influxdb.DBRPMappingV2{ - ID: 200, - Database: "database", - RetentionPolicy: "retention_policyB", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - }, - }, - { - name: "find non existing dbrp", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database", - RetentionPolicy: "retention_policyA", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - }, - }, - args: args{ - OrgID: MustIDBase16(dbrpOrg3ID), - ID: 200, - }, - wants: wants{ - err: dbrp.ErrDBRPNotFound, - }, - }, - { - name: "find existing dbrp but wrong orgID", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database", - RetentionPolicy: "retention_policyA", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg3ID), - BucketID: MustIDBase16(dbrpBucketAID), - }, - }, - }, - args: args{ - OrgID: MustIDBase16(dbrpOrg2ID), - ID: 100, - }, - wants: wants{ - err: dbrp.ErrDBRPNotFound, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s, done := init(tt.fields, t) - defer done() - ctx := context.Background() - - dbrpMapping, err := s.FindByID(ctx, tt.args.OrgID, tt.args.ID) - if (err != nil) != (tt.wants.err != nil) { - t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err) - } - - if err != nil && tt.wants.err != nil { - if err.Error() != tt.wants.err.Error() { - t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error()) - } - } - - if diff := cmp.Diff(dbrpMapping, tt.wants.dbrpMapping, DBRPMappingCmpOptionsV2...); diff != "" { - t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) - } - }) - } -} - -func UpdateDBRPMappingV2( - init func(DBRPMappingFieldsV2, *testing.T) (influxdb.DBRPMappingServiceV2, func()), - t *testing.T, -) { - type args struct { - dbrpMapping *influxdb.DBRPMappingV2 - } - type wants struct { - err error - dbrpMappings []*influxdb.DBRPMappingV2 - } - - tests := []struct { - name string - fields DBRPMappingFieldsV2 - args args - wants wants - }{ - { - name: "basic update", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - }, - { - name: "update invalid dbrp", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - ID: 100, - Database: "./", // invalid db name. - RetentionPolicy: "retention_policy2", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket2ID), - }, - }, - wants: wants{ - err: dbrp.ErrInvalidDBRP(fmt.Errorf("database must contain at least one character and only be letters, numbers, '_', '-', and '.'")), - dbrpMappings: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - }, - { - name: "error dbrp not found", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket2ID), - }, - }, - wants: wants{ - err: dbrp.ErrDBRPNotFound, - dbrpMappings: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - }, - { - name: "update unchangeable fields", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - ID: 100, - Database: "wont_change", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket2ID), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - }, - { - name: "update to same orgID, db, and rp", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - wants: wants{ - err: dbrp.ErrDBRPAlreadyExists("another DBRP mapping with same orgID, db, and rp exists"), - dbrpMappings: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - }, - }, - { - name: "update default when only one dbrp is present", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - ID: 100, - Database: "wont_change", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket2ID), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{{ - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }}, - }, - }, - { - name: "set default when more dbrps are present", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 300, - Database: "database1", - RetentionPolicy: "retention_policy3", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 300, - Database: "database1", - RetentionPolicy: "retention_policy3", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - }, - }, - { - name: "unset default when more dbrps are present", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 300, - Database: "database1", - RetentionPolicy: "retention_policy3", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - }, - args: args{ - dbrpMapping: &influxdb.DBRPMappingV2{ - ID: 300, - Database: "database1", - RetentionPolicy: "retention_policy3", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 300, - Database: "database1", - RetentionPolicy: "retention_policy3", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s, done := init(tt.fields, t) - defer done() - ctx := context.Background() - err := s.Update(ctx, tt.args.dbrpMapping) - if (err != nil) != (tt.wants.err != nil) { - t.Errorf("expected error '%v' got '%v'", tt.wants.err, err) - } - - if err != nil && tt.wants.err != nil { - if err.Error() != tt.wants.err.Error() { - t.Errorf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error()) - } - } - - dbrpMappings, _, err := s.FindMany(ctx, influxdb.DBRPMappingFilterV2{}) - if err != nil { - t.Fatalf("failed to retrieve dbrps: %v", err) - } - if diff := cmp.Diff(dbrpMappings, tt.wants.dbrpMappings, DBRPMappingCmpOptionsV2...); diff != "" { - t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) - } - }) - } -} - -func DeleteDBRPMappingV2( - init func(DBRPMappingFieldsV2, *testing.T) (influxdb.DBRPMappingServiceV2, func()), - t *testing.T, -) { - type args struct { - OrgID platform.ID - ID platform.ID - } - type wants struct { - err error - dbrpMappings []*influxdb.DBRPMappingV2 - } - - tests := []struct { - name string - fields DBRPMappingFieldsV2 - args args - wants wants - }{ - { - name: "delete existing dbrp", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database2", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket2ID), - }, - }, - }, - args: args{ - OrgID: MustIDBase16(dbrpOrg1ID), - ID: 100, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{{ - ID: 200, - Database: "database2", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket2ID), - }}, - }, - }, - { - name: "delete default dbrp", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 300, - Database: "database1", - RetentionPolicy: "retention_policy3", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 400, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 500, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - }, - args: args{ - OrgID: MustIDBase16(dbrpOrg1ID), - ID: 200, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - // The first one becomes the default one. - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 300, - Database: "database1", - RetentionPolicy: "retention_policy3", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 400, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 500, - Database: "database1", - RetentionPolicy: "retention_policy2", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - }, - }, - }, - { - name: "delete non-existing dbrp", - fields: DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database2", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket2ID), - }, - }, - }, - args: args{ - OrgID: MustIDBase16(dbrpOrg2ID), - ID: 100, - }, - wants: wants{ - dbrpMappings: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database2", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket2ID), - }, - }, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s, done := init(tt.fields, t) - defer done() - ctx := context.Background() - err := s.Delete(ctx, tt.args.OrgID, tt.args.ID) - if (err != nil) != (tt.wants.err != nil) { - t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err) - } - - if err != nil && tt.wants.err != nil { - if err.Error() != tt.wants.err.Error() { - t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error()) - } - } - - filter := influxdb.DBRPMappingFilterV2{} - dbrpMappings, _, err := s.FindMany(ctx, filter) - if err != nil { - t.Fatalf("failed to retrieve dbrps: %v", err) - } - if diff := cmp.Diff(dbrpMappings, tt.wants.dbrpMappings, DBRPMappingCmpOptionsV2...); diff != "" { - t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) - } - }) - } -} - -func MiscDBRPMappingV2( - init func(DBRPMappingFieldsV2, *testing.T) (influxdb.DBRPMappingServiceV2, func()), - t *testing.T, -) { - fields := DBRPMappingFieldsV2{ - DBRPMappingsV2: []*influxdb.DBRPMappingV2{ - { - ID: 100, - Database: "database1", - RetentionPolicy: "retention_policy1", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg1ID), - BucketID: MustIDBase16(dbrpBucket1ID), - }, - { - ID: 200, - Database: "database2", - RetentionPolicy: "retention_policy2", - Default: true, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket2ID), - }, - }, - } - s, done := init(fields, t) - defer done() - ctx := context.Background() - - t.Run("defaults are ok", func(t *testing.T) { - if !fields.DBRPMappingsV2[0].Default || !fields.DBRPMappingsV2[1].Default { - t.Errorf("should be default") - } - }) - - t.Run("what is inited is present", func(t *testing.T) { - filter := influxdb.DBRPMappingFilterV2{} - dbrpMappings, _, err := s.FindMany(ctx, filter) - if err != nil { - t.Fatalf("failed to retrieve dbrps: %v", err) - } - if diff := cmp.Diff(dbrpMappings, fields.DBRPMappingsV2, DBRPMappingCmpOptionsV2...); diff != "" { - t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) - } - }) - - t.Run("delete works", func(t *testing.T) { - err := s.Delete(ctx, fields.DBRPMappingsV2[0].OrganizationID, fields.DBRPMappingsV2[0].ID) - if err != nil { - t.Fatalf("failed to delete: %v", err) - } - err = s.Delete(ctx, fields.DBRPMappingsV2[1].OrganizationID, fields.DBRPMappingsV2[1].ID) - if err != nil { - t.Fatalf("failed to delete: %v", err) - } - }) - - t.Run("nothing left", func(t *testing.T) { - filter := influxdb.DBRPMappingFilterV2{} - dbrpMappings, _, err := s.FindMany(ctx, filter) - if err != nil { - t.Fatalf("failed to retrieve dbrps: %v", err) - } - if diff := cmp.Diff(dbrpMappings, []*influxdb.DBRPMappingV2{}, DBRPMappingCmpOptionsV2...); diff != "" { - t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) - } - }) - - t.Run("new one is still ok", func(t *testing.T) { - m := &influxdb.DBRPMappingV2{ - ID: 300, - Database: "database2", - RetentionPolicy: "retention_policy2", - Default: false, - OrganizationID: MustIDBase16(dbrpOrg2ID), - BucketID: MustIDBase16(dbrpBucket2ID), - } - if err := s.Create(ctx, m); err != nil { - t.Fatalf("failed to create: %v", err) - } - got, err := s.FindByID(ctx, m.OrganizationID, m.ID) - if err != nil { - t.Fatalf("failed to retrieve dbrp: %v", err) - } - if diff := cmp.Diff(m, got, DBRPMappingCmpOptionsV2...); diff != "" { - t.Errorf("dbrpMappings are different -got/+want\ndiff %s", diff) - } - if !m.Default { - t.Errorf("should be default") - } - }) -} diff --git a/testing/util.go b/testing/util.go index 7eb6ebe9324..6a0bf3c5663 100644 --- a/testing/util.go +++ b/testing/util.go @@ -13,6 +13,13 @@ import ( "github.com/stretchr/testify/require" ) +func strPtr(s string) *string { + return &s +} +func boolPtr(b bool) *bool { + return &b +} + // TODO(goller): remove opPrefix argument func diffPlatformErrors(name string, actual, expected error, opPrefix string, t *testing.T) { t.Helper() diff --git a/tests/client.go b/tests/client.go index 64690469d5a..f419c2262a1 100644 --- a/tests/client.go +++ b/tests/client.go @@ -319,13 +319,13 @@ func (c *Client) MustCreateDBRPMapping(t *testing.T) platform.ID { t.Helper() ctx := context.Background() - m := &influxdb.DBRPMappingV2{ + m := &influxdb.DBRPMapping{ Database: "db", RetentionPolicy: "rp", OrganizationID: c.OrgID, BucketID: c.BucketID, } - if err := c.DBRPMappingServiceV2.Create(ctx, m); err != nil { + if err := c.DBRPMappingService.Create(ctx, m); err != nil { t.Fatalf("unable to create DBRP mapping: %v", err) } return m.ID @@ -422,7 +422,7 @@ func (c *Client) DeleteResource(t *testing.T, r influxdb.ResourceType, id platfo case influxdb.ChecksResourceType: // 16 return c.DeleteCheck(ctx, id) case influxdb.DBRPResourceType: // 17 - return c.DBRPMappingServiceV2.Delete(ctx, c.OrgID, id) + return c.DBRPMappingService.Delete(ctx, c.OrgID, id) } return nil } @@ -535,7 +535,7 @@ func (c *Client) FindAll(t *testing.T, r influxdb.ResourceType) ([]platform.ID, ids = append(ids, r.ID) } case influxdb.DBRPResourceType: // 17 - rs, _, err := c.DBRPMappingServiceV2.FindMany(ctx, influxdb.DBRPMappingFilterV2{OrgID: &c.OrgID}) + rs, _, err := c.DBRPMappingService.FindMany(ctx, influxdb.DBRPMappingFilter{OrgID: &c.OrgID}) if err != nil { return nil, err } diff --git a/v1/coordinator/shard_mapper.go b/v1/coordinator/shard_mapper.go index e93946cc36b..cf3a02c70d1 100644 --- a/v1/coordinator/shard_mapper.go +++ b/v1/coordinator/shard_mapper.go @@ -32,7 +32,7 @@ type LocalShardMapper struct { ShardGroup(ids []uint64) tsdb.ShardGroup } - DBRP influxdb.DBRPMappingServiceV2 + DBRP influxdb.DBRPMappingService } // MapShards maps the sources to the appropriate shards into an IteratorCreator. @@ -63,7 +63,7 @@ func (e *LocalShardMapper) mapShards(ctx context.Context, a *LocalShardMapping, // using. if _, ok := a.ShardMap[source]; !ok { // lookup bucket and create info - mappings, _, err := e.DBRP.FindMany(ctx, influxdb.DBRPMappingFilterV2{ + mappings, _, err := e.DBRP.FindMany(ctx, influxdb.DBRPMappingFilter{ OrgID: &orgID, Database: &s.Database, RetentionPolicy: &s.RetentionPolicy, diff --git a/v1/coordinator/shard_mapper_test.go b/v1/coordinator/shard_mapper_test.go index 87eefbd2bc9..60bf240cb1c 100644 --- a/v1/coordinator/shard_mapper_test.go +++ b/v1/coordinator/shard_mapper_test.go @@ -22,13 +22,13 @@ func TestLocalShardMapper(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - dbrp := mocks.NewMockDBRPMappingServiceV2(ctrl) + dbrp := mocks.NewMockDBRPMappingService(ctrl) orgID := platform.ID(0xff00) bucketID := platform.ID(0xffee) db := "db0" rp := "rp0" - filt := influxdb.DBRPMappingFilterV2{OrgID: &orgID, Database: &db, RetentionPolicy: &rp} - res := []*influxdb.DBRPMappingV2{{Database: db, RetentionPolicy: rp, OrganizationID: orgID, BucketID: bucketID}} + filt := influxdb.DBRPMappingFilter{OrgID: &orgID, Database: &db, RetentionPolicy: &rp} + res := []*influxdb.DBRPMapping{{Database: db, RetentionPolicy: rp, OrganizationID: orgID, BucketID: bucketID}} dbrp.EXPECT(). FindMany(gomock.Any(), filt). Times(2). diff --git a/v1/coordinator/statement_executor.go b/v1/coordinator/statement_executor.go index 7f1173ee95c..6352f090d69 100644 --- a/v1/coordinator/statement_executor.go +++ b/v1/coordinator/statement_executor.go @@ -35,7 +35,7 @@ type StatementExecutor struct { // ShardMapper for mapping shards when executing a SELECT statement. ShardMapper query.ShardMapper - DBRP influxdb.DBRPMappingServiceV2 + DBRP influxdb.DBRPMappingService // Select statement limits MaxSelectPointN int @@ -319,7 +319,7 @@ func (e *StatementExecutor) createIterators(ctx context.Context, stmt *influxql. func (e *StatementExecutor) executeShowDatabasesStatement(ctx context.Context, q *influxql.ShowDatabasesStatement, ectx *query.ExecutionContext) (models.Rows, error) { row := &models.Row{Name: "databases", Columns: []string{"name"}} - dbrps, _, err := e.DBRP.FindMany(ctx, influxdb.DBRPMappingFilterV2{ + dbrps, _, err := e.DBRP.FindMany(ctx, influxdb.DBRPMappingFilter{ OrgID: &ectx.OrgID, }) if err != nil { @@ -349,9 +349,9 @@ func (e *StatementExecutor) executeShowDatabasesStatement(ctx context.Context, q return []*models.Row{row}, nil } -func (e *StatementExecutor) getDefaultRP(ctx context.Context, database string, ectx *query.ExecutionContext) (*influxdb.DBRPMappingV2, error) { +func (e *StatementExecutor) getDefaultRP(ctx context.Context, database string, ectx *query.ExecutionContext) (*influxdb.DBRPMapping, error) { defaultRP := true - mappings, n, err := e.DBRP.FindMany(ctx, influxdb.DBRPMappingFilterV2{ + mappings, n, err := e.DBRP.FindMany(ctx, influxdb.DBRPMappingFilter{ OrgID: &ectx.OrgID, Database: &database, Default: &defaultRP, @@ -440,7 +440,7 @@ func (e *StatementExecutor) executeShowRetentionPoliciesStatement(ctx context.Co return nil, ErrDatabaseNameRequired } - dbrps, _, err := e.DBRP.FindMany(ctx, influxdb.DBRPMappingFilterV2{ + dbrps, _, err := e.DBRP.FindMany(ctx, influxdb.DBRPMappingFilter{ OrgID: &ectx.OrgID, Database: &q.Database, }) @@ -716,7 +716,7 @@ func (e *StatementExecutor) normalizeMeasurement(ctx context.Context, m *influxq } // TODO(sgc): Validate database; fetch default RP - filter := influxdb.DBRPMappingFilterV2{ + filter := influxdb.DBRPMappingFilter{ OrgID: &ectx.OrgID, Database: &m.Database, } @@ -744,7 +744,7 @@ func (e *StatementExecutor) normalizeMeasurement(ctx context.Context, m *influxq return nil } -type mappings []*influxdb.DBRPMappingV2 +type mappings []*influxdb.DBRPMapping func (m mappings) DefaultRetentionPolicy(db string) string { for _, v := range m { diff --git a/v1/coordinator/statement_executor_test.go b/v1/coordinator/statement_executor_test.go index c19aec18544..388493541ea 100644 --- a/v1/coordinator/statement_executor_test.go +++ b/v1/coordinator/statement_executor_test.go @@ -43,11 +43,11 @@ func TestQueryExecutor_ExecuteQuery_SelectStatement(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - dbrp := mocks.NewMockDBRPMappingServiceV2(ctrl) + dbrp := mocks.NewMockDBRPMappingService(ctrl) orgID := platform.ID(0xff00) empty := "" - filt := influxdb.DBRPMappingFilterV2{OrgID: &orgID, Database: &empty, RetentionPolicy: &empty} - res := []*influxdb.DBRPMappingV2{{}} + filt := influxdb.DBRPMappingFilter{OrgID: &orgID, Database: &empty, RetentionPolicy: &empty} + res := []*influxdb.DBRPMapping{{}} dbrp.EXPECT(). FindMany(gomock.Any(), filt). Return(res, 1, nil) @@ -109,11 +109,11 @@ func TestQueryExecutor_ExecuteQuery_MaxSelectBucketsN(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - dbrp := mocks.NewMockDBRPMappingServiceV2(ctrl) + dbrp := mocks.NewMockDBRPMappingService(ctrl) orgID := platform.ID(0xff00) empty := "" - filt := influxdb.DBRPMappingFilterV2{OrgID: &orgID, Database: &empty, RetentionPolicy: &empty} - res := []*influxdb.DBRPMappingV2{{}} + filt := influxdb.DBRPMappingFilter{OrgID: &orgID, Database: &empty, RetentionPolicy: &empty} + res := []*influxdb.DBRPMapping{{}} dbrp.EXPECT(). FindMany(gomock.Any(), filt). Return(res, 1, nil) @@ -227,11 +227,11 @@ func TestStatementExecutor_NormalizeStatement(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - dbrp := mocks.NewMockDBRPMappingServiceV2(ctrl) + dbrp := mocks.NewMockDBRPMappingService(ctrl) orgID := platform.ID(0xff00) bucketID := platform.ID(0xffee) - filt := influxdb.DBRPMappingFilterV2{OrgID: &orgID, Database: &testCase.expectedDB} - res := []*influxdb.DBRPMappingV2{{Database: testCase.expectedDB, RetentionPolicy: testCase.expectedRP, OrganizationID: orgID, BucketID: bucketID, Default: true}} + filt := influxdb.DBRPMappingFilter{OrgID: &orgID, Database: &testCase.expectedDB} + res := []*influxdb.DBRPMapping{{Database: testCase.expectedDB, RetentionPolicy: testCase.expectedRP, OrganizationID: orgID, BucketID: bucketID, Default: true}} dbrp.EXPECT(). FindMany(gomock.Any(), filt). Return(res, 1, nil) @@ -331,10 +331,10 @@ func TestQueryExecutor_ExecuteQuery_ShowDatabases(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - dbrp := mocks.NewMockDBRPMappingServiceV2(ctrl) + dbrp := mocks.NewMockDBRPMappingService(ctrl) orgID := platform.ID(0xff00) - filt := influxdb.DBRPMappingFilterV2{OrgID: &orgID} - res := []*influxdb.DBRPMappingV2{ + filt := influxdb.DBRPMappingFilter{OrgID: &orgID} + res := []*influxdb.DBRPMapping{ {Database: "db1", OrganizationID: orgID, BucketID: 0xffe0}, {Database: "db2", OrganizationID: orgID, BucketID: 0xffe1}, {Database: "db3", OrganizationID: orgID, BucketID: 0xffe2}, @@ -393,7 +393,7 @@ type QueryExecutor struct { MetaClient MetaClient TSDBStore *internal.TSDBStoreMock - DBRP *mocks.MockDBRPMappingServiceV2 + DBRP *mocks.MockDBRPMappingService StatementExecutor *coordinator.StatementExecutor LogOutput bytes.Buffer } @@ -439,7 +439,7 @@ func NewQueryExecutor(t *testing.T, opts ...optFn) *QueryExecutor { type optFn func(qe *QueryExecutor) -func WithDBRP(dbrp *mocks.MockDBRPMappingServiceV2) optFn { +func WithDBRP(dbrp *mocks.MockDBRPMappingService) optFn { return func(qe *QueryExecutor) { qe.DBRP = dbrp }