Project

General

Profile

« Previous | Next » 

Revision 45831c5a

Added by Marek Hulán about 8 years ago

Fixes #14264 - improve primary interface selection

(cherry picked from commit 308f611d9bb17173588a6f59c7a914b551923a7b)

View differences:

app/controllers/api/v2/hosts_controller.rb
params[:interfaces_attributes] = params[:interfaces_attributes].map do |nic_attr|
interface_attributes(nic_attr)
end
#if no primary interface set explicitly, set first one as primary
unless params[:interfaces_attributes].any?{|i| i[:primary]}
params[:interfaces_attributes].first[:primary] = true
end
end
params = host.apply_inherited_attributes(params) if host
params
app/models/host/base.rb
end
def build_required_interfaces(attrs = {})
self.interfaces.build(attrs.merge(:primary => true, :type => 'Nic::Managed')) if self.primary_interface.nil?
if self.primary_interface.nil?
if self.interfaces.empty?
self.interfaces.build(attrs.merge(:primary => true, :type => 'Nic::Managed'))
else
interface = self.interfaces.first
interface.attributes = attrs
interface.primary = true
end
end
self.primary_interface.provision = true if self.provision_interface.nil?
end
test/functional/api/v2/hosts_controller_test.rb
assert_response :unprocessable_entity
end
test "when no primary interface is set, defaulting to first one" do
disable_orchestration
nics = nics_attrs
nics.each do |nic|
nic.delete(:primary)
end
post :create, { :host => basic_attrs.merge!(:interfaces_attributes => nics) }
assert_response :created
end
test 'when ":restrict_registered_smart_proxies" is false, HTTP requests should be able to import facts' do
User.current = users(:one) #use an unprivileged user, not apiadmin
Setting[:restrict_registered_smart_proxies] = false
test/unit/host_test.rb
assert_equal "#{host.shortname}.yourdomain.net", host.name
end
test '.new should build host with primary interface' do
host = Host.new
assert host.primary_interface
assert_equal 1, host.interfaces.size
end
test '.new should mark one interfaces as primary if none was chosen explicitly' do
host = Host.new(:interfaces_attributes => [ {:ip => '192.168.0.1' }, { :ip => '192.168.1.2' } ])
assert host.primary_interface
assert_equal 2, host.interfaces.size
end
test '.new does not reset primary flag if it was set explicitly' do
host = Host.new(:interfaces_attributes => [ {:ip => '192.168.0.1' }, { :ip => '192.168.1.2', :primary => true } ])
assert_equal 2, host.interfaces.size
assert_equal '192.168.1.2', host.primary_interface.ip
end
test "host should not save without primary interface" do
host = FactoryGirl.build(:host, :managed)
host.interfaces = []

Also available in: Unified diff