influxdb/inmem/telegraf.go

196 lines
5.3 KiB
Go
Raw Normal View History

2018-10-15 19:17:01 +00:00
package inmem
import (
"context"
platform "github.com/influxdata/influxdb"
2018-10-15 19:17:01 +00:00
)
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 := OpPrefix + platform.OpFindTelegrafConfigByID
2018-10-15 19:17:01 +00:00
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: platform.ErrTelegrafConfigNotFound,
2018-10-15 19:17:01 +00:00
}
}
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.TelegrafConfigFilter) (*platform.TelegrafConfig, error) {
op := OpPrefix + platform.OpFindTelegrafConfig
tcs, n, err := s.FindTelegrafConfigs(ctx, filter, platform.FindOptions{Limit: 1})
2018-10-15 19:17:01 +00:00
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.TelegrafConfigFilter, opt ...platform.FindOptions) ([]*platform.TelegrafConfig, int, *platform.Error) {
2018-10-15 19:17:01 +00:00
tcs := make([]*platform.TelegrafConfig, 0)
m, _, err := s.FindUserResourceMappings(ctx, filter.UserResourceMappingFilter)
2018-10-15 19:17:01 +00:00
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 && platform.ErrorCode(err) != platform.ENotFound {
2018-10-15 19:17:01 +00:00
return nil, 0, &platform.Error{
// return internal error, for any mapping issue
Err: err,
}
}
if tc != nil {
// Restrict results by organization ID, if it has been provided
if filter.OrganizationID != nil && filter.OrganizationID.Valid() && tc.OrganizationID != *filter.OrganizationID {
continue
}
tcs = append(tcs, tc)
2018-10-15 19:17:01 +00:00
}
}
2018-10-15 19:17:01 +00:00
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.TelegrafConfigFilter, opt ...platform.FindOptions) (tcs []*platform.TelegrafConfig, n int, err error) {
op := OpPrefix + platform.OpFindTelegrafConfigs
2018-10-15 19:17:01 +00:00
var pErr *platform.Error
tcs, n, pErr = s.findTelegrafConfigs(ctx, filter)
if pErr != nil {
pErr.Op = op
err = pErr
}
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,
}
}
if !tc.OrganizationID.Valid() {
return &platform.Error{
Code: platform.EEmptyValue,
Msg: platform.ErrTelegrafConfigInvalidOrganizationID,
}
}
2018-10-15 19:17:01 +00:00
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 {
op := OpPrefix + platform.OpCreateTelegrafConfig
2018-10-15 19:17:01 +00:00
tc.ID = s.IDGenerator.ID()
pErr := s.putTelegrafConfig(ctx, tc)
if pErr != nil {
pErr.Op = op
return pErr
}
urm := &platform.UserResourceMapping{
ResourceID: tc.ID,
UserID: userID,
UserType: platform.Owner,
ResourceType: platform.TelegrafsResourceType,
2018-10-15 19:17:01 +00:00
}
if err := s.CreateUserResourceMapping(ctx, urm); err != nil {
return err
2018-10-15 19:17:01 +00:00
}
return nil
2018-10-15 19:17:01 +00:00
}
// 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 := OpPrefix + platform.OpUpdateTelegrafConfig
current, 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
// OrganizationID can not be updated
tc.OrganizationID = current.OrganizationID
2018-10-15 19:17:01 +00:00
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 := OpPrefix + platform.OpDeleteTelegrafConfig
2018-10-15 19:17:01 +00:00
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{
ResourceID: id,
ResourceType: platform.TelegrafsResourceType,
2018-10-15 19:17:01 +00:00
})
if err != nil {
return &platform.Error{
Code: platform.ErrorCode(err),
Op: op,
Err: err,
}
}
return nil
}