Skip to content

Commit 754bd15

Browse files
committed
RHINENG-15506: use candlepin bulk API
1 parent c561311 commit 754bd15

6 files changed

Lines changed: 37 additions & 51 deletions

File tree

base/candlepin/candlepin.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ import (
1212
log "github.com/sirupsen/logrus"
1313
)
1414

15-
type ConsumersUpdateRequest struct {
16-
Environments []ConsumersUpdateEnvironment `json:"environments"`
17-
}
18-
19-
type ConsumersUpdateEnvironment struct {
20-
ID string `json:"id"`
15+
type ConsumersEnvironmentsRequest struct {
16+
ConsumerUuids []string `json:"consumerUuids"`
17+
EnvironmentIDs []string `json:"environmentIds"`
2118
}
2219

2320
type ConsumersUpdateResponse struct {

base/utils/gin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
const (
1616
KeyApiver = "apiver"
1717
KeyAccount = "account"
18+
KeyOrgID = "org_id"
1819
KeyUser = "user"
1920
KeySystem = "system_cn"
2021
KeyInventoryGroups = "inventoryGroups"

manager/controllers/template_subscribed_systems_update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ func TemplateSubscribedSystemsUpdateHandler(c *gin.Context) {
4646
return
4747
}
4848

49-
modified, err := assignCandlepinEnvironment(c, db, account, &template.EnvironmentID, systemList, nil)
49+
err = assignCandlepinEnvironment(c, db, account, &template.EnvironmentID, systemList, nil)
5050
if err != nil {
5151
return
5252
}
5353

54-
err = assignTemplateSystems(c, db, account, template, modified)
54+
err = assignTemplateSystems(c, db, account, template, systemList)
5555
if err != nil {
5656
return
5757
}
5858

5959
// re-evaluate systems added/removed from templates
6060
if config.EnableTemplateChangeEval {
61-
inventoryAIDs := kafka.InventoryIDs2InventoryAIDs(account, modified)
61+
inventoryAIDs := kafka.InventoryIDs2InventoryAIDs(account, systemList)
6262
kafka.EvaluateBaselineSystems(inventoryAIDs)
6363
}
6464
c.Status(http.StatusOK)

manager/controllers/template_systems_delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ func TemplateSystemsDeleteHandler(c *gin.Context) {
3939
return
4040
}
4141

42-
modified, err := assignCandlepinEnvironment(c, db, account, nil, req.Systems, groups)
42+
err = assignCandlepinEnvironment(c, db, account, nil, req.Systems, groups)
4343
if err != nil {
4444
return
4545
}
4646

4747
// unassign system from template => assign NULL as template_id
48-
err = assignTemplateSystems(c, db, account, nil, modified)
48+
err = assignTemplateSystems(c, db, account, nil, req.Systems)
4949
if err != nil {
5050
return
5151
}

manager/controllers/template_systems_update.go

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ func TemplateSystemsUpdateHandler(c *gin.Context) {
7373
return
7474
}
7575

76-
modified, err := assignCandlepinEnvironment(c, db, account, &template.EnvironmentID, req.Systems, groups)
76+
err = assignCandlepinEnvironment(c, db, account, &template.EnvironmentID, req.Systems, groups)
7777
if err != nil {
7878
return
7979
}
8080

81-
err = assignTemplateSystems(c, db, account, template, modified)
81+
err = assignTemplateSystems(c, db, account, template, req.Systems)
8282
if err != nil {
8383
return
8484
}
@@ -187,9 +187,9 @@ func templateArchVersionMatch(
187187
return err
188188
}
189189

190-
func callCandlepin(ctx context.Context, consumer string, request *candlepin.ConsumersUpdateRequest) (
190+
func callCandlepin(ctx context.Context, owner string, request *candlepin.ConsumersEnvironmentsRequest) (
191191
*candlepin.ConsumersUpdateResponse, error) {
192-
candlepinEnvConsumersURL := utils.CoreCfg.CandlepinAddress + "/consumers/" + consumer
192+
candlepinEnvConsumersURL := utils.CoreCfg.CandlepinAddress + "/owner/" + owner + "/consumers/environments"
193193
candlepinFunc := func() (interface{}, *http.Response, error) {
194194
candlepinResp := candlepin.ConsumersUpdateResponse{}
195195
resp, err := candlepinClient.Request(&ctx, http.MethodPut, candlepinEnvConsumersURL, request, &candlepinResp)
@@ -207,70 +207,57 @@ func callCandlepin(ctx context.Context, consumer string, request *candlepin.Cons
207207
candlepinRespPtr, err := utils.HTTPCallRetry(candlepinFunc,
208208
candlepin.CandlepinExpRetries, candlepin.CandlepinRetries, http.StatusServiceUnavailable)
209209
if err != nil {
210-
return nil, errors.Wrap(err, "candlepin /consumers call failed")
210+
return nil, errors.Wrap(err, "candlepin call "+candlepinEnvConsumersURL+" failed")
211211
}
212212
return candlepinRespPtr.(*candlepin.ConsumersUpdateResponse), nil
213213
}
214214

215215
func assignCandlepinEnvironment(c *gin.Context, db *gorm.DB, accountID int, env *string, inventoryIDs []string,
216-
groups map[string]string) ([]string, error) {
217-
var assignedIDs []string
218-
var consumers = []struct {
216+
groups map[string]string) error {
217+
var hosts = []struct {
219218
InventoryID string
220219
Consumer *string
221220
}{}
222221

223222
err := database.Systems(db, accountID, groups).
224223
Select("ih.id as inventory_id, ih.system_profile->>'owner_id' as consumer").
225-
Where("ih.id in (?)", inventoryIDs).Find(&consumers).Error
224+
Where("ih.id in (?)", inventoryIDs).Find(&hosts).Error
226225
if err != nil {
227226
LogAndRespError(c, err, "Database error")
228-
return nil, err
227+
return err
229228
}
230229

231230
// check if all systems have owner_id
232-
for _, consumer := range consumers {
233-
if consumer.Consumer == nil {
234-
err = errors2.Join(err, errors.Errorf("'%s'", consumer.InventoryID))
231+
consumers := make([]string, 0, len(hosts))
232+
for _, host := range hosts {
233+
if host.Consumer == nil {
234+
err = errors2.Join(err, errors.Errorf("'%s'", host.InventoryID))
235+
continue
235236
}
237+
consumers = append(consumers, *host.Consumer)
236238
}
237239
if err != nil {
238240
err = errors2.Join(errors.New("missing owner_id for systems"), err)
239241
LogAndRespBadRequest(c, err, err.Error())
240-
return nil, err
242+
return err
241243
}
242244

243-
environments := []candlepin.ConsumersUpdateEnvironment{}
245+
environments := []string{}
244246
if env != nil {
245-
environments = []candlepin.ConsumersUpdateEnvironment{{ID: *env}}
247+
environments = []string{*env}
246248
}
247-
updateReq := candlepin.ConsumersUpdateRequest{
248-
Environments: environments,
249+
updateReq := candlepin.ConsumersEnvironmentsRequest{
250+
EnvironmentIDs: environments,
251+
ConsumerUuids: consumers,
249252
}
250-
for _, consumer := range consumers {
251-
resp, apiErr := callCandlepin(base.Context, *consumer.Consumer, &updateReq)
252-
// check response
253-
if apiErr != nil {
254-
if resp == nil {
255-
resp = &candlepin.ConsumersUpdateResponse{Message: "call to candlepin failed"}
256-
}
257-
err = errors2.Join(err, apiErr, errors.New(resp.Message))
258-
} else {
259-
assignedIDs = append(assignedIDs, consumer.InventoryID)
260-
}
261-
}
262-
253+
resp, err := callCandlepin(base.Context, c.GetString(utils.KeyOrgID), &updateReq)
254+
// check response
263255
if err != nil {
264-
// we do not want to fail whole API if a single call to candlepin fails
265-
// just log the error
266-
utils.LogWarn(err)
267-
if len(assignedIDs) == 0 {
268-
// fail if none of the systems could be assigned to a template
269-
LogAndRespStatusError(c, http.StatusFailedDependency, err, "candlepin call failed, no systems assigned")
270-
return nil, err
271-
}
256+
LogAndRespBadRequest(c, err, resp.Message)
257+
return err
272258
}
273-
return assignedIDs, nil
259+
260+
return nil
274261
}
275262

276263
func checkInventoryIDs(db *gorm.DB, accountID int, inventoryIDs []string, groups map[string]string) (err error) {

manager/middlewares/authentication.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func findAccount(c *gin.Context, orgID string) bool {
7474
AccountIDCache.Values[orgID] = accID
7575
c.Set(utils.KeyAccount, accID)
7676
}
77+
c.Set(utils.KeyOrgID, orgID)
7778
return true
7879
}
7980

0 commit comments

Comments
 (0)