Skip to content

Commit

Permalink
Fixes #36690 - Validate *all* products before saving to sync plans
Browse files Browse the repository at this point in the history
  • Loading branch information
sjha4 committed Aug 21, 2023
1 parent 05b73f7 commit fe45b5c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/controllers/katello/api/v2/sync_plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def destroy
param :id, String, :desc => N_("ID of the sync plan"), :required => true
param :product_ids, Array, :desc => N_("List of product ids to add to the sync plan"), :required => true
def add_products
@sync_plan.validate_and_update_products force_update: true
products = ::Katello::Product.where(:id => params[:product_ids]).editable
@sync_plan.product_ids = (@sync_plan.product_ids + products.collect { |p| p.id }).uniq
@sync_plan.save!
Expand All @@ -103,6 +104,7 @@ def add_products
param :id, String, :desc => N_("ID of the sync plan"), :required => true
param :product_ids, Array, :desc => N_("List of product ids to remove from the sync plan"), :required => true
def remove_products
@sync_plan.validate_and_update_products force_update: true
products = ::Katello::Product.where(:id => params[:product_ids]).editable
@sync_plan.product_ids = (@sync_plan.product_ids - products.collect { |p| p.id }).uniq
@sync_plan.save!
Expand Down
20 changes: 20 additions & 0 deletions app/models/katello/sync_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,25 @@ def rec_logic_changed?
def cron_status_mismatch?
self.interval != CUSTOM_CRON && !(self.cron_expression.nil? || self.cron_expression.eql?(''))
end

def validate_and_update_products(force_update: false)
sync_plan_products = Product.where(id: self.product_ids).select { |p| p.enabled? }
if sync_plan_products.length == self.product_ids.length
Rails.logger.info "Successfully validated presence of enabled sync plan products!"
return
end
sync_plan_product_ids = sync_plan_products.pluck(:id)
if sync_plan_product_ids.length < self.product_ids.length
missing_ids = self.product_ids - sync_plan_product_ids
Rails.logger.warn "Sync plan products with following ids are either disabled or don't exist: #{missing_ids}"
if force_update
Rails.logger.info "Updating sync plan with valid and enabled product ids: #{sync_plan_product_ids}"
self.product_ids = sync_plan_product_ids
self.save!
else
Rails.logger.warn "Some sync plan products are invalid/disabled. Please run validate_and_update_products(force_update: true) on the sync_plan from `foreman-rake console`"
end
end
end
end
end

0 comments on commit fe45b5c

Please sign in to comment.