-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathmongo.go
279 lines (232 loc) · 6.6 KB
/
mongo.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2019 The OpenSDS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mongo
import (
"context"
"errors"
"math"
"sync"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/micro/go-micro/v2/metadata"
log "github.com/sirupsen/logrus"
"github.com/opensds/multi-cloud/api/pkg/common"
"github.com/opensds/multi-cloud/backend/pkg/model"
)
type mongoRepository struct {
session *mgo.Session
}
var defaultDBName = "multi-cloud"
var defaultCollection = "backends"
var defaultSspCollection = "ssps"
var mutex sync.Mutex
var mongoRepo = &mongoRepository{}
func Init(host string) *mongoRepository {
mutex.Lock()
defer mutex.Unlock()
if mongoRepo.session != nil {
return mongoRepo
}
session, err := mgo.Dial(host)
if err != nil {
panic(err)
}
session.SetMode(mgo.Monotonic, true)
mongoRepo.session = session
return mongoRepo
}
// The implementation of Repository
func UpdateFilter(m bson.M, filter map[string]string) error {
for k, v := range filter {
m[k] = interface{}(v)
}
return nil
}
func UpdateContextFilter(ctx context.Context, m bson.M) error {
// if context is admin, no need filter by tenantId.
md, ok := metadata.FromContext(ctx)
if !ok {
log.Error("get context failed")
return errors.New("get context failed")
}
isAdmin, _ := md[common.CTX_KEY_IS_ADMIN]
if isAdmin != common.CTX_VAL_TRUE {
tenantId, _ := md[common.CTX_REPRE_TENANT]
if tenantId == "" {
tenantId, ok = md[common.CTX_KEY_TENANT_ID]
if !ok {
log.Error("get tenantid failed")
return errors.New("get tenantid failed")
}
}
m["tenantId"] = tenantId
}
return nil
}
func (repo *mongoRepository) CreateBackend(ctx context.Context, backend *model.Backend) (*model.Backend, error) {
session := repo.session.Copy()
defer session.Close()
if backend.Id == "" {
backend.Id = bson.NewObjectId()
}
err := session.DB(defaultDBName).C(defaultCollection).Insert(backend)
if err != nil {
return nil, err
}
return backend, nil
}
func (repo *mongoRepository) DeleteBackend(ctx context.Context, id string) error {
session := repo.session.Copy()
defer session.Close()
m := bson.M{"_id": bson.ObjectIdHex(id)}
err := UpdateContextFilter(ctx, m)
if err != nil {
return err
}
return session.DB(defaultDBName).C(defaultCollection).Remove(m)
}
func (repo *mongoRepository) UpdateBackend(ctx context.Context,
backend *model.Backend) (*model.Backend, error) {
session := repo.session.Copy()
defer session.Close()
m := bson.M{"_id": backend.Id}
err := UpdateContextFilter(ctx, m)
if err != nil {
return nil, err
}
err = session.DB(defaultDBName).C(defaultCollection).Update(m, backend)
if err != nil {
return nil, err
}
return backend, nil
}
func (repo *mongoRepository) GetBackend(ctx context.Context, id string) (*model.Backend,
error) {
session := repo.session.Copy()
defer session.Close()
m := bson.M{"_id": bson.ObjectIdHex(id)}
err := UpdateContextFilter(ctx, m)
if err != nil {
return nil, err
}
var backend = &model.Backend{}
collection := session.DB(defaultDBName).C(defaultCollection)
err = collection.Find(m).One(backend)
if err != nil {
return nil, err
}
return backend, nil
}
func (repo *mongoRepository) ListBackend(ctx context.Context, limit, offset int,
query interface{}) ([]*model.Backend, error) {
session := repo.session.Copy()
defer session.Close()
if limit == 0 {
limit = math.MinInt32
}
var backends []*model.Backend
m := bson.M{}
UpdateFilter(m, query.(map[string]string))
err := UpdateContextFilter(ctx, m)
if err != nil {
return nil, err
}
log.Infof("ListBackend, limit=%d, offset=%d, m=%+v\n", limit, offset, m)
err = session.DB(defaultDBName).C(defaultCollection).Find(m).Skip(offset).Limit(limit).All(&backends)
if err != nil {
return nil, err
}
return backends, nil
}
func (repo *mongoRepository) CreateSsp(ctx context.Context, ssp *model.Ssp) (*model.Ssp, error) {
log.Debug("received request to create ssp in db")
session := repo.session.Copy()
defer session.Close()
if ssp.Id == "" {
ssp.Id = bson.NewObjectId()
}
err := session.DB(defaultDBName).C(defaultSspCollection).Insert(ssp)
if err != nil {
return nil, err
}
return ssp, nil
}
func (repo *mongoRepository) DeleteSsp(ctx context.Context, id string) error {
log.Debug("received request to delete ssp from db")
session := repo.session.Copy()
defer session.Close()
m := bson.M{"_id": bson.ObjectIdHex(id)}
err := UpdateContextFilter(ctx, m)
if err != nil {
return err
}
return session.DB(defaultDBName).C(defaultSspCollection).Remove(m)
}
func (repo *mongoRepository) UpdateSsp(ctx context.Context, ssp *model.Ssp) (*model.Ssp, error) {
log.Debug("received request to update ssp")
session := repo.session.Copy()
defer session.Close()
m := bson.M{"_id": ssp.Id}
err := UpdateContextFilter(ctx, m)
if err != nil {
return nil, err
}
err = session.DB(defaultDBName).C(defaultSspCollection).Update(m, ssp)
if err != nil {
return nil, err
}
return ssp, nil
}
func (repo *mongoRepository) ListSsps(ctx context.Context, limit, offset int, query interface{}) ([]*model.Ssp, error) {
log.Debug("received request to list ssps")
session := repo.session.Copy()
defer session.Close()
if limit == 0 {
limit = math.MinInt32
}
var ssps []*model.Ssp
m := bson.M{}
UpdateFilter(m, query.(map[string]string))
err := UpdateContextFilter(ctx, m)
if err != nil {
return nil, err
}
log.Infof("ListSsps, limit=%d, offset=%d, m=%+v\n", limit, offset, m)
err = session.DB(defaultDBName).C(defaultSspCollection).Find(m).Skip(offset).Limit(limit).All(&ssps)
if err != nil {
return nil, err
}
return ssps, nil
}
func (repo *mongoRepository) GetSsp(ctx context.Context, id string) (*model.Ssp,
error) {
log.Debug("received request to get ssp details")
session := repo.session.Copy()
defer session.Close()
m := bson.M{"_id": bson.ObjectIdHex(id)}
err := UpdateContextFilter(ctx, m)
if err != nil {
return nil, err
}
var ssp = &model.Ssp{}
collection := session.DB(defaultDBName).C(defaultSspCollection)
err = collection.Find(m).One(ssp)
if err != nil {
return nil, err
}
return ssp, nil
}
func (repo *mongoRepository) Close() {
repo.session.Close()
}