Skip to content

Commit

Permalink
return url for snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
jlsherrill committed Nov 26, 2024
1 parent 8409a7c commit 6c85e43
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pkg/dao/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func GetDaoRegistry(db *gorm.DB) *DaoRegistry {
Domain: domainDaoImpl{db: db, pulpClient: pulp_client.GetGlobalPulpClient(), cpClient: candlepin_client.NewCandlepinClient()},
PackageGroup: packageGroupDaoImpl{db: db},
Environment: environmentDaoImpl{db: db},
Template: templateDaoImpl{db: db},
Template: templateDaoImpl{db: db, pulpClient: pulp_client.GetGlobalPulpClient()},
}
return &reg
}
Expand Down
49 changes: 36 additions & 13 deletions pkg/dao/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ import (
ce "github.com/content-services/content-sources-backend/pkg/errors"
"github.com/content-services/content-sources-backend/pkg/event"
"github.com/content-services/content-sources-backend/pkg/models"
"github.com/content-services/content-sources-backend/pkg/pulp_client"
"github.com/jackc/pgx/v5/pgconn"
"golang.org/x/exp/slices"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type templateDaoImpl struct {
db *gorm.DB
db *gorm.DB
pulpClient pulp_client.PulpGlobalClient
}

func GetTemplateDao(db *gorm.DB, pulpClient pulp_client.PulpGlobalClient) TemplateDao {
return &templateDaoImpl{
db: db,
pulpClient: pulpClient,
}
}

func (t templateDaoImpl) DBToApiError(e error) *ce.DaoError {
Expand Down Expand Up @@ -186,13 +195,19 @@ func (t templateDaoImpl) softDeleteTemplateRepoConfigs(tx *gorm.DB, templateUUID
}

func (t templateDaoImpl) Fetch(ctx context.Context, orgID string, uuid string, includeSoftDel bool) (api.TemplateResponse, error) {
var respTemplate api.TemplateResponse
modelTemplate, err := t.fetch(ctx, orgID, uuid, includeSoftDel)
if err != nil {
return api.TemplateResponse{}, err
}
templatesModelToApi(modelTemplate, &respTemplate)
return respTemplate, nil
pulpContentPath := ""
if config.Get().Features.Snapshots.Enabled {
var err error
pulpContentPath, err = t.pulpClient.GetContentPath(ctx)
if err != nil {
return api.TemplateResponse{}, err
}
}
return templatesConvertToResponses([]models.Template{modelTemplate}, pulpContentPath)[0], nil
}

func (t templateDaoImpl) fetch(ctx context.Context, orgID string, uuid string, includeSoftDel bool) (models.Template, error) {
Expand Down Expand Up @@ -316,7 +331,15 @@ func (t templateDaoImpl) List(ctx context.Context, orgID string, paginationData
return api.TemplateCollectionResponse{}, totalTemplates, t.DBToApiError(filteredDB.Error)
}

responses := templatesConvertToResponses(templates)
pulpContentPath := ""
if config.Get().Features.Snapshots.Enabled {
var err error
pulpContentPath, err = t.pulpClient.GetContentPath(ctx)
if err != nil {
return api.TemplateCollectionResponse{}, 0, err
}
}
responses := templatesConvertToResponses(templates, pulpContentPath)

return api.TemplateCollectionResponse{Data: responses}, totalTemplates, nil
}
Expand Down Expand Up @@ -625,13 +648,6 @@ func templatesModelToApi(model models.Template, apiTemplate *api.TemplateRespons
apiTemplate.Version = model.Version
apiTemplate.Arch = model.Arch
apiTemplate.Date = model.Date.UTC()
apiTemplate.RepositoryUUIDS = make([]string, 0) // prevent null responses
for _, tRepoConfig := range model.TemplateRepositoryConfigurations {
apiTemplate.RepositoryUUIDS = append(apiTemplate.RepositoryUUIDS, tRepoConfig.RepositoryConfigurationUUID)
snap := api.SnapshotResponse{}
SnapshotModelToApi(tRepoConfig.Snapshot, &snap)
apiTemplate.Snapshots = append(apiTemplate.Snapshots, snap)
}
apiTemplate.CreatedBy = model.CreatedBy
apiTemplate.LastUpdatedBy = model.LastUpdatedBy
apiTemplate.CreatedAt = model.CreatedAt.UTC()
Expand Down Expand Up @@ -664,10 +680,17 @@ func templatesModelToApi(model models.Template, apiTemplate *api.TemplateRespons
}
}

func templatesConvertToResponses(templates []models.Template) []api.TemplateResponse {
func templatesConvertToResponses(templates []models.Template, pulpContentPath string) []api.TemplateResponse {
responses := make([]api.TemplateResponse, len(templates))
for i := 0; i < len(templates); i++ {
templatesModelToApi(templates[i], &responses[i])
// Add in associations (Repository Config UUIDs and Snapshots)
responses[i].RepositoryUUIDS = make([]string, 0) // prevent null responses
for _, tRepoConfig := range templates[i].TemplateRepositoryConfigurations {
responses[i].RepositoryUUIDS = append(responses[i].RepositoryUUIDS, tRepoConfig.RepositoryConfigurationUUID)
snaps := snapshotConvertToResponses([]models.Snapshot{tRepoConfig.Snapshot}, pulpContentPath)
responses[i].Snapshots = append(responses[i].Snapshots, snaps[0])
}
}
return responses
}
52 changes: 33 additions & 19 deletions pkg/dao/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"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"
"github.com/content-services/content-sources-backend/pkg/seeds"
"github.com/content-services/content-sources-backend/pkg/utils"
"github.com/google/uuid"
Expand All @@ -22,6 +23,7 @@ import (

type TemplateSuite struct {
*DaoSuite
pulpClient *pulp_client.MockPulpGlobalClient
}

func TestTemplateSuite(t *testing.T) {
Expand All @@ -30,8 +32,19 @@ func TestTemplateSuite(t *testing.T) {
suite.Run(t, &testTemplateSuite)
}

func (s *TemplateSuite) templateDao() templateDaoImpl {
return templateDaoImpl{
db: s.tx,
pulpClient: s.pulpClient,
}
}
func (s *TemplateSuite) SetupTest() {
s.DaoSuite.SetupTest()
s.pulpClient = &pulp_client.MockPulpGlobalClient{}
s.pulpClient.On("GetContentPath", context.Background()).Return(testContentPath, nil)
}
func (s *TemplateSuite) TestCreate() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()

orgID := orgIDTest
_, err := seeds.SeedRepositoryConfigurations(s.tx, 2, seeds.SeedOptions{OrgID: orgID})
Expand Down Expand Up @@ -69,7 +82,7 @@ func (s *TemplateSuite) TestCreate() {
}

func (s *TemplateSuite) TestCreateDeleteCreateSameName() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()

orgID := orgIDTest
_, err := seeds.SeedRepositoryConfigurations(s.tx, 2, seeds.SeedOptions{OrgID: orgID})
Expand Down Expand Up @@ -121,7 +134,7 @@ func (s *TemplateSuite) TestCreateDeleteCreateSameName() {
}

func (s *TemplateSuite) TestFetch() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()

var found models.Template
_, err := seeds.SeedTemplates(s.tx, 1, seeds.TemplateSeedOptions{OrgID: orgIDTest})
Expand All @@ -142,7 +155,7 @@ func (s *TemplateSuite) TestFetch() {
}

func (s *TemplateSuite) TestFetchSoftDeleted() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()

var found models.Template
_, err := seeds.SeedTemplates(s.tx, 1, seeds.TemplateSeedOptions{OrgID: orgIDTest})
Expand All @@ -167,7 +180,7 @@ func (s *TemplateSuite) TestFetchSoftDeleted() {
}

func (s *TemplateSuite) TestFetchSoftDeletedRepoConfig() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
found := models.Template{}

template, rcIds := s.seedWithRepoConfig(orgIDTest, 1)
Expand Down Expand Up @@ -196,7 +209,7 @@ func (s *TemplateSuite) TestFetchSoftDeletedRepoConfig() {
}

func (s *TemplateSuite) TestFetchNotFound() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()

var found models.Template
_, err := seeds.SeedTemplates(s.tx, 1, seeds.TemplateSeedOptions{OrgID: orgIDTest})
Expand All @@ -217,7 +230,7 @@ func (s *TemplateSuite) TestFetchNotFound() {
}

func (s *TemplateSuite) TestList() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
var err error
var found []models.Template
var total int64
Expand All @@ -242,10 +255,11 @@ func (s *TemplateSuite) TestList() {
assert.Equal(s.T(), 2, len(responses.Data[0].Snapshots))
assert.NotEmpty(s.T(), responses.Data[0].Snapshots[0].UUID)
assert.NotEmpty(s.T(), responses.Data[0].Snapshots[0].RepositoryName)
assert.NotEmpty(s.T(), responses.Data[0].Snapshots[0].URL)
}

func (s *TemplateSuite) TestListNoTemplates() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
var err error
var found []models.Template
var total int64
Expand All @@ -261,7 +275,7 @@ func (s *TemplateSuite) TestListNoTemplates() {
}

func (s *TemplateSuite) TestListPageLimit() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
var err error
var found []models.Template
var total int64
Expand All @@ -286,7 +300,7 @@ func (s *TemplateSuite) TestListPageLimit() {
}

func (s *TemplateSuite) TestListFilters() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
var err error
var found []models.Template
var total int64
Expand Down Expand Up @@ -344,7 +358,7 @@ func (s *TemplateSuite) TestListFilters() {
}

func (s *TemplateSuite) TestListFilterSearch() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
var err error
var found []models.Template
var total int64
Expand Down Expand Up @@ -410,7 +424,7 @@ func (s *TemplateSuite) TestListBySnapshot() {
}

func (s *TemplateSuite) TestDelete() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
var err error
var found models.Template

Expand Down Expand Up @@ -448,7 +462,7 @@ func (s *TemplateSuite) TestDelete() {
}

func (s *TemplateSuite) TestDeleteNotFound() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
var err error

_, err = seeds.SeedTemplates(s.tx, 1, seeds.TemplateSeedOptions{OrgID: orgIDTest})
Expand Down Expand Up @@ -486,7 +500,7 @@ func (s *TemplateSuite) TestDeleteNotFound() {
}

func (s *TemplateSuite) TestClearDeletedAt() {
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
var err error
var found models.Template

Expand Down Expand Up @@ -561,7 +575,7 @@ func (s *TemplateSuite) TestUpdate() {
err := s.tx.Where("org_id = ?", orgIDTest).Find(&repoConfigs).Error
assert.NoError(s.T(), err)

templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
_, err = templateDao.Update(context.Background(), orgIDTest, origTempl.UUID, api.TemplateUpdateRequest{Description: utils.Ptr("scratch"), RepositoryUUIDS: []string{rcUUIDs[0]}, Name: utils.Ptr("test-name")})
require.NoError(s.T(), err)
found := s.fetchTemplate(origTempl.UUID)
Expand Down Expand Up @@ -610,7 +624,7 @@ func (s *TemplateSuite) TestGetRepoChanges() {
snap2 := s.createSnapshot(repoConfigs[1])
snap3 := s.createSnapshot(repoConfigs[2])

templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
req := api.TemplateRequest{
Name: utils.Ptr("test template"),
RepositoryUUIDS: []string{repoConfigs[0].UUID, repoConfigs[1].UUID, repoConfigs[2].UUID},
Expand Down Expand Up @@ -640,7 +654,7 @@ func (s *TemplateSuite) TestGetRepoChanges() {
func (s *TemplateSuite) TestUpdateLastUpdateTask() {
template, _ := s.seedWithRepoConfig(orgIDTest, 1)

templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
taskUUID := uuid.NewString()
err := templateDao.UpdateLastUpdateTask(context.Background(), taskUUID, orgIDTest, template.UUID)
require.NoError(s.T(), err)
Expand All @@ -652,7 +666,7 @@ func (s *TemplateSuite) TestUpdateLastUpdateTask() {
func (s *TemplateSuite) TestUpdateLastError() {
template, _ := s.seedWithRepoConfig(orgIDTest, 1)

templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
lastUpdateSnapshotError := "test error"
err := templateDao.UpdateLastError(context.Background(), orgIDTest, template.UUID, lastUpdateSnapshotError)
require.NoError(s.T(), err)
Expand All @@ -664,7 +678,7 @@ func (s *TemplateSuite) TestUpdateLastError() {
func (s *TemplateSuite) TestSetEnvironmentCreated() {
template, _ := s.seedWithRepoConfig(orgIDTest, 1)
assert.False(s.T(), template.RHSMEnvironmentCreated)
templateDao := templateDaoImpl{db: s.tx}
templateDao := s.templateDao()
err := templateDao.SetEnvironmentCreated(context.Background(), template.UUID)
require.NoError(s.T(), err)
found := s.fetchTemplate(template.UUID)
Expand Down

0 comments on commit 6c85e43

Please sign in to comment.