From 1195534dcd8e180fc1b85230aeecf9b200039ba3 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Mon, 6 Jan 2020 10:34:50 +0100 Subject: [PATCH] Automation REST handler & filter cleanup --- compose/rest/automation.go | 28 +++++++++++++--------------- pkg/corredor/filter.go | 10 +++++++--- pkg/corredor/filter_test.go | 4 ++-- pkg/corredor/service.go | 2 +- pkg/corredor/service_test.go | 4 ++-- pkg/corredor/util.go | 17 ++++++++++++++++- pkg/corredor/util_test.go | 8 ++++---- system/rest/automation.go | 27 +++++++++++++-------------- 8 files changed, 58 insertions(+), 42 deletions(-) diff --git a/compose/rest/automation.go b/compose/rest/automation.go index c834cac5b..540e227dc 100644 --- a/compose/rest/automation.go +++ b/compose/rest/automation.go @@ -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) { diff --git a/pkg/corredor/filter.go b/pkg/corredor/filter.go index bf40312a0..d3bc4f524 100644 --- a/pkg/corredor/filter.go +++ b/pkg/corredor/filter.go @@ -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] diff --git a/pkg/corredor/filter_test.go b/pkg/corredor/filter_test.go index 1ad61e8ad..a0cb1e723 100644 --- a/pkg/corredor/filter_test.go +++ b/pkg/corredor/filter_test.go @@ -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"}) } diff --git a/pkg/corredor/service.go b/pkg/corredor/service.go index b1c00ef4c..79e1fc5d6 100644 --- a/pkg/corredor/service.go +++ b/pkg/corredor/service.go @@ -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 ( diff --git a/pkg/corredor/service_test.go b/pkg/corredor/service_test.go index 248fdecc1..48b81a5ac 100644 --- a/pkg/corredor/service_test.go +++ b/pkg/corredor/service_test.go @@ -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) ) diff --git a/pkg/corredor/util.go b/pkg/corredor/util.go index 9de6702bd..9b70d09d4 100644 --- a/pkg/corredor/util.go +++ b/pkg/corredor/util.go @@ -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 +} diff --git a/pkg/corredor/util_test.go b/pkg/corredor/util_test.go index c7fb3eb58..4ed47a4fe 100644 --- a/pkg/corredor/util_test.go +++ b/pkg/corredor/util_test.go @@ -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))) diff --git a/system/rest/automation.go b/system/rest/automation.go index 86c21c213..84a5de1b8 100644 --- a/system/rest/automation.go +++ b/system/rest/automation.go @@ -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) {