Revision 9f88e8a1
Added by Tom Caspy almost 9 years ago
app/models/lookup_value.rb | ||
---|---|---|
validate :value_present?
|
||
delegate :key, :to => :lookup_key
|
||
before_validation :sanitize_match
|
||
|
||
before_validation :validate_and_cast_value, :unless => Proc.new{|p| p.use_puppet_default }
|
||
validate :validate_list, :validate_regexp, :ensure_fqdn_exists, :ensure_hostgroup_exists
|
||
validate :validate_value, :ensure_fqdn_exists, :ensure_hostgroup_exists
|
||
|
||
attr_accessor :host_or_hostgroup
|
||
attr_writer :managed_id, :hostgroup_id
|
||
... | ... | |
lookup_key.value_before_type_cast self.value
|
||
end
|
||
|
||
def validate_value
|
||
Foreman::Parameters::Validator.new(self,
|
||
:type => lookup_key.validator_type,
|
||
:validate_with => lookup_key.validator_rule,
|
||
:getter => :value).validate!
|
||
end
|
||
|
||
private
|
||
|
||
#TODO check multi match with matchers that have space (hostgroup = web servers,environment = production)
|
||
... | ... | |
return true if self.marked_for_destruction? or !self.value.is_a? String
|
||
begin
|
||
unless self.lookup_key.contains_erb?(value)
|
||
self.value = lookup_key.cast_validate_value self.value
|
||
Foreman::Parameters::Caster.new(self, :attribute_name => :value, :to => lookup_key.key_type).cast!
|
||
end
|
||
true
|
||
rescue StandardError, SyntaxError => e
|
||
... | ... | |
end
|
||
end
|
||
|
||
def validate_regexp
|
||
return true if (lookup_key.validator_type != 'regexp' || (lookup_key.contains_erb?(value) && Setting[:interpolate_erb_in_parameters]))
|
||
valid = (value =~ /#{lookup_key.validator_rule}/)
|
||
errors.add(:value, _("is invalid")) unless valid
|
||
valid
|
||
end
|
||
|
||
def validate_list
|
||
return true if (lookup_key.validator_type != 'list' || (lookup_key.contains_erb?(value) && Setting[:interpolate_erb_in_parameters]))
|
||
valid = lookup_key.validator_rule.split(LookupKey::KEY_DELM).map(&:strip).include?(value)
|
||
errors.add(:value, _("%{value} is not one of %{rules}") % { :value => value, :rules => lookup_key.validator_rule }) unless valid
|
||
valid
|
||
end
|
||
|
||
def ensure_fqdn_exists
|
||
md = ensure_matcher(/fqdn=(.*)/)
|
||
return md if md == true || md == false
|
Also available in: Unified diff
fixes #10232 - moving validations and casting out of lookup key and value