-
Notifications
You must be signed in to change notification settings - Fork 25
/
update_repository.go
143 lines (129 loc) · 4.42 KB
/
update_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package tasks
import (
"context"
"encoding/json"
"fmt"
caliri "github.com/content-services/caliri/release/v4"
"github.com/content-services/content-sources-backend/pkg/api"
"github.com/content-services/content-sources-backend/pkg/candlepin_client"
"github.com/content-services/content-sources-backend/pkg/config"
"github.com/content-services/content-sources-backend/pkg/dao"
"github.com/content-services/content-sources-backend/pkg/db"
"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/tasks/queue"
)
type UpdateRepositoryPayload struct {
RepositoryConfigUUID string
}
type UpdateRepository struct {
orgID string
domainName string
ctx context.Context
payload *UpdateRepositoryPayload
task *models.TaskInfo
daoReg *dao.DaoRegistry
repoConfig api.RepositoryResponse
cpClient candlepin_client.CandlepinClient
pulpClient pulp_client.PulpClient
}
func UpdateRepositoryHandler(ctx context.Context, task *models.TaskInfo, _ *queue.Queue) error {
if config.Get().Clients.Candlepin.Server == "" {
return nil
}
opts := UpdateRepositoryPayload{}
if err := json.Unmarshal(task.Payload, &opts); err != nil {
return fmt.Errorf("payload incorrect type for " + config.UpdateRepositoryTask)
}
logger := LogForTask(task.Id.String(), task.Typename, task.RequestID)
ctxWithLogger := logger.WithContext(ctx)
daoReg := dao.GetDaoRegistry(db.DB)
domainName, err := daoReg.Domain.Fetch(ctxWithLogger, task.OrgId)
if err != nil {
return err
}
pulpClient := pulp_client.GetPulpClientWithDomain(domainName)
cpClient := candlepin_client.NewCandlepinClient()
repo, err := daoReg.RepositoryConfig.Fetch(ctx, task.OrgId, opts.RepositoryConfigUUID)
if err != nil {
return fmt.Errorf("could not fetch repository config %w", err)
}
ur := UpdateRepository{
daoReg: daoReg,
payload: &opts,
task: task,
ctx: ctx,
domainName: domainName,
cpClient: cpClient,
orgID: task.OrgId,
repoConfig: repo,
pulpClient: pulpClient,
}
return ur.Run()
}
func (ur *UpdateRepository) Run() error {
content, err := ur.cpClient.FetchContent(ur.ctx, ur.orgID, ur.repoConfig.UUID)
if err != nil {
return err
}
if content == nil {
// Content may have not been created yet, if not we don't need to do anything
return nil
}
if err := ur.UpdateCPContent(*content); err != nil {
if err != nil {
return fmt.Errorf("could not udpate GPG Key: %w", err)
}
}
if err := ur.UpdateContentOverrides(); err != nil {
if err != nil {
return fmt.Errorf("could not update environment overrides: %w", err)
}
}
return nil
}
// UpdateCPContent updates the content in candlepin (name & GPG Key)
func (ur *UpdateRepository) UpdateCPContent(content caliri.ContentDTO) error {
expected := GenContentDto(ur.repoConfig)
err := ur.cpClient.UpdateContent(ur.ctx, ur.orgID, ur.repoConfig.UUID, expected)
if err != nil {
return fmt.Errorf("could not update repository for gpg key update: %w", err)
}
return nil
}
func (ur *UpdateRepository) UpdateContentOverrides() error {
templates, _, err := ur.daoReg.Template.List(ur.ctx, ur.orgID, api.PaginationData{Limit: -1}, api.TemplateFilterData{
RepositoryUUIDs: []string{ur.repoConfig.UUID},
})
if err != nil {
return fmt.Errorf("could not list templates for repo config %w", err)
}
for _, template := range templates.Data {
existingOverrides, err := ur.cpClient.FetchContentOverridesForRepo(ur.ctx, template.UUID, ur.repoConfig.Label)
if err != nil {
return fmt.Errorf("could not list content for template & repo config %w", err)
}
path, err := ur.pulpClient.GetContentPath(ur.ctx)
if err != nil {
return err
}
expected, err := ContentOverridesForRepo(ur.orgID, ur.domainName, template.UUID, path, ur.repoConfig)
if err != nil {
return fmt.Errorf("could not generate overrides %w", err)
}
unneeded := UnneededOverrides(existingOverrides, expected)
if len(unneeded) > 0 {
err = ur.cpClient.RemoveContentOverrides(ur.ctx, candlepin_client.GetEnvironmentID(template.UUID), unneeded)
if err != nil {
return fmt.Errorf("could not remove overrides %w", err)
}
}
if len(expected) > 0 {
err = ur.cpClient.UpdateContentOverrides(ur.ctx, candlepin_client.GetEnvironmentID(template.UUID), expected)
if err != nil {
return fmt.Errorf("could not update overrides %w", err)
}
}
}
return nil
}