Skip to content

Commit

Permalink
Expose a method to determine if a zone can be deleted
Browse files Browse the repository at this point in the history
This will be used by the UI instead of their current, separate logic
  • Loading branch information
carbonin committed Jan 27, 2020
1 parent 5e87435 commit d27d378
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
12 changes: 9 additions & 3 deletions app/models/zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ def ntp_reload_queue
servers.each(&:ntp_reload_queue)
end

def message_for_illegal_delete
return _("cannot delete default zone") if name == "default"
return _("cannot delete maintenance zone") if self == miq_region.maintenance_zone
return _("zone name '%{name}' is used by a server") % {:name => name} if miq_servers.present?
_("zone name '%{name}' is used by a provider") % {:name => name} if ext_management_systems.present?
end

protected

def remove_servers_if_podified
Expand All @@ -257,8 +264,7 @@ def create_server_if_podified
end

def check_zone_in_use_on_destroy
raise _("cannot delete default zone") if name == "default"
raise _("cannot delete maintenance zone") if self == miq_region.maintenance_zone
raise _("zone name '%{name}' is used by a server") % {:name => name} unless miq_servers.blank?
msg = message_for_illegal_delete
raise msg if msg
end
end
34 changes: 26 additions & 8 deletions spec/models/zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@
described_class.seed
expect(described_class.maintenance_zone.visible).to eq(false)
end

it "cannot be destroyed" do
described_class.seed
expect { described_class.maintenance_zone.destroy! }.to raise_error(RuntimeError)
end
end

context "validate multi region" do
Expand All @@ -226,15 +231,30 @@
end

it "doesn't create a server for the zone when not podified" do
zone = Zone.create!(:name => "my_zone", :description => "some zone")
zone = FactoryBot.create(:zone)
expect(zone.miq_servers.count).to eq(0)
end

it "fails to destroy a zone with servers when not podified" do
MiqRegion.seed
zone = Zone.create!(:name => "my_zone", :description => "some zone")
zone.miq_servers.create!(:name => "my_server")
expect { zone.destroy! }.to raise_error(RuntimeError)
describe "#destroy" do
before { MiqRegion.seed }

it "fails for a zone with servers when not podified" do
zone = FactoryBot.create(:zone)
zone.miq_servers.create!(:name => "my_server")
expect { zone.destroy! }.to raise_error(RuntimeError)
end

it "fails for the default zone" do
described_class.seed
expect { described_class.default_zone.destroy! }.to raise_error(RuntimeError)
end

it "fails for a zone with a provider" do
zone = FactoryBot.create(:zone)
FactoryBot.create(:ext_management_system, :zone => zone)

expect { zone.destroy! }.to raise_error(RuntimeError)
end
end

context "when podified" do
Expand Down Expand Up @@ -262,8 +282,6 @@
end
end



it ".destroy deletes the server in the zone" do
MiqRegion.seed
zone = Zone.create!(:name => "my_zone", :description => "some zone")
Expand Down

0 comments on commit d27d378

Please sign in to comment.