2018-09-14 23:30:05 +00:00
|
|
|
package inmem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/influxdata/platform"
|
|
|
|
kerrors "github.com/influxdata/platform/kit/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FindMacroByID implements the platform.MacroService interface
|
|
|
|
func (s *Service) FindMacroByID(ctx context.Context, id platform.ID) (*platform.Macro, error) {
|
|
|
|
i, ok := s.macroKV.Load(id.String())
|
|
|
|
if !ok {
|
|
|
|
return nil, kerrors.Errorf(kerrors.NotFound, "macro with ID %v not found", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro, ok := i.(*platform.Macro)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("type %T is not a macro", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return macro, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindMacros implements the platform.MacroService interface
|
|
|
|
func (s *Service) FindMacros(ctx context.Context) ([]*platform.Macro, error) {
|
|
|
|
var err error
|
|
|
|
var macros []*platform.Macro
|
|
|
|
s.macroKV.Range(func(k, v interface{}) bool {
|
|
|
|
macro, ok := v.(*platform.Macro)
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("type %T is not a macro", v)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
macros = append(macros, macro)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return macros, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateMacro implements the platform.MacroService interface
|
|
|
|
func (s *Service) CreateMacro(ctx context.Context, m *platform.Macro) error {
|
|
|
|
m.ID = s.IDGenerator.ID()
|
2018-09-18 15:30:52 +00:00
|
|
|
return s.ReplaceMacro(ctx, m)
|
2018-09-14 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateMacro implements the platform.MacroService interface
|
|
|
|
func (s *Service) UpdateMacro(ctx context.Context, id platform.ID, update *platform.MacroUpdate) (*platform.Macro, error) {
|
|
|
|
macro, err := s.FindMacroByID(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := update.Apply(macro); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:30:52 +00:00
|
|
|
if err := s.ReplaceMacro(ctx, macro); err != nil {
|
2018-09-14 23:30:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return macro, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteMacro implements the platform.MacroService interface
|
|
|
|
func (s *Service) DeleteMacro(ctx context.Context, id platform.ID) error {
|
|
|
|
_, err := s.FindMacroByID(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.macroKV.Delete(id.String())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:30:52 +00:00
|
|
|
// ReplaceMacro stores a Macro in the key value store
|
|
|
|
func (s *Service) ReplaceMacro(ctx context.Context, m *platform.Macro) error {
|
2018-09-14 23:30:05 +00:00
|
|
|
s.macroKV.Store(m.ID.String(), m)
|
|
|
|
return nil
|
|
|
|
}
|