Skip to content

Commit

Permalink
Fixes #37137 - Use insert_all when creating available module streams (#…
Browse files Browse the repository at this point in the history
…10878)

* Fixes #37137 - Use insert_all when creating available module streams
  • Loading branch information
jeremylenz authored Feb 28, 2024
1 parent 99c7857 commit 7458f70
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
63 changes: 45 additions & 18 deletions app/models/katello/concerns/host_managed_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,40 +325,67 @@ def import_enabled_repositories(repos)
content_facet.update_repositories_by_paths(paths.compact)
end

def import_module_streams(module_streams)
streams = {}
module_streams.each do |module_stream|
stream = AvailableModuleStream.create_or_find_by!(name: module_stream["name"],
context: module_stream["context"],
stream: module_stream["stream"])
streams[stream.id] = module_stream
def available_module_stream_id_from(name:, stream:, context:)
@indexed_available_module_streams ||= Katello::AvailableModuleStream.all.index_by do |available_module_stream|
"#{available_module_stream.name}-#{available_module_stream.stream}-#{available_module_stream.context}"
end
sync_available_module_stream_associations(streams)
@indexed_available_module_streams["#{name}-#{stream}-#{context}"]&.id
end

def import_module_streams(module_streams)
# module_streams looks like this
# {"name"=>"389-ds", "stream"=>"1.4", "version"=>"8030020201203210520", "context"=>"e114a9e7", "arch"=>"x86_64", "profiles"=>[], "installed_profiles"=>[], "status"=>"default", "active"=>false}
streams = module_streams.map do |module_stream|
{
name: module_stream["name"],
stream: module_stream["stream"],
context: module_stream["context"]
}
end
if streams.any?
AvailableModuleStream.insert_all(
streams,
unique_by: %w[name stream context],
returning: %w[id name stream context]
)
end
indexed_module_streams = module_streams.index_by do |module_stream|
available_module_stream_id_from(
name: module_stream["name"],
stream: module_stream["stream"],
context: module_stream["context"]
)
end
sync_available_module_stream_associations(indexed_module_streams)
end

def sync_available_module_stream_associations(new_available_module_streams)
upgradable_streams = self.host_available_module_streams.where(:available_module_stream_id => new_available_module_streams.keys)
new_associated_ids = new_available_module_streams.keys.compact
upgradable_streams = self.host_available_module_streams.where(:available_module_stream_id => new_associated_ids)
old_associated_ids = self.available_module_stream_ids
delete_ids = old_associated_ids - new_available_module_streams.keys
delete_ids = old_associated_ids - new_associated_ids

if delete_ids.any?
self.host_available_module_streams.where(:available_module_stream_id => delete_ids).delete_all
end

new_ids = new_available_module_streams.keys - old_associated_ids
new_ids.each do |new_id|
new_ids = new_associated_ids - old_associated_ids

hams_to_create = new_ids.map do |new_id|
module_stream = new_available_module_streams[new_id]
status = module_stream["status"]
# Set status to "unknown" only if the active field is in use and set to false and the module is enabled
if enabled_module_stream_inactive?(module_stream)
status = "unknown"
end
self.host_available_module_streams.create!(host_id: self.id,
available_module_stream_id: new_id,
installed_profiles: module_stream["installed_profiles"],
status: status)
end

{
host_id: self.id,
available_module_stream_id: new_id,
installed_profiles: module_stream["installed_profiles"],
status: status
}
end
HostAvailableModuleStream.insert_all(hams_to_create) if hams_to_create.any?
upgradable_streams.each do |hams|
module_stream = new_available_module_streams[hams.available_module_stream_id]
shared_keys = hams.attributes.keys & module_stream.keys
Expand Down
5 changes: 5 additions & 0 deletions test/models/concerns/host_managed_extensions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ def test_import_modules_with_active_field
assert_equal 2, @foreman_host.host_available_module_streams.unknown.count
end

def clear_cached_available_module_streams
@foreman_host.instance_variable_set(:@indexed_available_module_streams, nil)
end

def test_import_modules_with_update
modules_json = [make_module_json("enabled21111", "enabled")]
prior_count = HostAvailableModuleStream.count
Expand All @@ -384,6 +388,7 @@ def test_import_modules_with_update
assert_empty @foreman_host.reload.host_available_module_streams
assert_equal prior_count, HostAvailableModuleStream.count

clear_cached_available_module_streams
@foreman_host.import_module_streams([make_module_json("xxxx", "enabled", 'blah', ["default"])])
assert_equal "enabled", @foreman_host.reload.host_available_module_streams.first.status
assert_equal ["default"], @foreman_host.reload.host_available_module_streams.first.installed_profiles
Expand Down

0 comments on commit 7458f70

Please sign in to comment.