diff --git a/app/models/miq_server.rb b/app/models/miq_server.rb index 9b7b9bcd81e..8c15adfea7f 100644 --- a/app/models/miq_server.rb +++ b/app/models/miq_server.rb @@ -474,7 +474,7 @@ def tenant_identity end def miq_region - ::MiqRegion.my_region + MiqRegion.find_by(:region => region_id) end def self.display_name(number = 1) diff --git a/spec/lib/vmdb/settings_spec.rb b/spec/lib/vmdb/settings_spec.rb index 9a2cab34e3e..d39f673ac92 100644 --- a/spec/lib/vmdb/settings_spec.rb +++ b/spec/lib/vmdb/settings_spec.rb @@ -566,6 +566,81 @@ settings = Vmdb::Settings.for_resource(MiqServer.new(:zone => server.zone)) expect(settings.api.token_ttl).to eq "4.hour" end + + context "on global region" do + let(:key) { "yuri" } + let(:value_region_remote) { "region_remote" } + let(:value_server_remote) { "server_remote" } + let(:value_zone_remote) { "zone_remote" } + + before do + Zone.seed + @region_global = MiqRegion.first + @region_remote = FactoryBot.create(:miq_region, :id => ApplicationRecord.id_in_region(1, @region_global.region + 1)) + @zone_remote = FactoryBot.create(:zone, :id => ApplicationRecord.id_in_region(2, @region_remote.region)) + @server_remote = FactoryBot.create(:miq_server, :zone => @zone_remote, :id => ApplicationRecord.id_in_region(1, @region_remote.region)) + SettingsChange.create!(:id => ApplicationRecord.id_in_region(1, @region_global.region), + :resource_id => @region_global.id, + :resource_type => "MiqRegion", + :key => "/#{key}", + :value => "value from global region") + zone_global = FactoryBot.create(:zone, :id => ApplicationRecord.id_in_region(1, @region_global.region)) + SettingsChange.create!(:id => ApplicationRecord.id_in_region(2, @region_global.region), + :resource_id => zone_global.id, + :resource_type => "Zone", + :key => "/#{key}", + :value => "value from global zone") + end + + it "applies settings from remote sever if there are specified" do + SettingsChange.create!(:id => ApplicationRecord.id_in_region(1, @region_remote.region), + :resource_id => @server_remote.id, + :resource_type => "MiqServer", + :key => "/#{key}", + :value => value_server_remote) + expect(Vmdb::Settings.for_resource(@server_remote)[key]).to eq(value_server_remote) + end + + it "applies settings from remote region if settings on remote server not specified" do + SettingsChange.create!(:id => ApplicationRecord.id_in_region(1, @region_remote.region), + :resource_id => @region_remote.id, + :resource_type => "MiqRegion", + :key => "/#{key}", + :value => value_region_remote) + expect(Vmdb::Settings.for_resource(@server_remote)[key]).to eq(value_region_remote) + end + + it "applies settings from remote zone if settings on remote server not specified" do + SettingsChange.create!(:id => ApplicationRecord.id_in_region(1, @region_remote.region), + :resource_id => @zone_remote.id, + :resource_type => "Zone", + :key => "/#{key}", + :value => value_zone_remote) + expect(Vmdb::Settings.for_resource(@server_remote)[key]).to eq(value_zone_remote) + end + + it "loads settings from correct level of hirerarchy" do + SettingsChange.create!(:id => ApplicationRecord.id_in_region(1, @region_remote.region), + :resource_id => @region_remote.id, + :resource_type => "MiqRegion", + :key => "/#{key}", + :value => value_region_remote) + SettingsChange.create!(:id => ApplicationRecord.id_in_region(2, @region_remote.region), + :resource_id => @zone_remote.id, + :resource_type => "Zone", + :key => "/#{key}", + :value => value_zone_remote) + SettingsChange.create!(:id => ApplicationRecord.id_in_region(3, @region_remote.region), + :resource_id => @server_remote.id, + :resource_type => "MiqServer", + :key => "/#{key}", + :value => value_server_remote) + + expect(Vmdb::Settings.for_resource(@server_remote)[key]).to eq(value_server_remote) + expect(Vmdb::Settings.for_resource(@zone_remote)[key]).to eq(value_zone_remote) + expect(Vmdb::Settings.for_resource(@region_remote)[key]).to eq(value_region_remote) + end + end end describe ".for_resource_yaml" do diff --git a/spec/models/miq_server_spec.rb b/spec/models/miq_server_spec.rb index a614ffb3765..4af57967d7d 100644 --- a/spec/models/miq_server_spec.rb +++ b/spec/models/miq_server_spec.rb @@ -442,6 +442,23 @@ end end + describe "#miq_region" do + before do + Zone.seed + @region1 = MiqRegion.first.region + @region2 = FactoryBot.create(:miq_region, :id => MiqRegion.id_in_region(1, @region1 + 1)).region + zone = FactoryBot.create(:zone, :id => Zone.id_in_region(2, @region2)) + + @server1 = FactoryBot.create(:miq_server) + @server2 = FactoryBot.create(:miq_server, :zone => zone, :id => MiqServer.id_in_region(1, @region2)) + end + + it "returns region this server belongs to" do + expect(@server1.miq_region.region).to eq(@region1) + expect(@server2.miq_region.region).to eq(@region2) + end + end + describe ".zone_is_modifiable?" do it "is true when there are multiple zones in the region" do FactoryBot.create(:miq_server)