Project

General

Profile

« Previous | Next » 

Revision f2d5f955

Added by Marek Hulán about 9 years ago

Fixes #9618 - make identifier uniq per host

View differences:

app/models/host/base.rb
:allow_blank => true,
:message => (_("Owner type needs to be one of the following: %s") % OWNER_TYPES.join(', ')) }
validate :host_has_required_interfaces
validate :uniq_interfaces_identifiers
# primary interface is mandatory because of delegated methods so we build it if it's missing
# similar for provision interface
......
errors.add :interfaces, _("managed host must have one provision interface")
end
end
# we can't use standard unique validation on interface since we can't properly handle :scope => :host_id
# for new hosts host_id does not exist at that moment, validation would work only for persisted records
def uniq_interfaces_identifiers
success = true
identifiers = []
self.interfaces.each do |interface|
next if interface.identifier.blank?
if identifiers.include?(interface.identifier)
interface.errors.add :identifier, :taken
success = false
end
identifiers.push(interface.identifier)
end
errors.add(:interfaces, _('some interfaces are invalid')) unless success
success
end
end
end
test/unit/host_test.rb
assert_kind_of Nic::Managed, host.interfaces.find_by_identifier('eth1')
end
test "host can't have more interfaces with the same identifier" do
host = FactoryGirl.build(:host, :managed)
host.primary_interface.identifier = 'eth0'
nic = host.interfaces.build(:identifier => 'eth0')
refute host.valid?
assert_present nic.errors[:identifier]
assert_present host.errors[:interfaces]
nic.identifier = 'eth1'
host.valid?
refute_includes nic.errors.keys, :identifier
refute_includes host.errors.keys, :interfaces
end
# Token tests
test "built should clean tokens" do
......
end
h = FactoryGirl.create(:host, :managed)
assert h.interfaces.create :mac => "cabbccddeeff", :host => h, :type => 'Nic::BMC',
:provider => "IPMI", :username => "root", :password => "secret", :ip => "10.35.19.35"
:provider => "IPMI", :username => "root", :password => "secret", :ip => "10.35.19.35",
:identifier => 'eth2'
as_user :one do
assert h.update_attributes!("interfaces_attributes" => {"0" => {"mac"=>"59:52:10:1e:45:16"}})
end

Also available in: Unified diff