Revision f8d94608
Added by Amos Benari over 11 years ago
- ID f8d946082e58b60213a27ded3e1e5f5373d976de
app/models/lookup_value.rb | ||
---|---|---|
class LookupValue < ActiveRecord::Base
|
||
include Authorization
|
||
belongs_to :lookup_key
|
||
belongs_to :lookup_key, :counter_cache => true
|
||
validates_uniqueness_of :match, :scope => :lookup_key_id
|
||
validates_presence_of :match, :value
|
||
validates_presence_of :match
|
||
delegate :key, :to => :lookup_key
|
||
validate :validate_range, :validate_list, :validate_regexp, :validate_match
|
||
before_validation :sanitize_match
|
||
before_validation :validate_and_cast_value
|
||
validate :validate_list, :validate_regexp
|
||
|
||
serialize :value
|
||
|
||
scope :default, :conditions => { :match => "default" }, :limit => 1
|
||
|
||
... | ... | |
value
|
||
end
|
||
|
||
private
|
||
def value_before_type_cast
|
||
return self.value if lookup_key.nil?
|
||
lookup_key.value_before_type_cast self.value
|
||
end
|
||
|
||
# TODO: ensures that the match contain only allowed path elements
|
||
def validate_match
|
||
def as_json(options={})
|
||
super({:only => [:value, :match, :lookup_key_id, :id]}.merge(options))
|
||
end
|
||
|
||
private
|
||
|
||
#TODO check multi match with matchers that have space (hostgroup = web servers,environment = production)
|
||
def sanitize_match
|
||
self.match = match.split(LookupKey::KEY_DELM).map {|s| s.split(LookupKey::EQ_DELM).map(&:strip).join(LookupKey::EQ_DELM)}.join(LookupKey::KEY_DELM) unless match.blank?
|
||
end
|
||
|
||
def validate_and_cast_value
|
||
return true if self.marked_for_destruction?
|
||
begin
|
||
self.value = lookup_key.cast_validate_value self.value
|
||
true
|
||
rescue
|
||
errors.add(:value, "is invalid #{lookup_key.key_type}")
|
||
false
|
||
end
|
||
end
|
||
|
||
def validate_regexp
|
||
return true unless (lookup_key.validator_type == 'regexp')
|
||
errors.add(:value, "is invalid") and return false unless (value =~ /#{lookup_key.validator_rule}/)
|
||
end
|
||
|
||
def validate_range
|
||
return true unless (lookup_key.validator_type == 'range')
|
||
errors.add(:value, "not within range #{lookup_key.validator_rule}") and return false unless eval(lookup_key.validator_rule).include?(value)
|
||
end
|
||
|
||
def validate_list
|
||
return true unless (lookup_key.validator_type == 'list')
|
||
errors.add(:value, "not in list") and return false unless lookup_key.validator_rule.split(LookupKey::KEY_DELM).map(&:strip).include?(value)
|
||
end
|
||
|
||
def as_json(options={})
|
||
super({:only => [:value, :match, :lookup_key_id, :id]}.merge(options))
|
||
errors.add(:value, "#{value} is not one of #{lookup_key.validator_rule}") and return false unless lookup_key.validator_rule.split(LookupKey::KEY_DELM).map(&:strip).include?(value)
|
||
end
|
||
|
||
end
|
Also available in: Unified diff
fixes #832 - adds parameterized class support
Credits:
This patch adds the following featuresThis patch is based on the original work of Olivier Favre
<olivier@yakaz.com> many many thanks!
signature - each class can have a different set of parameters per environment.
users can add regexp or class names that the importer should ignore.
common usage case for this is classes such as ::config, ::install etc.
in order to use the new ENC format for puppet 2.6.5+ you should enable the
Parametrized_Classes_in_ENC and Enable_Smart_Variables_in_ENC within Foreman Settings
This is the initial patch just to get param classes support in, follow-up patches
would include a better UI and the relevant UI updates to host edit page etc.
Signed-off-by: Ohad Levy <ohadlevy@gmail.com>