diff --git a/app/models/nic/base.rb b/app/models/nic/base.rb index 6015a9e3831d..826e663f4a7c 100644 --- a/app/models/nic/base.rb +++ b/app/models/nic/base.rb @@ -21,8 +21,9 @@ class Base < ActiveRecord::Base validates :mac, :uniqueness => {:scope => :virtual}, :if => Proc.new { |nic| nic.managed? && nic.host && nic.host.managed? && !nic.host.compute? && !nic.virtual? }, :allow_blank => true + validate :compute_resource_requires_fields, :if => Proc.new {|nic| nic.mac.blank? && (nic.managed? && nic.host && nic.host.managed? && !nic.host.compute? &&!nic.virtual?)} validates :mac, :presence => true, - :if => Proc.new { |nic| nic.managed? && nic.host_managed? && !nic.host.compute? && !nic.virtual? } + :if => Proc.new { |nic| !nic.skip_mac_validation && nic.managed? && nic.host && nic.host.managed? && !nic.host.compute? &&!nic.virtual? } validates :mac, :mac_address => true, :allow_blank => true # TODO uniq on primary per host @@ -71,6 +72,8 @@ def belongs_to_counter_cache_before_destroy_for_domain # provider specific attributes serialize :compute_attributes, Hash + attr_reader :skip_mac_validation + class Jail < ::Safemode::Jail allow :managed?, :subnet, :virtual?, :physical?, :mac, :ip, :identifier, :attached_to, :link, :tag, :domain, :vlanid, :bond_options, :attached_devices, :mode, @@ -148,6 +151,13 @@ def host_managed? protected + def compute_resource_requires_fields + if host.compute_resource.present? + host.errors.add(:compute_profile, _("you must specify either compute attributes or a compute profile")) + @skip_mac_validation = true + end + end + def uniq_fields_with_hosts self.virtual? ? [] : [:mac] end diff --git a/test/unit/host_test.rb b/test/unit/host_test.rb index 2871f1cb2178..e97cd4ad474a 100644 --- a/test/unit/host_test.rb +++ b/test/unit/host_test.rb @@ -96,6 +96,13 @@ class HostTest < ActiveSupport::TestCase assert_equal true, host.valid? end + test "should require compute attributes or compute profile in case of a virtual compute resource" do + host = FactoryGirl.build(:host, :with_dhcp_orchestration, :mac => nil) + refute host.valid? + refute host.errors.messages.keys.include?("interfaces.mac") + assert host.errors.messages.keys.include?(:compute_profile) + end + test "should fix ip address if a leading zero is used" do host = Host.create :name => "myhost", :mac => "aabbccddeeff", :ip => "123.01.02.03" assert_equal "123.1.2.3", host.ip