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 Sep 8, 2023
1 parent 05b73f7 commit 562b8f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/controllers/katello/api/v2/sync_plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Api::V2::SyncPlansController < Api::V2::ApiController
:add_products, :remove_products]
before_action :set_organization, :only => [:update, :show, :destroy, :sync,
:add_products, :remove_products]
before_action :validate_sync_plan_products, :only => [:update, :add_products, :remove_products]

def_param_group :sync_plan do
param :name, String, :desc => N_("sync plan name"), :required => true, :action_aware => true
Expand Down Expand Up @@ -127,5 +128,9 @@ def sync_plan_params
def set_organization
@organization ||= @sync_plan.try(:organization)
end

def validate_sync_plan_products
@sync_plan.validate_and_update_products force_update: true
end
end
end
17 changes: 17 additions & 0 deletions app/models/katello/sync_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,22 @@ 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? }
return if sync_plan_products.length == self.product_ids.length
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 562b8f6

Please sign in to comment.