diff --git a/app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb b/app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb index bfb57b166a1..e94615f01c9 100644 --- a/app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb +++ b/app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb @@ -259,14 +259,7 @@ def server_status def facts User.current = User.anonymous_admin @host.update_candlepin_associations(rhsm_params) - if params[:environments] - new_envs = params[:environments].map do |env| - get_content_view_environment("cp_id", env['id']) - end - new_envs.compact! - Rails.logger.debug "Setting new content view environments for host #{@host.to_label}: #{new_envs.map(&:label)}" - @host.content_facet.content_view_environments = new_envs - end + update_environments_from_facts(params[:environments]) if params[:environments] update_host_registered_through(@host, request.headers) @host.refresh_statuses([::Katello::RhelLifecycleStatus]) render :json => {:content => _("Facts successfully updated.")}, :status => :ok @@ -412,6 +405,18 @@ def find_activation_keys activation_keys end + def update_environments_from_facts(param_environments) + return unless param_environments.present? + new_envs = param_environments.map do |env| + get_content_view_environment("cp_id", env['id']) + end + new_envs.compact! + Rails.logger.debug "Setting new content view environments for host #{@host.to_label}: #{new_envs.map(&:label)}" + @host.content_facet.content_view_environments = new_envs + rescue ::Katello::Errors::MultiEnvironmentNotSupportedError => e + fail HttpErrors::BadRequest, e.message + end + def get_content_view_environment(key, value) cve = nil if value diff --git a/app/models/katello/host/content_facet.rb b/app/models/katello/host/content_facet.rb index 89c4c8d4dfc..00ece328df9 100644 --- a/app/models/katello/host/content_facet.rb +++ b/app/models/katello/host/content_facet.rb @@ -101,6 +101,10 @@ def single_lifecycle_environment end def content_view_environments=(new_cves) + if new_cves.length > 1 && !Setting['allow_multiple_content_views'] + fail ::Katello::Errors::MultiEnvironmentNotSupportedError, + _("Assigning a host to multiple content view environments is not enabled.") + end super(new_cves) Katello::ContentViewEnvironmentContentFacet.reprioritize_for_content_facet(self, new_cves) self.content_view_environments.reload unless self.new_record? diff --git a/lib/katello/plugin.rb b/lib/katello/plugin.rb index 117abb27f6c..924ef320a3d 100644 --- a/lib/katello/plugin.rb +++ b/lib/katello/plugin.rb @@ -363,7 +363,7 @@ def katello_template_setting_values(name) type: :boolean, default: false, full_name: N_('Allow multiple content views'), - description: N_("Allow a host to be registered to multiple content view environments with 'subscription-manager register --environments'.") # TODO: update this description when AKs support this setting as well + description: N_("Allow a host to be assigned to multiple content view environments with 'subscription-manager register --environments' or 'subscription-manager environments --set'.") # TODO: update this description when AKs support this setting as well setting 'content_default_http_proxy', type: :string, diff --git a/test/models/host/content_facet_test.rb b/test/models/host/content_facet_test.rb index 0f262502677..9cf0ed54d27 100644 --- a/test/models/host/content_facet_test.rb +++ b/test/models/host/content_facet_test.rb @@ -63,12 +63,21 @@ def test_in_content_view_version_environments end def test_content_view_environments= + Setting['allow_multiple_content_views'] = true Katello::ContentViewEnvironmentContentFacet.expects(:reprioritize_for_content_facet).twice content_facet.content_view_environments.reload content_facet.content_view_environments = [katello_content_view_environments(:library_dev_view_dev), katello_content_view_environments(:library_dev_staging_view_dev)] assert_equal 2, content_facet.content_view_environments.length end + def test_multi_cv_not_enabled + Setting['allow_multiple_content_views'] = false + assert_equal 1, content_facet.content_view_environments.length + assert_raises(::Katello::Errors::MultiEnvironmentNotSupportedError) do + content_facet.content_view_environments = [katello_content_view_environments(:library_dev_view_dev), katello_content_view_environments(:library_dev_staging_view_dev)] + end + end + def test_audit_for_content_facet org = taxonomies(:empty_organization) host1 = ::Host::Managed.create!(:name => 'foohost.example.com', :managed => false, :organization_id => org.id) @@ -441,6 +450,10 @@ def setup assert host_one end + def teardown + Setting['allow_multiple_content_views'] = false + end + def test_content_view_search assert_includes ::Host::Managed.search_for("content_view = \"#{view.name}\""), host end