@@ -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
215215func 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
276263func checkInventoryIDs (db * gorm.DB , accountID int , inventoryIDs []string , groups map [string ]string ) (err error ) {
0 commit comments