Skip to content

Commit ee5beba

Browse files
committed
Moved Vm#find_all_by_mac_address_and_hostname_and_ipaddress to EvmServer
1 parent db84cd1 commit ee5beba

File tree

4 files changed

+76
-75
lines changed

4 files changed

+76
-75
lines changed

app/models/vm.rb

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,6 @@ def enforce_single_service_parent?
4444
true
4545
end
4646

47-
def self.find_all_by_mac_address_and_hostname_and_ipaddress(mac_address, hostname, ipaddress)
48-
return [] if mac_address.blank? && hostname.blank? && ipaddress.blank?
49-
50-
include = [:vm_or_template]
51-
references = []
52-
conds = [["hardwares.vm_or_template_id IS NOT NULL"]]
53-
if mac_address
54-
conds[0] << "guest_devices.address = ?"
55-
conds << mac_address
56-
include << :nics
57-
references << :guest_devices
58-
end
59-
if hostname
60-
conds[0] << "networks.hostname = ?"
61-
conds << hostname
62-
include << :networks
63-
references << :networks
64-
end
65-
if ipaddress
66-
conds[0] << "networks.ipaddress = ?"
67-
conds << ipaddress
68-
include << :networks
69-
references << :networks
70-
end
71-
conds[0] = "(#{conds[0].join(" AND ")})"
72-
73-
Hardware.includes(include.uniq)
74-
.references(references.uniq)
75-
.where(conds)
76-
.map(&:vm_or_template).select { |vm| vm.kind_of?(Vm) }
77-
end
78-
7947
def running_processes
8048
pl = {}
8149
if (reason = unsupported_reason(:collect_running_processes))

lib/workers/evm_server.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def save_local_network_info
186186

187187
def set_local_server_vm
188188
if @current_server.vm_id.nil?
189-
vms = Vm.find_all_by_mac_address_and_hostname_and_ipaddress(@current_server.mac_address, @current_server.hostname, @current_server.ipaddress)
189+
vms = Vm.find_vms_by_mac_address_and_hostname_and_ipaddress(@current_server.mac_address, @current_server.hostname, @current_server.ipaddress)
190190
if vms.length > 1
191191
_log.warn("Found multiple Vms that may represent this MiqServer: #{vms.collect(&:id).sort.inspect}")
192192
elsif vms.length == 1
@@ -195,6 +195,38 @@ def set_local_server_vm
195195
end
196196
end
197197

198+
def find_vms_by_mac_address_and_hostname_and_ipaddress(mac_address, hostname, ipaddress)
199+
return [] if mac_address.blank? && hostname.blank? && ipaddress.blank?
200+
201+
include = [:vm_or_template]
202+
references = []
203+
conds = [["hardwares.vm_or_template_id IS NOT NULL"]]
204+
if mac_address
205+
conds[0] << "guest_devices.address = ?"
206+
conds << mac_address
207+
include << :nics
208+
references << :guest_devices
209+
end
210+
if hostname
211+
conds[0] << "networks.hostname = ?"
212+
conds << hostname
213+
include << :networks
214+
references << :networks
215+
end
216+
if ipaddress
217+
conds[0] << "networks.ipaddress = ?"
218+
conds << ipaddress
219+
include << :networks
220+
references << :networks
221+
end
222+
conds[0] = "(#{conds[0].join(" AND ")})"
223+
224+
Hardware.includes(include.uniq)
225+
.references(references.uniq)
226+
.where(conds)
227+
.map(&:vm_or_template).select { |vm| vm.kind_of?(Vm) }
228+
end
229+
198230
def reset_server_runtime_info
199231
@current_server.update(
200232
:drb_uri => nil,

spec/lib/workers/evm_server_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,47 @@
188188
end
189189
end
190190
end
191+
192+
context "#find_vms_by_mac_address_and_hostname_and_ipaddress (private)" do
193+
subject { described_class.new }
194+
before do
195+
@hardware1 = FactoryBot.create(:hardware)
196+
@vm1 = FactoryBot.create(:vm_vmware, :hardware => @hardware1)
197+
198+
@hardware2 = FactoryBot.create(:hardware)
199+
@vm2 = FactoryBot.create(:vm_vmware, :hardware => @hardware2)
200+
end
201+
202+
it "mac_address" do
203+
address = "ABCDEFG"
204+
guest_device = FactoryBot.create(:guest_device, :address => address, :device_type => "ethernet")
205+
@hardware1.guest_devices << guest_device
206+
207+
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, address, nil, nil))
208+
.to eql([@vm1])
209+
end
210+
211+
it "hostname" do
212+
hostname = "ABCDEFG"
213+
network = FactoryBot.create(:network, :hostname => hostname)
214+
@hardware1.networks << network
215+
216+
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, nil, hostname, nil))
217+
.to eql([@vm1])
218+
end
219+
220+
it "ipaddress" do
221+
ipaddress = "127.0.0.1"
222+
network = FactoryBot.create(:network, :ipaddress => ipaddress)
223+
@hardware1.networks << network
224+
225+
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, nil, nil, ipaddress))
226+
.to eql([@vm1])
227+
end
228+
229+
it "returns an empty list when all are blank" do
230+
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, nil, nil, nil)).to eq([])
231+
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, '', '', '')).to eq([])
232+
end
233+
end
191234
end

spec/models/vm_spec.rb

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,48 +48,6 @@
4848
expect { vm.validate_native_console_support }.to raise_error MiqException::RemoteConsoleNotSupportedError
4949
end
5050

51-
context ".find_all_by_mac_address_and_hostname_and_ipaddress" do
52-
before do
53-
@hardware1 = FactoryBot.create(:hardware)
54-
@vm1 = FactoryBot.create(:vm_vmware, :hardware => @hardware1)
55-
56-
@hardware2 = FactoryBot.create(:hardware)
57-
@vm2 = FactoryBot.create(:vm_vmware, :hardware => @hardware2)
58-
end
59-
60-
it "mac_address" do
61-
address = "ABCDEFG"
62-
guest_device = FactoryBot.create(:guest_device, :address => address, :device_type => "ethernet")
63-
@hardware1.guest_devices << guest_device
64-
65-
expect(described_class.find_all_by_mac_address_and_hostname_and_ipaddress(address, nil, nil))
66-
.to eql([@vm1])
67-
end
68-
69-
it "hostname" do
70-
hostname = "ABCDEFG"
71-
network = FactoryBot.create(:network, :hostname => hostname)
72-
@hardware1.networks << network
73-
74-
expect(described_class.find_all_by_mac_address_and_hostname_and_ipaddress(nil, hostname, nil))
75-
.to eql([@vm1])
76-
end
77-
78-
it "ipaddress" do
79-
ipaddress = "127.0.0.1"
80-
network = FactoryBot.create(:network, :ipaddress => ipaddress)
81-
@hardware1.networks << network
82-
83-
expect(described_class.find_all_by_mac_address_and_hostname_and_ipaddress(nil, nil, ipaddress))
84-
.to eql([@vm1])
85-
end
86-
87-
it "returns an empty list when all are blank" do
88-
expect(described_class.find_all_by_mac_address_and_hostname_and_ipaddress(nil, nil, nil)).to eq([])
89-
expect(described_class.find_all_by_mac_address_and_hostname_and_ipaddress('', '', '')).to eq([])
90-
end
91-
end
92-
9351
context "with relationships of multiple types" do
9452
before do
9553
@rp = FactoryBot.create(:resource_pool, :name => "RP")

0 commit comments

Comments
 (0)