Skip to content

Commit

Permalink
Fixes 4952: add snaps list with temp list/fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
jlsherrill committed Nov 5, 2024
1 parent fe09e98 commit 12d5e2c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 21 deletions.
8 changes: 8 additions & 0 deletions api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
39 changes: 20 additions & 19 deletions pkg/api/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions pkg/dao/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions pkg/dao/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions pkg/models/template_repository_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down

0 comments on commit 12d5e2c

Please sign in to comment.