Skip to content

Commit

Permalink
Fixes #10479 - prevent validation error on host creation when domain …
Browse files Browse the repository at this point in the history
…contains capital letters
  • Loading branch information
tbrisker committed May 21, 2015
1 parent 1a282cb commit 7cd42dd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Domain < ActiveRecord::Base

accepts_nested_attributes_for :domain_parameters, :allow_destroy => true
include ParameterValidators
validates :name, :presence => true, :uniqueness => true
validates :name, :presence => true, :uniqueness => { :case_sensitive => false }
validates :fullname, :uniqueness => true, :allow_blank => true, :allow_nil => true

scoped_search :on => [:name, :fullname], :complete_value => true
Expand Down
2 changes: 1 addition & 1 deletion app/models/nic/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def name=(*args)
end

def shortname
domain.nil? ? name : name.to_s.chomp("." + domain.name)
domain.nil? ? name : name.sub(/\.#{Regexp.escape domain.name}/i, '')
end

def validated?
Expand Down
8 changes: 4 additions & 4 deletions app/models/nic/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ def normalize_name
# no hostname was given or a domain was selected, since this is before validation we need to ignore
# it and let the validations to produce an error
return if name.empty?
if domain.nil? && name.match(/\./) && !changed_attributes['domain_id'].present?
if domain.nil? && name.include?('.') && !changed_attributes['domain_id'].present?
# try to assign the domain automatically based on our existing domains from the host FQDN
self.domain = Domain.all.select{|d| name.match(/#{d.name}\Z/)}.first rescue nil
self.domain = Domain.all.detect{|d| name.match(/#{d.name}\Z/i)}
elsif persisted? && changed_attributes['domain_id'].present?
# if we've just updated the domain name, strip off the old one
old_domain = Domain.find(changed_attributes["domain_id"])
# Remove the old domain, until fqdn will be set as the full name
self.name.chomp!("." + old_domain.to_s)
self.name.sub!(/\.#{Regexp.escape old_domain.name}/i, '')
end
# name should be fqdn
self.name = fqdn
# A managed host we should know the domain for; and the shortname shouldn't include a period
# This only applies for unattended=true, as otherwise the name field includes the domain
errors.add(:name, _("must not include periods")) if ( host && host.managed? && managed? && shortname.include?(".") && SETTINGS[:unattended] )
self.name = Net::Validations.normalize_hostname(name) if self.name.present?
self.name = Net::Validations.normalize_hostname(name)
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/unit/nics/managed_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ class ManagedTest < ActiveSupport::TestCase
assert_nil nic.domain
end

test "#normalize_hostname accepts domain even if it contains capital letters" do
domain = FactoryGirl.create(:domain, :name => "CAPITAL.com")
nic = setup_primary_nic_with_name(" Host.#{domain.name}", :domain => nil)
nic.send(:normalize_name)
assert_equal "host.#{domain.name.downcase}", nic.name
assert_equal domain, nic.domain
end

test "#normalize_hostname keeps domain nil if it can't find such domain based on name" do
nic = setup_primary_nic_with_name(" Host", :domain => nil)
nic.send(:normalize_name)
Expand Down

0 comments on commit 7cd42dd

Please sign in to comment.