From 12d5e2c750d6f4895b24507df5fb0bfa81742210 Mon Sep 17 00:00:00 2001 From: Justin Sherrill Date: Tue, 5 Nov 2024 16:06:17 -0500 Subject: [PATCH] Fixes 4952: add snaps list with temp list/fetch --- api/docs.go | 8 ++++ api/openapi.json | 8 ++++ pkg/api/templates.go | 39 ++++++++++--------- pkg/dao/templates.go | 7 +++- pkg/dao/templates_test.go | 3 ++ .../template_repository_configuration.go | 1 + 6 files changed, 45 insertions(+), 21 deletions(-) diff --git a/api/docs.go b/api/docs.go index 6bf3df1db..f4af8364b 100644 --- a/api/docs.go +++ b/api/docs.go @@ -4832,6 +4832,14 @@ const docTemplate = `{ "description": "Environment ID used by subscription-manager and candlepin", "type": "string" }, + "snapshots": { + "description": "The list of snapshots in use by the template", + "type": "array", + "items": { + "$ref": "#/definitions/api.SnapshotResponse" + }, + "readOnly": true + }, "updated_at": { "description": "Datetime template was last updated", "type": "string" diff --git a/api/openapi.json b/api/openapi.json index 92eff29e0..4520dd545 100644 --- a/api/openapi.json +++ b/api/openapi.json @@ -1451,6 +1451,14 @@ "description": "Environment ID used by subscription-manager and candlepin", "type": "string" }, + "snapshots": { + "description": "The list of snapshots in use by the template", + "items": { + "$ref": "#/components/schemas/api.SnapshotResponse" + }, + "readOnly": true, + "type": "array" + }, "updated_at": { "description": "Datetime template was last updated", "type": "string" diff --git a/pkg/api/templates.go b/pkg/api/templates.go index b4ccba9e1..79e44ae03 100644 --- a/pkg/api/templates.go +++ b/pkg/api/templates.go @@ -22,25 +22,26 @@ type TemplateRequest struct { } type TemplateResponse struct { - UUID string `json:"uuid" readonly:"true"` - Name string `json:"name"` // Name of the template - OrgID string `json:"org_id"` // Organization ID of the owner - Description string `json:"description"` // Description of the template - Arch string `json:"arch"` // Architecture of the template - Version string `json:"version"` // Version of the template - Date time.Time `json:"date"` // Latest date to include snapshots for - RepositoryUUIDS []string `json:"repository_uuids"` // Repositories added to the template - RHSMEnvironmentID string `json:"rhsm_environment_id"` // Environment ID used by subscription-manager and candlepin - CreatedBy string `json:"created_by"` // User that created the template - LastUpdatedBy string `json:"last_updated_by"` // User that most recently updated the template - CreatedAt time.Time `json:"created_at"` // Datetime template was created - UpdatedAt time.Time `json:"updated_at"` // Datetime template was last updated - DeletedAt gorm.DeletedAt `json:"-" swaggerignore:"true"` // Datetime template was deleted - UseLatest bool `json:"use_latest"` // Use latest snapshot for all repositories in the template - LastUpdateSnapshotError string `json:"last_update_snapshot_error"` // Error of last update_latest_snapshot task that updated the template - LastUpdateTaskUUID string `json:"last_update_task_uuid,omitempty"` // UUID of the last update_template_content task that updated the template - LastUpdateTask *TaskInfoResponse `json:"last_update_task,omitempty"` // Response of last update_template_content task that updated the template - RHSMEnvironmentCreated bool `json:"rhsm_environment_created" readonly:"true"` // Whether the candlepin environment is created and systems can be added + UUID string `json:"uuid" readonly:"true"` + Name string `json:"name"` // Name of the template + OrgID string `json:"org_id"` // Organization ID of the owner + Description string `json:"description"` // Description of the template + Arch string `json:"arch"` // Architecture of the template + Version string `json:"version"` // Version of the template + Date time.Time `json:"date"` // Latest date to include snapshots for + RepositoryUUIDS []string `json:"repository_uuids"` // Repositories added to the template + Snapshots []SnapshotResponse `json:"snapshots" readonly:"true"` // The list of snapshots in use by the template + RHSMEnvironmentID string `json:"rhsm_environment_id"` // Environment ID used by subscription-manager and candlepin + CreatedBy string `json:"created_by"` // User that created the template + LastUpdatedBy string `json:"last_updated_by"` // User that most recently updated the template + CreatedAt time.Time `json:"created_at"` // Datetime template was created + UpdatedAt time.Time `json:"updated_at"` // Datetime template was last updated + DeletedAt gorm.DeletedAt `json:"-" swaggerignore:"true"` // Datetime template was deleted + UseLatest bool `json:"use_latest"` // Use latest snapshot for all repositories in the template + LastUpdateSnapshotError string `json:"last_update_snapshot_error"` // Error of last update_latest_snapshot task that updated the template + LastUpdateTaskUUID string `json:"last_update_task_uuid,omitempty"` // UUID of the last update_template_content task that updated the template + LastUpdateTask *TaskInfoResponse `json:"last_update_task,omitempty"` // Response of last update_template_content task that updated the template + RHSMEnvironmentCreated bool `json:"rhsm_environment_created" readonly:"true"` // Whether the candlepin environment is created and systems can be added } // We use a separate struct because version and arch cannot be updated diff --git a/pkg/dao/templates.go b/pkg/dao/templates.go index b34212f3e..aef705a96 100644 --- a/pkg/dao/templates.go +++ b/pkg/dao/templates.go @@ -202,7 +202,7 @@ func (t templateDaoImpl) fetch(ctx context.Context, orgID string, uuid string, i query = query.Unscoped() } err := query.Where("uuid = ? AND org_id = ?", UuidifyString(uuid), orgID). - Preload("TemplateRepositoryConfigurations"). + Preload("TemplateRepositoryConfigurations.Snapshot.RepositoryConfiguration"). Preload("LastUpdateTask"). First(&modelTemplate).Error if err != nil { @@ -336,7 +336,7 @@ func (t templateDaoImpl) InternalOnlyFetchByName(ctx context.Context, name strin } func (t templateDaoImpl) filteredDbForList(orgID string, filteredDB *gorm.DB, filterData api.TemplateFilterData) *gorm.DB { - filteredDB = filteredDB.Where("org_id = ? ", orgID) + filteredDB = filteredDB.Where("org_id = ? ", orgID).Preload("TemplateRepositoryConfigurations.Snapshot.RepositoryConfiguration") if filterData.Name != "" { filteredDB = filteredDB.Where("name = ?", filterData.Name) @@ -624,6 +624,9 @@ func templatesModelToApi(model models.Template, apiTemplate *api.TemplateRespons 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 diff --git a/pkg/dao/templates_test.go b/pkg/dao/templates_test.go index 7b9418d41..db87e9d57 100644 --- a/pkg/dao/templates_test.go +++ b/pkg/dao/templates_test.go @@ -239,6 +239,9 @@ func (s *TemplateSuite) TestList() { assert.True(s.T(), responses.Data[0].CreatedAt.Equal(found[0].CreatedAt)) assert.True(s.T(), responses.Data[0].UpdatedAt.Equal(found[0].UpdatedAt)) assert.Equal(s.T(), responses.Data[0].UseLatest, found[0].UseLatest) + 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) } func (s *TemplateSuite) TestListNoTemplates() { diff --git a/pkg/models/template_repository_configuration.go b/pkg/models/template_repository_configuration.go index a0f4f7f49..99d600d68 100644 --- a/pkg/models/template_repository_configuration.go +++ b/pkg/models/template_repository_configuration.go @@ -10,6 +10,7 @@ type TemplateRepositoryConfiguration struct { RepositoryConfigurationUUID string `json:"repository_configuration_uuid" gorm:"not null"` TemplateUUID string `json:"template_uuid" gorm:"not null"` SnapshotUUID string `json:"snapshot_uuid" gorm:"not null"` + Snapshot Snapshot `json:"snapshot" gorm:"foreignkey:SnapshotUUID"` DistributionHref string `json:"distribution_href"` DeletedAt gorm.DeletedAt `json:"deleted_at"` }