Bug #21929
openneed short host name matchers
Description
Short Host Name
--------------
http://projects.theforeman.org/issues/17521 introduces the idea of a system with a short name.
However certain customers have hosts with the following configuration
1) Short name for hostname in db. So when using the api they want to be able operate the system based on short name. They consider the name of a host merely an alias to be used in their existing scripts.
2) Hosts have an interface attached. So puppet fact on FDQN indicates a fully formed name (i.e. foo.example.com)
3) Also have the UUID_for_certificates enabled via 'Settings -> Puppet -> Use UUID for certificates'
However customers with short host names are not going to be able to match based on either the short host name or fqdn.
This is because of matching code that says
https://github.com/theforeman/foreman/blob/develop/app/models/lookup_value.rb#L80-L89 -> ensure_fqdn_exists method says
return true if Host.unscoped.find_by_name(fqdn) || host_or_hostgroup.try(:new_record?) || (host_or_hostgroup.present? && host_or_hostgroup.type_changed? && host_or_hostgroup.type == "Host::Managed")
and https://github.com/theforeman/foreman/blob/develop/app/models/host/base.rb#L325-L327 that says
def lookup_value_match "fqdn=#{fqdn || name}" end
If the customer says
"fqdn matcher with the name -> foo.example.com" -> "Host.unscoped.find_by_name(fqdn)" is going to fail.
OTOH
If the customer says
"fqdn matcher with the name -> foo" -> "fqdn=#{fqdn || name}" of the lookup_value_match is going to fail.
Appropriate solution is to either
1) make the fqdn matcher actually match fqdn facts along with name (so Host.unscoped.find_by_name or Host.unscoped.find_by_fqdn_fact) . Somethign like
diff --git a/app/models/lookup_value.rb b/app/models/lookup_value.rb index 24741d24a6..3a557b8b3c 100644 --- a/app/models/lookup_value.rb +++ b/app/models/lookup_value.rb @@ -81,7 +81,8 @@ def ensure_fqdn_exists md = ensure_matcher(/fqdn=(.*)/) return md if md == true || md == false fqdn = md[1].split(LookupKey::KEY_DELM)[0] - return true if Host.unscoped.find_by_name(fqdn) || host_or_hostgroup.try(:new_record?) || + fqdn_matched = Host.unscoped.where(:name => fqdn).any? || Host.unscoped.search_for("facts.fqdn=#{fqdn}").any? + return true if fqdn_matched || host_or_hostgroup.try(:new_record?) || (host_or_hostgroup.present? && host_or_hostgroup.type_changed? && host_or_hostgroup.type == "Host::Managed") errors.add(:match, _("%{match} does not match an existing host") % { :match => "fqdn=#{fqdn}" })
OR
2)
Create a new short hostname matcher and add the appropriate plumbing so that the customer can set a matcher that says
"shortname with value => foo" and make that figure out.