Project

General

Profile

« Previous | Next » 

Revision a525db23

Added by Stephen Benjamin almost 6 years ago

fixes #23232 - fix error when domain not in taxonomy

This ensures that if domain_id is set, but domain not found (for example
when domain is in another taxonomy), the shortname still removes the
domain from the host's FQDN.

View differences:

app/models/nic/base.rb
end
def shortname
domain.nil? ? name : name.to_s.chomp("." + domain.name)
if domain
name.to_s.chomp("." + domain.name)
elsif domain_id && (unscoped_domain = Domain.unscoped.find_by(id: domain_id))
# If domain is nil, but domain_id is set, domain could be
# in another taxonomy. Don't fail to create a correct shortname.
name.to_s.chomp("." + unscoped_domain.name)
else
name
end
end
def validated?
app/models/nic/interface.rb
end
def fqdn_before_last_save
domain_before_last_save = Domain.find(domain_id_before_last_save) if domain_id_before_last_save.present?
domain_before_last_save = Domain.unscoped.find(domain_id_before_last_save) if domain_id_before_last_save.present?
return name_before_last_save if name_before_last_save.blank? || domain_before_last_save.blank?
name_before_last_save.include?('.') ? name_before_last_save : "#{name_before_last_save}.#{domain_before_last_save}"
end
......
return if name.empty?
if domain_id.nil? && name.include?('.') && changed_attributes['domain_id'].blank?
# try to assign the domain automatically based on our existing domains from the host FQDN
self.domain = Domain.find_by(:name => name.partition('.')[2])
self.domain = Domain.unscoped.find_by(:name => name.partition('.')[2])
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"])
old_domain = Domain.unscoped.find(changed_attributes["domain_id"])
# Remove the old domain, until fqdn will be set as the full name
self.name = self.name.chomp('.' + old_domain.to_s)
end
test/models/hosts/base_test.rb
assert host.primary_interface
assert_equal 1, host.interfaces.size
end
test 'shortname periods check considers domain outside taxonomy scope' do
host_org = FactoryBot.create(:organization)
other_org = FactoryBot.create(:organization)
domain = FactoryBot.create(:domain, organizations: [other_org])
refute domain.organizations.include?(host_org)
host = FactoryBot.create(:host, organization: host_org, domain_id: domain.id)
host = Host.find(host.id)
assert host.valid?
end
end
end
test/models/nic_test.rb
nic_name = 'hostname.sub.bigdomain'
interface = FactoryBot.build_stubbed(:nic_managed, :name => nic_name)
subdomain = FactoryBot.create(:domain, :name => 'sub.bigdomain')
Domain.expects(:find_by).with(:name => subdomain.name).returns(subdomain)
interface.send(:normalize_name)
assert_equal subdomain, interface.domain
end
......
nic_name = 'hostname.undefined-subdomain.bigdomain'
FactoryBot.create(:domain, :name => 'bigdomain')
interface = FactoryBot.build_stubbed(:nic_managed, :name => nic_name)
Domain.expects(:find_by).
with(:name => 'undefined-subdomain.bigdomain').
returns(nil)
interface.send(:normalize_name)
refute interface.domain
end

Also available in: Unified diff