forked from raystack/entropy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.go
65 lines (54 loc) · 1.46 KB
/
registry.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
package modules
import (
"context"
"fmt"
"os"
"reflect"
"strings"
"sync"
"github.com/goto/entropy/core/module"
"github.com/goto/entropy/pkg/errors"
)
// Registry maintains a list of supported/enabled modules.
type Registry struct {
mu sync.RWMutex
modules map[string]module.Descriptor
}
func (mr *Registry) GetDriver(_ context.Context, mod module.Module) (module.Driver, module.Descriptor, error) {
mr.mu.RLock()
defer mr.mu.RUnlock()
desc, found := mr.modules[mod.Name]
if !found {
return nil, module.Descriptor{}, errors.ErrNotFound
}
driver, err := desc.DriverFactory(mod.Configs)
if err != nil {
return nil, module.Descriptor{}, errors.ErrInvalid.
WithMsgf("failed to initialise module").
WithCausef(err.Error())
}
return driver, desc, nil
}
// Register adds a module to the registry.
func (mr *Registry) Register(desc module.Descriptor) error {
mr.mu.Lock()
defer mr.mu.Unlock()
if mr.modules == nil {
mr.modules = map[string]module.Descriptor{}
}
if v, exists := mr.modules[desc.Kind]; exists {
return errors.ErrConflict.
WithMsgf("module '%s' is already registered for kind '%s'", reflect.TypeOf(v), desc.Kind)
}
for i, action := range desc.Actions {
if err := action.Sanitise(); err != nil {
return err
}
desc.Actions[i] = action
}
secretKey := fmt.Sprintf("%s_SECRET", strings.ToUpper(desc.Kind))
secrets := os.Getenv(secretKey)
desc.Secrets = strings.Split(secrets, ",")
mr.modules[desc.Kind] = desc
return nil
}