feat(influxdb): add scraper filter

pull/13388/head
Kelvin Wang 2019-04-12 12:45:48 -04:00
parent 54525e10fc
commit 717fcc44b3
17 changed files with 735 additions and 362 deletions

View File

@ -12,11 +12,15 @@ var _ influxdb.ScraperTargetStoreService = (*ScraperTargetStoreService)(nil)
// against it appropriately.
type ScraperTargetStoreService struct {
influxdb.UserResourceMappingService
influxdb.OrganizationService
s influxdb.ScraperTargetStoreService
}
// NewScraperTargetStoreService constructs an instance of an authorizing scraper target store serivce.
func NewScraperTargetStoreService(s influxdb.ScraperTargetStoreService, urm influxdb.UserResourceMappingService) *ScraperTargetStoreService {
func NewScraperTargetStoreService(s influxdb.ScraperTargetStoreService,
urm influxdb.UserResourceMappingService,
org influxdb.OrganizationService,
) *ScraperTargetStoreService {
return &ScraperTargetStoreService{
UserResourceMappingService: urm,
s: s,
@ -68,10 +72,10 @@ func (s *ScraperTargetStoreService) GetTargetByID(ctx context.Context, id influx
}
// ListTargets retrieves all scraper targets that match the provided filter and then filters the list down to only the resources that are authorized.
func (s *ScraperTargetStoreService) ListTargets(ctx context.Context) ([]influxdb.ScraperTarget, error) {
func (s *ScraperTargetStoreService) ListTargets(ctx context.Context, filter influxdb.ScraperTargetFilter) ([]influxdb.ScraperTarget, error) {
// TODO: we'll likely want to push this operation into the database eventually since fetching the whole list of data
// will likely be expensive.
ss, err := s.s.ListTargets(ctx)
ss, err := s.s.ListTargets(ctx, filter)
if err != nil {
return nil, err
}

View File

@ -104,7 +104,7 @@ func TestScraperTargetStoreService_GetTargetByID(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := authorizer.NewScraperTargetStoreService(tt.fields.ScraperTargetStoreService, mock.NewUserResourceMappingService())
s := authorizer.NewScraperTargetStoreService(tt.fields.ScraperTargetStoreService, mock.NewUserResourceMappingService(), mock.NewOrganizationService())
ctx := context.Background()
ctx = influxdbcontext.SetAuthorizer(ctx, &Authorizer{[]influxdb.Permission{tt.args.permission}})
@ -137,7 +137,7 @@ func TestScraperTargetStoreService_ListTargets(t *testing.T) {
name: "authorized to see all scrapers",
fields: fields{
ScraperTargetStoreService: &mock.ScraperTargetStoreService{
ListTargetsF: func(ctx context.Context) ([]influxdb.ScraperTarget, error) {
ListTargetsF: func(ctx context.Context, filter influxdb.ScraperTargetFilter) ([]influxdb.ScraperTarget, error) {
return []influxdb.ScraperTarget{
{
ID: 1,
@ -184,7 +184,7 @@ func TestScraperTargetStoreService_ListTargets(t *testing.T) {
name: "authorized to access a single orgs scrapers",
fields: fields{
ScraperTargetStoreService: &mock.ScraperTargetStoreService{
ListTargetsF: func(ctx context.Context) ([]influxdb.ScraperTarget, error) {
ListTargetsF: func(ctx context.Context, filter influxdb.ScraperTargetFilter) ([]influxdb.ScraperTarget, error) {
return []influxdb.ScraperTarget{
{
ID: 1,
@ -228,12 +228,13 @@ func TestScraperTargetStoreService_ListTargets(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := authorizer.NewScraperTargetStoreService(tt.fields.ScraperTargetStoreService, mock.NewUserResourceMappingService())
s := authorizer.NewScraperTargetStoreService(tt.fields.ScraperTargetStoreService, mock.NewUserResourceMappingService(),
mock.NewOrganizationService())
ctx := context.Background()
ctx = influxdbcontext.SetAuthorizer(ctx, &Authorizer{[]influxdb.Permission{tt.args.permission}})
ts, err := s.ListTargets(ctx)
ts, err := s.ListTargets(ctx, influxdb.ScraperTargetFilter{})
influxdbtesting.ErrorsEqual(t, err, tt.wants.err)
if diff := cmp.Diff(ts, tt.wants.scrapers, scraperCmpOptions...); diff != "" {
@ -343,7 +344,8 @@ func TestScraperTargetStoreService_UpdateTarget(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := authorizer.NewScraperTargetStoreService(tt.fields.ScraperTargetStoreService, mock.NewUserResourceMappingService())
s := authorizer.NewScraperTargetStoreService(tt.fields.ScraperTargetStoreService, mock.NewUserResourceMappingService(),
mock.NewOrganizationService())
ctx := context.Background()
ctx = influxdbcontext.SetAuthorizer(ctx, &Authorizer{tt.args.permissions})
@ -448,7 +450,8 @@ func TestScraperTargetStoreService_RemoveTarget(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := authorizer.NewScraperTargetStoreService(tt.fields.ScraperTargetStoreService, mock.NewUserResourceMappingService())
s := authorizer.NewScraperTargetStoreService(tt.fields.ScraperTargetStoreService, mock.NewUserResourceMappingService(),
mock.NewOrganizationService())
ctx := context.Background()
ctx = influxdbcontext.SetAuthorizer(ctx, &Authorizer{tt.args.permissions})
@ -530,7 +533,8 @@ func TestScraperTargetStoreService_AddTarget(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := authorizer.NewScraperTargetStoreService(tt.fields.ScraperTargetStoreService, mock.NewUserResourceMappingService())
s := authorizer.NewScraperTargetStoreService(tt.fields.ScraperTargetStoreService, mock.NewUserResourceMappingService(),
mock.NewOrganizationService())
ctx := context.Background()
ctx = influxdbcontext.SetAuthorizer(ctx, &Authorizer{[]influxdb.Permission{tt.args.permission}})

View File

@ -5,14 +5,14 @@ import (
"encoding/json"
bolt "github.com/coreos/bbolt"
platform "github.com/influxdata/influxdb"
influxdb "github.com/influxdata/influxdb"
)
var (
scraperBucket = []byte("scraperv2")
)
var _ platform.ScraperTargetStoreService = (*Client)(nil)
var _ influxdb.ScraperTargetStoreService = (*Client)(nil)
func (c *Client) initializeScraperTargets(ctx context.Context, tx *bolt.Tx) error {
if _, err := tx.CreateBucketIfNotExists([]byte(scraperBucket)); err != nil {
@ -22,22 +22,51 @@ func (c *Client) initializeScraperTargets(ctx context.Context, tx *bolt.Tx) erro
}
// ListTargets will list all scrape targets.
func (c *Client) ListTargets(ctx context.Context) (list []platform.ScraperTarget, err error) {
list = make([]platform.ScraperTarget, 0)
func (c *Client) ListTargets(ctx context.Context, filter influxdb.ScraperTargetFilter) (list []influxdb.ScraperTarget, err error) {
list = make([]influxdb.ScraperTarget, 0)
err = c.db.View(func(tx *bolt.Tx) (err error) {
cur := tx.Bucket(scraperBucket).Cursor()
for k, v := cur.First(); k != nil; k, v = cur.Next() {
target := new(platform.ScraperTarget)
target := new(influxdb.ScraperTarget)
if err = json.Unmarshal(v, target); err != nil {
return err
}
if err != nil {
return err
}
if filter.IDs != nil {
if _, ok := filter.IDs[target.ID]; !ok {
continue
}
}
if filter.Name != nil && target.Name != *filter.Name {
continue
}
if filter.Org != nil {
o, err := c.findOrganizationByName(ctx, tx, *filter.Org)
if err != nil {
return err
}
if target.OrgID != o.ID {
continue
}
}
if filter.OrgID != nil {
o, err := c.findOrganizationByID(ctx, tx, *filter.OrgID)
if err != nil {
return err
}
if target.OrgID != o.ID {
continue
}
}
list = append(list, *target)
}
return err
})
if err != nil {
return nil, &platform.Error{
Op: getOp(platform.OpListTargets),
return nil, &influxdb.Error{
Op: getOp(influxdb.OpListTargets),
Err: err,
}
}
@ -45,19 +74,19 @@ func (c *Client) ListTargets(ctx context.Context) (list []platform.ScraperTarget
}
// AddTarget add a new scraper target into storage.
func (c *Client) AddTarget(ctx context.Context, target *platform.ScraperTarget, userID platform.ID) (err error) {
func (c *Client) AddTarget(ctx context.Context, target *influxdb.ScraperTarget, userID influxdb.ID) (err error) {
if !target.OrgID.Valid() {
return &platform.Error{
Code: platform.EInvalid,
return &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "provided organization ID has invalid format",
Op: OpPrefix + platform.OpAddTarget,
Op: OpPrefix + influxdb.OpAddTarget,
}
}
if !target.BucketID.Valid() {
return &platform.Error{
Code: platform.EInvalid,
return &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "provided bucket ID has invalid format",
Op: OpPrefix + platform.OpAddTarget,
Op: OpPrefix + influxdb.OpAddTarget,
}
}
err = c.db.Update(func(tx *bolt.Tx) error {
@ -65,25 +94,25 @@ func (c *Client) AddTarget(ctx context.Context, target *platform.ScraperTarget,
if err := c.putTarget(ctx, tx, target); err != nil {
return err
}
urm := &platform.UserResourceMapping{
urm := &influxdb.UserResourceMapping{
ResourceID: target.ID,
UserID: userID,
UserType: platform.Owner,
ResourceType: platform.ScraperResourceType,
UserType: influxdb.Owner,
ResourceType: influxdb.ScraperResourceType,
}
return c.createUserResourceMapping(ctx, tx, urm)
})
if err != nil {
return &platform.Error{
return &influxdb.Error{
Err: err,
Op: OpPrefix + platform.OpAddTarget,
Op: OpPrefix + influxdb.OpAddTarget,
}
}
return nil
}
// RemoveTarget removes a scraper target from the bucket.
func (c *Client) RemoveTarget(ctx context.Context, id platform.ID) error {
func (c *Client) RemoveTarget(ctx context.Context, id influxdb.ID) error {
err := c.db.Update(func(tx *bolt.Tx) error {
_, pe := c.findTargetByID(ctx, tx, id)
if pe != nil {
@ -91,35 +120,35 @@ func (c *Client) RemoveTarget(ctx context.Context, id platform.ID) error {
}
encID, err := id.Encode()
if err != nil {
return &platform.Error{
Code: platform.EInvalid,
return &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}
if err = tx.Bucket(scraperBucket).Delete(encID); err != nil {
return nil
}
return c.deleteUserResourceMappings(ctx, tx, platform.UserResourceMappingFilter{
return c.deleteUserResourceMappings(ctx, tx, influxdb.UserResourceMappingFilter{
ResourceID: id,
ResourceType: platform.ScraperResourceType,
ResourceType: influxdb.ScraperResourceType,
})
})
if err != nil {
return &platform.Error{
return &influxdb.Error{
Err: err,
Op: OpPrefix + platform.OpRemoveTarget,
Op: OpPrefix + influxdb.OpRemoveTarget,
}
}
return nil
}
// UpdateTarget updates a scraper target.
func (c *Client) UpdateTarget(ctx context.Context, update *platform.ScraperTarget, userID platform.ID) (target *platform.ScraperTarget, err error) {
op := getOp(platform.OpUpdateTarget)
var pe *platform.Error
func (c *Client) UpdateTarget(ctx context.Context, update *influxdb.ScraperTarget, userID influxdb.ID) (target *influxdb.ScraperTarget, err error) {
op := getOp(influxdb.OpUpdateTarget)
var pe *influxdb.Error
if !update.ID.Valid() {
return nil, &platform.Error{
Code: platform.EInvalid,
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Op: op,
Msg: "provided scraper target ID has invalid format",
}
@ -140,7 +169,7 @@ func (c *Client) UpdateTarget(ctx context.Context, update *platform.ScraperTarge
})
if err != nil {
return nil, &platform.Error{
return nil, &influxdb.Error{
Op: op,
Err: err,
}
@ -150,13 +179,13 @@ func (c *Client) UpdateTarget(ctx context.Context, update *platform.ScraperTarge
}
// GetTargetByID retrieves a scraper target by id.
func (c *Client) GetTargetByID(ctx context.Context, id platform.ID) (target *platform.ScraperTarget, err error) {
var pe *platform.Error
func (c *Client) GetTargetByID(ctx context.Context, id influxdb.ID) (target *influxdb.ScraperTarget, err error) {
var pe *influxdb.Error
err = c.db.View(func(tx *bolt.Tx) error {
target, pe = c.findTargetByID(ctx, tx, id)
if pe != nil {
return &platform.Error{
Op: getOp(platform.OpGetTargetByID),
return &influxdb.Error{
Op: getOp(influxdb.OpGetTargetByID),
Err: pe,
}
}
@ -168,31 +197,31 @@ func (c *Client) GetTargetByID(ctx context.Context, id platform.ID) (target *pla
return target, nil
}
func (c *Client) findTargetByID(ctx context.Context, tx *bolt.Tx, id platform.ID) (target *platform.ScraperTarget, pe *platform.Error) {
target = new(platform.ScraperTarget)
func (c *Client) findTargetByID(ctx context.Context, tx *bolt.Tx, id influxdb.ID) (target *influxdb.ScraperTarget, pe *influxdb.Error) {
target = new(influxdb.ScraperTarget)
encID, err := id.Encode()
if err != nil {
return nil, &platform.Error{
return nil, &influxdb.Error{
Err: err,
}
}
v := tx.Bucket(scraperBucket).Get(encID)
if len(v) == 0 {
return nil, &platform.Error{
Code: platform.ENotFound,
return nil, &influxdb.Error{
Code: influxdb.ENotFound,
Msg: "scraper target is not found",
}
}
if err := json.Unmarshal(v, target); err != nil {
return nil, &platform.Error{
return nil, &influxdb.Error{
Err: err,
}
}
return target, nil
}
func (c *Client) putTarget(ctx context.Context, tx *bolt.Tx, target *platform.ScraperTarget) (err error) {
func (c *Client) putTarget(ctx context.Context, tx *bolt.Tx, target *influxdb.ScraperTarget) (err error) {
v, err := json.Marshal(target)
if err != nil {
return err
@ -205,7 +234,7 @@ func (c *Client) putTarget(ctx context.Context, tx *bolt.Tx, target *platform.Sc
}
// PutTarget will put a scraper target without setting an ID.
func (c *Client) PutTarget(ctx context.Context, target *platform.ScraperTarget) error {
func (c *Client) PutTarget(ctx context.Context, target *influxdb.ScraperTarget) error {
return c.db.Update(func(tx *bolt.Tx) error {
return c.putTarget(ctx, tx, target)
})

View File

@ -26,6 +26,11 @@ func initScraperTargetStoreService(f platformtesting.TargetFields, t *testing.T)
t.Fatalf("failed to populate user resource mapping")
}
}
for _, o := range f.Organizations {
if err := c.PutOrganization(ctx, o); err != nil {
t.Fatalf("failed to populate orgs: %v", err)
}
}
return c, bolt.OpPrefix, func() {
defer closeFn()
for _, target := range f.Targets {
@ -33,6 +38,11 @@ func initScraperTargetStoreService(f platformtesting.TargetFields, t *testing.T)
t.Logf("failed to remove targets: %v", err)
}
}
for _, o := range f.Organizations {
if err := c.DeleteOrganization(ctx, o.ID); err != nil {
t.Logf("failed to remove org: %v", err)
}
}
}
}

View File

@ -107,7 +107,7 @@ func (s *Scheduler) doGather(ctx context.Context) {
span, ctx := tracing.StartSpanFromContext(ctx)
defer span.Finish()
targets, err := s.Targets.ListTargets(ctx)
targets, err := s.Targets.ListTargets(ctx, influxdb.ScraperTargetFilter{})
if err != nil {
s.Logger.Error("cannot list targets", zap.Error(err))
tracing.LogError(span, err)

View File

@ -193,6 +193,7 @@ go_memstats_gc_cpu_fraction 1.972734963012756e-05
type mockStorage struct {
sync.RWMutex
influxdb.UserResourceMappingService
influxdb.OrganizationService
TotalGatherJobs chan struct{}
Metrics map[time.Time]Metrics
Targets []influxdb.ScraperTarget
@ -208,7 +209,7 @@ func (s *mockStorage) Record(collected MetricsCollection) error {
return nil
}
func (s *mockStorage) ListTargets(ctx context.Context) (targets []influxdb.ScraperTarget, err error) {
func (s *mockStorage) ListTargets(ctx context.Context, filter influxdb.ScraperTargetFilter) (targets []influxdb.ScraperTarget, err error) {
s.RLock()
defer s.RUnlock()
if s.Targets == nil {

View File

@ -132,7 +132,9 @@ func NewAPIHandler(b *APIBackend) *APIHandler {
h.AuthorizationHandler = NewAuthorizationHandler(authorizationBackend)
scraperBackend := NewScraperBackend(b)
scraperBackend.ScraperStorageService = authorizer.NewScraperTargetStoreService(b.ScraperTargetStoreService, b.UserResourceMappingService)
scraperBackend.ScraperStorageService = authorizer.NewScraperTargetStoreService(b.ScraperTargetStoreService,
b.UserResourceMappingService,
b.OrganizationService)
h.ScraperHandler = NewScraperHandler(scraperBackend)
sourceBackend := NewSourceBackend(b)

View File

@ -223,11 +223,50 @@ func (h *ScraperHandler) handleGetScraperTarget(w http.ResponseWriter, r *http.R
}
}
type getScraperTargetsRequest struct {
filter influxdb.ScraperTargetFilter
}
func decodeScraperTargetsRequest(ctx context.Context, r *http.Request) (*getScraperTargetsRequest, error) {
qp := r.URL.Query()
req := &getScraperTargetsRequest{}
initialID := influxdb.InvalidID()
if ids, ok := qp["id"]; ok {
req.filter.IDs = make(map[influxdb.ID]bool)
for _, id := range ids {
i := initialID
if err := i.DecodeFromString(id); err != nil {
return nil, err
}
req.filter.IDs[i] = false
}
}
if name := qp.Get("name"); name != "" {
req.filter.Name = &name
}
if orgID := qp.Get("orgID"); orgID != "" {
id := influxdb.InvalidID()
if err := id.DecodeFromString(orgID); err != nil {
return nil, err
}
req.filter.OrgID = &id
} else if org := qp.Get("org"); org != "" {
req.filter.Org = &org
}
return req, nil
}
// handleGetScraperTargets is the HTTP handler for the GET /api/v2/scrapers route.
func (h *ScraperHandler) handleGetScraperTargets(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
targets, err := h.ScraperStorageService.ListTargets(ctx)
req, err := decodeScraperTargetsRequest(ctx, r)
if err != nil {
EncodeError(ctx, err, w)
return
}
targets, err := h.ScraperStorageService.ListTargets(ctx, req.filter)
if err != nil {
EncodeError(ctx, err, w)
return
@ -294,13 +333,27 @@ type ScraperService struct {
}
// ListTargets returns a list of all scraper targets.
func (s *ScraperService) ListTargets(ctx context.Context) ([]influxdb.ScraperTarget, error) {
func (s *ScraperService) ListTargets(ctx context.Context, filter influxdb.ScraperTargetFilter) ([]influxdb.ScraperTarget, error) {
url, err := newURL(s.Addr, targetsPath)
if err != nil {
return nil, err
}
query := url.Query()
if filter.IDs != nil {
for id := range filter.IDs {
query.Add("id", id.String())
}
}
if filter.Name != nil {
query.Set("name", *filter.Name)
}
if filter.OrgID != nil {
query.Set("orgID", filter.OrgID.String())
}
if filter.Org != nil {
query.Set("org", *filter.Org)
}
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {

View File

@ -12,7 +12,7 @@ import (
"go.uber.org/zap"
platform "github.com/influxdata/influxdb"
"github.com/influxdata/influxdb"
platcontext "github.com/influxdata/influxdb/context"
httpMock "github.com/influxdata/influxdb/http/mock"
"github.com/influxdata/influxdb/inmem"
@ -47,9 +47,9 @@ func NewMockScraperBackend() *ScraperBackend {
func TestService_handleGetScraperTargets(t *testing.T) {
type fields struct {
ScraperTargetStoreService platform.ScraperTargetStoreService
OrganizationService platform.OrganizationService
BucketService platform.BucketService
ScraperTargetStoreService influxdb.ScraperTargetStoreService
OrganizationService influxdb.OrganizationService
BucketService influxdb.BucketService
}
type args struct {
@ -72,28 +72,28 @@ func TestService_handleGetScraperTargets(t *testing.T) {
name: "get all scraper targets",
fields: fields{
OrganizationService: &mock.OrganizationService{
FindOrganizationByIDF: func(ctx context.Context, id platform.ID) (*platform.Organization, error) {
return &platform.Organization{
FindOrganizationByIDF: func(ctx context.Context, id influxdb.ID) (*influxdb.Organization, error) {
return &influxdb.Organization{
ID: platformtesting.MustIDBase16("0000000000000211"),
Name: "org1",
}, nil
},
},
BucketService: &mock.BucketService{
FindBucketByIDFn: func(ctx context.Context, id platform.ID) (*platform.Bucket, error) {
return &platform.Bucket{
FindBucketByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Bucket, error) {
return &influxdb.Bucket{
ID: platformtesting.MustIDBase16("0000000000000212"),
Name: "bucket1",
}, nil
},
},
ScraperTargetStoreService: &mock.ScraperTargetStoreService{
ListTargetsF: func(ctx context.Context) ([]platform.ScraperTarget, error) {
return []platform.ScraperTarget{
ListTargetsF: func(ctx context.Context, filter influxdb.ScraperTargetFilter) ([]influxdb.ScraperTarget, error) {
return []influxdb.ScraperTarget{
{
ID: targetOneID,
Name: "target-1",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
URL: "www.one.url",
OrgID: platformtesting.MustIDBase16("0000000000000211"),
BucketID: platformtesting.MustIDBase16("0000000000000212"),
@ -101,7 +101,7 @@ func TestService_handleGetScraperTargets(t *testing.T) {
{
ID: targetTwoID,
Name: "target-2",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
URL: "www.two.url",
OrgID: platformtesting.MustIDBase16("0000000000000211"),
BucketID: platformtesting.MustIDBase16("0000000000000212"),
@ -167,24 +167,24 @@ func TestService_handleGetScraperTargets(t *testing.T) {
name: "get all scraper targets when there are none",
fields: fields{
OrganizationService: &mock.OrganizationService{
FindOrganizationByIDF: func(ctx context.Context, id platform.ID) (*platform.Organization, error) {
return &platform.Organization{
FindOrganizationByIDF: func(ctx context.Context, id influxdb.ID) (*influxdb.Organization, error) {
return &influxdb.Organization{
ID: platformtesting.MustIDBase16("0000000000000211"),
Name: "org1",
}, nil
},
},
BucketService: &mock.BucketService{
FindBucketByIDFn: func(ctx context.Context, id platform.ID) (*platform.Bucket, error) {
return &platform.Bucket{
FindBucketByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Bucket, error) {
return &influxdb.Bucket{
ID: platformtesting.MustIDBase16("0000000000000212"),
Name: "bucket1",
}, nil
},
},
ScraperTargetStoreService: &mock.ScraperTargetStoreService{
ListTargetsF: func(ctx context.Context) ([]platform.ScraperTarget, error) {
return []platform.ScraperTarget{}, nil
ListTargetsF: func(ctx context.Context, filter influxdb.ScraperTargetFilter) ([]influxdb.ScraperTarget, error) {
return []influxdb.ScraperTarget{}, nil
},
},
},
@ -245,9 +245,9 @@ func TestService_handleGetScraperTargets(t *testing.T) {
func TestService_handleGetScraperTarget(t *testing.T) {
type fields struct {
OrganizationService platform.OrganizationService
BucketService platform.BucketService
ScraperTargetStoreService platform.ScraperTargetStoreService
OrganizationService influxdb.OrganizationService
BucketService influxdb.BucketService
ScraperTargetStoreService influxdb.ScraperTargetStoreService
}
type args struct {
@ -270,35 +270,35 @@ func TestService_handleGetScraperTarget(t *testing.T) {
name: "get a scraper target by id",
fields: fields{
OrganizationService: &mock.OrganizationService{
FindOrganizationByIDF: func(ctx context.Context, id platform.ID) (*platform.Organization, error) {
return &platform.Organization{
FindOrganizationByIDF: func(ctx context.Context, id influxdb.ID) (*influxdb.Organization, error) {
return &influxdb.Organization{
ID: platformtesting.MustIDBase16("0000000000000211"),
Name: "org1",
}, nil
},
},
BucketService: &mock.BucketService{
FindBucketByIDFn: func(ctx context.Context, id platform.ID) (*platform.Bucket, error) {
return &platform.Bucket{
FindBucketByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Bucket, error) {
return &influxdb.Bucket{
ID: platformtesting.MustIDBase16("0000000000000212"),
Name: "bucket1",
}, nil
},
},
ScraperTargetStoreService: &mock.ScraperTargetStoreService{
GetTargetByIDF: func(ctx context.Context, id platform.ID) (*platform.ScraperTarget, error) {
GetTargetByIDF: func(ctx context.Context, id influxdb.ID) (*influxdb.ScraperTarget, error) {
if id == targetOneID {
return &platform.ScraperTarget{
return &influxdb.ScraperTarget{
ID: targetOneID,
Name: "target-1",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
URL: "www.some.url",
OrgID: platformtesting.MustIDBase16("0000000000000211"),
BucketID: platformtesting.MustIDBase16("0000000000000212"),
}, nil
}
return nil, &platform.Error{
Code: platform.ENotFound,
return nil, &influxdb.Error{
Code: influxdb.ENotFound,
Msg: "scraper target is not found",
}
},
@ -379,7 +379,7 @@ func TestService_handleGetScraperTarget(t *testing.T) {
func TestService_handleDeleteScraperTarget(t *testing.T) {
type fields struct {
Service platform.ScraperTargetStoreService
Service influxdb.ScraperTargetStoreService
}
type args struct {
@ -402,7 +402,7 @@ func TestService_handleDeleteScraperTarget(t *testing.T) {
name: "delete a scraper target by id",
fields: fields{
Service: &mock.ScraperTargetStoreService{
RemoveTargetF: func(ctx context.Context, id platform.ID) error {
RemoveTargetF: func(ctx context.Context, id influxdb.ID) error {
if id == targetOneID {
return nil
}
@ -422,10 +422,10 @@ func TestService_handleDeleteScraperTarget(t *testing.T) {
name: "scraper target not found",
fields: fields{
Service: &mock.ScraperTargetStoreService{
RemoveTargetF: func(ctx context.Context, id platform.ID) error {
return &platform.Error{
Code: platform.ENotFound,
Msg: platform.ErrScraperTargetNotFound,
RemoveTargetF: func(ctx context.Context, id influxdb.ID) error {
return &influxdb.Error{
Code: influxdb.ENotFound,
Msg: influxdb.ErrScraperTargetNotFound,
}
},
},
@ -480,13 +480,13 @@ func TestService_handleDeleteScraperTarget(t *testing.T) {
func TestService_handlePostScraperTarget(t *testing.T) {
type fields struct {
OrganizationService platform.OrganizationService
BucketService platform.BucketService
ScraperTargetStoreService platform.ScraperTargetStoreService
OrganizationService influxdb.OrganizationService
BucketService influxdb.BucketService
ScraperTargetStoreService influxdb.ScraperTargetStoreService
}
type args struct {
target *platform.ScraperTarget
target *influxdb.ScraperTarget
}
type wants struct {
@ -505,32 +505,32 @@ func TestService_handlePostScraperTarget(t *testing.T) {
name: "create a new scraper target",
fields: fields{
OrganizationService: &mock.OrganizationService{
FindOrganizationByIDF: func(ctx context.Context, id platform.ID) (*platform.Organization, error) {
return &platform.Organization{
FindOrganizationByIDF: func(ctx context.Context, id influxdb.ID) (*influxdb.Organization, error) {
return &influxdb.Organization{
ID: platformtesting.MustIDBase16("0000000000000211"),
Name: "org1",
}, nil
},
},
BucketService: &mock.BucketService{
FindBucketByIDFn: func(ctx context.Context, id platform.ID) (*platform.Bucket, error) {
return &platform.Bucket{
FindBucketByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Bucket, error) {
return &influxdb.Bucket{
ID: platformtesting.MustIDBase16("0000000000000212"),
Name: "bucket1",
}, nil
},
},
ScraperTargetStoreService: &mock.ScraperTargetStoreService{
AddTargetF: func(ctx context.Context, st *platform.ScraperTarget, userID platform.ID) error {
AddTargetF: func(ctx context.Context, st *influxdb.ScraperTarget, userID influxdb.ID) error {
st.ID = targetOneID
return nil
},
},
},
args: args{
target: &platform.ScraperTarget{
target: &influxdb.ScraperTarget{
Name: "hello",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
BucketID: platformtesting.MustIDBase16("0000000000000212"),
OrgID: platformtesting.MustIDBase16("0000000000000211"),
URL: "www.some.url",
@ -579,7 +579,7 @@ func TestService_handlePostScraperTarget(t *testing.T) {
}
r := httptest.NewRequest("GET", "http://any.tld", bytes.NewReader(st))
r = r.WithContext(platcontext.SetAuthorizer(r.Context(), &platform.Authorization{}))
r = r.WithContext(platcontext.SetAuthorizer(r.Context(), &influxdb.Authorization{}))
w := httptest.NewRecorder()
h.handlePostScraperTarget(w, r)
@ -603,14 +603,14 @@ func TestService_handlePostScraperTarget(t *testing.T) {
func TestService_handlePatchScraperTarget(t *testing.T) {
type fields struct {
BucketService platform.BucketService
OrganizationService platform.OrganizationService
ScraperTargetStoreService platform.ScraperTargetStoreService
BucketService influxdb.BucketService
OrganizationService influxdb.OrganizationService
ScraperTargetStoreService influxdb.ScraperTargetStoreService
}
type args struct {
id string
update *platform.ScraperTarget
update *influxdb.ScraperTarget
}
type wants struct {
@ -629,29 +629,29 @@ func TestService_handlePatchScraperTarget(t *testing.T) {
name: "update a scraper target",
fields: fields{
OrganizationService: &mock.OrganizationService{
FindOrganizationByIDF: func(ctx context.Context, id platform.ID) (*platform.Organization, error) {
return &platform.Organization{
FindOrganizationByIDF: func(ctx context.Context, id influxdb.ID) (*influxdb.Organization, error) {
return &influxdb.Organization{
ID: platformtesting.MustIDBase16("0000000000000211"),
Name: "org1",
}, nil
},
},
BucketService: &mock.BucketService{
FindBucketByIDFn: func(ctx context.Context, id platform.ID) (*platform.Bucket, error) {
return &platform.Bucket{
FindBucketByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Bucket, error) {
return &influxdb.Bucket{
ID: platformtesting.MustIDBase16("0000000000000212"),
Name: "bucket1",
}, nil
},
},
ScraperTargetStoreService: &mock.ScraperTargetStoreService{
UpdateTargetF: func(ctx context.Context, t *platform.ScraperTarget, userID platform.ID) (*platform.ScraperTarget, error) {
UpdateTargetF: func(ctx context.Context, t *influxdb.ScraperTarget, userID influxdb.ID) (*influxdb.ScraperTarget, error) {
if t.ID == targetOneID {
return t, nil
}
return nil, &platform.Error{
Code: platform.ENotFound,
return nil, &influxdb.Error{
Code: influxdb.ENotFound,
Msg: "scraper target is not found",
}
},
@ -659,11 +659,11 @@ func TestService_handlePatchScraperTarget(t *testing.T) {
},
args: args{
id: targetOneIDString,
update: &platform.ScraperTarget{
update: &influxdb.ScraperTarget{
ID: targetOneID,
Name: "name",
BucketID: platformtesting.MustIDBase16("0000000000000212"),
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
URL: "www.example.url",
OrgID: platformtesting.MustIDBase16("0000000000000211"),
},
@ -697,37 +697,37 @@ func TestService_handlePatchScraperTarget(t *testing.T) {
name: "scraper target not found",
fields: fields{
OrganizationService: &mock.OrganizationService{
FindOrganizationByIDF: func(ctx context.Context, id platform.ID) (*platform.Organization, error) {
return &platform.Organization{
FindOrganizationByIDF: func(ctx context.Context, id influxdb.ID) (*influxdb.Organization, error) {
return &influxdb.Organization{
ID: platformtesting.MustIDBase16("0000000000000211"),
Name: "org1",
}, nil
},
},
BucketService: &mock.BucketService{
FindBucketByIDFn: func(ctx context.Context, id platform.ID) (*platform.Bucket, error) {
return &platform.Bucket{
FindBucketByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Bucket, error) {
return &influxdb.Bucket{
ID: platformtesting.MustIDBase16("0000000000000212"),
Name: "bucket1",
}, nil
},
},
ScraperTargetStoreService: &mock.ScraperTargetStoreService{
UpdateTargetF: func(ctx context.Context, upd *platform.ScraperTarget, userID platform.ID) (*platform.ScraperTarget, error) {
return nil, &platform.Error{
Code: platform.ENotFound,
Msg: platform.ErrScraperTargetNotFound,
UpdateTargetF: func(ctx context.Context, upd *influxdb.ScraperTarget, userID influxdb.ID) (*influxdb.ScraperTarget, error) {
return nil, &influxdb.Error{
Code: influxdb.ENotFound,
Msg: influxdb.ErrScraperTargetNotFound,
}
},
},
},
args: args{
id: targetOneIDString,
update: &platform.ScraperTarget{
update: &influxdb.ScraperTarget{
ID: targetOneID,
Name: "name",
BucketID: platformtesting.MustIDBase16("0000000000000212"),
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
URL: "www.example.url",
OrgID: platformtesting.MustIDBase16("0000000000000211"),
},
@ -766,7 +766,7 @@ func TestService_handlePatchScraperTarget(t *testing.T) {
Value: tt.args.id,
},
}))
r = r.WithContext(platcontext.SetAuthorizer(r.Context(), &platform.Authorization{}))
r = r.WithContext(platcontext.SetAuthorizer(r.Context(), &influxdb.Authorization{}))
w := httptest.NewRecorder()
h.handlePatchScraperTarget(w, r)
@ -788,7 +788,7 @@ func TestService_handlePatchScraperTarget(t *testing.T) {
}
}
func initScraperService(f platformtesting.TargetFields, t *testing.T) (platform.ScraperTargetStoreService, string, func()) {
func initScraperService(f platformtesting.TargetFields, t *testing.T) (influxdb.ScraperTargetStoreService, string, func()) {
t.Helper()
svc := inmem.NewService()
svc.IDGenerator = f.IDGenerator
@ -805,19 +805,18 @@ func initScraperService(f platformtesting.TargetFields, t *testing.T) (platform.
}
}
for _, o := range f.Organizations {
if err := svc.PutOrganization(ctx, o); err != nil {
t.Fatalf("failed to populate orgs")
}
}
scraperBackend := NewMockScraperBackend()
scraperBackend.ScraperStorageService = svc
scraperBackend.OrganizationService = &mock.OrganizationService{
FindOrganizationByIDF: func(ctx context.Context, id platform.ID) (*platform.Organization, error) {
return &platform.Organization{
ID: id,
Name: "org1",
}, nil
},
}
scraperBackend.OrganizationService = svc
scraperBackend.BucketService = &mock.BucketService{
FindBucketByIDFn: func(ctx context.Context, id platform.ID) (*platform.Bucket, error) {
return &platform.Bucket{
FindBucketByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Bucket, error) {
return &influxdb.Bucket{
ID: id,
Name: "bucket1",
}, nil
@ -827,16 +826,18 @@ func initScraperService(f platformtesting.TargetFields, t *testing.T) (platform.
handler := NewScraperHandler(scraperBackend)
server := httptest.NewServer(httpMock.NewAuthMiddlewareHandler(
handler,
&platform.Authorization{
&influxdb.Authorization{
UserID: platformtesting.MustIDBase16("020f755c3c082002"),
Token: "tok",
},
))
client := struct {
platform.UserResourceMappingService
influxdb.UserResourceMappingService
influxdb.OrganizationService
ScraperService
}{
UserResourceMappingService: svc,
OrganizationService: svc,
ScraperService: ScraperService{
Token: "tok",
Addr: server.URL,

View File

@ -742,9 +742,26 @@ paths:
summary: get all scraper targets
parameters:
- $ref: '#/components/parameters/TraceSpan'
- in: query
name: name
description: specifies the name of the scraper target.
schema:
type: string
- in: query
name: id
description: ID list of scraper targets to return. If both this and owner are specified, only ids is used.
schema:
type: array
items:
type: string
- in: query
name: orgID
description: specifies the organization of the resource
description: specifies the organization id of the scraper target
schema:
type: string
- in: query
name: org
description: specifies the organization name of the scraper target
schema:
type: string
responses:

View File

@ -4,28 +4,28 @@ import (
"context"
"fmt"
platform "github.com/influxdata/influxdb"
"github.com/influxdata/influxdb"
)
const (
errScraperTargetNotFound = "scraper target is not found"
)
var _ platform.ScraperTargetStoreService = (*Service)(nil)
var _ influxdb.ScraperTargetStoreService = (*Service)(nil)
func (s *Service) loadScraperTarget(id platform.ID) (*platform.ScraperTarget, *platform.Error) {
func (s *Service) loadScraperTarget(id influxdb.ID) (*influxdb.ScraperTarget, *influxdb.Error) {
i, ok := s.scraperTargetKV.Load(id.String())
if !ok {
return nil, &platform.Error{
Code: platform.ENotFound,
return nil, &influxdb.Error{
Code: influxdb.ENotFound,
Msg: errScraperTargetNotFound,
}
}
b, ok := i.(platform.ScraperTarget)
b, ok := i.(influxdb.ScraperTarget)
if !ok {
return nil, &platform.Error{
Code: platform.EInvalid,
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("type %T is not a scraper target", i),
}
}
@ -33,51 +33,79 @@ func (s *Service) loadScraperTarget(id platform.ID) (*platform.ScraperTarget, *p
}
// ListTargets will list all scrape targets.
func (s *Service) ListTargets(ctx context.Context) (list []platform.ScraperTarget, err error) {
list = make([]platform.ScraperTarget, 0)
func (s *Service) ListTargets(ctx context.Context, filter influxdb.ScraperTargetFilter) (list []influxdb.ScraperTarget, err error) {
list = make([]influxdb.ScraperTarget, 0)
s.scraperTargetKV.Range(func(_, v interface{}) bool {
b, ok := v.(platform.ScraperTarget)
target, ok := v.(influxdb.ScraperTarget)
if !ok {
err = &platform.Error{
Code: platform.EInvalid,
err = &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("type %T is not a scraper target", v),
}
return false
}
list = append(list, b)
if filter.IDs != nil {
if _, ok := filter.IDs[target.ID]; !ok {
return true
}
}
if filter.Name != nil && target.Name != *filter.Name {
return true
}
if filter.Org != nil {
o, orgErr := s.findOrganizationByName(ctx, *filter.Org)
if orgErr != nil {
err = orgErr
return false
}
if target.OrgID != o.ID {
return true
}
}
if filter.OrgID != nil {
o, orgErr := s.FindOrganizationByID(ctx, *filter.OrgID)
if orgErr != nil {
err = orgErr
return true
}
if target.OrgID != o.ID {
return true
}
}
list = append(list, target)
return true
})
return list, err
}
// AddTarget add a new scraper target into storage.
func (s *Service) AddTarget(ctx context.Context, target *platform.ScraperTarget, userID platform.ID) (err error) {
func (s *Service) AddTarget(ctx context.Context, target *influxdb.ScraperTarget, userID influxdb.ID) (err error) {
target.ID = s.IDGenerator.ID()
if !target.OrgID.Valid() {
return &platform.Error{
Code: platform.EInvalid,
return &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "provided organization ID has invalid format",
Op: OpPrefix + platform.OpAddTarget,
Op: OpPrefix + influxdb.OpAddTarget,
}
}
if !target.BucketID.Valid() {
return &platform.Error{
Code: platform.EInvalid,
return &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "provided bucket ID has invalid format",
Op: OpPrefix + platform.OpAddTarget,
Op: OpPrefix + influxdb.OpAddTarget,
}
}
if err := s.PutTarget(ctx, target); err != nil {
return &platform.Error{
Op: OpPrefix + platform.OpAddTarget,
return &influxdb.Error{
Op: OpPrefix + influxdb.OpAddTarget,
Err: err,
}
}
urm := &platform.UserResourceMapping{
urm := &influxdb.UserResourceMapping{
ResourceID: target.ID,
UserID: userID,
UserType: platform.Owner,
ResourceType: platform.ScraperResourceType,
UserType: influxdb.Owner,
ResourceType: influxdb.ScraperResourceType,
}
if err := s.CreateUserResourceMapping(ctx, urm); err != nil {
return err
@ -86,22 +114,22 @@ func (s *Service) AddTarget(ctx context.Context, target *platform.ScraperTarget,
}
// RemoveTarget removes a scraper target from the bucket.
func (s *Service) RemoveTarget(ctx context.Context, id platform.ID) error {
func (s *Service) RemoveTarget(ctx context.Context, id influxdb.ID) error {
if _, pe := s.loadScraperTarget(id); pe != nil {
return &platform.Error{
return &influxdb.Error{
Err: pe,
Op: OpPrefix + platform.OpRemoveTarget,
Op: OpPrefix + influxdb.OpRemoveTarget,
}
}
s.scraperTargetKV.Delete(id.String())
err := s.deleteUserResourceMapping(ctx, platform.UserResourceMappingFilter{
err := s.deleteUserResourceMapping(ctx, influxdb.UserResourceMappingFilter{
ResourceID: id,
ResourceType: platform.ScraperResourceType,
ResourceType: influxdb.ScraperResourceType,
})
if err != nil {
return &platform.Error{
Code: platform.ErrorCode(err),
Op: OpPrefix + platform.OpRemoveTarget,
return &influxdb.Error{
Code: influxdb.ErrorCode(err),
Op: OpPrefix + influxdb.OpRemoveTarget,
Err: err,
}
}
@ -110,18 +138,18 @@ func (s *Service) RemoveTarget(ctx context.Context, id platform.ID) error {
}
// UpdateTarget updates a scraper target.
func (s *Service) UpdateTarget(ctx context.Context, update *platform.ScraperTarget, userID platform.ID) (target *platform.ScraperTarget, err error) {
op := OpPrefix + platform.OpUpdateTarget
func (s *Service) UpdateTarget(ctx context.Context, update *influxdb.ScraperTarget, userID influxdb.ID) (target *influxdb.ScraperTarget, err error) {
op := OpPrefix + influxdb.OpUpdateTarget
if !update.ID.Valid() {
return nil, &platform.Error{
Code: platform.EInvalid,
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Op: op,
Msg: "provided scraper target ID has invalid format",
}
}
oldTarget, pe := s.loadScraperTarget(update.ID)
if pe != nil {
return nil, &platform.Error{
return nil, &influxdb.Error{
Op: op,
Err: pe,
}
@ -133,7 +161,7 @@ func (s *Service) UpdateTarget(ctx context.Context, update *platform.ScraperTarg
update.BucketID = oldTarget.BucketID
}
if err = s.PutTarget(ctx, update); err != nil {
return nil, &platform.Error{
return nil, &influxdb.Error{
Op: op,
Err: pe,
}
@ -143,11 +171,11 @@ func (s *Service) UpdateTarget(ctx context.Context, update *platform.ScraperTarg
}
// GetTargetByID retrieves a scraper target by id.
func (s *Service) GetTargetByID(ctx context.Context, id platform.ID) (target *platform.ScraperTarget, err error) {
var pe *platform.Error
func (s *Service) GetTargetByID(ctx context.Context, id influxdb.ID) (target *influxdb.ScraperTarget, err error) {
var pe *influxdb.Error
if target, pe = s.loadScraperTarget(id); pe != nil {
return nil, &platform.Error{
Op: OpPrefix + platform.OpGetTargetByID,
return nil, &influxdb.Error{
Op: OpPrefix + influxdb.OpGetTargetByID,
Err: pe,
}
}
@ -155,7 +183,7 @@ func (s *Service) GetTargetByID(ctx context.Context, id platform.ID) (target *pl
}
// PutTarget will put a scraper target without setting an ID.
func (s *Service) PutTarget(ctx context.Context, target *platform.ScraperTarget) error {
func (s *Service) PutTarget(ctx context.Context, target *influxdb.ScraperTarget) error {
s.scraperTargetKV.Store(target.ID.String(), *target)
return nil
}

View File

@ -4,11 +4,11 @@ import (
"context"
"testing"
platform "github.com/influxdata/influxdb"
"github.com/influxdata/influxdb"
platformtesting "github.com/influxdata/influxdb/testing"
)
func initScraperTargetStoreService(f platformtesting.TargetFields, t *testing.T) (platform.ScraperTargetStoreService, string, func()) {
func initScraperTargetStoreService(f platformtesting.TargetFields, t *testing.T) (influxdb.ScraperTargetStoreService, string, func()) {
s := NewService()
s.IDGenerator = f.IDGenerator
ctx := context.Background()
@ -22,6 +22,11 @@ func initScraperTargetStoreService(f platformtesting.TargetFields, t *testing.T)
t.Fatalf("failed to populate user resource mapping")
}
}
for _, o := range f.Organizations {
if err := s.PutOrganization(ctx, o); err != nil {
t.Fatalf("failed to populate orgs")
}
}
return s, OpPrefix, func() {}
}

View File

@ -96,17 +96,18 @@ func (s *Service) scrapersBucket(tx Tx) (Bucket, error) {
}
// ListTargets will list all scrape targets.
func (s *Service) ListTargets(ctx context.Context) ([]influxdb.ScraperTarget, error) {
func (s *Service) ListTargets(ctx context.Context, filter influxdb.ScraperTargetFilter) ([]influxdb.ScraperTarget, error) {
targets := []influxdb.ScraperTarget{}
err := s.kv.View(ctx, func(tx Tx) error {
var err error
targets, err = s.listTargets(ctx, tx)
targets, err = s.listTargets(ctx, tx, filter)
return err
})
return targets, err
}
func (s *Service) listTargets(ctx context.Context, tx Tx) ([]influxdb.ScraperTarget, error) {
func (s *Service) listTargets(ctx context.Context, tx Tx, filter influxdb.ScraperTargetFilter) ([]influxdb.ScraperTarget, error) {
targets := []influxdb.ScraperTarget{}
bucket, err := s.scrapersBucket(tx)
if err != nil {
return nil, err
@ -117,12 +118,37 @@ func (s *Service) listTargets(ctx context.Context, tx Tx) ([]influxdb.ScraperTar
return nil, UnexpectedScrapersBucketError(err)
}
targets := []influxdb.ScraperTarget{}
for k, v := cur.First(); k != nil; k, v = cur.Next() {
target, err := unmarshalScraper(v)
if err != nil {
return nil, err
}
if filter.IDs != nil {
if _, ok := filter.IDs[target.ID]; !ok {
continue
}
}
if filter.Name != nil && target.Name != *filter.Name {
continue
}
if filter.Org != nil {
o, err := s.findOrganizationByName(ctx, tx, *filter.Org)
if err != nil {
return nil, err
}
if target.OrgID != o.ID {
continue
}
}
if filter.OrgID != nil {
o, err := s.findOrganizationByID(ctx, tx, *filter.OrgID)
if err != nil {
return nil, err
}
if target.OrgID != o.ID {
continue
}
}
targets = append(targets, *target)
}
return targets, nil

View File

@ -64,11 +64,22 @@ func initScraperTargetStoreService(s kv.Store, f influxdbtesting.TargetFields, t
}
}
for _, o := range f.Organizations {
if err := svc.PutOrganization(ctx, o); err != nil {
t.Fatalf("failed to populate organization")
}
}
return svc, kv.OpPrefix, func() {
for _, target := range f.Targets {
if err := svc.RemoveTarget(ctx, target.ID); err != nil {
t.Logf("failed to remove targets: %v", err)
}
}
for _, o := range f.Organizations {
if err := svc.DeleteOrganization(ctx, o.ID); err != nil {
t.Logf("failed to remove orgs: %v", err)
}
}
}
}

View File

@ -11,7 +11,8 @@ var _ platform.ScraperTargetStoreService = &ScraperTargetStoreService{}
// ScraperTargetStoreService is a mock implementation of a platform.ScraperTargetStoreService.
type ScraperTargetStoreService struct {
UserResourceMappingService
ListTargetsF func(ctx context.Context) ([]platform.ScraperTarget, error)
OrganizationService
ListTargetsF func(ctx context.Context, filter platform.ScraperTargetFilter) ([]platform.ScraperTarget, error)
AddTargetF func(ctx context.Context, t *platform.ScraperTarget, userID platform.ID) error
GetTargetByIDF func(ctx context.Context, id platform.ID) (*platform.ScraperTarget, error)
RemoveTargetF func(ctx context.Context, id platform.ID) error
@ -19,8 +20,8 @@ type ScraperTargetStoreService struct {
}
// ListTargets lists all the scraper targets.
func (s *ScraperTargetStoreService) ListTargets(ctx context.Context) ([]platform.ScraperTarget, error) {
return s.ListTargetsF(ctx)
func (s *ScraperTargetStoreService) ListTargets(ctx context.Context, filter platform.ScraperTargetFilter) ([]platform.ScraperTarget, error) {
return s.ListTargetsF(ctx, filter)
}
// AddTarget adds a scraper target.

View File

@ -29,7 +29,8 @@ type ScraperTarget struct {
// ScraperTargetStoreService defines the crud service for ScraperTarget.
type ScraperTargetStoreService interface {
UserResourceMappingService
ListTargets(ctx context.Context) ([]ScraperTarget, error)
OrganizationService
ListTargets(ctx context.Context, filter ScraperTargetFilter) ([]ScraperTarget, error)
AddTarget(ctx context.Context, t *ScraperTarget, userID ID) error
GetTargetByID(ctx context.Context, id ID) (*ScraperTarget, error)
RemoveTarget(ctx context.Context, id ID) error
@ -38,8 +39,10 @@ type ScraperTargetStoreService interface {
// ScraperTargetFilter represents a set of filter that restrict the returned results.
type ScraperTargetFilter struct {
ID *ID `json:"id"`
Name *string `json:"name"`
IDs map[ID]bool `json:"ids"`
Name *string `json:"name"`
OrgID *ID `json:"orgID"`
Org *string `json:"org"`
}
// ScraperType defines the scraper methods.

View File

@ -7,7 +7,7 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
platform "github.com/influxdata/influxdb"
"github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/mock"
)
@ -17,19 +17,55 @@ const (
targetThreeID = "020f755c3c082002"
)
var (
target1 = influxdb.ScraperTarget{
Name: "name1",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
URL: "url1",
ID: MustIDBase16(targetOneID),
}
target2 = influxdb.ScraperTarget{
Name: "name2",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgTwoID),
BucketID: MustIDBase16(bucketTwoID),
URL: "url2",
ID: MustIDBase16(targetTwoID),
}
target3 = influxdb.ScraperTarget{
Name: "name3",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketThreeID),
URL: "url3",
ID: MustIDBase16(targetThreeID),
}
org1 = influxdb.Organization{
ID: MustIDBase16(orgOneID),
Name: "org1",
}
org2 = influxdb.Organization{
ID: MustIDBase16(orgTwoID),
Name: "org2",
}
)
// TargetFields will include the IDGenerator, and targets
type TargetFields struct {
IDGenerator platform.IDGenerator
Targets []*platform.ScraperTarget
UserResourceMappings []*platform.UserResourceMapping
IDGenerator influxdb.IDGenerator
Targets []*influxdb.ScraperTarget
UserResourceMappings []*influxdb.UserResourceMapping
Organizations []*influxdb.Organization
}
var targetCmpOptions = cmp.Options{
cmp.Comparer(func(x, y []byte) bool {
return bytes.Equal(x, y)
}),
cmp.Transformer("Sort", func(in []platform.ScraperTarget) []platform.ScraperTarget {
out := append([]platform.ScraperTarget(nil), in...) // Copy input to avoid mutating it
cmp.Transformer("Sort", func(in []influxdb.ScraperTarget) []influxdb.ScraperTarget {
out := append([]influxdb.ScraperTarget(nil), in...) // Copy input to avoid mutating it
sort.Slice(out, func(i, j int) bool {
return out[i].ID.String() > out[j].ID.String()
})
@ -39,12 +75,12 @@ var targetCmpOptions = cmp.Options{
// ScraperService tests all the service functions.
func ScraperService(
init func(TargetFields, *testing.T) (platform.ScraperTargetStoreService, string, func()), t *testing.T,
init func(TargetFields, *testing.T) (influxdb.ScraperTargetStoreService, string, func()), t *testing.T,
) {
t.Helper()
tests := []struct {
name string
fn func(init func(TargetFields, *testing.T) (platform.ScraperTargetStoreService, string, func()),
fn func(init func(TargetFields, *testing.T) (influxdb.ScraperTargetStoreService, string, func()),
t *testing.T)
}{
{
@ -77,18 +113,18 @@ func ScraperService(
// AddTarget testing.
func AddTarget(
init func(TargetFields, *testing.T) (platform.ScraperTargetStoreService, string, func()),
init func(TargetFields, *testing.T) (influxdb.ScraperTargetStoreService, string, func()),
t *testing.T,
) {
t.Helper()
type args struct {
userID platform.ID
target *platform.ScraperTarget
userID influxdb.ID
target *influxdb.ScraperTarget
}
type wants struct {
err error
targets []platform.ScraperTarget
userResourceMappings []*platform.UserResourceMapping
targets []influxdb.ScraperTarget
userResourceMappings []*influxdb.UserResourceMapping
}
tests := []struct {
name string
@ -100,32 +136,33 @@ func AddTarget(
name: "create targets with empty set",
fields: TargetFields{
IDGenerator: mock.NewIDGenerator(targetOneID, t),
Targets: []*platform.ScraperTarget{},
UserResourceMappings: []*platform.UserResourceMapping{},
Targets: []*influxdb.ScraperTarget{},
UserResourceMappings: []*influxdb.UserResourceMapping{},
Organizations: []*influxdb.Organization{&org1},
},
args: args{
userID: MustIDBase16(threeID),
target: &platform.ScraperTarget{
target: &influxdb.ScraperTarget{
Name: "name1",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
URL: "url1",
},
},
wants: wants{
userResourceMappings: []*platform.UserResourceMapping{
userResourceMappings: []*influxdb.UserResourceMapping{
{
ResourceID: MustIDBase16(oneID),
ResourceType: platform.ScraperResourceType,
ResourceType: influxdb.ScraperResourceType,
UserID: MustIDBase16(threeID),
UserType: platform.Owner,
UserType: influxdb.Owner,
},
},
targets: []platform.ScraperTarget{
targets: []influxdb.ScraperTarget{
{
Name: "name1",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
URL: "url1",
@ -138,11 +175,12 @@ func AddTarget(
name: "create target with invalid org id",
fields: TargetFields{
IDGenerator: mock.NewIDGenerator(targetTwoID, t),
UserResourceMappings: []*platform.UserResourceMapping{},
Targets: []*platform.ScraperTarget{
UserResourceMappings: []*influxdb.UserResourceMapping{},
Organizations: []*influxdb.Organization{&org1},
Targets: []*influxdb.ScraperTarget{
{
Name: "name1",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
URL: "url1",
@ -151,25 +189,25 @@ func AddTarget(
},
},
args: args{
target: &platform.ScraperTarget{
target: &influxdb.ScraperTarget{
ID: MustIDBase16(targetTwoID),
Name: "name2",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
BucketID: MustIDBase16(bucketTwoID),
URL: "url2",
},
},
wants: wants{
err: &platform.Error{
Code: platform.EInvalid,
err: &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "provided organization ID has invalid format",
Op: platform.OpAddTarget,
Op: influxdb.OpAddTarget,
},
userResourceMappings: []*platform.UserResourceMapping{},
targets: []platform.ScraperTarget{
userResourceMappings: []*influxdb.UserResourceMapping{},
targets: []influxdb.ScraperTarget{
{
Name: "name1",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
URL: "url1",
@ -182,11 +220,12 @@ func AddTarget(
name: "create target with invalid bucket id",
fields: TargetFields{
IDGenerator: mock.NewIDGenerator(targetTwoID, t),
UserResourceMappings: []*platform.UserResourceMapping{},
Targets: []*platform.ScraperTarget{
UserResourceMappings: []*influxdb.UserResourceMapping{},
Organizations: []*influxdb.Organization{&org1},
Targets: []*influxdb.ScraperTarget{
{
Name: "name1",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
URL: "url1",
@ -195,25 +234,25 @@ func AddTarget(
},
},
args: args{
target: &platform.ScraperTarget{
target: &influxdb.ScraperTarget{
ID: MustIDBase16(targetTwoID),
Name: "name2",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgTwoID),
URL: "url2",
},
},
wants: wants{
err: &platform.Error{
Code: platform.EInvalid,
err: &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "provided bucket ID has invalid format",
Op: platform.OpAddTarget,
Op: influxdb.OpAddTarget,
},
userResourceMappings: []*platform.UserResourceMapping{},
targets: []platform.ScraperTarget{
userResourceMappings: []*influxdb.UserResourceMapping{},
targets: []influxdb.ScraperTarget{
{
Name: "name1",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
URL: "url1",
@ -226,55 +265,56 @@ func AddTarget(
name: "basic create target",
fields: TargetFields{
IDGenerator: mock.NewIDGenerator(targetTwoID, t),
Targets: []*platform.ScraperTarget{
Targets: []*influxdb.ScraperTarget{
{
Name: "name1",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
URL: "url1",
ID: MustIDBase16(targetOneID),
},
},
UserResourceMappings: []*platform.UserResourceMapping{
Organizations: []*influxdb.Organization{&org1, &org2},
UserResourceMappings: []*influxdb.UserResourceMapping{
{
ResourceID: MustIDBase16(targetOneID),
ResourceType: platform.ScraperResourceType,
ResourceType: influxdb.ScraperResourceType,
UserID: MustIDBase16(threeID),
UserType: platform.Member,
UserType: influxdb.Member,
},
},
},
args: args{
userID: MustIDBase16(threeID),
target: &platform.ScraperTarget{
target: &influxdb.ScraperTarget{
ID: MustIDBase16(targetTwoID),
Name: "name2",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgTwoID),
BucketID: MustIDBase16(bucketTwoID),
URL: "url2",
},
},
wants: wants{
userResourceMappings: []*platform.UserResourceMapping{
userResourceMappings: []*influxdb.UserResourceMapping{
{
ResourceID: MustIDBase16(oneID),
ResourceType: platform.ScraperResourceType,
ResourceType: influxdb.ScraperResourceType,
UserID: MustIDBase16(threeID),
UserType: platform.Member,
UserType: influxdb.Member,
},
{
ResourceID: MustIDBase16(twoID),
ResourceType: platform.ScraperResourceType,
ResourceType: influxdb.ScraperResourceType,
UserID: MustIDBase16(threeID),
UserType: platform.Owner,
UserType: influxdb.Owner,
},
},
targets: []platform.ScraperTarget{
targets: []influxdb.ScraperTarget{
{
Name: "name1",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
URL: "url1",
@ -282,7 +322,7 @@ func AddTarget(
},
{
Name: "name2",
Type: platform.PrometheusScraperType,
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgTwoID),
BucketID: MustIDBase16(bucketTwoID),
URL: "url2",
@ -301,16 +341,16 @@ func AddTarget(
diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)
defer s.RemoveTarget(ctx, tt.args.target.ID)
targets, err := s.ListTargets(ctx)
targets, err := s.ListTargets(ctx, influxdb.ScraperTargetFilter{})
if err != nil {
t.Fatalf("failed to retrieve scraper targets: %v", err)
}
if diff := cmp.Diff(targets, tt.wants.targets, targetCmpOptions...); diff != "" {
t.Errorf("scraper targets are different -got/+want\ndiff %s", diff)
}
urms, _, err := s.FindUserResourceMappings(ctx, platform.UserResourceMappingFilter{
urms, _, err := s.FindUserResourceMappings(ctx, influxdb.UserResourceMappingFilter{
UserID: tt.args.userID,
ResourceType: platform.ScraperResourceType,
ResourceType: influxdb.ScraperResourceType,
})
if err != nil {
t.Fatalf("failed to retrieve user resource mappings: %v", err)
@ -325,60 +365,192 @@ func AddTarget(
// ListTargets testing
func ListTargets(
init func(TargetFields, *testing.T) (platform.ScraperTargetStoreService, string, func()),
init func(TargetFields, *testing.T) (influxdb.ScraperTargetStoreService, string, func()),
t *testing.T,
) {
type args struct {
filter influxdb.ScraperTargetFilter
}
type wants struct {
targets []platform.ScraperTarget
targets []influxdb.ScraperTarget
err error
}
tests := []struct {
name string
fields TargetFields
args args
wants wants
}{
{
name: "find all targets",
name: "get all targets",
fields: TargetFields{
Targets: []*platform.ScraperTarget{
{
Name: "name1",
Type: platform.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
URL: "url1",
ID: MustIDBase16(targetOneID),
},
{
Name: "name2",
Type: platform.PrometheusScraperType,
OrgID: MustIDBase16(orgTwoID),
BucketID: MustIDBase16(bucketTwoID),
URL: "url2",
ID: MustIDBase16(targetTwoID),
},
Organizations: []*influxdb.Organization{
&org1,
&org2,
},
Targets: []*influxdb.ScraperTarget{
&target1,
&target2,
&target3,
},
},
args: args{
filter: influxdb.ScraperTargetFilter{},
},
wants: wants{
targets: []influxdb.ScraperTarget{
target1,
target2,
target3,
},
},
},
{
name: "filter by name",
fields: TargetFields{
Organizations: []*influxdb.Organization{
&org1,
&org2,
},
Targets: []*influxdb.ScraperTarget{
&target1,
&target2,
&target3,
},
},
args: args{
filter: influxdb.ScraperTargetFilter{
Name: strPtr(target2.Name),
},
},
wants: wants{
targets: []platform.ScraperTarget{
{
Name: "name1",
Type: platform.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
URL: "url1",
ID: MustIDBase16(targetOneID),
},
{
Name: "name2",
Type: platform.PrometheusScraperType,
OrgID: MustIDBase16(orgTwoID),
BucketID: MustIDBase16(bucketTwoID),
URL: "url2",
ID: MustIDBase16(targetTwoID),
},
targets: []influxdb.ScraperTarget{
target2,
},
},
},
{
name: "filter by id",
fields: TargetFields{
Organizations: []*influxdb.Organization{
&org1,
&org2,
},
Targets: []*influxdb.ScraperTarget{
&target1,
&target2,
&target3,
},
},
args: args{
filter: influxdb.ScraperTargetFilter{
IDs: map[influxdb.ID]bool{target2.ID: false},
},
},
wants: wants{
targets: []influxdb.ScraperTarget{
target2,
},
},
},
{
name: "filter targets by orgID",
fields: TargetFields{
Organizations: []*influxdb.Organization{
&org1,
&org2,
},
Targets: []*influxdb.ScraperTarget{
&target1,
&target2,
&target3,
},
},
args: args{
filter: influxdb.ScraperTargetFilter{
OrgID: idPtr(MustIDBase16(orgOneID)),
},
},
wants: wants{
targets: []influxdb.ScraperTarget{
target1,
target3,
},
},
},
{
name: "filter targets by orgID not exist",
fields: TargetFields{
Organizations: []*influxdb.Organization{
&org2,
},
Targets: []*influxdb.ScraperTarget{
&target1,
&target2,
&target3,
},
},
args: args{
filter: influxdb.ScraperTargetFilter{
OrgID: idPtr(MustIDBase16(orgOneID)),
},
},
wants: wants{
targets: []influxdb.ScraperTarget{},
err: &influxdb.Error{
Code: influxdb.ENotFound,
Msg: "organization not found",
},
},
},
{
name: "filter targets by org name",
fields: TargetFields{
Organizations: []*influxdb.Organization{
&org1,
&org2,
},
Targets: []*influxdb.ScraperTarget{
&target1,
&target2,
&target3,
},
},
args: args{
filter: influxdb.ScraperTargetFilter{
Org: strPtr("org1"),
},
},
wants: wants{
targets: []influxdb.ScraperTarget{
target1,
target3,
},
},
},
{
name: "filter targets by org name not exist",
fields: TargetFields{
Organizations: []*influxdb.Organization{
&org1,
},
Targets: []*influxdb.ScraperTarget{
&target1,
&target2,
&target3,
},
},
args: args{
filter: influxdb.ScraperTargetFilter{
Org: strPtr("org2"),
},
},
wants: wants{
targets: []influxdb.ScraperTarget{},
err: &influxdb.Error{
Code: influxdb.ENotFound,
Msg: `organization name "org2" not found`,
},
},
},
@ -388,7 +560,7 @@ func ListTargets(
s, opPrefix, done := init(tt.fields, t)
defer done()
ctx := context.Background()
targets, err := s.ListTargets(ctx)
targets, err := s.ListTargets(ctx, tt.args.filter)
diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)
if diff := cmp.Diff(targets, tt.wants.targets, targetCmpOptions...); diff != "" {
@ -400,16 +572,16 @@ func ListTargets(
// GetTargetByID testing
func GetTargetByID(
init func(TargetFields, *testing.T) (platform.ScraperTargetStoreService, string, func()),
init func(TargetFields, *testing.T) (influxdb.ScraperTargetStoreService, string, func()),
t *testing.T,
) {
t.Helper()
type args struct {
id platform.ID
id influxdb.ID
}
type wants struct {
err error
target *platform.ScraperTarget
target *influxdb.ScraperTarget
}
tests := []struct {
@ -421,7 +593,8 @@ func GetTargetByID(
{
name: "basic find target by id",
fields: TargetFields{
Targets: []*platform.ScraperTarget{
Organizations: []*influxdb.Organization{&org1},
Targets: []*influxdb.ScraperTarget{
{
ID: MustIDBase16(targetOneID),
Name: "target1",
@ -440,7 +613,7 @@ func GetTargetByID(
id: MustIDBase16(targetTwoID),
},
wants: wants{
target: &platform.ScraperTarget{
target: &influxdb.ScraperTarget{
ID: MustIDBase16(targetTwoID),
Name: "target2",
OrgID: MustIDBase16(orgOneID),
@ -451,7 +624,7 @@ func GetTargetByID(
{
name: "find target by id not find",
fields: TargetFields{
Targets: []*platform.ScraperTarget{
Targets: []*influxdb.ScraperTarget{
{
ID: MustIDBase16(targetOneID),
Name: "target1",
@ -470,9 +643,9 @@ func GetTargetByID(
id: MustIDBase16(threeID),
},
wants: wants{
err: &platform.Error{
Code: platform.ENotFound,
Op: platform.OpGetTargetByID,
err: &influxdb.Error{
Code: influxdb.ENotFound,
Op: influxdb.OpGetTargetByID,
Msg: "scraper target is not found",
},
},
@ -496,16 +669,16 @@ func GetTargetByID(
}
// RemoveTarget testing
func RemoveTarget(init func(TargetFields, *testing.T) (platform.ScraperTargetStoreService, string, func()),
func RemoveTarget(init func(TargetFields, *testing.T) (influxdb.ScraperTargetStoreService, string, func()),
t *testing.T) {
type args struct {
ID platform.ID
userID platform.ID
ID influxdb.ID
userID influxdb.ID
}
type wants struct {
err error
userResourceMappings []*platform.UserResourceMapping
targets []platform.ScraperTarget
userResourceMappings []*influxdb.UserResourceMapping
targets []influxdb.ScraperTarget
}
tests := []struct {
name string
@ -516,21 +689,22 @@ func RemoveTarget(init func(TargetFields, *testing.T) (platform.ScraperTargetSto
{
name: "delete targets using exist id",
fields: TargetFields{
UserResourceMappings: []*platform.UserResourceMapping{
Organizations: []*influxdb.Organization{&org1},
UserResourceMappings: []*influxdb.UserResourceMapping{
{
ResourceID: MustIDBase16(oneID),
UserID: MustIDBase16(threeID),
UserType: platform.Owner,
ResourceType: platform.ScraperResourceType,
UserType: influxdb.Owner,
ResourceType: influxdb.ScraperResourceType,
},
{
ResourceID: MustIDBase16(twoID),
UserID: MustIDBase16(threeID),
UserType: platform.Member,
ResourceType: platform.ScraperResourceType,
UserType: influxdb.Member,
ResourceType: influxdb.ScraperResourceType,
},
},
Targets: []*platform.ScraperTarget{
Targets: []*influxdb.ScraperTarget{
{
ID: MustIDBase16(targetOneID),
OrgID: MustIDBase16(orgOneID),
@ -548,15 +722,15 @@ func RemoveTarget(init func(TargetFields, *testing.T) (platform.ScraperTargetSto
userID: MustIDBase16(threeID),
},
wants: wants{
userResourceMappings: []*platform.UserResourceMapping{
userResourceMappings: []*influxdb.UserResourceMapping{
{
ResourceID: MustIDBase16(twoID),
UserID: MustIDBase16(threeID),
UserType: platform.Member,
ResourceType: platform.ScraperResourceType,
UserType: influxdb.Member,
ResourceType: influxdb.ScraperResourceType,
},
},
targets: []platform.ScraperTarget{
targets: []influxdb.ScraperTarget{
{
ID: MustIDBase16(targetTwoID),
OrgID: MustIDBase16(orgOneID),
@ -568,21 +742,22 @@ func RemoveTarget(init func(TargetFields, *testing.T) (platform.ScraperTargetSto
{
name: "delete targets using id that does not exist",
fields: TargetFields{
UserResourceMappings: []*platform.UserResourceMapping{
Organizations: []*influxdb.Organization{&org1},
UserResourceMappings: []*influxdb.UserResourceMapping{
{
ResourceID: MustIDBase16(oneID),
UserID: MustIDBase16(threeID),
UserType: platform.Owner,
ResourceType: platform.ScraperResourceType,
UserType: influxdb.Owner,
ResourceType: influxdb.ScraperResourceType,
},
{
ResourceID: MustIDBase16(twoID),
UserID: MustIDBase16(threeID),
UserType: platform.Member,
ResourceType: platform.ScraperResourceType,
UserType: influxdb.Member,
ResourceType: influxdb.ScraperResourceType,
},
},
Targets: []*platform.ScraperTarget{
Targets: []*influxdb.ScraperTarget{
{
ID: MustIDBase16(targetOneID),
OrgID: MustIDBase16(orgOneID),
@ -600,12 +775,12 @@ func RemoveTarget(init func(TargetFields, *testing.T) (platform.ScraperTargetSto
userID: MustIDBase16(threeID),
},
wants: wants{
err: &platform.Error{
Code: platform.ENotFound,
Op: platform.OpRemoveTarget,
err: &influxdb.Error{
Code: influxdb.ENotFound,
Op: influxdb.OpRemoveTarget,
Msg: "scraper target is not found",
},
targets: []platform.ScraperTarget{
targets: []influxdb.ScraperTarget{
{
ID: MustIDBase16(targetOneID),
OrgID: MustIDBase16(orgOneID),
@ -617,18 +792,18 @@ func RemoveTarget(init func(TargetFields, *testing.T) (platform.ScraperTargetSto
BucketID: MustIDBase16(bucketOneID),
},
},
userResourceMappings: []*platform.UserResourceMapping{
userResourceMappings: []*influxdb.UserResourceMapping{
{
ResourceID: MustIDBase16(oneID),
UserID: MustIDBase16(threeID),
UserType: platform.Owner,
ResourceType: platform.ScraperResourceType,
UserType: influxdb.Owner,
ResourceType: influxdb.ScraperResourceType,
},
{
ResourceID: MustIDBase16(twoID),
UserID: MustIDBase16(threeID),
UserType: platform.Member,
ResourceType: platform.ScraperResourceType,
UserType: influxdb.Member,
ResourceType: influxdb.ScraperResourceType,
},
},
},
@ -642,16 +817,16 @@ func RemoveTarget(init func(TargetFields, *testing.T) (platform.ScraperTargetSto
err := s.RemoveTarget(ctx, tt.args.ID)
diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)
targets, err := s.ListTargets(ctx)
targets, err := s.ListTargets(ctx, influxdb.ScraperTargetFilter{})
if err != nil {
t.Fatalf("failed to retrieve targets: %v", err)
}
if diff := cmp.Diff(targets, tt.wants.targets, targetCmpOptions...); diff != "" {
t.Errorf("targets are different -got/+want\ndiff %s", diff)
}
urms, _, err := s.FindUserResourceMappings(ctx, platform.UserResourceMappingFilter{
urms, _, err := s.FindUserResourceMappings(ctx, influxdb.UserResourceMappingFilter{
UserID: tt.args.userID,
ResourceType: platform.ScraperResourceType,
ResourceType: influxdb.ScraperResourceType,
})
if err != nil {
t.Fatalf("failed to retrieve user resource mappings: %v", err)
@ -665,17 +840,17 @@ func RemoveTarget(init func(TargetFields, *testing.T) (platform.ScraperTargetSto
// UpdateTarget testing
func UpdateTarget(
init func(TargetFields, *testing.T) (platform.ScraperTargetStoreService, string, func()),
init func(TargetFields, *testing.T) (influxdb.ScraperTargetStoreService, string, func()),
t *testing.T,
) {
type args struct {
url string
userID platform.ID
id platform.ID
userID influxdb.ID
id influxdb.ID
}
type wants struct {
err error
target *platform.ScraperTarget
target *influxdb.ScraperTarget
}
tests := []struct {
@ -687,7 +862,8 @@ func UpdateTarget(
{
name: "update url with blank id",
fields: TargetFields{
Targets: []*platform.ScraperTarget{
Organizations: []*influxdb.Organization{&org1},
Targets: []*influxdb.ScraperTarget{
{
ID: MustIDBase16(targetOneID),
URL: "url1",
@ -706,9 +882,9 @@ func UpdateTarget(
url: "changed",
},
wants: wants{
err: &platform.Error{
Code: platform.EInvalid,
Op: platform.OpUpdateTarget,
err: &influxdb.Error{
Code: influxdb.EInvalid,
Op: influxdb.OpUpdateTarget,
Msg: "provided scraper target ID has invalid format",
},
},
@ -716,7 +892,8 @@ func UpdateTarget(
{
name: "update url with non exist id",
fields: TargetFields{
Targets: []*platform.ScraperTarget{
Organizations: []*influxdb.Organization{&org1},
Targets: []*influxdb.ScraperTarget{
{
ID: MustIDBase16(targetOneID),
URL: "url1",
@ -736,9 +913,9 @@ func UpdateTarget(
url: "changed",
},
wants: wants{
err: &platform.Error{
Code: platform.ENotFound,
Op: platform.OpUpdateTarget,
err: &influxdb.Error{
Code: influxdb.ENotFound,
Op: influxdb.OpUpdateTarget,
Msg: "scraper target is not found",
},
},
@ -746,7 +923,8 @@ func UpdateTarget(
{
name: "update url",
fields: TargetFields{
Targets: []*platform.ScraperTarget{
Organizations: []*influxdb.Organization{&org1},
Targets: []*influxdb.ScraperTarget{
{
ID: MustIDBase16(targetOneID),
URL: "url1",
@ -766,7 +944,7 @@ func UpdateTarget(
url: "changed",
},
wants: wants{
target: &platform.ScraperTarget{
target: &influxdb.ScraperTarget{
ID: MustIDBase16(targetOneID),
URL: "changed",
OrgID: MustIDBase16(orgOneID),
@ -782,7 +960,7 @@ func UpdateTarget(
defer done()
ctx := context.Background()
upd := &platform.ScraperTarget{
upd := &influxdb.ScraperTarget{
ID: tt.args.id,
URL: tt.args.url,
}