Skip to content

Commit

Permalink
Fixes 2702: Add orgID to snapshot list endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrewgdewar committed Oct 19, 2023
1 parent 6f89eb3 commit 07951b1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 29 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
11 changes: 6 additions & 5 deletions pkg/dao/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ 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
Expand All @@ -71,12 +71,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_configurations.org_id = ?", orgID).
Where("repository_configuration_uuid = ?", UuidifyString(repoConfigUuid))

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

if filteredDB.Error != nil {
return api.SnapshotCollectionResponse{}, 0, filteredDB.Error
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.

8 changes: 4 additions & 4 deletions pkg/dao/snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (s *SnapshotsSuite) TestCreateAndList() {

snap := s.createSnapshot(rConfig)

collection, total, err := sDao.List(rConfig.UUID, pageData, filterData)
collection, total, err := sDao.List(rConfig.OrgID, rConfig.UUID, pageData, filterData)

repository, _ := repoDao.fetchRepoConfig(rConfig.OrgID, rConfig.UUID)
repositoryList, repoCount, _ := repoDao.List(rConfig.OrgID, api.PaginationData{Limit: -1}, api.FilterData{})
Expand Down Expand Up @@ -161,7 +161,7 @@ func (s *SnapshotsSuite) TestListNoSnapshots() {
err = tx.Create(&rConfig).Error
assert.NoError(t, err)

collection, total, err := sDao.List(rConfig.UUID, pageData, filterData)
collection, total, err := sDao.List(rConfig.OrgID, rConfig.UUID, pageData, filterData)
assert.NoError(t, err)
assert.Equal(t, int64(0), total)
assert.Equal(t, 0, len(collection.Data))
Expand Down Expand Up @@ -190,7 +190,7 @@ func (s *SnapshotsSuite) TestListPageLimit() {
s.createSnapshot(rConfig)
}

collection, total, err := sDao.List(rConfig.UUID, pageData, filterData)
collection, total, err := sDao.List(rConfig.OrgID, rConfig.UUID, pageData, filterData)
assert.NoError(t, err)
assert.Equal(t, int64(11), total)
assert.Equal(t, 10, len(collection.Data))
Expand All @@ -215,7 +215,7 @@ func (s *SnapshotsSuite) TestListNotFound() {

s.createSnapshot(rConfig)

collection, total, err := sDao.List("bad-uuid", pageData, filterData)
collection, total, err := sDao.List(rConfig.OrgID, "bad-uuid", pageData, filterData)
assert.Error(t, err)
daoError, ok := err.(*ce.DaoError)
assert.True(t, ok)
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (sh *SnapshotHandler) listSnapshots(c echo.Context) error {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error initializing pulp client", err.Error())
}

snapshots, totalSnaps, err := sh.DaoRegistry.Snapshot.List(uuid, pageData, filterData)
snapshots, totalSnaps, err := sh.DaoRegistry.Snapshot.List(orgID, uuid, pageData, filterData)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error listing repository snapshots", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (suite *SnapshotSuite) TestSnapshotList() {
uuid := "abcadaba"
orgID := test_handler.MockOrgId
suite.reg.Snapshot.On("InitializePulpClient", mock.AnythingOfType("*context.valueCtx"), orgID).Return(nil).Once()
suite.reg.Snapshot.On("List", uuid, paginationData, api.FilterData{}).Return(collection, int64(1), nil)
suite.reg.Snapshot.On("List", test_handler.MockOrgId, uuid, paginationData, api.FilterData{}).Return(collection, int64(1), nil)

path := fmt.Sprintf("%s/repositories/%s/snapshots/?limit=%d", fullRootPath(), uuid, 10)
req := httptest.NewRequest(http.MethodGet, path, nil)
Expand Down
8 changes: 4 additions & 4 deletions test/integration/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *SnapshotSuite) TestSnapshot() {
s.snapshotAndWait(taskClient, repo, repoUuid, accountId)

// Verify the snapshot was created
snaps, _, err := s.dao.Snapshot.List(repo.UUID, api.PaginationData{Limit: -1}, api.FilterData{})
snaps, _, err := s.dao.Snapshot.List(repo.OrgID, repo.UUID, api.PaginationData{Limit: -1}, api.FilterData{})
assert.NoError(s.T(), err)
assert.NotEmpty(s.T(), snaps)
time.Sleep(5 * time.Second)
Expand Down Expand Up @@ -138,7 +138,7 @@ func (s *SnapshotSuite) TestSnapshot() {
s.WaitOnTask(taskUuid)

// Verify the snapshot was deleted
snaps, _, err = s.dao.Snapshot.List(repo.UUID, api.PaginationData{Limit: -1}, api.FilterData{})
snaps, _, err = s.dao.Snapshot.List(repo.OrgID, repo.UUID, api.PaginationData{Limit: -1}, api.FilterData{})
assert.Error(s.T(), err)
assert.Empty(s.T(), snaps.Data)
time.Sleep(5 * time.Second)
Expand Down Expand Up @@ -185,7 +185,7 @@ func (s *SnapshotSuite) snapshotAndWait(taskClient client.TaskClient, repo api.R
s.WaitOnTask(taskUuid)

// Verify the snapshot was created
snaps, _, err := s.dao.Snapshot.List(repo.UUID, api.PaginationData{Limit: -1}, api.FilterData{})
snaps, _, err := s.dao.Snapshot.List(repo.OrgID, repo.UUID, api.PaginationData{Limit: -1}, api.FilterData{})
assert.NoError(s.T(), err)
assert.NotEmpty(s.T(), snaps)
time.Sleep(5 * time.Second)
Expand All @@ -211,7 +211,7 @@ func (s *SnapshotSuite) cancelAndWait(taskClient client.TaskClient, taskUUID uui
s.WaitOnCanceledTask(taskUUID)

// Verify the snapshot was not created
snaps, _, err := s.dao.Snapshot.List(repo.UUID, api.PaginationData{}, api.FilterData{})
snaps, _, err := s.dao.Snapshot.List(repo.OrgID, repo.UUID, api.PaginationData{}, api.FilterData{})
assert.NoError(s.T(), err)
assert.Equal(s.T(), api.SnapshotCollectionResponse{Data: []api.SnapshotResponse{}}, snaps)
}
Expand Down

0 comments on commit 07951b1

Please sign in to comment.