forked from kyma-project/kyma
/
storage.go
129 lines (106 loc) · 3.44 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package storage
import (
"github.com/kyma-project/kyma/components/remote-environment-broker/internal/storage/driver/memory"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
// Factory provides access to concrete storage.
// Multiple calls should to specific storage return the same storage instance.
type Factory interface {
RemoteEnvironment() RemoteEnvironment
Instance() Instance
InstanceOperation() InstanceOperation
}
// DriverType defines type of data storage
type DriverType string
const (
// DriverMemory is a driver to local in-memory store
DriverMemory DriverType = "memory"
)
// EntityName defines name of the entity in database
type EntityName string
const (
// EntityAll represents name of all entities
EntityAll EntityName = "all"
// EntityRemoteEnvironment represents name of remote environment entities
EntityRemoteEnvironment EntityName = "remoteenvironment"
// EntityInstance represents name of services instances entities
EntityInstance EntityName = "instance"
// EntityInstanceOperation represents name of instances operations entities
EntityInstanceOperation EntityName = "instanceOperation"
)
// ProviderConfig provides configuration to the database provider
type ProviderConfig struct{}
// ProviderConfigMap contains map of provided configurations for given entities
type ProviderConfigMap map[EntityName]ProviderConfig
// Config contains database configuration.
type Config struct {
Driver DriverType `json:"driver" valid:"required"`
Provide ProviderConfigMap `json:"provide" valid:"required"`
Memory memory.Config `json:"memory"`
}
// ConfigList is a list of configurations
type ConfigList []Config
// ConfigParse is parsing yaml file to the ConfigList
func ConfigParse(inByte []byte) (*ConfigList, error) {
var cl ConfigList
if err := yaml.Unmarshal(inByte, &cl); err != nil {
return nil, errors.Wrap(err, "while unmarshalling yaml")
}
return &cl, nil
}
// NewFactory is a factory for entities based on given ConfigList
func NewFactory(cl *ConfigList) (Factory, error) {
fact := concreteFactory{}
for _, cfg := range *cl {
var (
remoteEnvironmentFactory func() (RemoteEnvironment, error)
instanceFactory func() (Instance, error)
instanceOperationFactory func() (InstanceOperation, error)
)
switch cfg.Driver {
case DriverMemory:
remoteEnvironmentFactory = func() (RemoteEnvironment, error) {
return memory.NewRemoteEnvironment(), nil
}
instanceFactory = func() (Instance, error) {
return memory.NewInstance(), nil
}
instanceOperationFactory = func() (InstanceOperation, error) {
return memory.NewInstanceOperation(), nil
}
default:
return nil, errors.New("unknown driver type")
}
for em := range cfg.Provide {
switch em {
case EntityRemoteEnvironment:
fact.re, _ = remoteEnvironmentFactory()
case EntityInstance:
fact.instance, _ = instanceFactory()
case EntityInstanceOperation:
fact.op, _ = instanceOperationFactory()
case EntityAll:
fact.re, _ = remoteEnvironmentFactory()
fact.instance, _ = instanceFactory()
fact.op, _ = instanceOperationFactory()
default:
}
}
}
return &fact, nil
}
type concreteFactory struct {
re RemoteEnvironment
instance Instance
op InstanceOperation
}
func (f *concreteFactory) RemoteEnvironment() RemoteEnvironment {
return f.re
}
func (f *concreteFactory) Instance() Instance {
return f.instance
}
func (f *concreteFactory) InstanceOperation() InstanceOperation {
return f.op
}