From 586ec8fa4b3814b30f519af73888b729ca81b9c8 Mon Sep 17 00:00:00 2001 From: Dominik Matoulek Date: Tue, 11 Aug 2020 10:34:50 +0200 Subject: [PATCH] Fixes #30627 - mac address is not required for virtual resources --- app/models/nic/base.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/nic/base.rb b/app/models/nic/base.rb index 3e90dfd38754..725ea45edf1f 100644 --- a/app/models/nic/base.rb +++ b/app/models/nic/base.rb @@ -18,7 +18,7 @@ class Base < ApplicationRecord validate :mac_uniqueness, :if => proc { |nic| nic.managed? && nic.host && nic.host.managed? && !nic.host.compute? && !nic.virtual? && nic.mac.present? } validates :mac, :presence => true, - :if => proc { |nic| nic.managed? && nic.host_managed? && !nic.host.compute? && !nic.virtual? && (nic.provision? || nic.subnet.present? || nic.subnet6.present?) } + :if => proc { |nic| nic.managed? && nic.host_managed? && !nic.host.compute? && !nic.virtual? && !nic.host.compute_provides?(:mac) && (nic.provision? || nic.subnet.present? || nic.subnet6.present?) } validate :validate_mac_is_unicast, :if => proc { |nic| nic.managed? && !nic.virtual? } validates :mac, :mac_address => true, :allow_blank => true @@ -45,6 +45,8 @@ class Base < ApplicationRecord validates :subnet, :belongs_to_host_taxonomy => { :taxonomy => :organization } validates :subnet6, :belongs_to_host_taxonomy => { :taxonomy => :organization } + validate :check_blank_mac_for_virtual_resources, on: :create + scope :bmc, -> { where(:type => "Nic::BMC") } scope :bonds, -> { where(:type => "Nic::Bond") } scope :bridges, -> { where(:type => "Nic::Bridge") } @@ -358,6 +360,12 @@ def interface_attribute_uniqueness(attr, base = Nic::Base.where(nil)) db_candidates = db_candidates.select { |c| c.id != id && in_memory_candidates.map(&:id).include?(c.id) } errors.add(attr, :taken) if db_candidates.present? end + + def check_blank_mac_for_virtual_resources + if host.compute_provides?(:mac) && mac.present? + errors.add(:mac, _("can't be set for this compute resource because it's provided")) + end + end end end