diff --git a/app/models/katello/concerns/smart_proxy_extensions.rb b/app/models/katello/concerns/smart_proxy_extensions.rb index 3761f4110e6..75ad02e51e5 100644 --- a/app/models/katello/concerns/smart_proxy_extensions.rb +++ b/app/models/katello/concerns/smart_proxy_extensions.rb @@ -33,6 +33,7 @@ def refresh before_create :associate_default_locations before_create :associate_lifecycle_environments before_validation :set_default_download_policy + after_update :refresh_smart_proxy_sync_histories lazy_accessor :pulp_repositories, :initializer => lambda { |_s| pulp_node.extensions.repository.retrieve_all } @@ -471,6 +472,9 @@ def add_lifecycle_environment(environment) end def remove_lifecycle_environment(environment) + smart_proxy_helper = ::Katello::SmartProxyHelper.new(self) + repos = smart_proxy_helper.repositories_available_to_capsule(environment) + self.clear_smart_proxy_sync_histories(repos) unless repos.empty? self.lifecycle_environments.find(environment.id) unless self.lifecycle_environments.destroy(environment) fail _("Could not remove the lifecycle environment from the smart proxy") @@ -479,6 +483,17 @@ def remove_lifecycle_environment(environment) raise _("Lifecycle environment was not attached to the smart proxy; therefore, no changes were made.") end + def refresh_smart_proxy_sync_histories + smart_proxy_helper = ::Katello::SmartProxyHelper.new(self) + repos = smart_proxy_helper.repositories_available_to_capsule + repo_ids = repos.map(&:id) + if repo_ids.empty? + self.smart_proxy_sync_histories.delete_all + else + self.smart_proxy_sync_histories.where("repository_id NOT IN (?)", repo_ids).delete_all + end + end + def available_lifecycle_environments(organization_id = nil) scope = Katello::KTEnvironment.not_in_capsule(self) scope = scope.where(organization_id: organization_id) if organization_id