/
lookup_key.rb
321 lines (267 loc) · 9.84 KB
/
lookup_key.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
class LookupKey < ActiveRecord::Base
include Authorizable
include CounterCacheFix
KEY_TYPES = [N_("string"), N_("boolean"), N_("integer"), N_("real"), N_("array"), N_("hash"), N_("yaml"), N_("json")]
VALIDATOR_TYPES = [N_("regexp"), N_("list") ]
KEY_DELM = ","
EQ_DELM = "="
audited :associated_with => :audit_class, :allow_mass_assignment => true
validates_lengths_from_database
serialize :default_value
belongs_to :puppetclass, :inverse_of => :lookup_keys, :counter_cache => true
has_many :environment_classes, :dependent => :destroy
has_many :environments, :through => :environment_classes, :uniq => true
has_many :param_classes, :through => :environment_classes, :source => :puppetclass
def param_class
param_classes.first
end
def audit_class
param_class || puppetclass
end
has_many :lookup_values, :dependent => :destroy, :inverse_of => :lookup_key
accepts_nested_attributes_for :lookup_values,
:reject_if => lambda { |a| a[:value].blank? && (a[:use_puppet_default].nil? || a[:use_puppet_default] == "0")},
:allow_destroy => true
before_validation :validate_and_cast_default_value
validates :key, :uniqueness => {:scope => :is_param }, :unless => Proc.new{|p| p.is_param?}
validates :key, :presence => true
validates :puppetclass, :presence => true, :unless => Proc.new {|k| k.is_param?}
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
validate :validate_list, :validate_regexp
validates_associated :lookup_values
validate :ensure_type, :disable_merge_overrides, :disable_avoid_duplicates
before_save :sanitize_path
attr_name :key
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 => :avoid_duplicates, :complete_value => {:true => true, :false => false}
scoped_search :in => :param_classes, :on => :name, :rename => :puppetclass, :complete_value => true
scoped_search :in => :lookup_values, :on => :value, :rename => :value, :complete_value => true
default_scope lambda { order('lookup_keys.key') }
scope :override, lambda { where(:override => true) }
scope :smart_class_parameters_for_class, lambda {|puppetclass_ids, environment_id|
joins(:environment_classes).where(:environment_classes => {:puppetclass_id => puppetclass_ids, :environment_id => environment_id})
}
scope :parameters_for_class, lambda {|puppetclass_ids, environment_id|
override.smart_class_parameters_for_class(puppetclass_ids,environment_id)
}
scope :global_parameters_for_class, lambda {|puppetclass_ids|
where(:puppetclass_id => puppetclass_ids)
}
scope :smart_variables, lambda { where('lookup_keys.puppetclass_id > 0').readonly(false) }
scope :smart_class_parameters, lambda { where(:is_param => true).joins(:environment_classes).readonly(false) }
# 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
alias_attribute :override_values, :lookup_values
alias_attribute :override_value_ids, :lookup_value_ids
# to prevent errors caused by find_resource from override_values controller
def self.find_by_name(str)
nil
end
def to_label
"#{audit_class}::#{key}"
end
def is_smart_variable?
puppetclass_id.to_i > 0
end
def is_smart_class_parameter?
is_param? && environment_classes.any?
end
def supports_merge?
['array', 'hash'].include?(key_type)
end
def supports_uniq?
key_type == 'array'
end
def to_param
Parameterizable.parameterize("#{id}-#{key}")
end
def to_s
key
end
def path
path = read_attribute(:path)
path.blank? ? array2path(Setting["Default_variables_Lookup_Path"]) : path
end
def path=(v)
return unless v
using_default = v.tr("\r","") == array2path(Setting["Default_variables_Lookup_Path"])
write_attribute(:path, using_default ? nil : v)
end
def default_value_before_type_cast
value_before_type_cast default_value
end
def value_before_type_cast(val)
case key_type.to_sym
when :json, :array
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
when :yaml, :hash
val = YAML.dump val
val.sub!(/\A---\s*$\n/, '')
end unless key_type.blank?
val
end
# Returns the casted value, or raises a TypeError
def cast_validate_value(value)
method = "cast_value_#{key_type}".to_sym
return value unless self.respond_to? method, true
self.send(method, value) rescue raise TypeError
end
def path_elements
path.split.map do |paths|
paths.split(KEY_DELM).map do |element|
element
end
end
end
def contains_erb?(value)
value =~ /<%.*%>/
end
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
private
# Generate possible lookup values type matches to a given host
def path2matches(host)
raise ::Foreman::Exception.new(N_("Invalid Host")) unless host.class.model_name == "Host"
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
# tries to find the host attribute first, parameters and then fallback to a puppet fact.
def attr_to_value(host, element)
# direct host attribute
return host.send(element) if host.respond_to?(element)
# host parameter
return host.host_params[element] if host.host_params.include?(element)
# fact attribute
if (fn = host.fact_names.first(:conditions => { :name => element }))
return FactValue.where(:host_id => host.id, :fact_name_id => fn.id).first.value
end
end
def sanitize_path
self.path = path.tr("\s","").downcase unless path.blank?
end
def array2path(array)
raise ::Foreman::Exception.new(N_("invalid path")) unless array.is_a?(Array)
array.map do |sub_array|
sub_array.is_a?(Array) ? sub_array.join(KEY_DELM) : sub_array
end.join("\n")
end
def validate_and_cast_default_value
return true if default_value.nil? || contains_erb?(default_value)
begin
self.default_value = cast_validate_value self.default_value
true
rescue
errors.add(:default_value, _("is invalid"))
false
end
end
def cast_value_boolean(value)
casted = Foreman::Cast.to_bool(value)
raise TypeError if casted.nil?
casted
end
def cast_value_integer(value)
return value.to_i if value.is_a?(Numeric)
if value.is_a?(String)
if value =~ /^0x[0-9a-f]+$/i
value.to_i(16)
elsif value =~ /^0[0-7]+$/
value.to_i(8)
elsif value =~ /^-?\d+$/
value.to_i
else
raise TypeError
end
end
end
def cast_value_real(value)
return value if value.is_a? Numeric
if value.is_a?(String)
if value =~ /\A[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?\Z/
value.to_f
else
cast_value_integer value
end
end
end
def load_yaml_or_json(value)
return value unless value.is_a? String
begin
JSON.load value
rescue
YAML.load value
end
end
def cast_value_array(value)
return value if value.is_a? Array
return value.to_a if not value.is_a? String and value.is_a? Enumerable
value = load_yaml_or_json value
raise TypeError unless value.is_a? Array
value
end
def cast_value_hash(value)
return value if value.is_a? Hash
value = load_yaml_or_json value
raise TypeError unless value.is_a? Hash
value
end
def cast_value_yaml(value)
YAML.load value
end
def cast_value_json(value)
JSON.load value
end
def ensure_type
if puppetclass_id.present? and is_param?
self.errors.add(:base, _('Global variable or class Parameter, not both'))
end
end
def validate_regexp
return true if (validator_type != 'regexp' || (contains_erb?(default_value) && Setting[:interpolate_erb_in_parameters]))
valid = (default_value =~ /#{validator_rule}/)
errors.add(:default_value, _("is invalid")) unless valid
valid
end
def validate_list
return true if (validator_type != 'list' || (contains_erb?(default_value) && Setting[:interpolate_erb_in_parameters]))
valid = validator_rule.split(KEY_DELM).map(&:strip).include?(default_value)
errors.add(:default_value, _("%{default_value} is not one of %{validator_rule}") % { :default_value => default_value, :validator_rule => validator_rule }) unless valid
valid
end
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
end