@@ -10,28 +10,13 @@ import (
1010 "go.mongodb.org/mongo-driver/mongo"
1111 "go.mongodb.org/mongo-driver/mongo/options"
1212 "go.mongodb.org/mongo-driver/mongo/readpref"
13-
14- infrav1beta1 "github.com/doodlescheduling/k8sdb-controller/api/v1beta1"
1513)
1614
1715const (
1816 adminDatabase = "admin"
1917 usersCollection = "system.users"
2018)
2119
22- type Roles []Role
23- type Role struct {
24- Role string `json:"role" bson:"role"`
25- DB string `json:"db" bson:"db"`
26- }
27-
28- type Users []User
29- type User struct {
30- User string `json:"user" bson:"user"`
31- DB string `json:"db" bson:"db"`
32- Roles Roles `json:"roles" bson:"roles"`
33- }
34-
3520type MongoDBRepository struct {
3621 client * mongo.Client
3722}
@@ -70,7 +55,7 @@ func (m *MongoDBRepository) CreateDatabaseIfNotExists(ctx context.Context, datab
7055 return nil
7156}
7257
73- func (m * MongoDBRepository ) SetupUser (ctx context.Context , database string , username string , password string , roles []infrav1beta1. Role ) error {
58+ func (m * MongoDBRepository ) SetupUser (ctx context.Context , database string , username string , password string , roles Roles ) error {
7459 doesUserExist , err := m .doesUserExist (ctx , database , username )
7560 if err != nil {
7661 return err
@@ -139,7 +124,7 @@ func (m *MongoDBRepository) getAllUsers(ctx context.Context, database string, us
139124 return users , nil
140125}
141126
142- func (m * MongoDBRepository ) getRoles (database string , roles []infrav1beta1. Role ) []bson.M {
127+ func (m * MongoDBRepository ) getRoles (database string , roles Roles ) []bson.M {
143128 // by default, assign readWrite role (backward compatibility)
144129 if len (roles ) == 0 {
145130 return []bson.M {{
@@ -149,7 +134,7 @@ func (m *MongoDBRepository) getRoles(database string, roles []infrav1beta1.Role)
149134 }
150135 rs := make ([]bson.M , 0 )
151136 for _ , r := range roles {
152- db := r .Db
137+ db := r .DB
153138 if db == "" {
154139 db = database
155140 }
@@ -162,7 +147,7 @@ func (m *MongoDBRepository) getRoles(database string, roles []infrav1beta1.Role)
162147 return rs
163148}
164149
165- func (m * MongoDBRepository ) createUser (ctx context.Context , database string , username string , password string , roles []infrav1beta1. Role ) error {
150+ func (m * MongoDBRepository ) createUser (ctx context.Context , database string , username string , password string , roles Roles ) error {
166151 command := & bson.D {primitive.E {Key : "createUser" , Value : username }, primitive.E {Key : "pwd" , Value : password },
167152 primitive.E {Key : "roles" , Value : m .getRoles (database , roles )}}
168153 r := m .runCommand (ctx , database , command )
@@ -172,7 +157,7 @@ func (m *MongoDBRepository) createUser(ctx context.Context, database string, use
172157 return nil
173158}
174159
175- func (m * MongoDBRepository ) updateUserPasswordAndRoles (ctx context.Context , database string , username string , password string , roles []infrav1beta1. Role ) error {
160+ func (m * MongoDBRepository ) updateUserPasswordAndRoles (ctx context.Context , database string , username string , password string , roles Roles ) error {
176161 command := & bson.D {primitive.E {Key : "updateUser" , Value : username }, primitive.E {Key : "pwd" , Value : password },
177162 primitive.E {Key : "roles" , Value : m .getRoles (database , roles )}}
178163 r := m .runCommand (ctx , database , command )
0 commit comments