Skip to content

Commit 9c565b3

Browse files
authored
Fixes 2896: refactor pulp client in dao (#438)
* Fixes 2896: refactor pulp client in dao * add mock helper methods * make copy of receiver * fix tests
1 parent effd10c commit 9c565b3

22 files changed

+357
-269
lines changed

cmd/external-repos/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func enqueueSnapshotRepos(urls *[]string) error {
201201
}
202202
c := client.NewTaskClient(&q)
203203

204-
repoConfigDao := dao.GetRepositoryConfigDao(db.DB)
204+
repoConfigDao := dao.GetRepositoryConfigDao(db.DB, pulp_client.GetPulpClientWithDomain(context.Background(), ""))
205205
var filter *dao.ListRepoFilter
206206
if urls != nil {
207207
filter = &dao.ListRepoFilter{

hammer.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

pkg/dao/interfaces.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,22 @@ type DaoRegistry struct {
2424
func GetDaoRegistry(db *gorm.DB) *DaoRegistry {
2525
reg := DaoRegistry{
2626
RepositoryConfig: &repositoryConfigDaoImpl{
27-
db: db,
28-
yumRepo: &yum.Repository{},
27+
db: db,
28+
yumRepo: &yum.Repository{},
29+
pulpClient: pulp_client.GetPulpClientWithDomain(context.Background(), ""),
30+
ctx: context.Background(),
2931
},
3032
Rpm: rpmDaoImpl{db: db},
3133
Repository: repositoryDaoImpl{db: db},
3234
Metrics: metricsDaoImpl{db: db},
33-
Snapshot: &snapshotDaoImpl{db: db},
34-
TaskInfo: taskInfoDaoImpl{db: db},
35-
AdminTask: adminTaskInfoDaoImpl{db: db, pulpClient: pulp_client.GetGlobalPulpClient(context.Background())},
36-
Domain: domainDaoImpl{db: db},
35+
Snapshot: &snapshotDaoImpl{
36+
db: db,
37+
pulpClient: pulp_client.GetPulpClientWithDomain(context.Background(), ""),
38+
ctx: context.Background(),
39+
},
40+
TaskInfo: taskInfoDaoImpl{db: db},
41+
AdminTask: adminTaskInfoDaoImpl{db: db, pulpClient: pulp_client.GetGlobalPulpClient(context.Background())},
42+
Domain: domainDaoImpl{db: db},
3743
}
3844
return &reg
3945
}
@@ -55,7 +61,7 @@ type RepositoryConfigDao interface {
5561
InternalOnly_FetchRepoConfigsForRepoUUID(uuid string) []api.RepositoryResponse
5662
UpdateLastSnapshotTask(taskUUID string, orgID string, repoUUID string) error
5763
InternalOnly_RefreshRedHatRepo(request api.RepositoryRequest) (*api.RepositoryResponse, error)
58-
InitializePulpClient(ctx context.Context, orgID string) error
64+
WithContext(ctx context.Context) RepositoryConfigDao
5965
}
6066

6167
//go:generate mockery --name RpmDao --filename rpms_mock.go --inpackage
@@ -84,7 +90,7 @@ type SnapshotDao interface {
8490
Delete(snapUUID string) error
8591
FetchLatestSnapshot(repoConfigUUID string) (api.SnapshotResponse, error)
8692
GetRepositoryConfigurationFile(orgID, snapshotUUID, repoConfigUUID string) (string, error)
87-
InitializePulpClient(ctx context.Context, orgID string) error
93+
WithContext(ctx context.Context) SnapshotDao
8894
}
8995

9096
//go:generate mockery --name MetricsDao --filename metrics_mock.go --inpackage

pkg/dao/mock_helpers.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dao
2+
3+
import "github.com/stretchr/testify/mock"
4+
5+
func (m *MockRepositoryConfigDao) WithContextMock() *MockRepositoryConfigDao {
6+
m.On("WithContext", mock.AnythingOfType("*context.valueCtx")).Return(m)
7+
return m
8+
}
9+
10+
func (m *MockSnapshotDao) WithContextMock() *MockSnapshotDao {
11+
m.On("WithContext", mock.AnythingOfType("*context.valueCtx")).Return(m)
12+
return m
13+
}

pkg/dao/repository_configs.go

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,10 @@ type repositoryConfigDaoImpl struct {
2828
db *gorm.DB
2929
yumRepo yum.YumRepository
3030
pulpClient pulp_client.PulpClient
31+
ctx context.Context
3132
}
3233

33-
func GetRepositoryConfigDao(db *gorm.DB) RepositoryConfigDao {
34-
return &repositoryConfigDaoImpl{
35-
db: db,
36-
yumRepo: &yum.Repository{},
37-
}
38-
}
39-
40-
func GetRepositoryConfigDaoWithPulpClient(db *gorm.DB, pulpClient pulp_client.PulpClient) RepositoryConfigDao {
34+
func GetRepositoryConfigDao(db *gorm.DB, pulpClient pulp_client.PulpClient) RepositoryConfigDao {
4135
return &repositoryConfigDaoImpl{
4236
db: db,
4337
yumRepo: &yum.Repository{},
@@ -79,20 +73,10 @@ func DBErrorToApi(e error) *ce.DaoError {
7973
}
8074
}
8175

82-
func (r *repositoryConfigDaoImpl) InitializePulpClient(ctx context.Context, orgID string) error {
83-
if !config.Get().Features.Snapshots.Enabled {
84-
return nil
85-
}
86-
87-
dDao := GetDomainDao(r.db)
88-
domainName, err := dDao.Fetch(orgID)
89-
if err != nil {
90-
return err
91-
}
92-
93-
pulpClient := pulp_client.GetPulpClientWithDomain(context.TODO(), domainName)
94-
r.pulpClient = pulpClient
95-
return nil
76+
func (r *repositoryConfigDaoImpl) WithContext(ctx context.Context) RepositoryConfigDao {
77+
cpy := *r
78+
cpy.ctx = ctx
79+
return &cpy
9680
}
9781

9882
func (r repositoryConfigDaoImpl) Create(newRepoReq api.RepositoryRequest) (api.RepositoryResponse, error) {
@@ -265,7 +249,6 @@ func (r repositoryConfigDaoImpl) List(
265249
) (api.RepositoryCollectionResponse, int64, error) {
266250
var totalRepos int64
267251
repoConfigs := make([]models.RepositoryConfiguration, 0)
268-
var err error
269252
var contentPath string
270253

271254
filteredDB := r.filteredDbForList(OrgID, r.db, filterData)
@@ -304,8 +287,14 @@ func (r repositoryConfigDaoImpl) List(
304287
return api.RepositoryCollectionResponse{}, totalRepos, filteredDB.Error
305288
}
306289

307-
if r.pulpClient != nil && config.Get().Features.Snapshots.Enabled {
308-
contentPath, err = r.pulpClient.GetContentPath()
290+
if config.Get().Features.Snapshots.Enabled {
291+
dDao := domainDaoImpl{db: r.db}
292+
domain, err := dDao.Fetch(OrgID)
293+
if err != nil {
294+
return api.RepositoryCollectionResponse{}, totalRepos, err
295+
}
296+
297+
contentPath, err = r.pulpClient.WithContext(r.ctx).WithDomain(domain).GetContentPath()
309298
if err != nil {
310299
return api.RepositoryCollectionResponse{}, totalRepos, err
311300
}
@@ -404,10 +393,12 @@ func (r repositoryConfigDaoImpl) Fetch(orgID string, uuid string) (api.Repositor
404393
ModelToApiFields(repoConfig, &repo)
405394

406395
if repoConfig.LastSnapshot != nil && config.Get().Features.Snapshots.Enabled {
407-
if r.pulpClient == nil {
408-
return api.RepositoryResponse{}, fmt.Errorf("pulpClient cannot be nil")
396+
dDao := domainDaoImpl{db: r.db}
397+
domainName, err := dDao.Fetch(orgID)
398+
if err != nil {
399+
return api.RepositoryResponse{}, err
409400
}
410-
contentPath, err := r.pulpClient.GetContentPath()
401+
contentPath, err := r.pulpClient.WithContext(r.ctx).WithDomain(domainName).GetContentPath()
411402
if err != nil {
412403
return api.RepositoryResponse{}, err
413404
}

pkg/dao/repository_configs_mock.go

Lines changed: 16 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)