@@ -3,7 +3,6 @@ package db
33import (
44 "context"
55 "errors"
6- "time"
76
87 "go.mongodb.org/mongo-driver/bson"
98 "go.mongodb.org/mongo-driver/bson/primitive"
@@ -57,69 +56,65 @@ func NewMongoDBRepository(ctx context.Context, uri, database, username, password
5756 }, nil
5857}
5958
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 {
6360 return m .client .Disconnect (ctx )
6461}
6562
6663// CreateDatabaseIfNotExists is a dummy to apply to fulfill the contract,
6764// 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 {
6966 return nil
7067}
7168
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 )
7471 if err != nil {
7572 return err
7673 }
7774
7875 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 {
8077 return err
8178 }
82- if doesUserExistNow , err := m .doesUserExist (database , username ); err != nil {
79+ if doesUserExistNow , err := m .doesUserExist (ctx , database , username ); err != nil {
8380 return err
8481 } else if ! doesUserExistNow {
8582 return errors .New ("user doesn't exist after create" )
8683 }
8784 } else {
88- if err := m .updateUserPasswordAndRoles (database , username , password , roles ); err != nil {
85+ if err := m .updateUserPasswordAndRoles (ctx , database , username , password , roles ); err != nil {
8986 return err
9087 }
9188 }
9289
9390 return nil
9491}
9592
96- func (m * MongoDBRepository ) DropUser (database string , username string ) error {
93+ func (m * MongoDBRepository ) DropUser (ctx context. Context , database string , username string ) error {
9794 command := & bson.D {primitive.E {Key : "dropUser" , Value : username }}
98- r := m .runCommand (database , command )
95+ r := m .runCommand (ctx , database , command )
9996 if _ , err := r .DecodeBytes (); err != nil {
10097 return err
10198 }
10299 return nil
103100}
104101
105- func (m * MongoDBRepository ) EnableExtension (name string ) error {
102+ func (m * MongoDBRepository ) EnableExtension (ctx context. Context , name string ) error {
106103 // NOOP
107104 return nil
108105}
109106
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 )
112109 if err != nil {
113110 return false , err
114111 }
115112
116113 return users != nil && len (users ) > 0 , nil
117114}
118115
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 ) {
120117 users := make (Users , 0 )
121- ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
122- defer cancel ()
123118
124119 collection := m .client .Database (adminDatabase ).Collection (usersCollection )
125120 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)
163158 return rs
164159}
165160
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 {
167162 command := & bson.D {primitive.E {Key : "createUser" , Value : username }, primitive.E {Key : "pwd" , Value : password },
168163 primitive.E {Key : "roles" , Value : m .getRoles (database , roles )}}
169- r := m .runCommand (database , command )
164+ r := m .runCommand (ctx , database , command )
170165 if _ , err := r .DecodeBytes (); err != nil {
171166 return err
172167 }
173168 return nil
174169}
175170
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 {
177172 command := & bson.D {primitive.E {Key : "updateUser" , Value : username }, primitive.E {Key : "pwd" , Value : password },
178173 primitive.E {Key : "roles" , Value : m .getRoles (database , roles )}}
179- r := m .runCommand (database , command )
174+ r := m .runCommand (ctx , database , command )
180175 if _ , err := r .DecodeBytes (); err != nil {
181176 return err
182177 }
183178 return nil
184179}
185180
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 {
189182 return m .client .Database (database ).RunCommand (ctx , * command )
190183}
0 commit comments