-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcontroller.go
195 lines (157 loc) · 5.26 KB
/
controller.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Kodex (Community Edition - CE) - Privacy & Security Engineering Platform
// Copyright (C) 2019-2024 KIProtect GmbH (HRB 208395B) - Germany
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package kodex
import (
"fmt"
)
type ControllerMaker func(map[string]interface{}, Settings, *Definitions) (Controller, error)
type ControllerDefinitions map[string]ControllerMaker
var NotFound = fmt.Errorf("object not found")
var AlreadyInTransaction = fmt.Errorf("already in a transaction")
type Controller interface {
SetVar(key string, value interface{}) error
GetVar(key string) (interface{}, bool)
// Clone the controller
Clone() (Controller, error)
// Transaction Helpers
Begin() error
Commit() error
Rollback() error
// Retrieve Settings
Settings() Settings
// Initialize a plugin
InitializePlugin(Plugin) error
// Initialize all plugins as defined in the settings
InitializePlugins() error
// Streams
Streams(filters map[string]interface{}) ([]Stream, error)
Stream(streamID []byte) (Stream, error)
// Sources
Sources(filters map[string]interface{}) ([]Source, error)
Source(sourceID []byte) (Source, error)
// Destinations
Destinations(filters map[string]interface{}) ([]Destination, error)
Destination(destinationID []byte) (Destination, error)
// Configs
Config(configID []byte) (Config, error)
// Action Configs
ActionConfigs(filters map[string]interface{}) ([]ActionConfig, error)
ActionConfig(configID []byte) (ActionConfig, error)
Definitions() *Definitions
// Retrieve a list of streams by urgency
StreamsByUrgency(n int) ([]Stream, error)
// Retrieve a list of sources by urgency
SourcesByUrgency(n int) ([]SourceMap, error)
// Retrieve a list of destinations by urgency
DestinationsByUrgency(n int) ([]DestinationMap, error)
// Acquire a processable entity
Acquire(Processable, []byte) (bool, error)
// Release a processable entity
Release(Processable, []byte) (bool, error)
// Send a pingback with stats for a processable entity
Ping(Processable, ProcessingStats) error
// Datasets
Dataset(id []byte) (Dataset, error)
Datasets(filter map[string]any) ([]Dataset, error)
// Projects
MakeProject(id []byte) Project
Project(projectID []byte) (Project, error)
Projects(filters map[string]interface{}) ([]Project, error)
// Resets the database (warning, this is a destructive action...)
ResetDB() error
// Parameter store
ParameterStore() ParameterStore
// Run all hooks of the given name
RunHooks(name string, data interface{}) (interface{}, error)
}
/* Base Functionality */
type BaseController struct {
definitions *Definitions
parameterStore ParameterStore
vars map[string]interface{}
settings Settings
}
func MakeBaseController(settings Settings, definitions *Definitions) (BaseController, error) {
parameterStore, err := MakeParameterStore(settings, definitions)
if err != nil {
return BaseController{}, err
}
return BaseController{
parameterStore: parameterStore,
definitions: definitions,
settings: settings,
vars: map[string]interface{}{},
}, nil
}
func (b *BaseController) RunHooks(name string, data interface{}) (interface{}, error) {
var err error
hooks := b.definitions.HookDefinitions[name]
currentData := data
for _, hook := range hooks {
if currentData, err = hook.Hook(data); err != nil {
return nil, err
}
}
return currentData, nil
}
func (b *BaseController) ParameterStore() ParameterStore {
return b.parameterStore
}
func (b *BaseController) SetVar(key string, value interface{}) error {
b.vars[key] = value
return nil
}
func (b *BaseController) GetVar(key string) (interface{}, bool) {
value, ok := b.vars[key]
return value, ok
}
func (b *BaseController) Definitions() *Definitions {
return b.definitions
}
func (b *BaseController) InitializePlugin(plugin Plugin) error {
return plugin.Initialize(b.definitions)
}
func (b *BaseController) Settings() Settings {
return b.settings
}
func (b *BaseController) InitializePlugins() error {
pluginsSetting, err := b.settings.Get("plugins")
if err == nil {
pluginsList, ok := pluginsSetting.([]interface{})
if ok {
for _, pluginName := range pluginsList {
pluginNameStr, ok := pluginName.(string)
if !ok {
return fmt.Errorf("Plugins: expected a string")
}
if definition, ok := b.definitions.PluginDefinitions[pluginNameStr]; ok {
plugin, err := definition.Maker(nil)
if err != nil {
return err
}
if err := b.InitializePlugin(plugin); err != nil {
return err
} else {
Log.Infof("Successfully initialized plugin '%s'", pluginName)
}
} else {
return fmt.Errorf("plugin '%s' is not registered", pluginNameStr)
}
}
}
}
return nil
}