2018-10-15 19:17:01 +00:00
|
|
|
package inmem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/influxdata/platform"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ platform.TelegrafConfigStore = new(Service)
|
|
|
|
|
|
|
|
// FindTelegrafConfigByID returns a single telegraf config by ID.
|
|
|
|
func (s *Service) FindTelegrafConfigByID(ctx context.Context, id platform.ID) (tc *platform.TelegrafConfig, err error) {
|
|
|
|
op := "inmem/find telegraf config by id"
|
|
|
|
var pErr *platform.Error
|
|
|
|
tc, pErr = s.findTelegrafConfigByID(ctx, id)
|
|
|
|
if pErr != nil {
|
|
|
|
pErr.Op = op
|
|
|
|
err = pErr
|
|
|
|
}
|
|
|
|
return tc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) findTelegrafConfigByID(ctx context.Context, id platform.ID) (*platform.TelegrafConfig, *platform.Error) {
|
|
|
|
if !id.Valid() {
|
|
|
|
return nil, &platform.Error{
|
|
|
|
Code: platform.EEmptyValue,
|
|
|
|
Err: platform.ErrInvalidID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result, found := s.telegrafConfigKV.Load(id)
|
|
|
|
if !found {
|
|
|
|
return nil, &platform.Error{
|
|
|
|
Code: platform.ENotFound,
|
|
|
|
Msg: fmt.Sprintf("telegraf config with ID %v not found", id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tc := new(platform.TelegrafConfig)
|
|
|
|
*tc = result.(platform.TelegrafConfig)
|
|
|
|
return tc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindTelegrafConfig returns the first telegraf config that matches filter.
|
|
|
|
func (s *Service) FindTelegrafConfig(ctx context.Context, filter platform.UserResourceMappingFilter) (*platform.TelegrafConfig, error) {
|
|
|
|
op := "inmem/find telegraf config"
|
|
|
|
tcs, n, err := s.FindTelegrafConfigs(ctx, filter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if n > 0 {
|
|
|
|
return tcs[0], nil
|
|
|
|
}
|
|
|
|
return nil, &platform.Error{
|
|
|
|
Code: platform.ENotFound,
|
|
|
|
Op: op,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) findTelegrafConfigs(ctx context.Context, filter platform.UserResourceMappingFilter, opt ...platform.FindOptions) ([]*platform.TelegrafConfig, int, *platform.Error) {
|
|
|
|
tcs := make([]*platform.TelegrafConfig, 0)
|
|
|
|
m, _, err := s.FindUserResourceMappings(ctx, filter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, &platform.Error{
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(m) == 0 {
|
2018-11-14 02:30:32 +00:00
|
|
|
return tcs, 0, nil
|
2018-10-15 19:17:01 +00:00
|
|
|
}
|
|
|
|
for _, item := range m {
|
|
|
|
tc, err := s.findTelegrafConfigByID(ctx, item.ResourceID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, &platform.Error{
|
|
|
|
// return internal error, for any mapping issue
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tcs = append(tcs, tc)
|
|
|
|
}
|
|
|
|
if len(tcs) == 0 {
|
|
|
|
return nil, 0, &platform.Error{
|
|
|
|
Msg: "inconsistent user resource mapping and telegraf config",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tcs, len(tcs), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindTelegrafConfigs returns a list of telegraf configs that match filter and the total count of matching telegraf configs.
|
|
|
|
// Additional options provide pagination & sorting.
|
|
|
|
func (s *Service) FindTelegrafConfigs(ctx context.Context, filter platform.UserResourceMappingFilter, opt ...platform.FindOptions) (tcs []*platform.TelegrafConfig, n int, err error) {
|
|
|
|
op := "inmem/find telegraf configs"
|
|
|
|
var pErr *platform.Error
|
|
|
|
tcs, n, pErr = s.findTelegrafConfigs(ctx, filter)
|
|
|
|
if pErr != nil {
|
|
|
|
pErr.Op = op
|
|
|
|
err = pErr
|
|
|
|
}
|
2018-11-01 17:26:18 +00:00
|
|
|
return tcs, n, err
|
2018-10-15 19:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) putTelegrafConfig(ctx context.Context, tc *platform.TelegrafConfig) *platform.Error {
|
|
|
|
if !tc.ID.Valid() {
|
|
|
|
return &platform.Error{
|
|
|
|
Code: platform.EEmptyValue,
|
|
|
|
Err: platform.ErrInvalidID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.telegrafConfigKV.Store(tc.ID, *tc)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTelegrafConfig creates a new telegraf config and sets b.ID with the new identifier.
|
2018-12-21 16:05:55 +00:00
|
|
|
func (s *Service) CreateTelegrafConfig(ctx context.Context, tc *platform.TelegrafConfig, userID platform.ID) error {
|
2018-10-15 19:17:01 +00:00
|
|
|
op := "inmem/create telegraf config"
|
|
|
|
tc.ID = s.IDGenerator.ID()
|
|
|
|
err := s.CreateUserResourceMapping(ctx, &platform.UserResourceMapping{
|
2018-12-28 23:02:19 +00:00
|
|
|
ResourceID: tc.ID,
|
|
|
|
UserID: userID,
|
|
|
|
UserType: platform.Owner,
|
|
|
|
Resource: platform.TelegrafsResource,
|
2018-10-15 19:17:01 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pErr := s.putTelegrafConfig(ctx, tc)
|
|
|
|
if pErr != nil {
|
|
|
|
pErr.Op = op
|
|
|
|
err = pErr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTelegrafConfig updates a single telegraf config.
|
|
|
|
// Returns the new telegraf config after update.
|
2018-12-21 16:05:55 +00:00
|
|
|
func (s *Service) UpdateTelegrafConfig(ctx context.Context, id platform.ID, tc *platform.TelegrafConfig, userID platform.ID) (*platform.TelegrafConfig, error) {
|
2018-10-15 19:17:01 +00:00
|
|
|
var err error
|
|
|
|
op := "inmem/update telegraf config"
|
2018-12-21 16:05:55 +00:00
|
|
|
_, pErr := s.findTelegrafConfigByID(ctx, id)
|
2018-10-15 19:17:01 +00:00
|
|
|
if pErr != nil {
|
|
|
|
pErr.Op = op
|
|
|
|
err = pErr
|
2018-10-24 15:13:30 +00:00
|
|
|
return nil, err
|
2018-10-15 19:17:01 +00:00
|
|
|
}
|
|
|
|
tc.ID = id
|
|
|
|
pErr = s.putTelegrafConfig(ctx, tc)
|
|
|
|
if pErr != nil {
|
|
|
|
pErr.Op = op
|
|
|
|
err = pErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return tc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTelegrafConfig removes a telegraf config by ID.
|
|
|
|
func (s *Service) DeleteTelegrafConfig(ctx context.Context, id platform.ID) error {
|
|
|
|
op := "inmem/delete telegraf config"
|
|
|
|
var err error
|
|
|
|
if !id.Valid() {
|
|
|
|
return &platform.Error{
|
|
|
|
Op: op,
|
|
|
|
Code: platform.EEmptyValue,
|
|
|
|
Err: platform.ErrInvalidID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, pErr := s.findTelegrafConfigByID(ctx, id); pErr != nil {
|
|
|
|
return pErr
|
|
|
|
}
|
|
|
|
s.telegrafConfigKV.Delete(id)
|
|
|
|
|
|
|
|
err = s.deleteUserResourceMapping(ctx, platform.UserResourceMappingFilter{
|
2018-12-28 23:02:19 +00:00
|
|
|
ResourceID: id,
|
|
|
|
Resource: platform.TelegrafsResource,
|
2018-10-15 19:17:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return &platform.Error{
|
|
|
|
Code: platform.ErrorCode(err),
|
|
|
|
Op: op,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|