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 #6187 - Write library env destroy code in dynflow #4324

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/controllers/katello/api/v2/environments_controller.rb
Expand Up @@ -132,8 +132,7 @@ def destroy
@environment.destroy
respond_for_destroy
else
fail HttpErrors::BadRequest,
_("Environment %s has a successor. Only the last environment on a path can be deleted.") % @environment.name
fail HttpErrors::BadRequest, @environment.errors.full_messages.join(" ")
end
end

Expand Down
27 changes: 27 additions & 0 deletions app/lib/actions/katello/environment/library_destroy.rb
@@ -0,0 +1,27 @@
#
# Copyright 2014 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

module Actions
module Katello
module Environment
class LibraryDestroy < Actions::EntryAction

# this is what organization destroy uses to remove the org's library
def plan(environment)
environment.library_deletion = true
plan_action(::Actions::Candlepin::Environment::Destroy, cp_id: environment.cp_id)
environment.destroy!
end
end
end
end
end
41 changes: 15 additions & 26 deletions app/models/katello/kt_environment.rb
Expand Up @@ -20,9 +20,7 @@ class KTEnvironment < Katello::Model
include Ext::LabelFromName

# RAILS3458: before_destroys before associations. see http://tinyurl.com/rails3458
before_destroy :is_deletable?
before_destroy :delete_core_environments
before_destroy :delete_default_view_version
before_destroy :is_deletable?, :prepend => true

belongs_to :organization, :class_name => "Organization", :inverse_of => :environments
has_many :activation_keys, :class_name => "Katello::ActivationKey",
Expand All @@ -38,16 +36,16 @@ class KTEnvironment < Katello::Model

has_many :repositories, :class_name => "Katello::Repository", dependent: :destroy, foreign_key: :environment_id
has_many :systems, :class_name => "Katello::System", :inverse_of => :environment,
:dependent => :destroy, :foreign_key => :environment_id
:dependent => :restrict, :foreign_key => :environment_id
has_many :distributors, :class_name => "Katello::Distributor", :inverse_of => :environment,
:dependent => :destroy, :foreign_key => :environment_id
:dependent => :restrict, :foreign_key => :environment_id
has_many :content_view_environments, :class_name => "Katello::ContentViewEnvironment",
:foreign_key => :environment_id, :inverse_of => :environment, :dependent => :destroy
:foreign_key => :environment_id, :inverse_of => :environment, :dependent => :restrict
has_many :content_view_puppet_environments, :class_name => "Katello::ContentViewPuppetEnvironment",
:foreign_key => :environment_id, :inverse_of => :environment, :dependent => :destroy
:foreign_key => :environment_id, :inverse_of => :environment, :dependent => :restrict
has_many :content_view_versions, :through => :content_view_environments, :inverse_of => :environments
has_many :content_views, :through => :content_view_environments, :inverse_of => :environments
has_many :content_view_histories, :class_name => "Katello::ContentViewHistory", :dependent => :destroy,
has_many :content_view_histories, :class_name => "Katello::ContentViewHistory", :dependent => :restrict,
:inverse_of => :environment, :foreign_key => :katello_environment_id

has_many :users, :foreign_key => :default_environment_id, :inverse_of => :default_environment, :dependent => :nullify
Expand Down Expand Up @@ -89,6 +87,8 @@ class KTEnvironment < Katello::Model
scoped_search :on => :name, :complete_value => true
scoped_search :on => :organization_id, :complete_value => true

attr_accessor :library_deletion

def library?
self.library
end
Expand Down Expand Up @@ -150,15 +150,18 @@ def promoting_to?
def is_deletable?
return true if self.organization.nil? || self.organization.being_deleted?

if library?
if library? && !library_deletion
errors.add :base, _("Library environments may not be deleted.")
return false
elsif !successor.nil?
errors.add :base, _("Environment %s has a successor. Only the last environment on a path can be deleted") % self.name
return false
end

return true
if (views = content_views).any?
errors.add(:base, _("Cannot delete '%{env}' due to attached content views: %{views}.") %
{env: name, views: views.map(&:name).to_sentence})
end

return errors.empty?
end

#Unlike path which only gives the path from this environment going forward
Expand Down Expand Up @@ -290,20 +293,6 @@ def available_releases
end
end

def delete_core_environments
# For each content view associated with this lifecycle environment, there may be
# a puppet environment (in the core/Foreman), so let's delete those
self.content_views.each do |content_view|
if foreman_env = Environment.find_by_katello_id(self.organization, self, content_view)
foreman_env.destroy
end
end
end

def delete_default_view_version
self.default_content_view_version.destroy if library?
end

private

def self.humanize_class_name
Expand Down