forked from motiv-labs/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb_repository.go
132 lines (106 loc) · 3.18 KB
/
mongodb_repository.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
package api
import (
log "github.com/sirupsen/logrus"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
const (
collectionName string = "api_specs"
)
// MongoRepository represents a mongodb repository
type MongoRepository struct {
session *mgo.Session
}
// NewMongoAppRepository creates a mongo API definition repo
func NewMongoAppRepository(session *mgo.Session) (*MongoRepository, error) {
return &MongoRepository{session}, nil
}
// FindAll fetches all the API definitions available
func (r *MongoRepository) FindAll() ([]*Definition, error) {
result := []*Definition{}
session, coll := r.getSession()
defer session.Close()
err := coll.Find(nil).All(&result)
if err != nil {
return nil, err
}
return result, nil
}
// FindByName find an API definition by name
func (r *MongoRepository) FindByName(name string) (*Definition, error) {
return r.findOneByQuery(bson.M{"name": name})
}
// FindByListenPath find an API definition by proxy listen path
func (r *MongoRepository) FindByListenPath(path string) (*Definition, error) {
return r.findOneByQuery(bson.M{"proxy.listen_path": path})
}
func (r *MongoRepository) findOneByQuery(query interface{}) (*Definition, error) {
var result = NewDefinition()
session, coll := r.getSession()
defer session.Close()
err := coll.Find(query).One(&result)
if err != nil {
if err == mgo.ErrNotFound {
return nil, ErrAPIDefinitionNotFound
}
return nil, err
}
return result, err
}
// Exists searches an existing API definition by its listen_path
func (r *MongoRepository) Exists(def *Definition) (bool, error) {
return exists(r, def)
}
// Add adds an API definition to the repository
func (r *MongoRepository) Add(definition *Definition) error {
session, coll := r.getSession()
defer session.Close()
isValid, err := definition.Validate()
if false == isValid && err != nil {
log.WithError(err).Error("Validation errors")
return err
}
_, err = coll.Upsert(bson.M{"name": definition.Name}, definition)
if err != nil {
log.WithField("name", definition.Name).Error("There was an error adding the resource")
return err
}
log.WithField("name", definition.Name).Debug("Resource added")
return nil
}
// Remove removes an API definition from the repository
func (r *MongoRepository) Remove(name string) error {
session, coll := r.getSession()
defer session.Close()
err := coll.Remove(bson.M{"name": name})
if err != nil {
if err == mgo.ErrNotFound {
return ErrAPIDefinitionNotFound
}
log.WithField("name", name).Error("There was an error removing the resource")
return err
}
log.WithField("name", name).Debug("Resource removed")
return nil
}
// FindValidAPIHealthChecks retrieves all active apis that has health check configured
func (r *MongoRepository) FindValidAPIHealthChecks() ([]*Definition, error) {
session, coll := r.getSession()
defer session.Close()
query := bson.M{
"active": true,
"health_check.url": bson.M{
"$exists": true,
},
}
result := []*Definition{}
if err := coll.Find(query).All(&result); err != nil {
return nil, err
}
return result, nil
}
func (r *MongoRepository) getSession() (*mgo.Session, *mgo.Collection) {
session := r.session.Copy()
coll := session.DB("").C(collectionName)
return session, coll
}