Skip to content

Commit

Permalink
fixes #9249 - associated_host fixed for all compute resources
Browse files Browse the repository at this point in the history
  • Loading branch information
unorthodoxgeek authored and dLobatog committed Feb 11, 2015
1 parent 8742991 commit 81a02cd
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 10 deletions.
7 changes: 7 additions & 0 deletions app/models/compute_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ def nested_attributes_for(type, opts)
end.compact
end

def associate_by(name, attributes)
Host.authorized(:view_hosts, Host).joins(:primary_interface).
where(:nics => {:primary => true}).
where("nics.#{name}" => attributes).
first
end

private

def set_attributes_hash
Expand Down
2 changes: 1 addition & 1 deletion app/models/compute_resources/foreman/model/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def update_required?(old_attrs, new_attrs)
end

def associated_host(vm)
Host.authorized(:view_hosts, Host).where(:ip => [vm.public_ip_address, vm.private_ip_address]).first
associate_by("ip", [vm.public_ip_address, vm.private_ip_address])
end

def user_data_supported?
Expand Down
2 changes: 1 addition & 1 deletion app/models/compute_resources/foreman/model/libvirt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def hypervisor
end

def associated_host(vm)
Host.authorized(:view_hosts, Host).where(:mac => vm.mac).first
associate_by("mac", vm.mac)
end

protected
Expand Down
2 changes: 1 addition & 1 deletion app/models/compute_resources/foreman/model/openstack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def console(uuid)
end

def associated_host(vm)
Host.authorized(:view_hosts, Host).where(:ip => [vm.floating_ip_address, vm.private_ip_address]).first
associate_by("ip", [vm.floating_ip_address, vm.private_ip_address])
end

def flavor_name(flavor_ref)
Expand Down
6 changes: 1 addition & 5 deletions app/models/compute_resources/foreman/model/ovirt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,7 @@ def update_required?(old_attrs, new_attrs)
end

def associated_host(vm)
Host.authorized(:view_hosts, Host).
joins(:primary_interface).
where(:nics => {:primary => true}).
where('nics.mac' => vm.interfaces.map { |i| i.mac }).
first
associate_by("mac", vm.interfaces.map(&:mac))
end

def self.provider_friendly_name
Expand Down
2 changes: 1 addition & 1 deletion app/models/compute_resources/foreman/model/rackspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def ensure_valid_region
end

def associated_host(vm)
Host.authorized(:view_hosts, Host).where(:ip => [vm.public_ip_address, vm.private_ip_address]).first
associate_by("ip", [vm.public_ip_address, vm.private_ip_address])
end

def user_data_supported?
Expand Down
2 changes: 1 addition & 1 deletion app/models/compute_resources/foreman/model/vmware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def pubkey_hash=(key)
end

def associated_host(vm)
Host.authorized(:view_hosts, Host).where(:mac => vm.mac).first
associate_by("mac", vm.mac)
end

def self.provider_friendly_name
Expand Down
10 changes: 10 additions & 0 deletions test/unit/compute_resources/ec2_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'test_helper'

class EC2Test < ActiveSupport::TestCase
test "#associated_host matches any NIC" do
host = FactoryGirl.create(:host, :ip => '10.0.0.154')
cr = FactoryGirl.build(:ec2_cr)
iface = mock('iface1', :public_ip_address => '10.0.0.154', :private_ip_address => "10.1.1.1")
assert_equal host, as_admin { cr.associated_host(iface) }
end
end
10 changes: 10 additions & 0 deletions test/unit/compute_resources/libvirt_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'test_helper'

class LibvirtTest < ActiveSupport::TestCase
test "#associated_host matches any NIC" do
host = FactoryGirl.create(:host, :mac => 'ca:d0:e6:32:16:97')
cr = FactoryGirl.build(:libvirt_cr)
iface = mock('iface1', :mac => 'ca:d0:e6:32:16:97')
assert_equal host, as_admin { cr.associated_host(iface) }
end
end
10 changes: 10 additions & 0 deletions test/unit/compute_resources/openstack_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'test_helper'

class OpenstackTest < ActiveSupport::TestCase
test "#associated_host matches any NIC" do
host = FactoryGirl.create(:host, :ip => '10.0.0.154')
cr = FactoryGirl.build(:openstack_cr)
iface = mock('iface1', :floating_ip_address => '10.0.0.154', :private_ip_address => "10.1.1.1")
assert_equal host, as_admin { cr.associated_host(iface) }
end
end
10 changes: 10 additions & 0 deletions test/unit/compute_resources/rackspace_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'test_helper'

class RackspaceTest < ActiveSupport::TestCase
test "#associated_host matches any NIC" do
host = FactoryGirl.create(:host, :ip => '10.0.0.154')
cr = FactoryGirl.build(:rackspace_cr)
iface = mock('iface1', :public_ip_address => '10.0.0.154', :private_ip_address => "10.1.1.1")
assert_equal host, as_admin { cr.associated_host(iface) }
end
end
7 changes: 7 additions & 0 deletions test/unit/compute_resources/vmware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,11 @@ class VmwareTest < ActiveSupport::TestCase
assert_equal "network-17", attrs_in["interfaces_attributes"]["0"]["network"]
end
end

test "#associated_host matches any NIC" do
host = FactoryGirl.create(:host, :mac => 'ca:d0:e6:32:16:97')
cr = FactoryGirl.build(:vmware_cr)
iface = mock('iface1', :mac => 'ca:d0:e6:32:16:97')
assert_equal host, as_admin { cr.associated_host(iface) }
end
end

0 comments on commit 81a02cd

Please sign in to comment.