3
0

Automation REST handler & filter cleanup

This commit is contained in:
Denis Arh
2020-01-06 10:34:50 +01:00
parent 4d6cb13f70
commit 1195534dcd
8 changed files with 58 additions and 42 deletions

View File

@@ -13,27 +13,25 @@ import (
var _ = errors.Wrap
type Automation struct {
// xxx service.XXXService
}
type (
Automation struct{}
)
func (Automation) New() *Automation {
return &Automation{}
}
func (ctrl *Automation) List(ctx context.Context, r *request.AutomationList) (interface{}, error) {
f := corredor.ManualScriptFilter{
ResourceTypes: r.ResourceTypes,
EventTypes: r.EventTypes,
ExcludeServerScripts: r.ExcludeServerScripts,
ExcludeClientScripts: r.ExcludeClientScripts,
}
f.PrefixResource("compose")
scripts, _, err := corredor.Service().FindOnManual(f)
return scripts, err
return corredor.GenericListHandler(
corredor.Service(),
corredor.Filter{
ResourceTypes: r.ResourceTypes,
EventTypes: r.EventTypes,
ExcludeServerScripts: r.ExcludeServerScripts,
ExcludeClientScripts: r.ExcludeClientScripts,
},
"compose",
)
}
func (ctrl *Automation) Trigger(ctx context.Context, r *request.AutomationTrigger) (interface{}, error) {

View File

@@ -5,16 +5,20 @@ import (
)
type (
ManualScriptFilter struct {
Filter struct {
ResourceTypes []string
EventTypes []string
ExcludeServerScripts bool
ExcludeClientScripts bool
Page uint `json:"page"`
PerPage uint `json:"perPage"`
Count uint `json:"count"`
}
)
// PrefixResource adds service string (if not already there) to all resources
func (f *ManualScriptFilter) PrefixResource(service string) {
// PrefixResources adds service string (if not already there) to all resources
func (f *Filter) PrefixResources(service string) {
for i := range f.ResourceTypes {
if !strings.HasPrefix(f.ResourceTypes[i], service) {
f.ResourceTypes[i] = service + ":" + f.ResourceTypes[i]

View File

@@ -7,11 +7,11 @@ import (
)
func TestManualScriptFilterResourcePrefixing(t *testing.T) {
f := &ManualScriptFilter{
f := &Filter{
ResourceTypes: []string{"system", "system:one", "two"},
}
f.PrefixResource("system")
f.PrefixResources("system")
assert.New(t).Equal(f.ResourceTypes, []string{"system", "system:one", "system:two"})
}

View File

@@ -144,7 +144,7 @@ func (svc *service) Load(ctx context.Context) {
}
// FindManual returns filtered list of scripts that can be manually triggered
func (svc service) FindOnManual(filter ManualScriptFilter) (out ScriptSet, f ManualScriptFilter, err error) {
func (svc service) Find(filter Filter) (out ScriptSet, f Filter, err error) {
f = filter
var (

View File

@@ -88,14 +88,14 @@ func TestFindOnManual(t *testing.T) {
},
},
}
filter = ManualScriptFilter{
filter = Filter{
ResourceTypes: []string{"res"},
EventTypes: []string{"ev"},
ExcludeServerScripts: false,
ExcludeClientScripts: false,
}
o, _, err = svc.FindOnManual(filter)
o, _, err = svc.Find(filter)
a = assert.New(t)
)

View File

@@ -8,6 +8,13 @@ import (
"github.com/cortezaproject/corteza-server/pkg/slice"
)
type (
automationListSetPayload struct {
Filter Filter `json:"filter"`
Set []*Script `json:"set"`
}
)
// removes onManual event type from trigger
// returns true if event type was removed or
// false if there was no onManual event
@@ -84,7 +91,7 @@ func encodeArguments(args map[string]string, key string, val interface{}) (err e
}
// Creates a filter fn for script filtering
func makeScriptFilter(f ManualScriptFilter) func(s *Script) (b bool, err error) {
func makeScriptFilter(f Filter) func(s *Script) (b bool, err error) {
return func(s *Script) (b bool, err error) {
b = true
if len(f.ResourceTypes) > 0 {
@@ -123,3 +130,11 @@ func makeScriptFilter(f ManualScriptFilter) func(s *Script) (b bool, err error)
return
}
}
// GenericListHandler returns filtered list of scripts
func GenericListHandler(svc *service, f Filter, resourcePrefix string) (p *automationListSetPayload, err error) {
f.PrefixResources(resourcePrefix)
p = &automationListSetPayload{}
p.Set, p.Filter, err = svc.Find(f)
return p, err
}

View File

@@ -137,22 +137,22 @@ func TestScriptFilterMaker(t *testing.T) {
f func(s *Script) (b bool, err error)
)
f = makeScriptFilter(ManualScriptFilter{})
f = makeScriptFilter(Filter{})
a.True(strip(f(s1)))
a.True(strip(f(s2)))
a.True(strip(f(s3)))
f = makeScriptFilter(ManualScriptFilter{ResourceTypes: []string{"res"}})
f = makeScriptFilter(Filter{ResourceTypes: []string{"res"}})
a.True(strip(f(s1)))
a.False(strip(f(s2)))
a.False(strip(f(s3)))
f = makeScriptFilter(ManualScriptFilter{EventTypes: []string{"ev"}})
f = makeScriptFilter(Filter{EventTypes: []string{"ev"}})
a.True(strip(f(s1)))
a.False(strip(f(s2)))
a.False(strip(f(s3)))
f = makeScriptFilter(ManualScriptFilter{EventTypes: []string{"ev", "foo"}})
f = makeScriptFilter(Filter{EventTypes: []string{"ev", "foo"}})
a.True(strip(f(s1)))
a.True(strip(f(s2)))
a.False(strip(f(s3)))

View File

@@ -13,26 +13,25 @@ import (
var _ = errors.Wrap
type Automation struct {
// xxx service.XXXService
}
type (
Automation struct{}
)
func (Automation) New() *Automation {
return &Automation{}
}
func (ctrl *Automation) List(ctx context.Context, r *request.AutomationList) (interface{}, error) {
f := corredor.ManualScriptFilter{
ResourceTypes: r.ResourceTypes,
ExcludeServerScripts: r.ExcludeServerScripts,
ExcludeClientScripts: r.ExcludeClientScripts,
}
f.PrefixResource("system")
scripts, _, err := corredor.Service().FindOnManual(f)
return scripts, err
return corredor.GenericListHandler(
corredor.Service(),
corredor.Filter{
ResourceTypes: r.ResourceTypes,
EventTypes: r.EventTypes,
ExcludeServerScripts: r.ExcludeServerScripts,
ExcludeClientScripts: r.ExcludeClientScripts,
},
"system",
)
}
func (ctrl *Automation) Trigger(ctx context.Context, r *request.AutomationTrigger) (interface{}, error) {