Skip to content

Commit

Permalink
Fixes #36897 - Hosts now follow existing facet standard for hostgroup…
Browse files Browse the repository at this point in the history
… inheritance

This is a complicated topic; more info in Katello PR #10841. Added logic to have host edit form always return values for CV and LE, regardless of whether they can be edited.
  • Loading branch information
qcjames53 committed Feb 9, 2024
1 parent 45be62a commit 5508acb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 65 deletions.

This file was deleted.

22 changes: 0 additions & 22 deletions app/models/katello/concerns/host_managed_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,6 @@ def inherited_attributes
inherited_attrs
end

def apply_inherited_attributes(attributes, initialized = true)
attributes = super(attributes, initialized) || {}
facet_attrs = attributes&.[]('content_facet_attributes')
return attributes if facet_attrs.blank?
cv_id = facet_attrs['content_view_id']
lce_id = facet_attrs['lifecycle_environment_id']
if initialized && (cv_id.blank? || lce_id.blank?)
if cv_id.blank?
Rails.logger.info "Hostgroup has no content view assigned; using host's existing content view"
facet_attrs['content_view_id'] = content_facet&.single_content_view&.id
end
if lce_id.blank?
Rails.logger.info "Hostgroup has no lifecycle environment assigned; using host's existing lifecycle environment"
facet_attrs['lifecycle_environment_id'] = content_facet&.single_lifecycle_environment&.id
end
attributes['content_facet_attributes'] = facet_attrs
else
Rails.logger.debug "Hostgroup has content view and lifecycle environment assigned; using those"
end
attributes
end

def smart_proxy_ids
ids = super
ids << content_source_id
Expand Down
4 changes: 3 additions & 1 deletion app/models/katello/concerns/hostgroup_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ def inherited_ancestry_attribute(attribute, facet)
# it will return the value 2.
facet_model = Facets.registered_facets[facet].hostgroup_configuration.model
value = facet_model.where.not(attribute => nil).joins(:hostgroup).merge(
::Hostgroup.where(id: self.ancestor_ids).reorder(ancestry: :desc)
::Hostgroup.where(id: self.ancestor_ids).reorder(
"#{::Hostgroup.table_name}.ancestry desc nulls last"
)
).pick(attribute)
end
value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,27 @@ end %>
<% env_select_name = using_hostgroups_page? ? 'hostgroup[lifecycle_environment_id]' : 'host[content_facet_attributes][lifecycle_environment_id]' %>
<% env_select_attr = using_hostgroups_page? ? 'lifecycle_environment' : 'content_facet.single_lifecycle_environment' %>

<%= field(f, env_select_attr, {:label => _("Lifecycle Environment")}) do
if using_hostgroups_page?
select_tag env_select_id, lifecycle_environment_options(@hostgroup, :include_blank => blank_or_inherit_with_id(f, :lifecycle_environment)),
:class => 'form-control', :name => env_select_name
else
select_tag env_select_id, lifecycle_environment_options(@host, :selected_host_group => @hostgroup || @host.hostgroup, :include_blank => blank_or_inherit_with_id(f, :lifecycle_environment)), :class => 'form-control', :name => env_select_name, :disabled => cv_lce_disabled?
end
end %>
<%= field(f, env_select_attr, {:label => _("Lifecycle Environment")}) do %>
<% if using_hostgroups_page? %>
<%= select_tag env_select_id, lifecycle_environment_options(@hostgroup, :include_blank => blank_or_inherit_with_id(f, :lifecycle_environment)), :class => 'form-control', :name => env_select_name %>
<% elsif cv_lce_disabled? %>
<%= hidden_field_tag 'host[content_facet_attributes][lifecycle_environment_id]', fetch_lifecycle_environment(@host).try(:id) %>
<%= select_tag env_select_id, lifecycle_environment_options(@host, :selected_host_group => @hostgroup || @host.hostgroup, :include_blank => blank_or_inherit_with_id(f, :lifecycle_environment)), :class => 'form-control', :name => env_select_name, :disabled => true %>
<% else %>
<%= select_tag env_select_id, lifecycle_environment_options(@host, :selected_host_group => @hostgroup || @host.hostgroup, :include_blank => blank_or_inherit_with_id(f, :lifecycle_environment)), :class => 'form-control', :name => env_select_name %>
<% end %>
<% end %>

<% cv_select_id = using_hostgroups_page? ? :hostgroup_content_view_id : :host_content_view_id %>
<% cv_select_name = using_hostgroups_page? ? 'hostgroup[content_view_id]' : 'host[content_facet_attributes][content_view_id]' %>
<% cv_select_attr = using_hostgroups_page? ? 'content_view' : 'content_facet.single_content_view' %>
<%= field(f, cv_select_attr, {:label => _("Content View")}) do
if using_hostgroups_page?
select_tag cv_select_id, content_views_for_host(@hostgroup, :include_blank => blank_or_inherit_with_id(f, :content_view)), :data => {"spinner_path" => spinner_path},
:class => 'form-control', :name => cv_select_name
else
select_tag cv_select_id, content_views_for_host(@host, :selected_host_group => @hostgroup || @host.hostgroup, :include_blank => blank_or_inherit_with_id(f, :content_view)), :data => {"spinner_path" => spinner_path}, :class => 'form-control', :name => cv_select_name, :disabled => cv_lce_disabled?
end
end %>
<%= field(f, cv_select_attr, {:label => _("Content View")}) do %>
<% if using_hostgroups_page? %>
<%= select_tag cv_select_id, content_views_for_host(@hostgroup, :include_blank => blank_or_inherit_with_id(f, :content_view)), :data => {"spinner_path" => spinner_path}, :class => 'form-control', :name => cv_select_name %>
<% elsif cv_lce_disabled? %>
<%= hidden_field_tag 'host[content_facet_attributes][content_view_id]', fetch_content_view(@host).try(:id) %>
<%= select_tag cv_select_id, content_views_for_host(@host, :selected_host_group => @hostgroup || @host.hostgroup, :include_blank => blank_or_inherit_with_id(f, :content_view)), :data => {"spinner_path" => spinner_path}, :class => 'form-control', :name => cv_select_name, :disabled => true %>
<% else %>
<%= select_tag cv_select_id, content_views_for_host(@host, :selected_host_group => @hostgroup || @host.hostgroup, :include_blank => blank_or_inherit_with_id(f, :content_view)), :data => {"spinner_path" => spinner_path}, :class => 'form-control', :name => cv_select_name %>
<% end %>
<% end %>
2 changes: 0 additions & 2 deletions lib/katello/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ class Engine < ::Rails::Engine

#Controller extensions
::HostsController.include Katello::Concerns::HostsControllerExtensions
::HostsController.include Katello::Concerns::ContentFacetHostsControllerExtensions
::SmartProxiesController.include Katello::Concerns::SmartProxiesControllerExtensions
::RegistrationCommandsController.prepend Katello::Concerns::RegistrationCommandsControllerExtensions

Expand Down Expand Up @@ -194,7 +193,6 @@ class Engine < ::Rails::Engine

#Api controller extensions
::Api::V2::HostsController.include Katello::Concerns::Api::V2::HostsControllerExtensions
::Api::V2::HostsController.include Katello::Concerns::ContentFacetHostsControllerExtensions
::Api::V2::HostsBulkActionsController.include Katello::Concerns::Api::V2::HostsBulkActionsControllerExtensions
::Api::V2::HostgroupsController.include Katello::Concerns::Api::V2::HostgroupsControllerExtensions
::Api::V2::SmartProxiesController.include Katello::Concerns::Api::V2::SmartProxiesControllerExtensions
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/api/v2/hosts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_host_update_with_env_only
:lifecycle_environment_id => @dev.id
}
}, session: set_session_user
assert_response :error
assert_response 422
end

def test_host_update_with_cv_only
Expand All @@ -92,7 +92,7 @@ def test_host_update_with_cv_only
:content_view_id => @cv2.id
}
}, session: set_session_user
assert_response :error
assert_response 422
end

def test_host_update_with_invalid_env
Expand Down

0 comments on commit 5508acb

Please sign in to comment.