/
action_manager.go
87 lines (75 loc) · 2 KB
/
action_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package action
import (
"fmt"
"github.com/ekara-platform/engine/ansible"
"github.com/ekara-platform/engine/component"
"github.com/ekara-platform/engine/util"
)
//Result represents the result of an action
type Result interface {
//IsSuccess returns true id the action execution was successful
IsSuccess() bool
//AsJson returns the action returned content as JSON
AsJson() (string, error)
//AsYaml returns the action returned content as YAML
AsYaml() (string, error)
//AsPlainText returns the action returned content as plain text
AsPlainText() ([]string, error)
}
type (
//Manager is the manager of all action available into the engine
Manager interface {
// Run executes an engine action
Run(id ActionID) (Result, error)
}
actionManager struct {
// launchContext from the engine holding this action manager
lC util.LaunchContext
// the component manager
cM component.Manager
// the ansible manager
aM ansible.Manager
// available actions
actions map[ActionID]Action
}
)
//CreateActionManager initializes the action manager and its content
func CreateActionManager(lC util.LaunchContext, cM component.Manager, aM ansible.Manager) Manager {
am := actionManager{
lC: lC,
cM: cM,
aM: aM,
actions: make(map[ActionID]Action),
}
for _, a := range allActions() {
am.actions[a.id] = a
}
return &am
}
func (am *actionManager) empty() bool {
return len(am.actions) == 0
}
//get returns the action corresponding to the given id.
func (am *actionManager) get(id ActionID) (Action, error) {
if val, ok := am.actions[id]; ok {
return val, nil
}
return Action{}, fmt.Errorf("unsupported action")
}
//Run launches the action corresponding to the given id.
func (am *actionManager) Run(id ActionID) (Result, error) {
a, e := am.get(id)
if e != nil {
return nil, e
}
report, res, e := a.run(am)
if e != nil {
return nil, e
}
loc, e := writeReport(*report, am.lC.Ef().Output)
if e != nil {
return nil, e
}
am.lC.Log().Printf(LogReportWritten, loc)
return res, nil
}