Skip to content

Commit

Permalink
fixes #9882 - in case of creation of compute resource without compute…
Browse files Browse the repository at this point in the history
… profile, show relevant error message
  • Loading branch information
unorthodoxgeek committed Jun 25, 2015
1 parent 3dfaf19 commit 70e5a6b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app/models/nic/base.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/unit/host_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 70e5a6b

Please sign in to comment.