foreman/app/models/lookup_keys/lookup_key.rb @ 6b4b489f
0265427b | Ohad Levy | class LookupKey < ActiveRecord::Base
|
|
acfbc458 | Marek Hulan | include Authorizable
|
|
ae4998bc | Tomer Brisker | include CounterCacheFix
|
|
9afa092e | Ohad Levy | ||
4c4f30bc | Tomer Brisker | KEY_TYPES = [N_("string"), N_("boolean"), N_("integer"), N_("real"), N_("array"), N_("hash"), N_("yaml"), N_("json")]
|
|
VALIDATOR_TYPES = [N_("regexp"), N_("list") ]
|
|||
f8d94608 | Amos Benari | ||
9afa092e | Ohad Levy | KEY_DELM = ","
|
|
EQ_DELM = "="
|
|||
09ce8a63 | Ori Rabin | VALUE_REGEX =/\A[^#{KEY_DELM}]+#{EQ_DELM}[^#{KEY_DELM}]+(#{KEY_DELM}[^#{KEY_DELM}]+#{EQ_DELM}[^#{KEY_DELM}]+)*\Z/
|
|
9afa092e | Ohad Levy | ||
09f31315 | Tomer Brisker | audited :associated_with => :audit_class, :allow_mass_assignment => true, :except => :lookup_values_count
|
|
3034e8e2 | Ori Rabin | validates_lengths_from_database
|
|
971fee79 | Dominic Cleal | ||
f8d94608 | Amos Benari | serialize :default_value
|
|
9afa092e | Ohad Levy | has_many :lookup_values, :dependent => :destroy, :inverse_of => :lookup_key
|
|
8e53b7c9 | Ori Rabin | accepts_nested_attributes_for :lookup_values,
|
|
09ce8a63 | Ori Rabin | :reject_if => :reject_invalid_lookup_values,
|
|
8e53b7c9 | Ori Rabin | :allow_destroy => true
|
|
f8d94608 | Amos Benari | ||
6b4b489f | Ori Rabin | before_validation :cast_default_value
|
|
3034e8e2 | Ori Rabin | ||
f2c78d4a | Joseph Magen | validates :key, :presence => true
|
|
cba97ace | Joseph Magen | validates :validator_type, :inclusion => { :in => VALIDATOR_TYPES, :message => N_("invalid")}, :allow_blank => true, :allow_nil => true
|
|
validates :key_type, :inclusion => {:in => KEY_TYPES, :message => N_("invalid")}, :allow_blank => true, :allow_nil => true
|
|||
9f88e8a1 | Tom Caspy | validate :validate_default_value
|
|
0265427b | Ohad Levy | validates_associated :lookup_values
|
|
6b4b489f | Ori Rabin | validate :disable_merge_overrides, :disable_avoid_duplicates, :disable_merge_default
|
|
0265427b | Ohad Levy | ||
9afa092e | Ohad Levy | before_save :sanitize_path
|
|
3b656b8d | Joseph Mitchell Magen | attr_name :key
|
|
9afa092e | Ohad Levy | ||
6b4b489f | Ori Rabin | def self.inherited(child)
|
|
child.instance_eval do
|
|||
scoped_search :on => :key, :complete_value => true, :default_order => true
|
|||
scoped_search :on => :lookup_values_count, :rename => :values_count
|
|||
scoped_search :on => :override, :complete_value => {:true => true, :false => false}
|
|||
scoped_search :on => :merge_overrides, :complete_value => {:true => true, :false => false}
|
|||
scoped_search :on => :merge_default, :complete_value => {:true => true, :false => false}
|
|||
scoped_search :on => :avoid_duplicates, :complete_value => {:true => true, :false => false}
|
|||
scoped_search :in => :lookup_values, :on => :value, :rename => :value, :complete_value => true
|
|||
end
|
|||
end
|
|||
9afa092e | Ohad Levy | ||
bb3572ff | Daniel Lobato | default_scope -> { order('lookup_keys.key') }
|
|
f2c78d4a | Joseph Magen | ||
bb3572ff | Daniel Lobato | scope :override, -> { where(:override => true) }
|
|
9afa092e | Ohad Levy | ||
21bf889a | Joseph Mitchell Magen | # new methods for API instead of revealing db names
|
|
alias_attribute :parameter, :key
|
|||
alias_attribute :variable, :key
|
|||
alias_attribute :parameter_type, :key_type
|
|||
alias_attribute :variable_type, :key_type
|
|||
alias_attribute :override_value_order, :path
|
|||
alias_attribute :override_values_count, :lookup_values_count
|
|||
bbf64d99 | Joseph Magen | alias_attribute :override_values, :lookup_values
|
|
alias_attribute :override_value_ids, :lookup_value_ids
|
|||
21bf889a | Joseph Mitchell Magen | ||
# to prevent errors caused by find_resource from override_values controller
|
|||
def self.find_by_name(str)
|
|||
nil
|
|||
end
|
|||
6b4b489f | Ori Rabin | def audit_class
|
|
self
|
|||
21bf889a | Joseph Mitchell Magen | end
|
|
6b4b489f | Ori Rabin | def to_label
|
|
"#{audit_class}::#{key}"
|
|||
21bf889a | Joseph Mitchell Magen | end
|
|
ea4eec44 | Ori Rabin | def supports_merge?
|
|
['array', 'hash'].include?(key_type)
|
|||
end
|
|||
def supports_uniq?
|
|||
key_type == 'array'
|
|||
end
|
|||
9afa092e | Ohad Levy | def to_param
|
|
e768c976 | Tomas Strachota | Parameterizable.parameterize("#{id}-#{key}")
|
|
0265427b | Ohad Levy | end
|
|
b28fdce4 | Ohad Levy | def to_s
|
|
key
|
|||
end
|
|||
9afa092e | Ohad Levy | def path
|
|
f8d94608 | Amos Benari | path = read_attribute(:path)
|
|
path.blank? ? array2path(Setting["Default_variables_Lookup_Path"]) : path
|
|||
9afa092e | Ohad Levy | end
|
|
def path=(v)
|
|||
77a8080a | Ohad Levy | return unless v
|
|
f8d94608 | Amos Benari | using_default = v.tr("\r","") == array2path(Setting["Default_variables_Lookup_Path"])
|
|
write_attribute(:path, using_default ? nil : v)
|
|||
end
|
|||
09ce8a63 | Ori Rabin | def reject_invalid_lookup_values(attributes)
|
|
attributes[:match].empty? ||
|
|||
(attributes[:value].blank? &&
|
|||
(attributes[:use_puppet_default].nil? || attributes[:use_puppet_default] == "0"))
|
|||
end
|
|||
f8d94608 | Amos Benari | def default_value_before_type_cast
|
|
value_before_type_cast default_value
|
|||
end
|
|||
5f029ed6 | Daniel Lobato | def value_before_type_cast(val)
|
|
75bd351e | Ohad Levy | case key_type.to_sym
|
|
801a90d1 | Stephen Benjamin | when :json, :array
|
|
8dcb01a4 | Shlomi Zadok | begin
|
|
val = JSON.dump(val)
|
|||
rescue JSON::GeneratorError => error
|
|||
## http://projects.theforeman.org/issues/9553
|
|||
## @TODO: remove when upgrading to json >= 1.8
|
|||
logger.debug "Fallback to quirks mode from error: '#{error}'"
|
|||
val = JSON.dump_in_quirks_mode(val)
|
|||
end
|
|||
f8d94608 | Amos Benari | when :yaml, :hash
|
|
val = YAML.dump val
|
|||
d076d573 | Joseph Mitchell Magen | val.sub!(/\A---\s*$\n/, '')
|
|
75bd351e | Ohad Levy | end unless key_type.blank?
|
|
f8d94608 | Amos Benari | val
|
|
end
|
|||
71291a46 | Dmitri Dolguikh | def path_elements
|
|
path.split.map do |paths|
|
|||
paths.split(KEY_DELM).map do |element|
|
|||
element
|
|||
end
|
|||
end
|
|||
end
|
|||
f8a56f5b | Marek Hulan | def contains_erb?(value)
|
|
value =~ /<%.*%>/
|
|||
end
|
|||
0575b5b0 | Tom Caspy | def overridden?(host)
|
|
return false unless host.is_a?(Host::Base) || host.is_a?(Hostgroup)
|
|||
lookup_values.find_by_match(host.send(:lookup_value_match)).present?
|
|||
end
|
|||
6b4b489f | Ori Rabin | def puppet?
|
|
false
|
|||
end
|
|||
9afa092e | Ohad Levy | private
|
|
# Generate possible lookup values type matches to a given host
|
|||
5f029ed6 | Daniel Lobato | def path2matches(host)
|
|
bfbf7ed8 | Lukas Zapletal | raise ::Foreman::Exception.new(N_("Invalid Host")) unless host.class.model_name == "Host"
|
|
9afa092e | Ohad Levy | matches = []
|
|
path_elements.each do |rule|
|
|||
match = []
|
|||
rule.each do |element|
|
|||
match << "#{element}#{EQ_DELM}#{attr_to_value(host,element)}"
|
|||
end
|
|||
matches << match.join(KEY_DELM)
|
|||
end
|
|||
matches
|
|||
end
|
|||
# translates an element such as domain to its real value per host
|
|||
8ea536e3 | Ohad Levy | # tries to find the host attribute first, parameters and then fallback to a puppet fact.
|
|
5f029ed6 | Daniel Lobato | def attr_to_value(host, element)
|
|
9afa092e | Ohad Levy | # direct host attribute
|
|
return host.send(element) if host.respond_to?(element)
|
|||
8ea536e3 | Ohad Levy | # host parameter
|
|
return host.host_params[element] if host.host_params.include?(element)
|
|||
9afa092e | Ohad Levy | # fact attribute
|
|
67799065 | Ohad Levy | if (fn = host.fact_names.first(:conditions => { :name => element }))
|
|
017e1049 | Ohad Levy | return FactValue.where(:host_id => host.id, :fact_name_id => fn.id).first.value
|
|
9afa092e | Ohad Levy | end
|
|
end
|
|||
def sanitize_path
|
|||
self.path = path.tr("\s","").downcase unless path.blank?
|
|||
end
|
|||
5f029ed6 | Daniel Lobato | def array2path(array)
|
|
bfbf7ed8 | Lukas Zapletal | raise ::Foreman::Exception.new(N_("invalid path")) unless array.is_a?(Array)
|
|
9afa092e | Ohad Levy | array.map do |sub_array|
|
|
sub_array.is_a?(Array) ? sub_array.join(KEY_DELM) : sub_array
|
|||
end.join("\n")
|
|||
end
|
|||
9f88e8a1 | Tom Caspy | def cast_default_value
|
|
f8a56f5b | Marek Hulan | return true if default_value.nil? || contains_erb?(default_value)
|
|
f8d94608 | Amos Benari | begin
|
|
9f88e8a1 | Tom Caspy | Foreman::Parameters::Caster.new(self, :attribute_name => :default_value, :to => key_type).cast!
|
|
f8d94608 | Amos Benari | true
|
|
rescue
|
|||
bfbf7ed8 | Lukas Zapletal | errors.add(:default_value, _("is invalid"))
|
|
f8d94608 | Amos Benari | false
|
|
end
|
|||
end
|
|||
5f029ed6 | Daniel Lobato | def load_yaml_or_json(value)
|
|
f8d94608 | Amos Benari | return value unless value.is_a? String
|
|
begin
|
|||
JSON.load value
|
|||
rescue
|
|||
YAML.load value
|
|||
end
|
|||
end
|
|||
9f88e8a1 | Tom Caspy | def validate_default_value
|
|
Foreman::Parameters::Validator.new(self,
|
|||
:type => validator_type,
|
|||
:validate_with => validator_rule,
|
|||
:getter => :default_value).validate!
|
|||
9afa092e | Ohad Levy | end
|
|
ea4eec44 | Ori Rabin | def disable_merge_overrides
|
|
if merge_overrides && !supports_merge?
|
|||
self.errors.add(:merge_overrides, _("can only be set for array or hash"))
|
|||
end
|
|||
end
|
|||
def disable_avoid_duplicates
|
|||
if avoid_duplicates && (!merge_overrides || !supports_uniq?)
|
|||
self.errors.add(:avoid_duplicates, _("can only be set for arrays that have merge_overrides set to true"))
|
|||
end
|
|||
end
|
|||
8347a5ef | Ori Rabin | ||
def disable_merge_default
|
|||
if merge_default && !merge_overrides
|
|||
self.errors.add(:merge_default, _("can only be set when merge overrides is set"))
|
|||
end
|
|||
end
|
|||
0265427b | Ohad Levy | end
|