This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
providers.go
83 lines (77 loc) · 2.52 KB
/
providers.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
package storage
import (
"context"
"crypto/tls"
stategate "github.com/autom8ter/stategate/gen/grpc/go"
"github.com/autom8ter/stategate/internal/errorz"
"github.com/autom8ter/stategate/internal/logger"
"github.com/autom8ter/stategate/internal/storage/mongo"
"github.com/pkg/errors"
mongo2 "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap"
"time"
)
type Provider interface {
SetEntity(ctx context.Context, object *stategate.Entity) *errorz.Error
SaveEvent(ctx context.Context, event *stategate.Event) *errorz.Error
GetEntity(ctx context.Context, ref *stategate.EntityRef) (*stategate.Entity, *errorz.Error)
DelEntity(ctx context.Context, ref *stategate.EntityRef) *errorz.Error
SearchEntities(ctx context.Context, ref *stategate.SearchEntitiesOpts) (*stategate.Entities, *errorz.Error)
SearchEvents(ctx context.Context, ref *stategate.SearchEventOpts) (*stategate.Events, *errorz.Error)
Close() error
}
type ProviderName string
const (
MONGO_STORAGE ProviderName = "mongo"
)
var AllProviderNames = []ProviderName{MONGO_STORAGE}
func GetStorageProvider(lgger *logger.Logger, providerConfig map[string]string) (Provider, error) {
if providerConfig == nil {
return nil, errors.Errorf("empty backend storage provider config")
}
name := providerConfig["name"]
if name == "" {
return nil, errors.New("storage provider: empty name")
}
var tlsConfig *tls.Config
if providerConfig["client_cert_file"] != "" && providerConfig["client_key_file"] != "" {
cer, err := tls.LoadX509KeyPair(providerConfig["tls_cert"], providerConfig["tls_key"])
if err != nil {
lgger.Error("failed to load tls config", zap.Error(err))
return nil, err
}
tlsConfig = &tls.Config{
Certificates: []tls.Certificate{cer},
}
}
switch ProviderName(name) {
case MONGO_STORAGE:
db := providerConfig["database"]
if db == "" {
return nil, errors.New("mongo config: empty database")
}
uri := providerConfig["addr"]
if uri == "" {
return nil, errors.New("mongo config: empty addr")
}
opts := options.Client()
opts.ApplyURI(uri)
if tlsConfig != nil {
opts.TLSConfig = tlsConfig
}
client, err := mongo2.NewClient(opts)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
return nil, err
}
return mongo.NewProvider(client.Database(db)), nil
default:
return nil, errors.Errorf("unsupported backend provider: %s. must be one of: %v", name, AllProviderNames)
}
}