diff --git a/app/models/host/base.rb b/app/models/host/base.rb index 2398ef8d42f4..79cba0165052 100644 --- a/app/models/host/base.rb +++ b/app/models/host/base.rb @@ -115,6 +115,7 @@ def dup :domain=, :domain_id=, :domain_name=, :to => :primary_interface attr_writer :updated_virtuals + def updated_virtuals @updated_virtuals ||= [] end @@ -351,6 +352,14 @@ def render_template(template:, **params) template.render(host: self, **params) end + def to_label + if Setting[:append_domain_name_for_hosts] + name + else + name.split('.')[0] + end + end + private def parse_ip_address(address, ignore_link_local: true) diff --git a/app/services/name_synchronizer.rb b/app/services/name_synchronizer.rb index dc00a585d5b4..91715c41c8bb 100644 --- a/app/services/name_synchronizer.rb +++ b/app/services/name_synchronizer.rb @@ -25,6 +25,6 @@ def sync_required? private def interface_name - Setting[:append_domain_name_for_hosts] ? @interface.name : @interface.shortname + @interface.name end end diff --git a/app/views/api/v2/hosts/base.json.rabl b/app/views/api/v2/hosts/base.json.rabl index 417623b70925..25558e49ebf6 100644 --- a/app/views/api/v2/hosts/base.json.rabl +++ b/app/views/api/v2/hosts/base.json.rabl @@ -1,3 +1,11 @@ object @host -attributes :name, :id +attributes :id + +node :name do |name| + if Setting[:append_domain_name_for_hosts] + name.split('.')[0] + else + name + end +end diff --git a/test/models/lookup_value_test.rb b/test/models/lookup_value_test.rb index 1bfab0e68ab8..96cbb94b9d43 100644 --- a/test/models/lookup_value_test.rb +++ b/test/models/lookup_value_test.rb @@ -58,11 +58,11 @@ def valid_attrs2 test "can create lookup value if match fqdn= does match existing host" do as_admin do - Setting[:append_domain_name_for_hosts] = false + Setting[:append_domain_name_for_hosts] = false # This affects the view only domain = FactoryBot.create(:domain) host = FactoryBot.create(:host, interfaces: [FactoryBot.build(:nic_managed, identifier: 'fqdn_test', primary: true, domain: domain)]) attrs = { :match => "fqdn=#{host.primary_interface.fqdn}", :value => "123", :lookup_key_id => lookup_key.id } - refute_match /#{domain.name}/, host.name, "#{host.name} shouldn't be FQDN" + refute_match /#{domain.name}/, host.name.split('.')[0], "#{host.name} shouldn't be FQDN" assert_difference('LookupValue.count') do LookupValue.create!(attrs) end