fix(query): add LookupName method to dependency interfaces (needed by flux to()) (#14498)

pull/14538/head
Christopher M. Wolff 2019-08-01 10:10:43 -07:00 committed by GitHub
parent 0cefcf1597
commit e142f6ca7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 22 deletions

41
mock/dependencies.go Normal file
View File

@ -0,0 +1,41 @@
package mock
import (
"context"
platform "github.com/influxdata/influxdb"
)
// BucketLookup implements the BucketLookup interface needed by flux "from" and "to".
type BucketLookup struct{}
func (BucketLookup) Lookup(_ context.Context, orgID platform.ID, name string) (platform.ID, bool) {
if name == "my-bucket" {
return platform.ID(1), true
}
return platform.InvalidID(), false
}
func (BucketLookup) LookupName(_ context.Context, orgID platform.ID, id platform.ID) string {
if id == 1 {
return "my-bucket"
}
return ""
}
// OrganizationLookup implements the OrganizationLookup interface needed by flux "from" and "to".
type OrganizationLookup struct{}
func (OrganizationLookup) Lookup(_ context.Context, name string) (platform.ID, bool) {
if name == "my-org" {
return platform.ID(2), true
}
return platform.InvalidID(), false
}
func (OrganizationLookup) LookupName(_ context.Context, id platform.ID) string {
if id == 2 {
return "my-org"
}
return ""
}

View File

@ -31,6 +31,21 @@ func (b *BucketLookup) Lookup(ctx context.Context, orgID platform.ID, name strin
return bucket.ID, true
}
// LookupName returns an bucket name given its organization ID and its bucket ID.
func (b *BucketLookup) LookupName(ctx context.Context, orgID platform.ID, id platform.ID) string {
oid := platform.ID(orgID)
id = platform.ID(id)
filter := platform.BucketFilter{
OrganizationID: &oid,
ID: &id,
}
bucket, err := b.BucketService.FindBucket(ctx, filter)
if err != nil || bucket == nil {
return ""
}
return bucket.Name
}
func (b *BucketLookup) FindAllBuckets(ctx context.Context, orgID platform.ID) ([]*platform.Bucket, int) {
oid := platform.ID(orgID)
filter := platform.BucketFilter{
@ -66,3 +81,19 @@ func (o *OrganizationLookup) Lookup(ctx context.Context, name string) (platform.
}
return org.ID, true
}
// LookupName returns an organization name given its ID.
func (o *OrganizationLookup) LookupName(ctx context.Context, id platform.ID) string {
id = platform.ID(id)
org, err := o.OrganizationService.FindOrganization(
ctx,
platform.OrganizationFilter{
ID: &id,
},
)
if err != nil || org == nil {
return ""
}
return org.Name
}

View File

@ -20,10 +20,12 @@ type HostLookup interface {
type BucketLookup interface {
Lookup(ctx context.Context, orgID platform.ID, name string) (platform.ID, bool)
LookupName(ctx context.Context, orgID platform.ID, id platform.ID) string
}
type OrganizationLookup interface {
Lookup(ctx context.Context, name string) (platform.ID, bool)
LookupName(ctx context.Context, id platform.ID) string
}
type Dependencies struct {

View File

@ -116,8 +116,8 @@ func TestToOpSpec_BucketsAccessed(t *testing.T) {
}
func TestTo_Process(t *testing.T) {
oid, _ := (mockOrgLookup{}).Lookup(context.Background(), "my-org")
bid, _ := (mockBucketLookup{}).Lookup(context.Background(), oid, "my-bucket")
oid, _ := mock.OrganizationLookup{}.Lookup(context.Background(), "my-org")
bid, _ := mock.BucketLookup{}.Lookup(context.Background(), oid, "my-bucket")
type wanted struct {
result *mock.PointsWriter
tables []*executetest.Table
@ -739,30 +739,12 @@ c _hello=4 41`),
func mockDependencies() influxdb.ToDependencies {
return influxdb.ToDependencies{
BucketLookup: mockBucketLookup{},
OrganizationLookup: mockOrgLookup{},
BucketLookup: mock.BucketLookup{},
OrganizationLookup: mock.OrganizationLookup{},
PointsWriter: new(mock.PointsWriter),
}
}
type mockBucketLookup struct{}
func (mockBucketLookup) Lookup(_ context.Context, orgID platform.ID, name string) (platform.ID, bool) {
if name == "my-bucket" {
return platform.ID(1), true
}
return platform.InvalidID(), false
}
type mockOrgLookup struct{}
func (mockOrgLookup) Lookup(_ context.Context, name string) (platform.ID, bool) {
if name == "my-org" {
return platform.ID(2), true
}
return platform.InvalidID(), false
}
func pointsToStr(points []models.Point) string {
outStr := ""
for _, x := range points {