@@ -30,11 +30,11 @@ type User struct {
30
30
Roles Roles `json:"roles" bson:"roles"`
31
31
}
32
32
33
- type MongoDBServer struct {
33
+ type MongoDBRepository struct {
34
34
client * mongo.Client
35
35
}
36
36
37
- func NewMongoDBServer (ctx context.Context , uri , username , password string ) (Handler , error ) {
37
+ func NewMongoDBRepository (ctx context.Context , uri , database , username , password string ) (Handler , error ) {
38
38
o := options .Client ().ApplyURI (uri )
39
39
o .SetAuth (options.Credential {
40
40
Username : username ,
@@ -50,24 +50,24 @@ func NewMongoDBServer(ctx context.Context, uri, username, password string) (Hand
50
50
return nil , err
51
51
}
52
52
53
- return & MongoDBServer {
53
+ return & MongoDBRepository {
54
54
client : client ,
55
55
}, nil
56
56
}
57
57
58
- func (m * MongoDBServer ) Close () error {
58
+ func (m * MongoDBRepository ) Close () error {
59
59
ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
60
60
defer cancel ()
61
61
return m .client .Disconnect (ctx )
62
62
}
63
63
64
64
// CreateDatabaseIfNotExists is a dummy to apply to fullfill the contract,
65
65
// we don't need to create the database on MongoDB
66
- func (m * MongoDBServer ) CreateDatabaseIfNotExists (database string ) error {
66
+ func (m * MongoDBRepository ) CreateDatabaseIfNotExists (database string ) error {
67
67
return nil
68
68
}
69
69
70
- func (m * MongoDBServer ) SetupUser (database string , username string , password string ) error {
70
+ func (m * MongoDBRepository ) SetupUser (database string , username string , password string ) error {
71
71
doesUserExist , err := m .doesUserExist (database , username )
72
72
if err != nil {
73
73
return err
@@ -91,7 +91,21 @@ func (m *MongoDBServer) SetupUser(database string, username string, password str
91
91
return nil
92
92
}
93
93
94
- func (m * MongoDBServer ) doesUserExist (database string , username string ) (bool , error ) {
94
+ func (m * MongoDBRepository ) DropUser (database string , username string ) error {
95
+ command := & bson.D {primitive.E {Key : "dropUser" , Value : username }}
96
+ r := m .runCommand (database , command )
97
+ if _ , err := r .DecodeBytes (); err != nil {
98
+ return err
99
+ }
100
+ return nil
101
+ }
102
+
103
+ func (m * MongoDBRepository ) EnableExtension (name string ) error {
104
+ // NOOP
105
+ return nil
106
+ }
107
+
108
+ func (m * MongoDBRepository ) doesUserExist (database string , username string ) (bool , error ) {
95
109
users , err := m .getAllUsers (database , username )
96
110
if err != nil {
97
111
return false , err
@@ -100,7 +114,7 @@ func (m *MongoDBServer) doesUserExist(database string, username string) (bool, e
100
114
return users != nil && len (users ) > 0 , nil
101
115
}
102
116
103
- func (m * MongoDBServer ) getAllUsers (database string , username string ) (Users , error ) {
117
+ func (m * MongoDBRepository ) getAllUsers (database string , username string ) (Users , error ) {
104
118
users := make (Users , 0 )
105
119
ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
106
120
defer cancel ()
@@ -124,7 +138,7 @@ func (m *MongoDBServer) getAllUsers(database string, username string) (Users, er
124
138
return users , nil
125
139
}
126
140
127
- func (m * MongoDBServer ) createUser (database string , username string , password string ) error {
141
+ func (m * MongoDBRepository ) createUser (database string , username string , password string ) error {
128
142
command := & bson.D {primitive.E {Key : "createUser" , Value : username }, primitive.E {Key : "pwd" , Value : password },
129
143
primitive.E {Key : "roles" , Value : []bson.M {{"role" : "readWrite" , "db" : database }}}}
130
144
r := m .runCommand (database , command )
@@ -134,7 +148,7 @@ func (m *MongoDBServer) createUser(database string, username string, password st
134
148
return nil
135
149
}
136
150
137
- func (m * MongoDBServer ) updateUserPasswordAndRoles (database string , username string , password string ) error {
151
+ func (m * MongoDBRepository ) updateUserPasswordAndRoles (database string , username string , password string ) error {
138
152
command := & bson.D {primitive.E {Key : "updateUser" , Value : username }, primitive.E {Key : "pwd" , Value : password },
139
153
primitive.E {Key : "roles" , Value : []bson.M {{"role" : "readWrite" , "db" : database }}}}
140
154
r := m .runCommand (database , command )
@@ -144,16 +158,7 @@ func (m *MongoDBServer) updateUserPasswordAndRoles(database string, username str
144
158
return nil
145
159
}
146
160
147
- func (m * MongoDBServer ) DropUser (database string , username string ) error {
148
- command := & bson.D {primitive.E {Key : "dropUser" , Value : username }}
149
- r := m .runCommand (database , command )
150
- if _ , err := r .DecodeBytes (); err != nil {
151
- return err
152
- }
153
- return nil
154
- }
155
-
156
- func (m * MongoDBServer ) runCommand (database string , command * bson.D ) * mongo.SingleResult {
161
+ func (m * MongoDBRepository ) runCommand (database string , command * bson.D ) * mongo.SingleResult {
157
162
ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
158
163
defer cancel ()
159
164
return m .client .Database (database ).RunCommand (ctx , * command )
0 commit comments