Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #37137 - Use insert_all when creating available module streams #10878

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]
ianballou marked this conversation as resolved.
Show resolved Hide resolved
}
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
Loading