influxdb/inmem/scraper.go

190 lines
4.6 KiB
Go
Raw Normal View History

package inmem
import (
"context"
"fmt"
2019-04-12 16:45:48 +00:00
"github.com/influxdata/influxdb"
)
const (
errScraperTargetNotFound = "scraper target is not found"
)
2019-04-12 16:45:48 +00:00
var _ influxdb.ScraperTargetStoreService = (*Service)(nil)
2019-04-12 16:45:48 +00:00
func (s *Service) loadScraperTarget(id influxdb.ID) (*influxdb.ScraperTarget, *influxdb.Error) {
i, ok := s.scraperTargetKV.Load(id.String())
if !ok {
2019-04-12 16:45:48 +00:00
return nil, &influxdb.Error{
Code: influxdb.ENotFound,
Msg: errScraperTargetNotFound,
}
}
2019-04-12 16:45:48 +00:00
b, ok := i.(influxdb.ScraperTarget)
if !ok {
2019-04-12 16:45:48 +00:00
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("type %T is not a scraper target", i),
}
}
return &b, nil
}
// ListTargets will list all scrape targets.
2019-04-12 16:45:48 +00:00
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 {
2019-04-12 16:45:48 +00:00
target, ok := v.(influxdb.ScraperTarget)
if !ok {
2019-04-12 16:45:48 +00:00
err = &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("type %T is not a scraper target", v),
}
return false
}
2019-04-12 16:45:48 +00:00
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.
2019-04-12 16:45:48 +00:00
func (s *Service) AddTarget(ctx context.Context, target *influxdb.ScraperTarget, userID influxdb.ID) (err error) {
target.ID = s.IDGenerator.ID()
if !target.OrgID.Valid() {
2019-04-12 16:45:48 +00:00
return &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "provided organization ID has invalid format",
2019-04-12 16:45:48 +00:00
Op: OpPrefix + influxdb.OpAddTarget,
}
}
if !target.BucketID.Valid() {
2019-04-12 16:45:48 +00:00
return &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "provided bucket ID has invalid format",
2019-04-12 16:45:48 +00:00
Op: OpPrefix + influxdb.OpAddTarget,
}
}
if err := s.PutTarget(ctx, target); err != nil {
2019-04-12 16:45:48 +00:00
return &influxdb.Error{
Op: OpPrefix + influxdb.OpAddTarget,
Err: err,
}
}
2019-04-12 16:45:48 +00:00
urm := &influxdb.UserResourceMapping{
ResourceID: target.ID,
UserID: userID,
2019-04-12 16:45:48 +00:00
UserType: influxdb.Owner,
ResourceType: influxdb.ScraperResourceType,
}
if err := s.CreateUserResourceMapping(ctx, urm); err != nil {
return err
}
return nil
}
// RemoveTarget removes a scraper target from the bucket.
2019-04-12 16:45:48 +00:00
func (s *Service) RemoveTarget(ctx context.Context, id influxdb.ID) error {
if _, pe := s.loadScraperTarget(id); pe != nil {
2019-04-12 16:45:48 +00:00
return &influxdb.Error{
Err: pe,
2019-04-12 16:45:48 +00:00
Op: OpPrefix + influxdb.OpRemoveTarget,
}
}
s.scraperTargetKV.Delete(id.String())
2019-04-12 16:45:48 +00:00
err := s.deleteUserResourceMapping(ctx, influxdb.UserResourceMappingFilter{
ResourceID: id,
2019-04-12 16:45:48 +00:00
ResourceType: influxdb.ScraperResourceType,
})
if err != nil {
2019-04-12 16:45:48 +00:00
return &influxdb.Error{
Code: influxdb.ErrorCode(err),
Op: OpPrefix + influxdb.OpRemoveTarget,
Err: err,
}
}
return nil
}
// UpdateTarget updates a scraper target.
2019-04-12 16:45:48 +00:00
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() {
2019-04-12 16:45:48 +00:00
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 {
2019-04-12 16:45:48 +00:00
return nil, &influxdb.Error{
Op: op,
Err: pe,
}
}
if !update.OrgID.Valid() {
update.OrgID = oldTarget.OrgID
}
if !update.BucketID.Valid() {
update.BucketID = oldTarget.BucketID
}
if err = s.PutTarget(ctx, update); err != nil {
2019-04-12 16:45:48 +00:00
return nil, &influxdb.Error{
Op: op,
Err: pe,
}
}
return update, nil
}
// GetTargetByID retrieves a scraper target by id.
2019-04-12 16:45:48 +00:00
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 {
2019-04-12 16:45:48 +00:00
return nil, &influxdb.Error{
Op: OpPrefix + influxdb.OpGetTargetByID,
Err: pe,
}
}
return target, nil
}
// PutTarget will put a scraper target without setting an ID.
2019-04-12 16:45:48 +00:00
func (s *Service) PutTarget(ctx context.Context, target *influxdb.ScraperTarget) error {
s.scraperTargetKV.Store(target.ID.String(), *target)
return nil
}