Skip to content

Commit 609dec8

Browse files
committed
remove api dependency from common
1 parent f555cd0 commit 609dec8

File tree

7 files changed

+40
-35
lines changed

7 files changed

+40
-35
lines changed

api/v1beta1/database_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type Role struct {
7676
Name string `json:"name"`
7777

7878
// +optional
79-
Db string `json:"db,omitempty"`
79+
DB string `json:"db,omitempty"`
8080
}
8181

8282
// Extension is a resource representing database extension

common/db/atlas.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66

77
"github.com/mongodb-forks/digest"
88
"go.mongodb.org/atlas/mongodbatlas"
9-
10-
infrav1beta1 "github.com/doodlescheduling/k8sdb-controller/api/v1beta1"
119
)
1210

1311
type AtlasRepository struct {
@@ -38,7 +36,7 @@ func (m *AtlasRepository) CreateDatabaseIfNotExists(ctx context.Context, databas
3836
return nil
3937
}
4038

41-
func (m *AtlasRepository) SetupUser(ctx context.Context, database string, username string, password string, roles []infrav1beta1.Role) error {
39+
func (m *AtlasRepository) SetupUser(ctx context.Context, database string, username string, password string, roles Roles) error {
4240
doesUserExist, err := m.doesUserExist(ctx, database, username)
4341
if err != nil {
4442
return err
@@ -81,7 +79,7 @@ func (m *AtlasRepository) doesUserExist(ctx context.Context, database string, us
8179
return true, err
8280
}
8381

84-
func (m *AtlasRepository) getRoles(database string, roles []infrav1beta1.Role) []mongodbatlas.Role {
82+
func (m *AtlasRepository) getRoles(database string, roles Roles) []mongodbatlas.Role {
8583
// by default, assign readWrite role (backward compatibility)
8684
if len(roles) == 0 {
8785
return []mongodbatlas.Role{{
@@ -92,7 +90,7 @@ func (m *AtlasRepository) getRoles(database string, roles []infrav1beta1.Role) [
9290

9391
rs := make([]mongodbatlas.Role, 0)
9492
for _, r := range roles {
95-
db := r.Db
93+
db := r.DB
9694
if db == "" {
9795
db = database
9896
}
@@ -106,7 +104,7 @@ func (m *AtlasRepository) getRoles(database string, roles []infrav1beta1.Role) [
106104
return rs
107105
}
108106

109-
func (m *AtlasRepository) createUser(ctx context.Context, database string, username string, password string, roles []infrav1beta1.Role) error {
107+
func (m *AtlasRepository) createUser(ctx context.Context, database string, username string, password string, roles Roles) error {
110108
user := &mongodbatlas.DatabaseUser{
111109
Username: username,
112110
Password: password,
@@ -122,7 +120,7 @@ func (m *AtlasRepository) createUser(ctx context.Context, database string, usern
122120
return nil
123121
}
124122

125-
func (m *AtlasRepository) updateUserPasswordAndRoles(ctx context.Context, database string, username string, password string, roles []infrav1beta1.Role) error {
123+
func (m *AtlasRepository) updateUserPasswordAndRoles(ctx context.Context, database string, username string, password string, roles Roles) error {
126124
user := &mongodbatlas.DatabaseUser{
127125
Username: username,
128126
Password: password,

common/db/handler.go renamed to common/db/db.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@ package db
22

33
import (
44
"context"
5-
6-
infrav1beta1 "github.com/doodlescheduling/k8sdb-controller/api/v1beta1"
75
)
86

7+
type Roles []Role
8+
type Role struct {
9+
Name string `json:"role" bson:"role"`
10+
DB string `json:"db" bson:"db"`
11+
}
12+
13+
type Users []User
14+
type User struct {
15+
User string `json:"user" bson:"user"`
16+
DB string `json:"db" bson:"db"`
17+
Roles Roles `json:"roles" bson:"roles"`
18+
}
19+
920
// Invoke a database handler
1021
type Invoke func(ctx context.Context, uri, database, username, password string) (Handler, error)
1122

1223
// Handler is a wrapper arround a certain database client
1324
type Handler interface {
1425
Close(ctx context.Context) error
15-
SetupUser(ctx context.Context, database string, username string, password string, roles []infrav1beta1.Role) error
26+
SetupUser(ctx context.Context, database string, username string, password string, roles Roles) error
1627
DropUser(ctx context.Context, database string, username string) error
1728
CreateDatabaseIfNotExists(ctx context.Context, database string) error
1829
EnableExtension(ctx context.Context, name string) error

common/db/mongodb.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1715
const (
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-
3520
type 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)

common/db/postgresql.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"net/url"
88

9-
infrav1beta1 "github.com/doodlescheduling/k8sdb-controller/api/v1beta1"
109
"github.com/jackc/pgx/v4"
1110
"github.com/jackc/pgx/v4/pgxpool"
1211
)
@@ -86,7 +85,7 @@ func (s *PostgreSQLRepository) CreateDatabaseIfNotExists(ctx context.Context, da
8685
}
8786
}
8887

89-
func (s *PostgreSQLRepository) SetupUser(ctx context.Context, database string, user string, password string, roles []infrav1beta1.Role) error {
88+
func (s *PostgreSQLRepository) SetupUser(ctx context.Context, database string, user string, password string, roles Roles) error {
9089
if err := s.createUserIfNotExists(ctx, user); err != nil {
9190
return err
9291
}

controllers/atlas_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func reconcileAtlasUser(database database, c client.Client, user user, recorder
133133
return user, ctrl.Result{Requeue: true}
134134
}
135135

136-
err = dbHandler.SetupUser(ctx, database.GetDatabaseName(), usr, pw, user.GetRoles())
136+
err = dbHandler.SetupUser(ctx, database.GetDatabaseName(), usr, pw, extractRoles(user.GetRoles()))
137137
if err != nil {
138138
msg := fmt.Sprintf("Failed to provison user account: %s", err.Error())
139139
recorder.Event(user, "Normal", "error", msg)

controllers/common_handler.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ func objectKey(object metav1.Object) client.ObjectKey {
5353
}
5454
}
5555

56+
func extractRoles(roles []infrav1beta1.Role) db.Roles {
57+
list := make(db.Roles, 0)
58+
for _, r := range roles {
59+
list = append(list, db.Role{
60+
Name: r.Name,
61+
DB: r.DB,
62+
})
63+
}
64+
65+
return list
66+
}
67+
5668
func extractCredentials(credentials *infrav1beta1.SecretReference, secret *corev1.Secret) (string, string, error) {
5769
var (
5870
user string
@@ -217,7 +229,7 @@ func reconcileUser(database database, c client.Client, invoke db.Invoke, user us
217229
return user, ctrl.Result{Requeue: true}
218230
}
219231

220-
err = dbHandler.SetupUser(ctx, database.GetDatabaseName(), usr, pw, user.GetRoles())
232+
err = dbHandler.SetupUser(ctx, database.GetDatabaseName(), usr, pw, extractRoles(user.GetRoles()))
221233
if err != nil {
222234
msg := fmt.Sprintf("Failed to provison user account: %s", err.Error())
223235
recorder.Event(user, "Normal", "error", msg)

0 commit comments

Comments
 (0)