/
service-group-mongo.go
127 lines (100 loc) · 4.03 KB
/
service-group-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
package servicegroup
import (
"errors"
"github.com/Glintt/gAPI/api/database"
"github.com/Glintt/gAPI/api/servicediscovery/constants"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
const SERVICE_GROUP_COLLECTION = "services_groups"
type ServiceGroupMongoRepository struct {
Session *mgo.Session
Db *mgo.Database
Collection *mgo.Collection
}
// OpenTransaction open new database transaction
func (sgmr *ServiceGroupMongoRepository) OpenTransaction() error {
return nil
}
// CommitTransaction commit database transaction
func (sgmr *ServiceGroupMongoRepository) CommitTransaction() {}
// RollbackTransaction rollback database transaction
func (sgmr *ServiceGroupMongoRepository) RollbackTransaction() {}
// Release release database transaction to the pool
func (sgmr *ServiceGroupMongoRepository) Release() {
database.MongoDBPool.Close(sgmr.Session)
}
// GetServiceGroups get list of service groups
func (sgmr *ServiceGroupMongoRepository) GetServiceGroups() ([]ServiceGroup, error) {
var servicesGroup []ServiceGroup
err := sgmr.Collection.Find(bson.M{}).All(&servicesGroup)
return servicesGroup, err
}
// GetServiceGroupById get service group by id
func (sgmr *ServiceGroupMongoRepository) GetServiceGroupById(serviceGroup string) (ServiceGroup, error) {
var sg ServiceGroup
var err error
if bson.IsObjectIdHex(serviceGroup) {
err = sgmr.Collection.FindId(bson.ObjectIdHex(serviceGroup)).One(&sg)
} else {
err = sgmr.Collection.Find(bson.M{"name": serviceGroup}).One(&sg)
}
return sg, err
}
// CreateServiceGroup create new service group
func (sgmr *ServiceGroupMongoRepository) CreateServiceGroup(serviceGroup ServiceGroup) error {
serviceGroup.Id = bson.NewObjectId()
index := mgo.Index{
Key: []string{"name"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := sgmr.Collection.EnsureIndex(index)
if err != nil {
return err
}
return sgmr.Collection.Insert(&serviceGroup)
}
// UpdateServiceGroup update an existing service group
func (sgmr *ServiceGroupMongoRepository) UpdateServiceGroup(serviceGroupID string, serviceGroup ServiceGroup) error {
serviceGroupIdObj := bson.ObjectIdHex(serviceGroupID)
return sgmr.Collection.UpdateId(serviceGroupIdObj, serviceGroup)
}
// DeleteServiceGroup delete an existing service group
func (sgmr *ServiceGroupMongoRepository) DeleteServiceGroup(serviceGroupID string) error {
serviceGroupIdObj := bson.ObjectIdHex(serviceGroupID)
err := sgmr.Collection.RemoveId(serviceGroupIdObj)
_, err = sgmr.Db.C(constants.SERVICES_COLLECTION).UpdateAll(bson.M{}, bson.M{"$set": bson.M{"groupid": nil}})
return err
}
// AddServiceToGroup add service to an existing service group
func (sgmr *ServiceGroupMongoRepository) AddServiceToGroup(serviceGroupID string, serviceID string) error {
serviceGroupIdHex := bson.ObjectIdHex(serviceGroupID)
serviceIdHex := bson.ObjectIdHex(serviceID)
removeFromAllGroups := bson.M{"$pull": bson.M{"services": serviceIdHex}}
updateGroup := bson.M{"$addToSet": bson.M{"services": serviceIdHex}}
updateService := bson.M{"$set": bson.M{"groupid": serviceGroupIdHex}}
err := sgmr.Db.C(constants.SERVICES_COLLECTION).UpdateId(serviceIdHex, updateService)
if err != nil {
return errors.New("Update Service failed")
}
_, err = sgmr.Collection.UpdateAll(bson.M{}, removeFromAllGroups)
if err != nil {
return errors.New("Update Service failed")
}
return sgmr.Collection.UpdateId(serviceGroupIdHex, updateGroup)
}
// RemoveServiceFromGroup remove service from an existing service group
func (sgmr *ServiceGroupMongoRepository) RemoveServiceFromGroup(serviceGroupID string, serviceID string) error {
serviceGroupIdHex := bson.ObjectIdHex(serviceGroupID)
serviceIdHex := bson.ObjectIdHex(serviceID)
updateGroup := bson.M{"$pull": bson.M{"services": serviceIdHex}}
updateService := bson.M{"$set": bson.M{"groupid": nil, "usegroupattributes": false}}
err := sgmr.Db.C(constants.SERVICES_COLLECTION).UpdateId(serviceIdHex, updateService)
if err != nil {
return err
}
return sgmr.Collection.UpdateId(serviceGroupIdHex, updateGroup)
}