-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbaas_pool.go
More file actions
52 lines (44 loc) · 1.8 KB
/
Copy pathdbaas_pool.go
File metadata and controls
52 lines (44 loc) · 1.8 KB
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
package dbaasbase
import (
"context"
"github.com/netcracker/qubership-core-lib-go-dbaas-base-client/v3/cache"
"github.com/netcracker/qubership-core-lib-go-dbaas-base-client/v3/model"
"github.com/netcracker/qubership-core-lib-go-dbaas-base-client/v3/model/rest"
)
type DbaaSPool struct {
poolCache *cache.DbaaSCache
Client DbaaSClient
}
type roleDiscriminator string
func (r roleDiscriminator) GetValue() string {
return string(r)
}
func NewDbaaSPool(options ...model.PoolOptions) *DbaaSPool {
poolCache := &cache.DbaaSCache{LogicalDbCache: make(map[cache.Key]interface{})}
var providers []model.LogicalDbProvider
if len(options) > 0 {
providers = options[0].LogicalDbProviders
}
providers = append(providers, newMountedSecretProvider())
client := NewDbaasClient(model.ClientOptions{LogicalDbProviders: providers})
return &DbaaSPool{poolCache: poolCache, Client: client}
}
func (p *DbaaSPool) GetOrCreateDb(ctx context.Context, dbType string, classifier map[string]interface{}, params rest.BaseDbParams) (*model.LogicalDb, error) {
key := cache.NewKeyWithDiscriminator(dbType, classifier, roleDiscriminator(params.Role))
db, err := p.poolCache.Cache(key, func() (interface{}, error) {
return p.Client.GetOrCreateDb(ctx, dbType, classifier, params)
})
if err != nil {
logger.Errorf("Can neither create db with classifier %+v nor get it from poolCache", classifier)
return nil, err
}
return db.(*model.LogicalDb), nil
}
func (p *DbaaSPool) GetConnection(ctx context.Context, dbType string, classifier map[string]interface{}, params rest.BaseDbParams) (map[string]interface{}, error) {
connection, err := p.Client.GetConnection(ctx, dbType, classifier, params)
if err != nil {
logger.Errorf("Can not get connection to db with classifier %+v", classifier)
return nil, err
}
return connection, nil
}