-
Notifications
You must be signed in to change notification settings - Fork 2
/
manager.go
71 lines (56 loc) · 1.93 KB
/
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
package redis
import (
"context"
"github.com/americanas-go/errors"
"github.com/americanas-go/ignite/go-redis/redis.v7"
vault "github.com/americanas-go/ignite/mittwald/vaultgo.v0"
"github.com/americanas-go/log"
)
// Manager represents a vault manager for redis client.
type Manager struct {
managedClient *redis.ManagedClient
options *vault.ManagerOptions
}
// NewManager returns a new vault manager with default options.
func NewManager(managedClient *redis.ManagedClient) vault.Manager {
o, err := vault.NewManagerOptionsWithPath(root)
if err != nil {
log.Fatalf(err.Error())
}
return NewManagerWithOptions(managedClient, o)
}
// NewManagerWithConfigPath returns a new vault manager with options from config path.
func NewManagerWithConfigPath(managedClient *redis.ManagedClient, path string) (vault.Manager, error) {
o, err := vault.NewManagerOptionsWithPath(path)
if err != nil {
return nil, err
}
return NewManagerWithOptions(managedClient, o), nil
}
// NewManagerWithOptions returns a new vault manager with options.
func NewManagerWithOptions(managedClient *redis.ManagedClient, options *vault.ManagerOptions) vault.Manager {
return &Manager{options: options, managedClient: managedClient}
}
// Options returns vault manager options.
func (m *Manager) Options() *vault.ManagerOptions {
return m.options
}
// Close closes redis client.
func (m *Manager) Close(ctx context.Context) error {
return m.managedClient.Client.Close()
}
// Configure configures redis client.
func (m *Manager) Configure(ctx context.Context, data map[string]interface{}) error {
var password string
var ok bool
if password, ok = data["password"].(string); !ok {
return errors.Internalf("password not found in data map")
}
m.managedClient.Options.Password = password
client, err := redis.NewClientWithOptions(ctx, m.managedClient.Options, m.managedClient.Plugins...)
if err != nil {
return err
}
m.managedClient.Client = client
return nil
}