Project

General

Profile

« Previous | Next » 

Revision 8c68024a

Added by Daniel Lobato Garcia over 10 years ago

fixes #2231 - hostgroup deletion is restricted to hostgroups without children

View differences:

app/controllers/api/v1/hostgroups_controller.rb
param :id, :identifier, :required => true
def destroy
process_response @hostgroup.destroy
if @hostgroup.has_children?
render :json => {'message'=> _("Cannot delete group %{current} because it has nested groups.") % { :current => @hostgroup.label } }, :status => :conflict
else
process_response @hostgroup.destroy
end
end
end
app/controllers/api/v2/hostgroups_controller.rb
param :id, :identifier, :required => true
def destroy
process_response @hostgroup.destroy
if @hostgroup.has_children?
render :json => {'message'=> _("Cannot delete group %{current} because it has nested groups.") % { :current => @hostgroup.label } }, :status => :conflict
else
process_response @hostgroup.destroy
end
end
end
app/controllers/hostgroups_controller.rb
end
def destroy
if @hostgroup.destroy
process_success
else
load_vars_for_ajax
begin
if @hostgroup.destroy
process_success
else
load_vars_for_ajax
process_error
end
rescue Ancestry::AncestryException
flash[:error] = _("Cannot delete group %{current} because it has nested groups.") % { :current => @hostgroup.label }
process_error
end
end
app/helpers/hostgroups_helper.rb
msg = [_("Are you sure?")]
if group.has_children?
msg << _("This group has nested groups!") + "\n"
msg << _("Deleting this group will unlink its nested groups and any associated puppet classes and / or parameters")
msg << _("Please delete all nested groups before deleting it.")
end
msg.join("\n")
end
app/models/hostgroup.rb
class Hostgroup < ActiveRecord::Base
has_ancestry :orphan_strategy => :rootify
has_ancestry :orphan_strategy => :restrict
include Authorization
include Taxonomix
include HostCommon
test/functional/api/v1/hostgroups_controller_test.rb
assert_response :success
end
test "blocks API deletion of hosts with children" do
assert hostgroups(:parent).has_children?
assert_no_difference('Hostgroup.count') do
delete :destroy, { :id => hostgroups(:parent).to_param }
end
assert_response :conflict
end
test "should create nested hostgroup with a parent" do
assert_difference('Hostgroup.count') do
post :create, { :hostgroup => valid_attrs.merge(:parent_id => hostgroups(:common).id) }
test/functional/api/v2/hostgroups_controller_test.rb
assert_response :success
end
test "blocks API deletion of hosts with children" do
assert hostgroups(:parent).has_children?
assert_no_difference('Hostgroup.count') do
delete :destroy, { :id => hostgroups(:parent).to_param }
end
assert_response :conflict
end
test "should create nested hostgroup with a parent" do
assert_difference('Hostgroup.count') do
post :create, { :hostgroup => valid_attrs.merge(:parent_id => hostgroups(:common).id) }
test/unit/hostgroup_test.rb
test "user with destroy permissions should be able to destroy" do
setup_user "destroy"
record = Hostgroup.first
record = hostgroups(:common)
as_admin do
record.hosts.destroy_all
record.hostgroup_classes.destroy_all
......
test "user with edit permissions should not be able to destroy" do
setup_user "edit"
record = Hostgroup.first
record = hostgroups(:common)
assert !record.destroy
assert !record.frozen?
end
......
assert_equal [Puppetclass.first, Puppetclass.last].sort, child.classes.sort
end
test "should remove relationships if deleting a parent hostgroup" do
assert (top = Hostgroup.create(:name => "topA"))
assert (second = Hostgroup.create(:name => "secondB", :parent_id => top.id))
test "blocks deletion of hosts with children" do
top = Hostgroup.create(:name => "topA")
second = Hostgroup.create(:name => "secondB", :parent_id => top.id)
assert top.has_children?
assert !second.is_root?
assert top.destroy
assert Hostgroup.find(second.id).is_root?
assert top.has_children?
assert_raise Ancestry::AncestryException do
top.destroy
end
end
test "changing name of hostgroup updates other hostgroup labels" do
......
assert_equal "new_common/db", hostgroup.label
end
test "deleting a hostgroup updates other hostgroup labels" do
test "deleting a hostgroup with children does not change labels" do
#setup - get label "common/db"
hostgroup = hostgroups(:db)
parent_hostgroup = hostgroups(:common)
......
hostgroup.reload
assert_equal "Common/db", hostgroup.label
#destory parent hostgroup
assert parent_hostgroup.destroy
# check if hostgroup(:db) label changed
#attempt to destroy parent hostgroup
begin
assert_not parent_hostgroup.destroy
rescue Ancestry::AncestryException
end
# check if hostgroup(:db) label remains the same
hostgroup.reload
assert_equal "db", hostgroup.label
assert_equal "Common/db", hostgroup.label
end
test "should find associated lookup_values" do

Also available in: Unified diff