2018-09-11 22:56:51 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
2019-09-18 18:32:45 +00:00
|
|
|
"context"
|
|
|
|
|
2018-09-11 22:56:51 +00:00
|
|
|
"github.com/influxdata/flux"
|
2019-04-10 21:40:05 +00:00
|
|
|
"github.com/influxdata/flux/ast"
|
|
|
|
"github.com/influxdata/flux/lang"
|
2019-01-08 00:37:16 +00:00
|
|
|
platform "github.com/influxdata/influxdb"
|
2018-09-11 22:56:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// BucketAwareOperationSpec specifies an operation that reads or writes buckets
|
|
|
|
type BucketAwareOperationSpec interface {
|
2019-03-08 21:18:11 +00:00
|
|
|
BucketsAccessed(orgID *platform.ID) (readBuckets, writeBuckets []platform.BucketFilter)
|
2018-09-11 22:56:51 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 18:32:45 +00:00
|
|
|
type constantSecretService struct{}
|
|
|
|
|
|
|
|
func (s constantSecretService) LoadSecret(ctx context.Context, k string) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2019-09-19 15:31:13 +00:00
|
|
|
func newDeps() flux.Dependencies {
|
|
|
|
deps := flux.NewDefaultDependencies()
|
2019-09-18 18:32:45 +00:00
|
|
|
deps.Deps.HTTPClient = nil
|
|
|
|
deps.Deps.URLValidator = nil
|
|
|
|
deps.Deps.SecretService = constantSecretService{}
|
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
2018-09-11 22:56:51 +00:00
|
|
|
// BucketsAccessed returns the set of buckets read and written by a query spec
|
2019-04-10 21:40:05 +00:00
|
|
|
func BucketsAccessed(ast *ast.Package, orgID *platform.ID) (readBuckets, writeBuckets []platform.BucketFilter, err error) {
|
2019-09-19 15:31:13 +00:00
|
|
|
ctx := newDeps().Inject(context.Background())
|
|
|
|
err = lang.WalkIR(ctx, ast, func(o *flux.Operation) error {
|
2018-09-11 22:56:51 +00:00
|
|
|
bucketAwareOpSpec, ok := o.Spec.(BucketAwareOperationSpec)
|
|
|
|
if ok {
|
2019-03-08 21:18:11 +00:00
|
|
|
opBucketsRead, opBucketsWritten := bucketAwareOpSpec.BucketsAccessed(orgID)
|
2018-09-11 22:56:51 +00:00
|
|
|
readBuckets = append(readBuckets, opBucketsRead...)
|
|
|
|
writeBuckets = append(writeBuckets, opBucketsWritten...)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return readBuckets, writeBuckets, nil
|
|
|
|
}
|