Skip to content

Commit

Permalink
Don't allow server zone changes in pods
Browse files Browse the repository at this point in the history
We auto-create a server per zone so a change should
never be necessary
  • Loading branch information
carbonin committed Feb 5, 2020
1 parent c5e7d98 commit f5cac27
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/models/miq_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class MiqServer < ApplicationRecord
virtual_delegate :description, :to => :zone, :prefix => true, :allow_nil => true, :type => :string

validate :validate_zone_not_maintenance?
validate :zone_unchanged_in_pods, :on => :update

GUID_FILE = Rails.root.join("GUID").freeze

Expand Down Expand Up @@ -479,4 +480,12 @@ def miq_region
def self.display_name(number = 1)
n_('Server', 'Servers', number)
end

private

def zone_unchanged_in_pods
return unless MiqEnvironment::Command.is_podified?

errors.add(:zone, N_('cannot be changed when running in containers')) if zone_id_changed?
end
end # class MiqServer
21 changes: 21 additions & 0 deletions spec/models/miq_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,25 @@
expect(s.description).to eq(s.name)
end
end

describe "#zone_unchanged_in_pods" do
it "allows zone changes when not in pods" do
server = FactoryBot.create(:miq_server)
zone = FactoryBot.create(:zone)

server.zone = zone
expect(server.zone_id_changed?).to be_truthy
expect(server.save).to be_truthy
end

it "prevents zone changes in pods" do
allow(MiqEnvironment::Command).to receive(:is_podified?).and_return(true)
server = FactoryBot.create(:miq_server)
zone = FactoryBot.create(:zone)

server.zone = zone
expect(server.zone_id_changed?).to be_truthy
expect(server.save).to be_falsey
end
end
end

0 comments on commit f5cac27

Please sign in to comment.