forked from kyma-project/control-plane
/
storage.go
87 lines (71 loc) · 2.33 KB
/
storage.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
package storage
import (
"github.com/gocraft/dbr"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/storage/driver/memory"
postgres "github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/storage/driver/postsql"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/storage/postsql"
"github.com/sirupsen/logrus"
)
type BrokerStorage interface {
Instances() Instances
Operations() Operations
Provisioning() Provisioning
Deprovisioning() Deprovisioning
Orchestrations() Orchestrations
RuntimeStates() RuntimeStates
}
const (
connectionRetries = 10
)
func NewFromConfig(cfg Config, cipher postgres.Cipher, log logrus.FieldLogger) (BrokerStorage, *dbr.Connection, error) {
log.Infof("Setting DB connection pool params: connectionMaxLifetime=%s "+
"maxIdleConnections=%d maxOpenConnections=%d", cfg.ConnMaxLifetime, cfg.MaxIdleConns, cfg.MaxOpenConns)
connection, err := postsql.InitializeDatabase(cfg.ConnectionURL(), connectionRetries, log)
if err != nil {
return nil, nil, err
}
connection.SetConnMaxLifetime(cfg.ConnMaxLifetime)
connection.SetMaxIdleConns(cfg.MaxIdleConns)
connection.SetMaxOpenConns(cfg.MaxOpenConns)
fact := postsql.NewFactory(connection)
operation := postgres.NewOperation(fact, cipher)
return storage{
instance: postgres.NewInstance(fact, operation, cipher),
operation: operation,
orchestrations: postgres.NewOrchestrations(fact),
runtimeStates: postgres.NewRuntimeStates(fact, cipher),
}, connection, nil
}
func NewMemoryStorage() BrokerStorage {
op := memory.NewOperation()
return storage{
operation: op,
instance: memory.NewInstance(op),
orchestrations: memory.NewOrchestrations(),
runtimeStates: memory.NewRuntimeStates(),
}
}
type storage struct {
instance Instances
operation Operations
orchestrations Orchestrations
runtimeStates RuntimeStates
}
func (s storage) Instances() Instances {
return s.instance
}
func (s storage) Operations() Operations {
return s.operation
}
func (s storage) Provisioning() Provisioning {
return s.operation
}
func (s storage) Deprovisioning() Deprovisioning {
return s.operation
}
func (s storage) Orchestrations() Orchestrations {
return s.orchestrations
}
func (s storage) RuntimeStates() RuntimeStates {
return s.runtimeStates
}