This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
/
handler.go
79 lines (70 loc) · 2.53 KB
/
handler.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
package plugins
import (
"errors"
"fmt"
logger "github.com/sirupsen/logrus"
)
// Interfaces stores module-name to ModuleInterface mapping for all loaded plugins
var Interfaces = make(map[string]ModuleInterface)
// Enforce simplifies enforcing data using specified plugin
//
// Function checks if plugin is loaded, verifies if stored interface is not null and then calls Enforce() method of stored interface.
// Returns error if any of steps above fails
func Enforce(moduleName string, params map[string]interface{}) (string, error) {
logger.Debugf("Enforce() requested for %v plugin", moduleName)
iface, ok := Interfaces[moduleName]
if !ok {
return "", fmt.Errorf("Plugin '%v' is not loaded", moduleName)
}
if iface == nil {
return "", errors.New("Internal error: nil pointer to plugin")
}
return iface.Enforce(params)
}
// Release simplifies releasing data using specified plugin
//
// Function checks if plugin is loaded, verifies if stored interface is not null and then calls Release() method of stored interface.
// Returns error if any of steps above fails
func Release(moduleName string, params map[string]interface{}) error {
logger.Debugf("Release() requested for %v plugin", moduleName)
iface, ok := Interfaces[moduleName]
if !ok {
return fmt.Errorf("Plugin '%v' is not loaded", moduleName)
}
if iface == nil {
return errors.New("Internal error: nil pointer to plugin")
}
return iface.Release(params)
}
// Validate simplifies data validation for selected plugin
//
// Function checks if plugin is loaded, verifies if stored interface is not null and then calls Validate() method of stored interface.
// Returns error if any of steps above fails
func Validate(moduleName string, params map[string]interface{}) error {
logger.Debugf("Validate() requested for %v plugin", moduleName)
iface, ok := Interfaces[moduleName]
if !ok {
return fmt.Errorf("Plugin '%v' is not loaded", moduleName)
}
if iface == nil {
return errors.New("Internal error: nil pointer to plugin")
}
return iface.Validate(params)
}
// Store stores provided module interface under provided name
//
// If module already exists or one of params is not correct (ex. nil interface)
// error is returned
func Store(name string, iface ModuleInterface) error {
if name == "" || iface == nil {
return errors.New("Invalid parameter")
}
_, ok := Interfaces[name]
if ok {
// module with this name already exists
return errors.New("Module already registered under this name")
}
logger.Debugf("Plugin %v saved in loaded plugins", name)
Interfaces[name] = iface
return nil
}