-
Notifications
You must be signed in to change notification settings - Fork 2
/
manager.go
77 lines (61 loc) · 2.13 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
72
73
74
75
76
77
package gocql
import (
"context"
"github.com/americanas-go/errors"
"github.com/americanas-go/ignite/gocql/gocql.v1"
vault "github.com/americanas-go/ignite/mittwald/vaultgo.v0"
"github.com/americanas-go/log"
)
// Manager represents a vault manager for cassandra client.
type Manager struct {
managedSession *gocql.ManagedSession
options *vault.ManagerOptions
}
// NewManager returns a new vault manager with default options.
func NewManager(managedSession *gocql.ManagedSession) vault.Manager {
o, err := vault.NewManagerOptionsWithPath(root)
if err != nil {
log.Fatalf(err.Error())
}
return NewManagerWithOptions(managedSession, o)
}
// NewManagerWithConfigPath returns a new vault manager with options from config path.
func NewManagerWithConfigPath(managedSession *gocql.ManagedSession, path string) (vault.Manager, error) {
o, err := vault.NewManagerOptionsWithPath(path)
if err != nil {
return nil, err
}
return NewManagerWithOptions(managedSession, o), nil
}
// NewManagerWithOptions returns a new vault manager with options.
func NewManagerWithOptions(managedSession *gocql.ManagedSession, options *vault.ManagerOptions) vault.Manager {
return &Manager{options: options, managedSession: managedSession}
}
// Options returns vault manager options.
func (m *Manager) Options() *vault.ManagerOptions {
return m.options
}
// Close closes cassandra client.
func (m *Manager) Close(ctx context.Context) error {
m.managedSession.Session.Close()
return nil
}
// Configure configures cassandra client.
func (m *Manager) Configure(ctx context.Context, data map[string]interface{}) error {
var username, password string
var ok bool
if username, ok = data["username"].(string); !ok {
return errors.Internalf("username not found in data map")
}
if password, ok = data["password"].(string); !ok {
return errors.Internalf("password not found in data map")
}
m.managedSession.Options.Username = username
m.managedSession.Options.Password = password
session, err := gocql.NewSessionWithOptions(ctx, m.managedSession.Options, m.managedSession.Plugins...)
if err != nil {
return err
}
m.managedSession.Session = session
return nil
}