Skip to content

Commit

Permalink
Fixes 4825: add template attribute for when env is created (#846)
Browse files Browse the repository at this point in the history
Fixes 4825: provide template attribute for when env is created
  • Loading branch information
jlsherrill authored Oct 18, 2024
1 parent cc27e68 commit 9f5e098
Show file tree
Hide file tree
Showing 29 changed files with 145 additions and 51 deletions.
5 changes: 5 additions & 0 deletions api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4741,6 +4741,11 @@ const docTemplate = `{
"type": "string"
}
},
"rhsm_environment_created": {
"description": "Whether the candlepin environment is created and systems can be added",
"type": "boolean",
"readOnly": true
},
"rhsm_environment_id": {
"description": "Environment ID used by subscription-manager and candlepin",
"type": "string"
Expand Down
5 changes: 5 additions & 0 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,11 @@
},
"type": "array"
},
"rhsm_environment_created": {
"description": "Whether the candlepin environment is created and systems can be added",
"readOnly": true,
"type": "boolean"
},
"rhsm_environment_id": {
"description": "Environment ID used by subscription-manager and candlepin",
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion db/migrations.latest
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20240919154238
20241011084507
5 changes: 5 additions & 0 deletions db/migrations/20241011084507_AddEnvironmentCreated.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BEGIN;

alter table templates drop column rhsm_environment_created;

COMMIT;
7 changes: 7 additions & 0 deletions db/migrations/20241011084507_AddEnvironmentCreated.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BEGIN;

alter table templates add column rhsm_environment_created boolean NOT NULL DEFAULT false;
update templates set rhsm_environment_created = true
where templates.uuid in (select object_uuid from tasks where type = 'update-template-content' and status = 'completed');

COMMIT;
35 changes: 18 additions & 17 deletions pkg/api/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,24 @@ 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
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
}

// We use a separate struct because version and arch cannot be updated
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/cache_mock.go

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

2 changes: 1 addition & 1 deletion pkg/candlepin_client/candlepin_client_mock.go

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

2 changes: 1 addition & 1 deletion pkg/dao/admin_tasks_mock.go

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

2 changes: 1 addition & 1 deletion pkg/dao/domain_dao_mock.go

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

2 changes: 1 addition & 1 deletion pkg/dao/environments_mock.go

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

1 change: 1 addition & 0 deletions pkg/dao/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,5 @@ type TemplateDao interface {
DeleteTemplateRepoConfigs(ctx context.Context, templateUUID string, keepRepoConfigUUIDs []string) error
UpdateLastUpdateTask(ctx context.Context, taskUUID string, orgID string, templateUUID string) error
UpdateLastError(ctx context.Context, orgID string, templateUUID string, lastUpdateSnapshotError string) error
SetEnvironmentCreated(ctx context.Context, templateUUID string) error
}
2 changes: 1 addition & 1 deletion pkg/dao/metrics_mock.go

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

2 changes: 1 addition & 1 deletion pkg/dao/package_groups_mock.go

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

2 changes: 1 addition & 1 deletion pkg/dao/repositories_mock.go

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

2 changes: 1 addition & 1 deletion pkg/dao/repository_configs_mock.go

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

2 changes: 1 addition & 1 deletion pkg/dao/rpms_mock.go

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

2 changes: 1 addition & 1 deletion pkg/dao/snapshots_mock.go

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

2 changes: 1 addition & 1 deletion pkg/dao/task_info_mock.go

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

15 changes: 15 additions & 0 deletions pkg/dao/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,20 @@ func (t templateDaoImpl) UpdateDistributionHrefs(ctx context.Context, templateUU
return nil
}

func (t templateDaoImpl) SetEnvironmentCreated(ctx context.Context, templateUUID string) error {
result := t.db.WithContext(ctx).Exec(`
UPDATE templates
SET rhsm_environment_created = true
AND uuid = ?`,
templateUUID,
)

if result.Error != nil {
return result.Error
}
return nil
}

func (t templateDaoImpl) UpdateLastUpdateTask(ctx context.Context, taskUUID string, orgID string, templateUUID string) error {
result := t.db.WithContext(ctx).Exec(`
UPDATE templates
Expand Down Expand Up @@ -522,6 +536,7 @@ func templatesUpdateApiToModel(api api.TemplateUpdateRequest, model *models.Temp
func templatesModelToApi(model models.Template, apiTemplate *api.TemplateResponse) {
apiTemplate.UUID = model.UUID
apiTemplate.RHSMEnvironmentID = candlepin_client.GetEnvironmentID(model.UUID)
apiTemplate.RHSMEnvironmentCreated = model.RHSMEnvironmentCreated
apiTemplate.OrgID = model.OrgID
apiTemplate.Name = model.Name
apiTemplate.Description = model.Description
Expand Down
20 changes: 19 additions & 1 deletion pkg/dao/templates_mock.go

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

10 changes: 10 additions & 0 deletions pkg/dao/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,13 @@ func (s *TemplateSuite) TestUpdateLastError() {
found := s.fetchTemplate(template.UUID)
assert.Equal(s.T(), lastUpdateSnapshotError, *found.LastUpdateSnapshotError)
}

func (s *TemplateSuite) TestSetEnvironmentCreated() {
template, _ := s.seedWithRepoConfig(orgIDTest, 1)
assert.False(s.T(), template.RHSMEnvironmentCreated)
templateDao := templateDaoImpl{db: s.tx}
err := templateDao.SetEnvironmentCreated(context.Background(), template.UUID)
require.NoError(s.T(), err)
found := s.fetchTemplate(template.UUID)
assert.True(s.T(), found.RHSMEnvironmentCreated)
}
1 change: 1 addition & 0 deletions pkg/models/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Template struct {
CreatedBy string
LastUpdatedBy string
UseLatest bool
RHSMEnvironmentCreated bool `json:"rhsm_environment_created" gorm:"column:rhsm_environment_created"`
LastUpdateSnapshotError *string `gorm:"default:null"`
LastUpdateTaskUUID string `json:"last_update_task_uuid" gorm:"default:null"`
LastUpdateTask *TaskInfo `json:"last_update_task" gorm:"foreignKey:last_update_task_uuid"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/pulp_client/pulp_client_mock.go

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

2 changes: 1 addition & 1 deletion pkg/pulp_client/pulp_global_client_mock.go

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

2 changes: 1 addition & 1 deletion pkg/tasks/client/client_mock.go

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

2 changes: 1 addition & 1 deletion pkg/tasks/queue/queue_mock.go

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

52 changes: 36 additions & 16 deletions pkg/tasks/update_template_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,18 @@ func UpdateTemplateContentHandler(ctx context.Context, task *models.TaskInfo, qu
if err != nil {
return err
}

// By creating the environment first, we don't block on pulp
env, err := t.RunEnvironmentCreate()
if err != nil {
return err
}

err = t.RunPulp()
if err != nil {
return err
}
return t.RunCandlepin()
return t.RunCandlepin(env)
}

type UpdateTemplateContent struct {
Expand Down Expand Up @@ -312,11 +319,34 @@ func getRHRepoContentPath(rawURL string) (string, error) {
return u.Path[1 : len(u.Path)-1], nil
}

func (t *UpdateTemplateContent) RunEnvironmentCreate() (*caliri.EnvironmentDTO, error) {
rhContentPath, err := t.pulpClient.GetContentPath(t.ctx)
if err != nil {
return nil, err
}
prefix, err := url.JoinPath(rhContentPath, t.rhDomainName, "templates", t.payload.TemplateUUID)
if err != nil {
return nil, err
}
env, err := t.fetchOrCreateEnvironment(prefix)
if err != nil {
return nil, err
}
if !t.template.RHSMEnvironmentCreated {
err := t.daoReg.Template.SetEnvironmentCreated(t.ctx, t.template.UUID)
if err != nil {
return nil, err
}
}

return env, nil
}

// RunCandlepin creates an environment for the template and content sets for each repository.
// Each content set's URL is the distribution URL created during RunPulp().
// May promote or demote content from the environment, depending on if the repository is being added or removed from template.
// If not created for the given org previously, this will also create a product and pool.
func (t *UpdateTemplateContent) RunCandlepin() error {
func (t *UpdateTemplateContent) RunCandlepin(env *caliri.EnvironmentDTO) error {
var err error

err = t.cpClient.CreateProduct(t.ctx, t.orgId)
Expand Down Expand Up @@ -357,20 +387,6 @@ func (t *UpdateTemplateContent) RunCandlepin() error {
return err
}

rhContentPath, err := t.pulpClient.GetContentPath(t.ctx)
if err != nil {
return err
}
prefix, err := url.JoinPath(rhContentPath, t.rhDomainName, "templates", t.payload.TemplateUUID)
if err != nil {
return err
}

env, err := t.fetchOrCreateEnvironment(prefix)
if err != nil {
return err
}

env, err = t.renameEnvironmentIfNeeded(env)
if err != nil {
return err
Expand Down Expand Up @@ -399,6 +415,10 @@ func (t *UpdateTemplateContent) RunCandlepin() error {
}
}

rhContentPath, err := t.pulpClient.GetContentPath(t.ctx)
if err != nil {
return err
}
overrideDtos, err := t.genOverrideDTOs(rhContentPath)
if err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions test/integration/update_template_content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (s *UpdateTemplateContentSuite) TestCreateCandlepinContent() {
}
tempResp, err := s.dao.Template.Create(ctx, reqTemplate)
assert.NoError(s.T(), err)
assert.False(s.T(), tempResp.RHSMEnvironmentCreated)

host, err := pulp_client.GetPulpClientWithDomain(domainName).GetContentPath(ctx)
require.NoError(s.T(), err)
Expand Down Expand Up @@ -183,6 +184,11 @@ func (s *UpdateTemplateContentSuite) TestCreateCandlepinContent() {
// Update template with new repository
payload := s.updateTemplateContentAndWait(orgID, tempResp.UUID, []string{repo1.UUID})

// Verify environment_created was set
tempResp, err = s.dao.Template.Fetch(ctx, orgID, tempResp.UUID, false)
assert.NoError(s.T(), err)
assert.True(s.T(), tempResp.RHSMEnvironmentCreated)

// Verify correct distribution has been created in pulp
err = s.getRequest(distPath1, identity.Identity{OrgID: repo1.OrgID, Internal: identity.Internal{OrgID: repo1.OrgID}}, 200)
assert.NoError(s.T(), err)
Expand Down

0 comments on commit 9f5e098

Please sign in to comment.