Skip to content

Commit 4943f41

Browse files
committed
Fixes #38270 - Don't create content view environment when CV is not published to LCE
1 parent 83498c4 commit 4943f41

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

app/lib/katello/errors.rb

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class InvalidRepositoryTypeError < StandardError; end
1414

1515
class MultiEnvironmentNotSupportedError < StandardError; end
1616

17+
class ContentViewEnvironmentError < StandardError; end
18+
1719
# unauthorized access
1820
class SecurityViolation < StandardError; end
1921

app/models/katello/content_view_environment.rb

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ContentViewEnvironment < Katello::Model
2121
validates_lengths_from_database
2222
validates :environment_id, uniqueness: {scope: :content_view_id}, presence: true
2323
validates :content_view_id, presence: true
24+
validates :content_view_version_id, presence: true
2425
validates_with Validators::ContentViewEnvironmentOrgValidator
2526
validates_with Validators::ContentViewEnvironmentCoherentDefaultValidator
2627

app/models/katello/host/content_facet.rb

+9-4
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def content_view_environment_labels
149149
end
150150

151151
# rubocop:disable Metrics/CyclomaticComplexity
152+
# rubocop:disable Metrics/PerceivedComplexity
152153
def assign_single_environment(
153154
content_view_id: nil, lifecycle_environment_id: nil, environment_id: nil,
154155
content_view: nil, lifecycle_environment: nil, environment: nil
@@ -165,11 +166,15 @@ def assign_single_environment(
165166
end
166167

167168
content_view_environment = ::Katello::ContentViewEnvironment
168-
.where(:content_view_id => content_view_id, :environment_id => lifecycle_environment_id)
169-
.first_or_create do |cve|
170-
Rails.logger.info("ContentViewEnvironment not found for content view '#{cve.content_view_name}' and environment '#{cve.environment&.name}'; creating a new one.")
169+
.find_by(:content_view_id => content_view_id, :environment_id => lifecycle_environment_id)
170+
if content_view_environment.nil?
171+
cv_name = ::Katello::ContentView.find_by(:id => content_view_id)&.name
172+
fail ::Katello::Errors::ContentViewEnvironmentError, _("Unable to find a content view with ID %s") % content_view_id if cv_name.nil?
173+
env_name = ::Katello::KTEnvironment.find_by(:id => lifecycle_environment_id)&.name
174+
fail ::Katello::Errors::ContentViewEnvironmentError, _("Unable to find a lifecycle environment with ID %s") % lifecycle_environment_id if env_name.nil?
175+
hypothetical_cve_label = "%s/%s" % [env_name, cv_name]
176+
fail ::Katello::Errors::ContentViewEnvironmentError, _("Cannot assign content view environment %s: The content view has either not been published or has not been promoted to that lifecycle environment.") % hypothetical_cve_label
171177
end
172-
fail _("Unable to create ContentViewEnvironment. Check the logs for more information.") if content_view_environment.nil?
173178

174179
self.content_view_environments = [content_view_environment]
175180
end

0 commit comments

Comments
 (0)