Skip to content

Commit

Permalink
Fixes 2702,Fixes 2887: Add orgID to snapshot list endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrewgdewar committed Oct 27, 2023
1 parent 6f89eb3 commit 12c7cb3
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 46 deletions.
2 changes: 1 addition & 1 deletion pkg/dao/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type RepositoryDao interface {
//go:generate mockery --name SnapshotDao --filename snapshots_mock.go --inpackage
type SnapshotDao interface {
Create(snap *models.Snapshot) error
List(repoConfigUuid string, paginationData api.PaginationData, _ api.FilterData) (api.SnapshotCollectionResponse, int64, error)
List(orgID string, repoConfigUuid string, paginationData api.PaginationData, filterData api.FilterData) (api.SnapshotCollectionResponse, int64, error)
FetchForRepoConfigUUID(repoConfigUUID string) ([]models.Snapshot, error)
Delete(snapUUID string) error
FetchLatestSnapshot(repoConfigUUID string) (api.SnapshotResponse, error)
Expand Down
34 changes: 23 additions & 11 deletions pkg/dao/repository_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func (r *repositoryConfigDaoImpl) InitializePulpClient(ctx context.Context, orgI
func (r repositoryConfigDaoImpl) Create(newRepoReq api.RepositoryRequest) (api.RepositoryResponse, error) {
var newRepo models.Repository
var newRepoConfig models.RepositoryConfiguration

if newRepoConfig.OrgID == config.RedHatOrg {
return api.RepositoryResponse{}, errors.New("Creating of Red Hat repositories is not permitted")
}

ApiFieldsToModel(newRepoReq, &newRepoConfig, &newRepo)

cleanedUrl := models.CleanupURL(newRepo.URL)
Expand All @@ -110,7 +115,7 @@ func (r repositoryConfigDaoImpl) Create(newRepoReq api.RepositoryRequest) (api.R
}

// reload the repoConfig to fetch repository info too
newRepoConfig, err := r.fetchRepoConfig(newRepoConfig.OrgID, newRepoConfig.UUID)
newRepoConfig, err := r.fetchRepoConfig(newRepoConfig.OrgID, newRepoConfig.UUID, false)
if err != nil {
return api.RepositoryResponse{}, DBErrorToApi(err)
}
Expand Down Expand Up @@ -362,7 +367,7 @@ func (r repositoryConfigDaoImpl) InternalOnly_FetchRepoConfigsForRepoUUID(uuid s
func (r repositoryConfigDaoImpl) Fetch(orgID string, uuid string) (api.RepositoryResponse, error) {
var repo api.RepositoryResponse

repoConfig, err := r.fetchRepoConfig(orgID, uuid)
repoConfig, err := r.fetchRepoConfig(orgID, uuid, true)
if err != nil {
return api.RepositoryResponse{}, err
}
Expand All @@ -383,19 +388,26 @@ func (r repositoryConfigDaoImpl) Fetch(orgID string, uuid string) (api.Repositor
return repo, nil
}

func (r repositoryConfigDaoImpl) fetchRepoConfig(orgID string, uuid string) (models.RepositoryConfiguration, error) {
// fetchRepConfig: "any" allows the fetching of red_hat repositories
func (r repositoryConfigDaoImpl) fetchRepoConfig(orgID string, uuid string, any bool) (models.RepositoryConfiguration, error) {
found := models.RepositoryConfiguration{}

orgIdsToCheck := []string{orgID}

if any {
orgIdsToCheck = append(orgIdsToCheck, config.RedHatOrg)
}

result := r.db.
Preload("Repository").Preload("LastSnapshot").
Where("UUID = ? AND ORG_ID = ?", UuidifyString(uuid), orgID).
Where("UUID = ? AND ORG_ID IN ?", UuidifyString(uuid), orgIdsToCheck).
First(&found)

if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return found, &ce.DaoError{NotFound: true, Message: "Could not find repository with UUID " + uuid}
} else {
return found, DBErrorToApi(result.Error)
}
return found, DBErrorToApi(result.Error)
}
return found, nil
}
Expand All @@ -413,9 +425,8 @@ func (r repositoryConfigDaoImpl) FetchByRepoUuid(orgID string, repoUuid string)
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return repo, &ce.DaoError{NotFound: true, Message: "Could not find repository with UUID " + repoUuid}
} else {
return repo, DBErrorToApi(result.Error)
}
return repo, DBErrorToApi(result.Error)
}

ModelToApiFields(repoConfig, &repo)
Expand All @@ -431,7 +442,8 @@ func (r repositoryConfigDaoImpl) Update(orgID, uuid string, repoParams api.Repos

// We are updating the repo config & snapshots, so bundle in a transaction
err = r.db.Transaction(func(tx *gorm.DB) error {
if repoConfig, err = r.fetchRepoConfig(orgID, uuid); err != nil {
// Setting "any" to false here to prevent updating red_hat repositories
if repoConfig, err = r.fetchRepoConfig(orgID, uuid, false); err != nil {
return err
}
ApiFieldsToModel(repoParams, &repoConfig, &repo)
Expand Down Expand Up @@ -522,7 +534,7 @@ func (r repositoryConfigDaoImpl) SoftDelete(orgID string, uuid string) error {
var repoConfig models.RepositoryConfiguration
var err error

if repoConfig, err = r.fetchRepoConfig(orgID, uuid); err != nil {
if repoConfig, err = r.fetchRepoConfig(orgID, uuid, false); err != nil {
return err
}

Expand Down Expand Up @@ -583,7 +595,7 @@ func (r repositoryConfigDaoImpl) bulkDelete(tx *gorm.DB, orgID string, uuids []s
var err error
var repoConfig models.RepositoryConfiguration

if repoConfig, err = r.fetchRepoConfig(orgID, uuids[i]); err != nil {
if repoConfig, err = r.fetchRepoConfig(orgID, uuids[i], false); err != nil {
dbErr = DBErrorToApi(err)
errors[i] = dbErr
tx.RollbackTo(save)
Expand Down
11 changes: 9 additions & 2 deletions pkg/dao/rpms.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/content-services/content-sources-backend/pkg/api"
"github.com/content-services/content-sources-backend/pkg/config"
ce "github.com/content-services/content-sources-backend/pkg/errors"
"github.com/content-services/content-sources-backend/pkg/models"
"github.com/content-services/yummy/pkg/yum"
Expand All @@ -28,7 +29,7 @@ func (r rpmDaoImpl) isOwnedRepository(orgID string, repositoryConfigUUID string)
var repoConfigs []models.RepositoryConfiguration
var count int64
if err := r.db.
Where("org_id = ? and uuid = ?", orgID, UuidifyString(repositoryConfigUUID)).
Where("org_id IN (?, ?) AND uuid = ?", orgID, config.RedHatOrg, UuidifyString(repositoryConfigUUID)).
Find(&repoConfigs).
Count(&count).
Error; err != nil {
Expand All @@ -40,7 +41,13 @@ func (r rpmDaoImpl) isOwnedRepository(orgID string, repositoryConfigUUID string)
return true, nil
}

func (r rpmDaoImpl) List(orgID string, repositoryConfigUUID string, limit int, offset int, search string, sortBy string) (api.RepositoryRpmCollectionResponse, int64, error) {
func (r rpmDaoImpl) List(
orgID string,
repositoryConfigUUID string,
limit int, offset int,
search string,
sortBy string,
) (api.RepositoryRpmCollectionResponse, int64, error) {
// Check arguments
if orgID == "" {
return api.RepositoryRpmCollectionResponse{}, 0, fmt.Errorf("orgID can not be an empty string")
Expand Down
61 changes: 61 additions & 0 deletions pkg/dao/rpms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,67 @@ func (s *RpmSuite) TestRpmList() {
assert.Equal(t, count, int64(0))
}

func (s *RpmSuite) TestRpmListRedHatRepositories() {
var err error
t := s.Suite.T()

redHatRepo := repoPublicTest.DeepCopy()
redHatRepo.URL = "https://www.public.redhat.com"
if err := s.tx.Create(redHatRepo).Error; err != nil {
s.FailNow("Preparing Repository record: %w", err)
}

redhatRepoConfig := repoConfigTest1.DeepCopy()
redhatRepoConfig.OrgID = config.RedHatOrg
redhatRepoConfig.Name = "Demo Redhat Repository Config"
redhatRepoConfig.RepositoryUUID = redHatRepo.Base.UUID
if err := s.tx.Create(redhatRepoConfig).Error; err != nil {
s.FailNow("Preparing RepositoryConfiguration record: %w", err)
}

// Prepare RepositoryRpm records
rpm1 := repoRpmTest1.DeepCopy()
rpm2 := repoRpmTest2.DeepCopy()
dao := GetRpmDao(s.tx)

err = s.tx.Create(&rpm1).Error
assert.NoError(t, err)
err = s.tx.Create(&rpm2).Error
assert.NoError(t, err)

// Add one red hat repo
err = s.tx.Create(&models.RepositoryRpm{
RepositoryUUID: redHatRepo.Base.UUID,
RpmUUID: rpm1.Base.UUID,
}).Error
assert.NoError(t, err)

//Add one regular repository
err = s.tx.Create(&models.RepositoryRpm{
RepositoryUUID: s.repo.Base.UUID,
RpmUUID: rpm2.Base.UUID,
}).Error

assert.NoError(t, err)

var repoRpmList api.RepositoryRpmCollectionResponse
var count int64

// Check red hat repo package (matched "-1" orgID)
repoRpmList, count, err = dao.List("ThisOrgIdWontMatter", redhatRepoConfig.Base.UUID, 10, 0, "", "")
assert.NoError(t, err)
assert.Equal(t, int64(1), count)
assert.Equal(t, repoRpmList.Meta.Count, count)
assert.Equal(t, repoRpmTest1.Name, repoRpmList.Data[0].Name) // Asserts name:asc by default

// Check custom repo package (checks orgId)
repoRpmList, count, err = dao.List(orgIDTest, s.repoConfig.Base.UUID, 10, 0, "", "")
assert.NoError(t, err)
assert.Equal(t, int64(1), count)
assert.Equal(t, repoRpmList.Meta.Count, count)
assert.Equal(t, repoRpmTest2.Name, repoRpmList.Data[0].Name) // Asserts name:asc by default
}

func (s *RpmSuite) TestRpmListRepoNotFound() {
t := s.Suite.T()
dao := GetRpmDao(s.tx)
Expand Down
29 changes: 21 additions & 8 deletions pkg/dao/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/content-services/content-sources-backend/pkg/api"
"github.com/content-services/content-sources-backend/pkg/config"
ce "github.com/content-services/content-sources-backend/pkg/errors"
"github.com/content-services/content-sources-backend/pkg/models"
"github.com/content-services/content-sources-backend/pkg/pulp_client"
Expand Down Expand Up @@ -48,17 +49,28 @@ func (sDao *snapshotDaoImpl) Create(s *models.Snapshot) error {
}

// List the snapshots for a given repository config
func (sDao *snapshotDaoImpl) List(repoConfigUuid string, paginationData api.PaginationData, _ api.FilterData) (api.SnapshotCollectionResponse, int64, error) {
func (sDao *snapshotDaoImpl) List(
orgID string,
repoConfigUUID string,
paginationData api.PaginationData,
_ api.FilterData,
) (api.SnapshotCollectionResponse, int64, error) {
var snaps []models.Snapshot
var totalSnaps int64
var repoConfig models.RepositoryConfiguration

// First check if repo config exists
result := sDao.db.Where("uuid = ?", UuidifyString(repoConfigUuid)).First(&repoConfig)
result := sDao.db.Where(
"repository_configurations.org_id IN (?,?) AND uuid = ?",
orgID,
config.RedHatOrg,
UuidifyString(repoConfigUUID)).
First(&repoConfig)

if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return api.SnapshotCollectionResponse{}, totalSnaps, &ce.DaoError{
Message: "Could not find repository with UUID " + repoConfigUuid,
Message: "Could not find repository with UUID " + repoConfigUUID,
NotFound: true,
}
}
Expand All @@ -71,12 +83,13 @@ func (sDao *snapshotDaoImpl) List(repoConfigUuid string, paginationData api.Pagi
order := convertSortByToSQL(paginationData.SortBy, sortMap, "created_at asc")

filteredDB := sDao.db.
Where("snapshots.repository_configuration_uuid = ?", UuidifyString(repoConfigUuid))
Model(&models.Snapshot{}).
Joins("JOIN repository_configurations ON repository_configuration_uuid = repository_configurations.uuid").
Where("repository_configuration_uuid = ?", UuidifyString(repoConfigUUID)).
Where("repository_configurations.org_id IN (?,?)", orgID, config.RedHatOrg)

// Get count
filteredDB.
Model(&snaps).
Count(&totalSnaps)
filteredDB.Count(&totalSnaps)

if filteredDB.Error != nil {
return api.SnapshotCollectionResponse{}, 0, filteredDB.Error
Expand Down Expand Up @@ -123,7 +136,7 @@ func (sDao *snapshotDaoImpl) Fetch(uuid string) (models.Snapshot, error) {

func (sDao *snapshotDaoImpl) GetRepositoryConfigurationFile(orgID, snapshotUUID, repoConfigUUID string) (string, error) {
rcDao := repositoryConfigDaoImpl{db: sDao.db}
repoConfig, err := rcDao.fetchRepoConfig(orgID, repoConfigUUID)
repoConfig, err := rcDao.fetchRepoConfig(orgID, repoConfigUUID, true)
if err != nil {
return "", err
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/dao/snapshots_mock.go

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

Loading

0 comments on commit 12c7cb3

Please sign in to comment.