-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_store.go
executable file
·196 lines (182 loc) · 7.11 KB
/
module_store.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package module_store
import (
"context"
"errors"
"fmt"
"github.com/eru-tech/eru/eru-alerts/channel"
"github.com/eru-tech/eru/eru-alerts/module_model"
logs "github.com/eru-tech/eru/eru-logs/eru-logs"
"github.com/eru-tech/eru/eru-store/store"
)
type StoreHolder struct {
Store ModuleStoreI
}
type ModuleStoreI interface {
store.StoreI
SaveProject(ctx context.Context, projectId string, realStore ModuleStoreI, persist bool) error
RemoveProject(ctx context.Context, projectId string, realStore ModuleStoreI) error
GetProjectConfig(ctx context.Context, projectId string) (*module_model.Project, error)
GetProjectList(ctx context.Context) []map[string]interface{}
SaveMessageTemplate(ctx context.Context, projectId string, messageTemplate channel.MessageTemplate, realStore ModuleStoreI) error
RemoveMessageTemplate(ctx context.Context, projectId string, templateName string, realStore ModuleStoreI) error
GetMessageTemplate(ctx context.Context, projectId string, templateId string, realStore ModuleStoreI) (channel.MessageTemplate, error)
SaveChannel(ctx context.Context, channelObj channel.ChannelI, projectId string, realStore ModuleStoreI, persist bool) error
RemoveChannel(ctx context.Context, channelName string, projectId string, realStore ModuleStoreI) error
GetChannel(ctx context.Context, channelName string, projectId string, realStore ModuleStoreI) (channel.ChannelI, error)
}
type ModuleStore struct {
Projects map[string]*module_model.Project `json:"projects"` //ProjectId is the key
}
type ModuleFileStore struct {
store.FileStore
ModuleStore
}
type ModuleDbStore struct {
store.DbStore
ModuleStore
}
func (ms *ModuleStore) SaveProject(ctx context.Context, projectId string, realStore ModuleStoreI, persist bool) error {
logs.WithContext(ctx).Debug("SaveProject - Start")
//TODO to handle edit project once new project attributes are finalized
if _, ok := ms.Projects[projectId]; !ok {
project := new(module_model.Project)
project.ProjectId = projectId
if ms.Projects == nil {
ms.Projects = make(map[string]*module_model.Project)
}
ms.Projects[projectId] = project
if persist == true {
logs.WithContext(ctx).Info("SaveStore called from SaveProject")
return realStore.SaveStore(ctx, "", realStore)
} else {
return nil
}
} else {
err := errors.New(fmt.Sprint("Project ", projectId, " already exists"))
logs.WithContext(ctx).Error(err.Error())
return err
}
}
func (ms *ModuleStore) RemoveProject(ctx context.Context, projectId string, realStore ModuleStoreI) error {
logs.WithContext(ctx).Debug("RemoveProject - Start")
if _, ok := ms.Projects[projectId]; ok {
delete(ms.Projects, projectId)
logs.WithContext(ctx).Info("SaveStore called from RemoveProject")
return realStore.SaveStore(ctx, "", realStore)
} else {
err := errors.New(fmt.Sprint("Project ", projectId, " does not exists"))
logs.WithContext(ctx).Error(err.Error())
return err
}
}
func (ms *ModuleStore) GetProjectConfig(ctx context.Context, projectId string) (*module_model.Project, error) {
logs.WithContext(ctx).Debug("GetProjectConfig - Start")
if _, ok := ms.Projects[projectId]; ok {
return ms.Projects[projectId], nil
} else {
err := errors.New(fmt.Sprint("Project ", projectId, " does not exists"))
logs.WithContext(ctx).Error(err.Error())
return nil, err
}
}
func (ms *ModuleStore) GetProjectList(ctx context.Context) []map[string]interface{} {
logs.WithContext(ctx).Debug("GetProjectList - Start")
projects := make([]map[string]interface{}, len(ms.Projects))
i := 0
for k := range ms.Projects {
project := make(map[string]interface{})
project["projectName"] = k
projects[i] = project
i++
}
return projects
}
func (ms *ModuleStore) SaveMessageTemplate(ctx context.Context, projectId string, messageTemplate channel.MessageTemplate, realStore ModuleStoreI) error {
logs.WithContext(ctx).Debug("SaveMessageTemplate - Start")
err := ms.checkProjectExists(ctx, projectId)
if err != nil {
return err
}
if ms.Projects[projectId].MessageTemplates == nil {
ms.Projects[projectId].MessageTemplates = make(map[string]channel.MessageTemplate)
}
ms.Projects[projectId].MessageTemplates[messageTemplate.TemplateName] = messageTemplate
return realStore.SaveStore(ctx, "", realStore)
}
func (ms *ModuleStore) RemoveMessageTemplate(ctx context.Context, projectId string, templateName string, realStore ModuleStoreI) error {
logs.WithContext(ctx).Debug("RemoveMessageTemplate - Start")
err := ms.checkProjectExists(ctx, projectId)
if err != nil {
return err
}
if _, ok := ms.Projects[projectId].MessageTemplates[templateName]; ok {
delete(ms.Projects[projectId].MessageTemplates, templateName)
return realStore.SaveStore(ctx, "", realStore)
} else {
err := errors.New(fmt.Sprint("MessageTemplates ", templateName, " does not exists"))
logs.WithContext(ctx).Error(err.Error())
return err
}
}
func (ms *ModuleStore) GetMessageTemplate(ctx context.Context, projectId string, templateName string, realStore ModuleStoreI) (channel.MessageTemplate, error) {
logs.WithContext(ctx).Debug("GetMessageTemplate - Start")
err := ms.checkProjectExists(ctx, projectId)
if err != nil {
return channel.MessageTemplate{}, err
}
if mt, ok := ms.Projects[projectId].MessageTemplates[templateName]; ok {
return mt, nil
} else {
err = errors.New(fmt.Sprint("MessageTemplates ", templateName, " does not exists"))
logs.WithContext(ctx).Error(err.Error())
return channel.MessageTemplate{}, err
}
}
func (ms *ModuleStore) SaveChannel(ctx context.Context, channelObj channel.ChannelI, projectId string, realStore ModuleStoreI, persist bool) error {
logs.WithContext(ctx).Debug("SaveChannel - Start")
prj, err := ms.GetProjectConfig(ctx, projectId)
if err != nil {
return err
}
err = prj.AddChannel(ctx, channelObj)
if persist == true {
return realStore.SaveStore(ctx, "", realStore)
}
return nil
}
func (ms *ModuleStore) RemoveChannel(ctx context.Context, channelName string, projectId string, realStore ModuleStoreI) error {
logs.WithContext(ctx).Debug("RemoveChannel - Start")
if prg, ok := ms.Projects[projectId]; ok {
cKey := fmt.Sprint(channelName)
if _, ok := prg.Channels[cKey]; ok {
delete(prg.Channels, cKey)
logs.WithContext(ctx).Info(("SaveStore called from RemoveChannel"))
return realStore.SaveStore(ctx, "", realStore)
} else {
err := errors.New(fmt.Sprint("Channel ", cKey, " does not exists"))
logs.WithContext(ctx).Error(err.Error())
return err
}
} else {
err := errors.New(fmt.Sprint("Project ", projectId, " does not exists"))
logs.WithContext(ctx).Error(err.Error())
return err
}
}
func (ms *ModuleStore) GetChannel(ctx context.Context, channelName string, projectId string, realStore ModuleStoreI) (channel.ChannelI, error) {
logs.WithContext(ctx).Debug("GetChannel - Start")
if prg, ok := ms.Projects[projectId]; ok {
cKey := fmt.Sprint(channelName)
if ch, ok := prg.Channels[cKey]; ok {
return ch, nil
} else {
err := errors.New(fmt.Sprint("Channel ", cKey, " does not exists"))
logs.WithContext(ctx).Error(err.Error())
return nil, err
}
} else {
err := errors.New(fmt.Sprint("Project ", projectId, " does not exists"))
logs.WithContext(ctx).Error(err.Error())
return nil, err
}
}