Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes 2896: refactor pulp client in dao #438

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/external-repos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func enqueueSnapshotRepos(urls *[]string) error {
}
c := client.NewTaskClient(&q)

repoConfigDao := dao.GetRepositoryConfigDao(db.DB)
repoConfigDao := dao.GetRepositoryConfigDao(db.DB, pulp_client.GetPulpClientWithDomain(context.Background(), ""))
var filter *dao.ListRepoFilter
if urls != nil {
filter = &dao.ListRepoFilter{
Expand Down
10 changes: 0 additions & 10 deletions hammer.py

This file was deleted.

22 changes: 14 additions & 8 deletions pkg/dao/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ type DaoRegistry struct {
func GetDaoRegistry(db *gorm.DB) *DaoRegistry {
reg := DaoRegistry{
RepositoryConfig: &repositoryConfigDaoImpl{
db: db,
yumRepo: &yum.Repository{},
db: db,
yumRepo: &yum.Repository{},
pulpClient: pulp_client.GetPulpClientWithDomain(context.Background(), ""),
ctx: context.Background(),
},
Rpm: rpmDaoImpl{db: db},
Repository: repositoryDaoImpl{db: db},
Metrics: metricsDaoImpl{db: db},
Snapshot: &snapshotDaoImpl{db: db},
TaskInfo: taskInfoDaoImpl{db: db},
AdminTask: adminTaskInfoDaoImpl{db: db, pulpClient: pulp_client.GetGlobalPulpClient(context.Background())},
Domain: domainDaoImpl{db: db},
Snapshot: &snapshotDaoImpl{
db: db,
pulpClient: pulp_client.GetPulpClientWithDomain(context.Background(), ""),
ctx: context.Background(),
},
TaskInfo: taskInfoDaoImpl{db: db},
AdminTask: adminTaskInfoDaoImpl{db: db, pulpClient: pulp_client.GetGlobalPulpClient(context.Background())},
Domain: domainDaoImpl{db: db},
}
return &reg
}
Expand All @@ -55,7 +61,7 @@ type RepositoryConfigDao interface {
InternalOnly_FetchRepoConfigsForRepoUUID(uuid string) []api.RepositoryResponse
UpdateLastSnapshotTask(taskUUID string, orgID string, repoUUID string) error
InternalOnly_RefreshRedHatRepo(request api.RepositoryRequest) (*api.RepositoryResponse, error)
InitializePulpClient(ctx context.Context, orgID string) error
WithContext(ctx context.Context) RepositoryConfigDao
}

//go:generate mockery --name RpmDao --filename rpms_mock.go --inpackage
Expand Down Expand Up @@ -84,7 +90,7 @@ type SnapshotDao interface {
Delete(snapUUID string) error
FetchLatestSnapshot(repoConfigUUID string) (api.SnapshotResponse, error)
GetRepositoryConfigurationFile(orgID, snapshotUUID, repoConfigUUID string) (string, error)
InitializePulpClient(ctx context.Context, orgID string) error
WithContext(ctx context.Context) SnapshotDao
}

//go:generate mockery --name MetricsDao --filename metrics_mock.go --inpackage
Expand Down
13 changes: 13 additions & 0 deletions pkg/dao/mock_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dao

import "github.com/stretchr/testify/mock"

func (m *MockRepositoryConfigDao) WithContextMock() *MockRepositoryConfigDao {
m.On("WithContext", mock.AnythingOfType("*context.valueCtx")).Return(m)
return m
}

func (m *MockSnapshotDao) WithContextMock() *MockSnapshotDao {
m.On("WithContext", mock.AnythingOfType("*context.valueCtx")).Return(m)
return m
}
47 changes: 19 additions & 28 deletions pkg/dao/repository_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,10 @@ type repositoryConfigDaoImpl struct {
db *gorm.DB
yumRepo yum.YumRepository
pulpClient pulp_client.PulpClient
ctx context.Context
}

func GetRepositoryConfigDao(db *gorm.DB) RepositoryConfigDao {
return &repositoryConfigDaoImpl{
db: db,
yumRepo: &yum.Repository{},
}
}

func GetRepositoryConfigDaoWithPulpClient(db *gorm.DB, pulpClient pulp_client.PulpClient) RepositoryConfigDao {
func GetRepositoryConfigDao(db *gorm.DB, pulpClient pulp_client.PulpClient) RepositoryConfigDao {
return &repositoryConfigDaoImpl{
db: db,
yumRepo: &yum.Repository{},
Expand Down Expand Up @@ -79,20 +73,10 @@ func DBErrorToApi(e error) *ce.DaoError {
}
}

func (r *repositoryConfigDaoImpl) InitializePulpClient(ctx context.Context, orgID string) error {
if !config.Get().Features.Snapshots.Enabled {
return nil
}

dDao := GetDomainDao(r.db)
domainName, err := dDao.Fetch(orgID)
if err != nil {
return err
}

pulpClient := pulp_client.GetPulpClientWithDomain(context.TODO(), domainName)
r.pulpClient = pulpClient
return nil
func (r *repositoryConfigDaoImpl) WithContext(ctx context.Context) RepositoryConfigDao {
cpy := *r
cpy.ctx = ctx
return &cpy
}

func (r repositoryConfigDaoImpl) Create(newRepoReq api.RepositoryRequest) (api.RepositoryResponse, error) {
Expand Down Expand Up @@ -265,7 +249,6 @@ func (r repositoryConfigDaoImpl) List(
) (api.RepositoryCollectionResponse, int64, error) {
var totalRepos int64
repoConfigs := make([]models.RepositoryConfiguration, 0)
var err error
var contentPath string

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

if r.pulpClient != nil && config.Get().Features.Snapshots.Enabled {
contentPath, err = r.pulpClient.GetContentPath()
if config.Get().Features.Snapshots.Enabled {
dDao := domainDaoImpl{db: r.db}
domain, err := dDao.Fetch(OrgID)
if err != nil {
return api.RepositoryCollectionResponse{}, totalRepos, err
}

contentPath, err = r.pulpClient.WithContext(r.ctx).WithDomain(domain).GetContentPath()
if err != nil {
return api.RepositoryCollectionResponse{}, totalRepos, err
}
Expand Down Expand Up @@ -404,10 +393,12 @@ func (r repositoryConfigDaoImpl) Fetch(orgID string, uuid string) (api.Repositor
ModelToApiFields(repoConfig, &repo)

if repoConfig.LastSnapshot != nil && config.Get().Features.Snapshots.Enabled {
if r.pulpClient == nil {
return api.RepositoryResponse{}, fmt.Errorf("pulpClient cannot be nil")
dDao := domainDaoImpl{db: r.db}
domainName, err := dDao.Fetch(orgID)
if err != nil {
return api.RepositoryResponse{}, err
}
contentPath, err := r.pulpClient.GetContentPath()
contentPath, err := r.pulpClient.WithContext(r.ctx).WithDomain(domainName).GetContentPath()
if err != nil {
return api.RepositoryResponse{}, err
}
Expand Down
30 changes: 16 additions & 14 deletions pkg/dao/repository_configs_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading