Skip to content

Commit e2e4c7a

Browse files
rbeuque74loopfz
authored andcommitted
fix: templates: Multiple templates import no longer breaks when depends of each others in sub-task pluggin (#79)
Fixes #74 Signed-off-by: Romain Beuque <[email protected]> Signed-off-by: Romain Beuque <[email protected]>
1 parent dabc1b4 commit e2e4c7a

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

models/tasktemplate/fromdir.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ import (
99
"github.com/ghodss/yaml"
1010
"github.com/juju/errors"
1111
"github.com/loopfz/gadgeto/zesty"
12+
"github.com/ovh/utask/pkg/templateimport"
1213
"github.com/sirupsen/logrus"
1314
)
1415

16+
var (
17+
discoveredTemplates []TaskTemplate = []TaskTemplate{}
18+
)
19+
1520
// LoadFromDir reads yaml-formatted task templates
1621
// from a folder and upserts them in database
1722
func LoadFromDir(dbp zesty.DBProvider, dir string) error {
@@ -31,8 +36,15 @@ func LoadFromDir(dbp zesty.DBProvider, dir string) error {
3136
if err := yaml.Unmarshal(tmpl, &tt); err != nil {
3237
return fmt.Errorf("failed to unmarshal '%s': '%s'", file.Name(), err)
3338
}
34-
verb := "Created"
39+
3540
tt.Normalize()
41+
42+
discoveredTemplates = append(discoveredTemplates, tt)
43+
templateimport.AddTemplate(tt.Name)
44+
}
45+
46+
for _, tt := range discoveredTemplates {
47+
verb := "Created"
3648
existing, err := LoadFromName(dbp, tt.Name)
3749
if err != nil {
3850
if !errors.IsNotFound(err) {
@@ -50,5 +62,6 @@ func LoadFromDir(dbp zesty.DBProvider, dir string) error {
5062
}
5163
logrus.Infof("%s task template '%s'", verb, tt.Name)
5264
}
65+
templateimport.CleanTemplates()
5366
return nil
5467
}

pkg/plugins/builtin/subtask/subtask.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/ovh/utask/models/tasktemplate"
1313
"github.com/ovh/utask/pkg/auth"
1414
"github.com/ovh/utask/pkg/plugins/taskplugin"
15+
"github.com/ovh/utask/pkg/templateimport"
1516
"github.com/ovh/utask/pkg/utils"
1617
)
1718

@@ -54,11 +55,18 @@ func validConfig(config interface{}) error {
5455
}
5556

5657
_, err = tasktemplate.LoadFromName(dbp, cfg.Template)
57-
if err != nil {
58+
if err != nil && !errors.IsNotFound(err) {
5859
return err
5960
}
6061

61-
return nil
62+
templates := templateimport.GetTemplates()
63+
for _, template := range templates {
64+
if template == cfg.Template {
65+
return nil
66+
}
67+
}
68+
69+
return errors.NotFoundf("sub-task template %q", cfg.Template)
6270
}
6371

6472
func exec(stepName string, config interface{}, ctx interface{}) (interface{}, interface{}, error) {

pkg/templateimport/templateimport.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package templateimport
2+
3+
import (
4+
"sync"
5+
)
6+
7+
var (
8+
mu sync.Mutex
9+
discoveredTemplates []string = []string{}
10+
)
11+
12+
// AddTemplate registers a template name currently being imported
13+
func AddTemplate(templateName string) {
14+
mu.Lock()
15+
defer mu.Unlock()
16+
discoveredTemplates = append(discoveredTemplates, templateName)
17+
}
18+
19+
// GetTemplates returns template names currently being imported.
20+
// Used when template needs to validate dependency with others templates (check existance, ...)
21+
func GetTemplates() []string {
22+
mu.Lock()
23+
defer mu.Unlock()
24+
return discoveredTemplates
25+
}
26+
27+
// CleanTemplates flushes the template names list being imported.
28+
func CleanTemplates() {
29+
mu.Lock()
30+
defer mu.Unlock()
31+
discoveredTemplates = []string{}
32+
}

0 commit comments

Comments
 (0)