forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
classification.rb
594 lines (464 loc) · 18 KB
/
classification.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
class Classification < ApplicationRecord
acts_as_tree
include ReadOnlyMixin
belongs_to :tag
virtual_column :name, :type => :string
virtual_column :ns, :type => :string
# from acts_as_tree
alias entries children
before_save :save_tag
before_destroy :validate_tag_mapping, :delete_tags_and_entries
validates :description, :presence => true, :length => {:maximum => 255}, :unique_within_region => {:scope => :parent_id}
NAME_MAX_LENGTH = 50
validates :name, :presence => true, :length => {:maximum => NAME_MAX_LENGTH}
validate :validate_format_of_name
validate :validate_uniqueness_on_tag_name
validates :syntax, :inclusion => {:in => %w[string integer boolean],
:message => "should be one of 'string', 'integer' or 'boolean'"}
scope :visible, -> { where(:show => true) }
scope :read_only, -> { where(:read_only => true) }
scope :writeable, -> { where(:read_only => false) }
scope :is_category, -> { where(:parent_id => nil) }
scope :is_entry, -> { where.not(:parent_id => nil) }
scope :with_writable_parents, -> { includes(:parent).where(:parents_classifications => {:read_only => false}) }
DEFAULT_NAMESPACE = "/managed".freeze
attribute :read_only, :default => false
attribute :syntax, :default => "string"
attribute :single_value, :default => false
attribute :show, :default => true
FIXTURE_FILE = FIXTURE_DIR.join("classifications.yml")
def self.hash_all_by_type_and_name(conditions = {})
ret = {}
where(conditions).is_category.includes(:tag).each do |c|
ret.store_path(c.name, :category, c)
end
where(conditions).is_entry.includes(:tag, :parent => :tag).each do |e|
ret.store_path(e.parent.name, :entry, e.name, e) unless e.parent.nil?
end
ret
end
def self.parent_ids(parent_ids)
where(:parent_id => parent_ids)
end
def self.tags_arel
Tag.arel_table
end
def self.with_tag_name
select(arel_table[Arel.star], tags_arel[:name].as('tag_name'))
.joins(:tag)
end
def self.managed
with_tag_name.where(tags_arel[:name].matches_regexp("/managed/[^\\/]+$"))
end
attr_writer :ns, :name
def ns
@ns ||= DEFAULT_NAMESPACE if new_record?
return @ns if tag.nil?
return @ns unless @ns.nil?
if category?
@ns = tag2ns(tag.name)
else
@ns = tag2ns(parent.tag.name) unless parent_id.nil?
end
end
# Disable certain rubocop rules that don't really work here. Namely the
# find_by warnings and .zero? warnings because of custom methods and
# cases where objects could be zero or a real object.
# rubocop:disable Rails/DynamicFindBy
# rubocop:disable Style/NumericPredicate
def self.classify(obj, category_name, entry_name, is_request = true)
cat = Classification.lookup_by_name(category_name, obj.region_id)
return " - FAILED. Tag category '#{category_name}' not found in region #{obj.region_id}" if cat.nil?
ent = cat.find_entry_by_name(entry_name, obj.region_id)
return " - FAILED. Tag name '#{entry_name}' not found in region #{obj.region_id}" if ent.nil?
return " - FAILED. Object already tagged with tag namespace set to 'none'" if obj.is_tagged_with?(ent.to_tag, :ns => "none")
ent.assign_entry_to(obj, is_request)
" - SUCCESS."
end
def self.unclassify(obj, category_name, entry_name, is_request = true)
cat = Classification.lookup_by_name(category_name, obj.region_id)
unless cat.nil?
ent = cat.find_entry_by_name(entry_name, obj.region_id)
ent.remove_entry_from(obj, is_request) unless ent.nil? || !obj.is_tagged_with?(ent.to_tag, :ns => "none")
end
end
def self.classify_by_tag(obj, tag, is_request = true)
parts = tag.split("/")
raise _("Tag %{tag} is not a category entry") % {:tag => tag} unless parts[1] == "managed"
entry_name = parts.pop
category_name = parts.pop
classify(obj, category_name, entry_name, is_request)
end
def self.unclassify_by_tag(obj, tag, is_request = true)
parts = tag.split("/")
raise _("Tag %{tag} is not a category entry") % {:tag => tag} unless parts[1] == "managed"
entry_name = parts.pop
category_name = parts.pop
unclassify(obj, category_name, entry_name, is_request)
end
def self.bulk_reassignment(options = {})
# options = {
# :model => Target class name
# :object_ids => Array of target ids
# :add_ids => Array of entry ids to be assigned to targets
# :delete_ids => Array of entry ids to be unassigned from targets
# }
model = options[:model].constantize
targets = model.where(:id => options[:object_ids]).includes(:taggings, :tags)
adds = where(:id => options[:add_ids]).includes(:tag)
adds.each { |a| raise _("Classification add id: [%{id}] is not an entry") % {:id => a.id} if a.category? }
deletes = where(:id => options[:delete_ids]).includes(:tag)
deletes.each { |d| raise _("Classification delete id: [%{id}] is not an entry") % {:id => d.id} if d.category? }
failed_deletes = Hash.new { |h, k| h[k] = [] }
failed_adds = Hash.new { |h, k| h[k] = [] }
targets.each do |t|
deletes.each do |d|
_log.info("Removing entry name: [#{d.name}] from #{options[:model]} name: #{t.name}")
begin
d.remove_entry_from(t)
rescue => err
_log.error("Error occurred while removing entry name: [#{d.name}] from #{options[:model]} name: #{t.name}")
_log.error("#{err.class} - #{err}")
failed_deletes[t] << d
end
end
adds.each do |a|
_log.info("Adding entry name: [#{a.name}] to #{options[:model]} name: #{t.name}")
begin
a.assign_entry_to(t)
rescue => err
_log.error("Error occurred while adding entry name: [#{a.name}] to #{options[:model]} name: #{t.name}")
_log.error("#{err.class} - #{err}")
failed_adds[t] << a
end
end
end
if failed_deletes.any? || failed_adds.any?
msg = _("Failures occurred during bulk reassignment.")
failed_deletes.each do |target, deletes|
names = deletes.collect(&:name).sort
msg += _(" Unable to remove the following tags from %{class_name} %{id}: %{names}.") %
{:class_name => target.class.name, :id => target.id, :names => names.join(", ")}
end
failed_adds.each do |target, adds|
names = adds.collect(&:name).sort
msg += _(" Unable to add the following tags to %{class_name} %{id}: %{names}.") %
{:class_name => target.class.name, :id => target.id, :names => names.join(", ")}
end
raise msg
end
true
end
def self.get_tags_from_object(obj)
tags = obj.tag_list(:ns => "/managed").split
tags.delete_if { |t| t =~ /^\/folder_path_/ }
end
def self.create_category!(options)
is_category.create!(options)
end
def self.categories(region_id = my_region_number, ns = DEFAULT_NAMESPACE)
cats = is_category.in_region(region_id).includes(:tag, :children)
cats.select { |c| c.ns == ns }
end
def self.category_names_for_perf_by_tag(region_id = my_region_number, ns = DEFAULT_NAMESPACE)
in_region(region_id).is_category.where(:perf_by_tag => true)
.includes(:tag)
.collect { |c| c.name if c.tag2ns(c.tag.name) == ns }
.compact
end
def self.find_assigned_entries(obj, ns = DEFAULT_NAMESPACE)
unless obj.respond_to?(:tag_with)
raise _("Class '%{name}' is not eligible for classification") % {:name => obj.class}
end
tag_ids = obj.tagged_with(:ns => ns).collect(&:id)
where(:tag_id => tag_ids)
end
def self.first_cat_entry(name, obj)
cat = lookup_by_name(name, obj.region_id)
return nil unless cat
find_assigned_entries(obj).each do |e|
return e if e.parent_id == cat.id
end
nil
end
# Splits a fully qualified tag into the namespace, category, and entry
def self.tag_name_split(tag_name)
parts = tag_name.split("/")
parts.shift
parts
end
# Splits a fully qualified tag into the namespace, category object, and entry object
def self.tag_name_to_objects(tag_name)
ns, cat, entry = tag_name_split(tag_name)
cat_obj = lookup_by_name(cat)
entry_obj = cat_obj && cat_obj.find_entry_by_name(entry)
return ns, cat_obj, entry_obj
end
# Builds the given tag into a format usable when calling to_model_hash.
def self.tag_to_model_hash(tag)
ns, cat, entry = tag_name_to_objects(tag.name)
h = {:id => tag.id, :name => tag.name, :namespace => ns}
%w[id name description single_value].each { |m| h[:"category_#{m}"] = cat.send(m) } unless cat.nil?
%w[id name description].each { |m| h[:"entry_#{m}"] = entry.send(m) } unless entry.nil?
h
end
def add_entry(options)
raise _("entries can only be added to classifications") unless category?
# Inherit from parent classification
options.merge!(:read_only => read_only, :syntax => syntax, :single_value => single_value, :ns => ns)
children.create!(options)
end
def lookup_by_entry(type)
raise _("method is only available for an entry") if category?
klass = type.constantize
unless klass.respond_to?(:find_tagged_with)
raise _("Class '%{type}' is not eligible for classification") % {:type => type}
end
klass.find_tagged_with(:any => name, :ns => ns, :cat => parent.name)
end
alias find_by_entry lookup_by_entry
Vmdb::Deprecation.deprecate_methods(self, :find_by_entry => :lookup_by_entry)
def assign_entry_to(obj, is_request = true)
raise _("method is only available for an entry") if category?
unless obj.respond_to?(:tag_with)
raise _("Class '%{name}' is not eligible for classification") % {:name => obj.class}
end
enforce_policy(obj, :request_assign_company_tag) if is_request
if parent.single_value?
obj.tag_with(name, :ns => ns, :cat => parent.name)
else
obj.tag_add(name, :ns => ns, :cat => parent.name)
end
obj.reload
enforce_policy(obj, :assigned_company_tag)
end
def remove_entry_from(obj, is_request = true)
enforce_policy(obj, :request_unassign_company_tag) if is_request
tags = obj.tag_list(:ns => ns, :cat => parent.name).split
tags.delete(name)
obj.tag_with(tags.join(" "), :ns => ns, :cat => parent.name)
obj.reload
enforce_policy(obj, :unassigned_company_tag)
end
def to_tag
tag.name unless tag.nil?
end
def category?
parent_id.nil?
end
def category
parent.try(:name)
end
def tag_name
attribute(:tag_name)
end
def name
@name ||= tag2name(tag_name || tag.name)
end
def self.lookup_category_by_description(description, region_id = my_region_number)
is_category.in_region(region_id).find_by(:description => description)
end
def find_entry_by_name(name, region_id = my_region_number)
self.class.lookup_by_name(name, region_id, ns, self)
end
# @param parent_id [Integer|Parent] node for the parent. This is only passed in for find_entry_by_name
def self.lookup_by_name(name, region_id = my_region_number, name_space = DEFAULT_NAMESPACE, parent_id = nil)
lookup_by_names([name], region_id, name_space, parent_id).first
end
singleton_class.send(:alias_method, :find_by_name, :lookup_by_name)
Vmdb::Deprecation.deprecate_methods(singleton_class, :find_by_name => :lookup_by_name)
# @param parent_id [Integer|Parent] node for the parent. This is only passed in for find_entry_by_name
def self.lookup_by_names(names, region_id = my_region_number, name_space = DEFAULT_NAMESPACE, parent_id = nil)
tag_names = names.map { |name| name2tag(name, parent_id, name_space) }
# NOTE: tags is a subselect - not an array of ids
tags = Tag.in_region(region_id).where(:name => tag_names).select(:id)
where(:tag_id => tags)
end
singleton_class.send(:alias_method, :find_by_names, :lookup_by_names)
Vmdb::Deprecation.deprecate_methods(singleton_class, :find_by_names => :lookup_by_names)
def tag2ns(tag)
unless tag.nil?
ta = tag.split("/")
ta[0..(ta.length - 2)].join("/")
end
end
def enforce_policy(obj, event)
return unless MiqEvent::SUPPORTED_POLICY_AND_ALERT_CLASSES.include?(obj.class.base_class)
return if parent.name == "power_state" # special case for old power state classifications - don't enforce policy since this is being changed by the system
mode = event.to_s.split("_").first # request/after
begin
MiqEvent.raise_evm_event(obj, event)
rescue MiqException::PolicyPreventAction => err
if mode == "request"
# if it's the "before_..." event we can still prevent it from proceeding. Otherwise it's too late.
_log.info("Event: [#{event}], #{err.message}")
raise
end
rescue Exception => err
_log.log_backtrace(err)
raise
end
end
def self.export_to_array
categories.inject([]) do |a, c|
a.concat(c.export_to_array)
end
end
def self.export_to_yaml
export_to_array.to_yaml
end
def export_to_array
h = attributes.except(*%w[id tag_id reserved parent_id])
h["name"] = name
h["entries"] = entries.collect(&:export_to_array).flatten if category?
[h]
end
def export_to_yaml
export_to_array.to_yaml
end
def self.import_from_hash(classification, parent = nil)
raise _("No Classification to Import") if classification.nil?
stats = {"categories" => 0, "entries" => 0}
if parent.nil? # category
cat = lookup_by_name(classification["name"])
if cat
_log.info("Skipping Classification (already in DB): Category: name=[#{classification["name"]}]")
return stats
end
_log.info("Importing Classification: Category: name=[#{classification["name"]}]")
classification.delete("parent_id")
entries = classification.delete("entries")
cat = create(classification)
stats["categories"] += 1
entries.each do |e|
stat, _e = import_from_hash(e, cat)
stats.each_key { |k| stats[k] += stat[k] }
end
return stats, cat
else
entry = parent.find_entry_by_name(classification["name"])
if entry
_log.info("Skipping Classification (already in DB): Category: name: [#{parent.name}], Entry: name=[#{classification["name"]}]")
return stats
end
_log.info("Importing Classification: Category: name: [#{parent.name}], Entry: name=[#{classification["name"]}]")
entry = create(classification.merge("parent_id" => parent.id))
stats["entries"] += 1
return stats, entry
end
end
def self.import_from_yaml(fd)
stats = {"categories" => 0, "entries" => 0}
input = YAML.load(fd)
input.each do |c|
stat, _c = import_from_hash(c)
stats.each_key { |k| stats[k] += stat[k] }
end
stats
end
# rubocop:disable Rails/SkipsModelValidations
def self.seed
YAML.load_file(FIXTURE_FILE).each do |c|
category = lookup_by_name(c[:name], my_region_number, (c[:ns] || DEFAULT_NAMESPACE))
next if category
category = is_category.new(c.except(:entries))
next unless category.valid? # HACK: Skip seeding if categories aren't valid/unique
_log.info("Creating category #{c[:name]}")
category.save!
add_entries_from_hash(category, c[:entries])
end
end
# rubocop:enable Rails/SkipsModelValidations
def self.sanitize_name(name)
name.downcase.tr('^a-z0-9_:', '_')[0, NAME_MAX_LENGTH]
end
def self.display_name(number = 1)
n_('Category', 'Categories', number)
end
def self.tag2human(tag)
c, e = tag.split("/")[2..-1]
cat = lookup_by_name(c)
cname = cat.nil? ? c.titleize : cat.description
ename = e.titleize
unless cat.nil?
ent = cat.find_entry_by_name(e)
ename = ent.description unless ent.nil?
end
"#{cname}: #{ename}"
end
def self.add_entries_from_hash(cat, entries)
entries.each do |entry|
ent = cat.find_entry_by_name(entry[:name])
ent ? ent.update!(entry) : cat.add_entry(entry)
end
end
private_class_method :add_entries_from_hash
def validate_uniqueness_on_tag_name
tag_name = Classification.name2tag(name, parent, ns)
exist_scope = Classification.default_scoped
.includes(:tag)
.where(:tags => {:name => tag_name})
.merge(Tag.in_region(region_id))
exist_scope = exist_scope.where.not(:id => id) unless new_record?
errors.add("name", "has already been taken") if exist_scope.exists?
end
def self.name2tag(name, parent_id = nil, ns = DEFAULT_NAMESPACE)
if parent_id.nil?
File.join(ns, name)
else
c = parent_id.kind_of?(Classification) ? parent_id : Classification.find(parent_id)
File.join(ns, c.name, name) if c
end
end
private
def validate_format_of_name
unless (name =~ /[^a-z0-9_:]/).nil?
errors.add("name", "must be lowercase alphanumeric characters, colons and underscores without spaces")
end
end
def tag2name(tag)
File.split(tag).last unless tag.nil?
end
def find_tag
tag_name = Classification.name2tag(name, parent, ns)
Tag.in_region(region_id).find_by(:name => tag_name)
end
def save_tag
tag_name = Classification.name2tag(name, parent, ns)
if tag_id.present? || tag.present?
tag.update(:name => tag_name) unless tag.name == tag_name
else
self.tag = Tag.in_region(region_id).find_or_create_by(:name => tag_name)
end
end
def delete_all_entries
entries.each do |e|
e.delete_assignments
e.delete_tag_and_taggings
end
end
def delete_assignments
AssignmentMixin.all_assignments(tag.name).destroy_all
end
def delete_tag_and_taggings
tag = find_tag
return if tag.nil?
tag.destroy
end
def delete_tags_and_entries
if category?
delete_all_entries
else # entry
delete_assignments
end
delete_tag_and_taggings
end
def validate_tag_mapping
if tag&.provider_tag_mappings&.any?
errors.add("", _("A Tag Mapping exists for this category and must be removed before deleting"))
throw :abort
end
end
# rubocop:enable Style/NumericPredicate
# rubocop:enable Rails/DynamicFindBy
end