@@ -3,7 +3,6 @@ package db
3
3
import (
4
4
"context"
5
5
"errors"
6
- "time"
7
6
8
7
"go.mongodb.org/mongo-driver/bson"
9
8
"go.mongodb.org/mongo-driver/bson/primitive"
@@ -57,69 +56,65 @@ func NewMongoDBRepository(ctx context.Context, uri, database, username, password
57
56
}, nil
58
57
}
59
58
60
- func (m * MongoDBRepository ) Close () error {
61
- ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
62
- defer cancel ()
59
+ func (m * MongoDBRepository ) Close (ctx context.Context ) error {
63
60
return m .client .Disconnect (ctx )
64
61
}
65
62
66
63
// CreateDatabaseIfNotExists is a dummy to apply to fulfill the contract,
67
64
// we don't need to create the database on MongoDB
68
- func (m * MongoDBRepository ) CreateDatabaseIfNotExists (database string ) error {
65
+ func (m * MongoDBRepository ) CreateDatabaseIfNotExists (ctx context. Context , database string ) error {
69
66
return nil
70
67
}
71
68
72
- func (m * MongoDBRepository ) SetupUser (database string , username string , password string , roles []infrav1beta1.Role ) error {
73
- doesUserExist , err := m .doesUserExist (database , username )
69
+ func (m * MongoDBRepository ) SetupUser (ctx context. Context , database string , username string , password string , roles []infrav1beta1.Role ) error {
70
+ doesUserExist , err := m .doesUserExist (ctx , database , username )
74
71
if err != nil {
75
72
return err
76
73
}
77
74
78
75
if ! doesUserExist {
79
- if err := m .createUser (database , username , password , roles ); err != nil {
76
+ if err := m .createUser (ctx , database , username , password , roles ); err != nil {
80
77
return err
81
78
}
82
- if doesUserExistNow , err := m .doesUserExist (database , username ); err != nil {
79
+ if doesUserExistNow , err := m .doesUserExist (ctx , database , username ); err != nil {
83
80
return err
84
81
} else if ! doesUserExistNow {
85
82
return errors .New ("user doesn't exist after create" )
86
83
}
87
84
} else {
88
- if err := m .updateUserPasswordAndRoles (database , username , password , roles ); err != nil {
85
+ if err := m .updateUserPasswordAndRoles (ctx , database , username , password , roles ); err != nil {
89
86
return err
90
87
}
91
88
}
92
89
93
90
return nil
94
91
}
95
92
96
- func (m * MongoDBRepository ) DropUser (database string , username string ) error {
93
+ func (m * MongoDBRepository ) DropUser (ctx context. Context , database string , username string ) error {
97
94
command := & bson.D {primitive.E {Key : "dropUser" , Value : username }}
98
- r := m .runCommand (database , command )
95
+ r := m .runCommand (ctx , database , command )
99
96
if _ , err := r .DecodeBytes (); err != nil {
100
97
return err
101
98
}
102
99
return nil
103
100
}
104
101
105
- func (m * MongoDBRepository ) EnableExtension (name string ) error {
102
+ func (m * MongoDBRepository ) EnableExtension (ctx context. Context , name string ) error {
106
103
// NOOP
107
104
return nil
108
105
}
109
106
110
- func (m * MongoDBRepository ) doesUserExist (database string , username string ) (bool , error ) {
111
- users , err := m .getAllUsers (database , username )
107
+ func (m * MongoDBRepository ) doesUserExist (ctx context. Context , database string , username string ) (bool , error ) {
108
+ users , err := m .getAllUsers (ctx , database , username )
112
109
if err != nil {
113
110
return false , err
114
111
}
115
112
116
113
return users != nil && len (users ) > 0 , nil
117
114
}
118
115
119
- func (m * MongoDBRepository ) getAllUsers (database string , username string ) (Users , error ) {
116
+ func (m * MongoDBRepository ) getAllUsers (ctx context. Context , database string , username string ) (Users , error ) {
120
117
users := make (Users , 0 )
121
- ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
122
- defer cancel ()
123
118
124
119
collection := m .client .Database (adminDatabase ).Collection (usersCollection )
125
120
cursor , err := collection .Find (ctx , bson.D {primitive.E {Key : "user" , Value : username }, primitive.E {Key : "db" , Value : database }})
@@ -163,28 +158,26 @@ func (m *MongoDBRepository) getRoles(database string, roles []infrav1beta1.Role)
163
158
return rs
164
159
}
165
160
166
- func (m * MongoDBRepository ) createUser (database string , username string , password string , roles []infrav1beta1.Role ) error {
161
+ func (m * MongoDBRepository ) createUser (ctx context. Context , database string , username string , password string , roles []infrav1beta1.Role ) error {
167
162
command := & bson.D {primitive.E {Key : "createUser" , Value : username }, primitive.E {Key : "pwd" , Value : password },
168
163
primitive.E {Key : "roles" , Value : m .getRoles (database , roles )}}
169
- r := m .runCommand (database , command )
164
+ r := m .runCommand (ctx , database , command )
170
165
if _ , err := r .DecodeBytes (); err != nil {
171
166
return err
172
167
}
173
168
return nil
174
169
}
175
170
176
- func (m * MongoDBRepository ) updateUserPasswordAndRoles (database string , username string , password string , roles []infrav1beta1.Role ) error {
171
+ func (m * MongoDBRepository ) updateUserPasswordAndRoles (ctx context. Context , database string , username string , password string , roles []infrav1beta1.Role ) error {
177
172
command := & bson.D {primitive.E {Key : "updateUser" , Value : username }, primitive.E {Key : "pwd" , Value : password },
178
173
primitive.E {Key : "roles" , Value : m .getRoles (database , roles )}}
179
- r := m .runCommand (database , command )
174
+ r := m .runCommand (ctx , database , command )
180
175
if _ , err := r .DecodeBytes (); err != nil {
181
176
return err
182
177
}
183
178
return nil
184
179
}
185
180
186
- func (m * MongoDBRepository ) runCommand (database string , command * bson.D ) * mongo.SingleResult {
187
- ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
188
- defer cancel ()
181
+ func (m * MongoDBRepository ) runCommand (ctx context.Context , database string , command * bson.D ) * mongo.SingleResult {
189
182
return m .client .Database (database ).RunCommand (ctx , * command )
190
183
}
0 commit comments