forked from ManageIQ/manageiq-ui-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.rb
1279 lines (1169 loc) · 52.1 KB
/
buttons.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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module ApplicationController::Buttons
extend ActiveSupport::Concern
included do
include Mixins::PlaybookOptions
end
def ab_group_edit
assert_privileges("ab_group_edit")
group_new_edit("edit")
end
def ab_group_new
assert_privileges("ab_group_new")
group_new_edit("new")
end
def ab_group_reorder
assert_privileges("ab_group_reorder")
case params[:button]
when "cancel"
add_flash(_("Button Group Reorder cancelled"))
@edit = session[:edit] = nil # clean out the saved info
ab_get_node_info(x_node) if x_active_tree == :ab_tree
replace_right_cell(:nodetype => x_node)
when "save"
return unless load_edit("group_reorder", "replace_cell__explorer")
# save group_index of each custombuttonset in set_data
if x_active_tree == :sandt_tree
button_order = []
st = ServiceTemplate.find_by_id(@sb[:applies_to_id])
end
@edit[:new][:fields].each_with_index do |field, i|
field_nodes = field.last.split('-')
button_order.push(field.last) if x_active_tree == :sandt_tree
if field_nodes.first == "cbg"
cs = CustomButtonSet.find_by_id(field_nodes.last)
cs.set_data[:group_index] = i + 1
cs.save!
end
end
if x_active_tree == :sandt_tree
st.options[:button_order] = button_order
st.save
end
add_flash(_("Button Group Reorder saved"))
@edit = session[:edit] = nil # clean out the saved info
ab_get_node_info(x_node) if x_active_tree == :ab_tree
replace_right_cell(:nodetype => x_node, :replace_trees => x_active_tree == :ab_tree ? [:ab] : [:sandt])
else
if params[:button] == "reset"
@changed = session[:changed] = false
add_flash(_("All changes have been reset"), :warning)
end
group_reorder_set_form_vars
@in_a_form = true
@lastaction = "automate_button"
@layout = "miq_ae_automate_button"
replace_right_cell(:nodetype => "group_reorder")
end
end
def group_reorder_field_changed
if params['selected_fields']
return unless load_edit("group_reorder", "replace_cell__explorer")
move_cols_up if params[:button] == "up"
move_cols_down if params[:button] == "down"
@changed = (@edit[:new] != @edit[:current])
@refresh_partial = "group_order_form"
render :update do |page|
page << javascript_prologue
page.replace("flash_msg_div", :partial => "layouts/flash_msg") unless @refresh_div && @refresh_div != "column_lists"
page.replace(@refresh_div, :partial => "shared/buttons/#{@refresh_partial}") if @refresh_div
page << "miqSparkle(false);"
page << javascript_for_miq_button_visibility_changed(@changed)
end
else
add_flash(_("No Button Group was selected!"), :error)
javascript_flash
end
end
def group_create
group_create_update("create")
end
def group_update
group_create_update("update")
end
def automate_button_field_changed
unless params[:target_class]
@edit = session[:edit]
@custom_button = @edit[:custom_button]
if params[:readonly]
@edit[:new][:readonly] = (params[:readonly] != "1")
end
copy_params_if_set(@edit[:new], params, %i(instance_name other_name object_message object_request))
ApplicationController::AE_MAX_RESOLUTION_FIELDS.times do |i|
f = ("attribute_" + (i + 1).to_s)
v = ("value_" + (i + 1).to_s)
@edit[:new][:attrs][i][0] = params[f] if params[f.to_sym]
@edit[:new][:attrs][i][1] = params[v] if params[v.to_sym]
end
@edit[:new][:display] = params[:display] == "1" if params[:display]
@edit[:new][:open_url] = params[:open_url] == "1" if params[:open_url]
copy_params_if_set(@edit[:new], params, %i(name target_attr_name display_for submit_how description button_icon button_color dialog_id disabled_text button_type inventory_type))
visibility_box_edit
if params[:button_type] == 'default'
clear_playbook_variables
end
if params[:button_type] == 'ansible_playbook'
initialize_playbook_variables
@edit[:new][:dialog_id] = nil
@edit[:new][:object_request] = CustomButton::PLAYBOOK_METHOD
end
end
render :update do |page|
page << javascript_prologue
if [:instance_name, :other_name, :target_class, :button_type].any? { |k| params.key?(k) }
@sb[:active_tab] = "ab_options_tab"
page.replace("ab_form", :partial => "shared/buttons/ab_form")
end
if params[:visibility_typ]
page.replace("form_role_visibility", :partial => "layouts/role_visibility", :locals => {:rec_id => (@custom_button.id || "new").to_s, :action => "automate_button_field_changed"})
end
unless params[:target_class]
@changed = session[:changed] = (@edit[:new] != @edit[:current])
page << javascript_for_miq_button_visibility(@changed)
end
page << "miqSparkle(false);"
end
end
# AJAX driven routine to delete a user
def ab_button_delete
assert_privileges("ab_button_delete")
custom_button = CustomButton.find(params[:id])
description = custom_button.description
audit = {:event => "custom_button_record_delete", :message => "[#{custom_button.description}] Record deleted", :target_id => custom_button.id, :target_class => "CustomButton", :userid => session[:userid]}
if custom_button.parent
automation_set = CustomButtonSet.find_by_id(custom_button.parent.id)
if automation_set
mems = automation_set.members
if mems.length > 1
mems.each do |m|
automation_set.remove_member(custom_button) if m.id == custom_button
end
else
automation_set.remove_member(custom_button)
end
end
end
if custom_button.destroy
AuditEvent.success(audit)
add_flash(_("Button \"%{name}\": Delete successful") % {:name => description})
id = x_node.split('_')
id.pop
self.x_node = id.join("_")
ab_get_node_info(x_node) if x_active_tree == :ab_tree
replace_right_cell(:nodetype => x_node, :replace_trees => x_active_tree == :ab_tree ? [:ab] : [:sandt])
else
custom_button.errors.each { |field, msg| add_flash("#{field.to_s.capitalize} #{msg}", :error) }
javascript_flash
end
end
def ab_button_new
assert_privileges("ab_button_new")
button_new_edit("new")
end
def ab_button_edit
assert_privileges("ab_button_edit")
button_new_edit("edit")
end
def button_update
button_create_update("update")
end
def button_create
button_create_update("create")
end
# AJAX driven routine to check for changes in ANY field on the form
def group_form_field_changed
return unless load_edit("bg_edit__#{params[:id]}", "replace_cell__explorer")
group_get_form_vars
@custom_button_set = @edit[:custom_button_set_id] ? CustomButtonSet.find_by_id(@edit[:custom_button_set_id]) : CustomButtonSet.new
@changed = (@edit[:new] != @edit[:current])
render :update do |page|
page << javascript_prologue
page.replace(@refresh_div, :partial => "shared/buttons/#{@refresh_partial}") if @refresh_div
if @flash_array
page.replace("flash_msg_div", :partial => "layouts/flash_msg")
else
page << javascript_for_miq_button_visibility(@changed)
end
end
end
# AJAX driven routine to delete a button group
def ab_group_delete
assert_privileges("ab_group_delete")
if x_node.split('_').last == "ub"
add_flash(_("'Unassigned Button Group' can not be deleted"), :error)
get_node_info
replace_right_cell(:nodetype => x_node)
return
end
custom_button_set = CustomButtonSet.find(params[:id])
description = custom_button_set.description
audit = {:event => "custom_button_set_record_delete", :message => "[#{custom_button_set.description}] Record deleted", :target_id => custom_button_set.id, :target_class => "CustomButtonSet", :userid => session[:userid]}
mems = custom_button_set.members
mems.each do |mem|
uri = CustomButton.find_by_id(mem.id)
uri.save!
custom_button_set.remove_member(mem)
end
if custom_button_set.destroy
AuditEvent.success(audit)
add_flash(_("Button Group \"%{name}\": Delete successful") % {:name => description})
id = x_node.split('_')
id.pop
self.x_node = id.join("_")
ab_get_node_info(x_node) if x_active_tree == :ab_tree
replace_right_cell(:nodetype => x_node, :replace_trees => x_active_tree == :ab_tree ? [:ab] : [:sandt])
else
custom_button_set.errors.each { |field, msg| add_flash("#{field.to_s.capitalize} #{msg}", :error) }
javascript_flash
end
end
private
BASE_MODEL_EXPLORER_CLASSES = [MiqGroup, MiqTemplate, Service, Switch, Tenant, User, Vm].freeze
APPLIES_TO_CLASS_BASE_MODELS = %w(AvailabilityZone CloudNetwork CloudObjectStoreContainer CloudSubnet CloudTenant
CloudVolume ContainerGroup ContainerImage ContainerNode ContainerProject
ContainerTemplate ContainerVolume EmsCluster ExtManagementSystem
GenericObject GenericObjectDefinition Host LoadBalancer
MiqGroup MiqTemp MiqTemplate NetworkRouter OrchestrationStack SecurityGroup Service
ServiceTemplate Storage Switch Tenant User Vm VmOrTemplate).freeze
def applies_to_class_model(applies_to_class)
# TODO: Give a better name for this concept, including ServiceTemplate using Service
# This should probably live in the model once this concept is defined.
unless APPLIES_TO_CLASS_BASE_MODELS.include?(applies_to_class)
raise ArgumentError, "Received: #{applies_to_class}, expected one of #{APPLIES_TO_CLASS_BASE_MODELS}"
end
case applies_to_class
when "ServiceTemplate"
Service
when "GenericObjectDefinition"
GenericObject
else
applies_to_class.constantize
end
end
def custom_button_done
url = SystemConsole.find_by(:vm => params[:id]).try(:url)
if url.present?
javascript_open_window(url)
else
render_flash(_('No url was returned from automate.'), :error)
end
end
def custom_buttons_invoke(button, objs)
if objs.length > 1 &&
(button.options && button.options.key?(:submit_how) && button.options[:submit_how].to_s == 'all')
button.invoke(objs)
else
objs.each { |obj| button.invoke(obj) }
end
end
def sync_playbook_dialog(button)
service_template = ServiceTemplate.find_by(:name => button.uri_attributes[:service_template_name])
dialog_id = nil
if service_template
service_template.resource_actions.each do |ra|
d = Dialog.where(:id => ra.dialog_id).first
dialog_id = d.id if d
end
end
if dialog_id && button.resource_action.dialog_id != dialog_id
button.resource_action.dialog_id = dialog_id
button.save
end
end
def custom_buttons(ids = nil, display_options = {})
button = CustomButton.find_by_id(params[:button_id])
cls = applies_to_class_model(button.applies_to_class)
@explorer = true if BASE_MODEL_EXPLORER_CLASSES.include?(cls)
ids ||= params[:id]
if ids.to_s == 'LIST'
objs = Rbac.filtered(cls.where(:id => find_checked_items))
obj = objs.first
else
obj = Rbac.filtered_object(cls.find(ids.to_i))
objs = [obj]
end
if objs.empty?
render_flash(_("Error executing custom button: No item was selected."), :error)
return
end
@right_cell_text = _("%{record} - \"%{button_text}\"") % {:record => obj.name, :button_text => button.name}
if button.resource_action.dialog_id
sync_playbook_dialog(button) if button.options.try(:[], :button_type) == 'ansible_playbook'
options = {
:header => @right_cell_text,
:target_id => obj.id,
:target_ids => objs.collect(&:id),
:target_kls => obj.class.name,
}
options[:dialog_locals] = DialogLocalService.new.determine_dialog_locals_for_custom_button(obj, button.name, button.resource_action, display_options)
options.merge!(display_options) unless display_options.empty?
dialog_initialize(button.resource_action, options)
elsif button.options && button.options.key?(:open_url) && button.options[:open_url]
# not supported for objs: cannot do wait for task for multiple tasks
task_id = button.invoke_async(obj)
initiate_wait_for_task(:task_id => task_id, :action => :custom_button_done)
else
begin
custom_buttons_invoke(button, objs)
rescue StandardError => bang
add_flash(_("Error executing: \"%{task_description}\" %{error_message}") %
{:task_description => params[:desc], :error_message => bang.message}, :error)
else
add_flash(_("\"%{task_description}\" was executed") % {:task_description => params[:desc]})
end
javascript_flash
end
end
def get_available_dialogs
@edit[:new][:available_dialogs] = {}
Dialog.all.each do |d|
@edit[:new][:available_dialogs][d.id] = d.label
end
end
def group_button_cancel(typ)
if typ == "update"
add_flash(_("Edit of Button Group \"%{name}\" was cancelled by the user") % {:name => @edit[:current][:name]})
else
add_flash(_("Add of new Button Group was cancelled by the user"))
end
@edit = session[:edit] = nil # clean out the saved info
ab_get_node_info(x_node) if x_active_tree == :ab_tree
replace_right_cell(:nodetype => x_node)
end
def group_button_add_save(typ)
assert_privileges(params[:button] == "add" ? "ab_group_new" : "ab_group_edit")
if @edit[:new][:name].blank?
render_flash(_("Name is required"), :error)
return
end
if @edit[:new][:description].blank?
render_flash(_("Description is required"), :error)
return
end
if @edit[:new][:button_icon].blank?
render_flash(_("Button Icon must be selected"), :error)
return
end
group_set_record_vars(@custom_button_set)
member_ids = @edit[:new][:fields].collect { |field| field[1] }
mems = CustomButton.where(:id => member_ids)
if typ == "update"
org_mems = @custom_button_set.members # clean up existing members
org_mems.each do |m|
uri = CustomButton.find(m.id)
uri.save
end
if @custom_button_set.save
if !mems.blank? # replace children if members were added/updated
@custom_button_set.replace_children(mems)
else # remove members if nothing was selected
@custom_button_set.remove_all_children
end
add_flash(_("Button Group \"%{name}\" was saved") % {:name => @edit[:new][:description]})
@edit = session[:edit] = nil # clean out the saved info
ab_get_node_info(x_node) if x_active_tree == :ab_tree
replace_right_cell(:nodetype => x_node, :replace_trees => x_active_tree == :ab_tree ? [:ab] : [:sandt])
else
@custom_button_set.errors.each do |field, msg|
add_flash(_("Error during 'edit': %{field_name} %{error_message}") %
{:field_name => field.to_s.capitalize, :error_message => msg}, :error)
end
@lastaction = "automate_button"
@layout = "miq_ae_automate_button"
render_flash
end
else
# set group_index of new record being added and exiting ones so they are in order incase some were deleted
all_sets = CustomButtonSet.find_all_by_class_name(@edit[:new][:applies_to_class])
all_sets.each_with_index do |group, i|
group.set_data[:group_index] = i + 1
group.save!
end
@custom_button_set.set_data[:group_index] = all_sets.length + 1
if @custom_button_set.save
@custom_button_set.replace_children(mems) unless mems.blank?
if x_active_tree == :sandt_tree
aset = CustomButtonSet.find_by_id(@custom_button_set.id)
# push new button at the end of button_order array
if aset
st = ServiceTemplate.find_by_id(@sb[:applies_to_id])
st.custom_button_sets.push(aset)
st.options[:button_order] ||= []
st.options[:button_order].push("cbg-#{aset.id}")
st.save
end
end
add_flash(_("Button Group \"%{name}\" was added") % {:name => @edit[:new][:description]})
@edit = session[:edit] = nil # clean out the saved info
ab_get_node_info(x_node) if x_active_tree == :ab_tree
replace_right_cell(:nodetype => x_node, :replace_trees => x_active_tree == :ab_tree ? [:ab] : [:sandt])
else
@custom_button_set.errors.each do |field, msg|
add_flash(_("Error during 'add': %{field_name} %{error_name}") %
{:field_name => field.to_s.capitalize, :error_message => msg}, :error)
end
@lastaction = "automate_button"
@layout = "miq_ae_automate_button"
render_flash
end
end
end
def group_button_reset
group_set_form_vars
@changed = session[:changed] = false
add_flash(_("All changes have been reset"), :warning)
@in_a_form = true
@lastaction = "automate_button"
@layout = "miq_ae_automate_button"
replace_right_cell(:nodetype => "group_edit")
end
def group_create_update(typ)
@edit = session[:edit]
@record = @custom_button_set = @edit[:custom_button_set_id] ?
CustomButtonSet.find_by_id(@edit[:custom_button_set_id]) : CustomButtonSet.new
@changed = (@edit[:new] != @edit[:current])
case params[:button]
when 'cancel' then group_button_cancel(typ)
when 'add', 'save' then group_button_add_save(typ)
when 'reset' then group_button_reset
end
end
def button_create_update(typ)
@edit = session[:edit]
@record = @custom_button = @edit[:custom_button]
@changed = (@edit[:new] != @edit[:current])
case params[:button]
when 'cancel' then ab_button_cancel(typ)
when 'add' then ab_button_add
when 'save' then ab_button_save
when 'reset' then ab_button_reset
when 'enablement_expression', 'visibility_expression' then ab_expression
end
end
def ab_expression
@changed = session[:changed] = (@edit[:new] != @edit[:current])
@expkey = params[:button].to_sym
@edit[:visibility_expression_table] = exp_build_table_or_nil(@edit[:new][:visibility_expression])
@edit[:enablement_expression_table] = exp_build_table_or_nil(@edit[:new][:enablement_expression])
@in_a_form = true
@sb[:active_tab] = "ab_advanced_tab"
replace_right_cell(:action => 'button_edit')
end
def ab_button_cancel(typ)
if typ == "update"
add_flash(_("Edit of Custom Button \"%{name}\" was cancelled by the user") % {:name => @edit[:current][:name]})
else
add_flash(_("Add of new Custom Button was cancelled by the user"))
end
@edit = session[:edit] = nil
ab_get_node_info(x_node) if x_active_tree == :ab_tree
replace_right_cell(:nodetype => x_node)
end
def ab_button_add
assert_privileges("ab_button_new")
@sb[:active_tab] = "ab_options_tab"
@resolve = session[:resolve]
name = @edit[:new][:instance_name].blank? ? @edit[:new][:other_name] : @edit[:new][:instance_name]
unless button_valid?
@breadcrumbs = []
drop_breadcrumb(:name => _("Edit of Button"), :url => "/miq_ae_customization/button_edit")
@lastaction = "automate_button"
@layout = "miq_ae_automate_button"
javascript_flash
return
end
attrs = {}
@edit[:new][:attrs].each do |a|
attrs[a[0].to_sym] = a[1] unless a[0].blank?
end
@edit[:uri] = MiqAeEngine.create_automation_object(name, attrs, :fqclass => @edit[:new][:starting_object], :message => @edit[:new][:object_message])
@edit[:new][:description] = @edit[:new][:description].strip == "" ? nil : @edit[:new][:description] unless @edit[:new][:description].nil?
button_set_record_vars(@custom_button)
nodes = x_node.split('_')
if nodes[0].split('-')[1] != "ub" && nodes.length >= 3
# if group is not unassigned group, add uri as a last member of the group
if x_active_tree == :ab_tree || nodes.length > 3
# find custombutton set in ab_tree or when adding button under a group
group_id = x_active_tree == :ab_tree ? nodes[2].split('-').last : nodes[3].split('-').last
@aset = CustomButtonSet.find(group_id)
mems = @aset.members
end
end
if @custom_button.save
add_flash(_("Custom Button \"%{name}\" was added") % {:name => @edit[:new][:description]})
@edit = session[:edit] = nil
au = CustomButton.find(@custom_button.id)
if @aset && nodes[0].split('-')[1] != "ub" && nodes.length >= 3
# if group is not unassigned group, add uri as a last member of the group
mems.push(au)
@aset.replace_children(mems)
@aset.set_data[:button_order] ||= []
@aset.set_data[:button_order].push(au.id)
@aset.save!
end
if x_active_tree == :sandt_tree
# push new button at the end of button_order array
st = ServiceTemplate.find(@sb[:applies_to_id])
st.custom_buttons.push(au) if nodes.length >= 3 && nodes[2].split('-').first != "cbg"
st.options[:button_order] ||= []
st.options[:button_order].push("cb-#{au.id}")
st.save
end
ab_get_node_info(x_node) if x_active_tree == :ab_tree
replace_right_cell(:nodetype => x_node, :replace_trees => x_active_tree == :ab_tree ? [:ab] : [:sandt])
else
@custom_button.errors.each do |field, msg|
add_flash(_("Error during 'add': %{error_message}") %
{:error_message => @custom_button.errors.full_message(field, msg)}, :error)
end
@lastaction = "automate_button"
@layout = "miq_ae_automate_button"
javascript_flash
end
end
def ab_button_save
assert_privileges("ab_button_edit")
@resolve = session[:resolve]
attrs = {}
@sb[:active_tab] = "ab_options_tab"
@edit[:new][:attrs].each do |a|
attrs[a[0].to_sym] = a[1] unless a[0].blank?
end
@edit[:uri] = MiqAeEngine.create_automation_object(ab_button_name, attrs, :fqclass => @edit[:new][:starting_object], :message => @edit[:new][:object_message])
@edit[:new][:description] = @edit[:new][:description].strip == "" ? nil : @edit[:new][:description] unless @edit[:new][:description].nil?
button_set_record_vars(@custom_button)
unless button_valid?
@breadcrumbs = []
drop_breadcrumb(:name => _("Edit of Button"), :url => "/miq_ae_customization/button_edit")
@lastaction = "automate_button"
@layout = "miq_ae_automate_button"
javascript_flash
return
end
if @custom_button.save
add_flash(_("Custom Button \"%{name}\" was saved") % {:name => @edit[:new][:description]})
@edit = session[:edit] = nil
ab_get_node_info(x_node) if x_active_tree == :ab_tree
build_filter_exp_table
replace_right_cell(:nodetype => x_node, :replace_trees => x_active_tree == :ab_tree ? [:ab] : [:sandt])
else
@custom_button.errors.each do |field, msg|
add_flash(_("Error during 'edit': %{field_name} %{error_message}") %
{:field_name => field.to_s.capitalize, :error_message => msg}, :error)
end
@breadcrumbs = []
drop_breadcrumb(:name => "Edit of Button", :url => "/miq_ae_customization/button_edit")
@lastaction = "automate_button"
@layout = "miq_ae_automate_button"
javascript_flash
end
end
def ab_button_reset
button_set_form_vars
@changed = session[:changed] = false
@sb[:active_tab] = "ab_options_tab"
add_flash(_("All changes have been reset"), :warning)
@in_a_form = true
@breadcrumbs = []
drop_breadcrumb(:name => _("Edit of Button"), :url => "/miq_ae_customization/button_edit")
@lastaction = "automate_button"
@layout = "miq_ae_automate_button"
replace_right_cell(:action => "button_edit")
end
# Set form variables for button add/edit
def group_reorder_set_form_vars
@edit = {}
@edit[:new] = {}
@edit[:current] = {}
@edit[:key] = "group_reorder"
@edit[:new][:fields] = []
@sb[:buttons_node] = true
if x_active_tree == :ab_tree
groups = CustomButtonSet.find_all_by_class_name(x_node.split('_').last)
groups.each do |g|
@edit[:new][:fields].push([g.name.split('|').first, "#{g.kind_of?(CustomButton) ? 'cb' : 'cbg'}-#{g.id}"])
end
else
st = ServiceTemplate.find_by_id(@sb[:applies_to_id])
groups = st.custom_button_sets + st.custom_buttons
if st.options && st.options[:button_order]
st.options[:button_order].each do |item_id|
groups.each do |g|
rec_id = "#{g.kind_of?(CustomButton) ? 'cb' : 'cbg'}-#{g.id}"
@edit[:new][:fields].push([g.name.split('|').first, rec_id]) if item_id == rec_id
end
end
end
end
@edit[:current] = copy_hash(@edit[:new])
@sb[:button_groups] = nil
session[:edit] = @edit
end
def group_new_edit(typ)
@record = @custom_button_set = typ == "new" ?
CustomButtonSet.new :
CustomButtonSet.find(params[:id])
if typ == "edit" && x_node.split('_').last == "ub"
add_flash(_("'Unassigned Button Group' can not be edited"), :error)
get_node_info
replace_right_cell(:nodetype => x_node)
return
end
group_set_form_vars
@right_cell_text = if typ == "new"
_("Adding a new Button Group")
else
_("Editing Button Group \"%{name}\"") % {:name => @custom_button_set.name.split('|').first}
end
@in_a_form = true
@lastaction = "automate_button"
@layout = "miq_ae_automate_button"
@sb[:button_groups] = nil
@sb[:buttons] = nil
# Symbol selection based on active controller
if controller_path == 'miq_ae_customization'
replace_right_cell(:nodetype => 'group_edit')
else
replace_right_cell(:action => 'group_edit')
end
end
def button_new_edit(typ)
@record = @custom_button = typ == "new" ?
CustomButton.new :
CustomButton.find(params[:id])
@sb[:active_tab] = "ab_options_tab"
button_set_form_vars
@in_a_form = true
@changed = session[:changed] = false
@breadcrumbs = []
@right_cell_text = if typ == "new"
_("Adding a new Button")
else
_("Editing Button \"%{name}\"") % {:name => @custom_button.name}
end
@lastaction = "automate_button"
@layout = "miq_ae_automate_button"
@sb[:buttons] = nil
@sb[:button_groups] = nil
# Symbol selection based on active controller
if controller_path == 'miq_ae_customization'
replace_right_cell(:nodetype => 'button_edit')
else
replace_right_cell(:action => 'button_edit')
end
end
# Set form variables for button add/edit
def group_set_form_vars
@sb[:buttons_node] = true
if session[:resolve]
@resolve = session[:resolve]
else
build_resolve_screen
end
@edit = {}
@edit[:new] = {}
@edit[:current] = {}
@edit[:key] = "bg_edit__#{@custom_button_set.id || "new"}"
@edit[:custom_button_set_id] = @custom_button_set.id
@edit[:rec_id] = @custom_button_set.try(:id)
@edit[:new][:name] = @custom_button_set[:name].split("|").first unless @custom_button_set[:name].blank?
@edit[:new][:applies_to_class] = @custom_button_set[:set_data] && @custom_button_set[:set_data][:applies_to_class] ? @custom_button_set[:set_data][:applies_to_class] : @sb[:applies_to_class]
@edit[:new][:description] = @custom_button_set.description
@edit[:new][:button_icon] = @custom_button_set[:set_data] && @custom_button_set[:set_data][:button_icon] ? @custom_button_set[:set_data][:button_icon] : ""
@edit[:new][:button_color] = @custom_button_set[:set_data] && @custom_button_set[:set_data][:button_color] ? @custom_button_set[:set_data][:button_color] : ""
@edit[:new][:display] = @custom_button_set[:set_data] && @custom_button_set[:set_data].key?(:display) ? @custom_button_set[:set_data][:display] : true
@edit[:new][:fields] = []
button_order = @custom_button_set[:set_data] && @custom_button_set[:set_data][:button_order] ? @custom_button_set[:set_data][:button_order] : nil
if button_order # show assigned buttons in order they were saved
button_order.each do |bidx|
@custom_button_set.members.each do |mem|
@edit[:new][:fields].push([mem.name, mem.id]) if bidx == mem.id unless @edit[:new][:fields].include?([mem.name, mem.id])
end
end
else
@custom_button_set.members.each do |mem|
@edit[:new][:fields].push([mem.name, mem.id])
end
end
@edit[:new][:available_fields] =
CustomButton.buttons_for(@sb[:applies_to_class])
.select { |u| u.parent.nil? }
.sort_by(&:name)
.collect { |u| [u.name, u.id] }
@edit[:current] = copy_hash(@edit[:new])
session[:edit] = @edit
end
def group_get_form_vars
case params[:button]
when 'right' then move_cols_left_right('right')
when 'left' then move_cols_left_right('left')
when 'up' then move_cols_up
when 'down' then move_cols_down
when 'top' then move_cols_top
when 'bottom' then move_cols_bottom
else copy_params_if_set(@edit[:new], params, %i(name description display button_icon button_color))
end
end
def move_cols_top
if !params[:selected_fields] || params[:selected_fields].length == 0 || params[:selected_fields][0] == ""
add_flash(_("No fields were selected to move top"), :error)
return
end
consecutive, first_idx, last_idx = selected_consecutive?
if !consecutive
add_flash(_("Select only one or consecutive fields to move to the top"), :error)
else
if first_idx > 0
@edit[:new][:fields][first_idx..last_idx].reverse_each do |field|
pulled = @edit[:new][:fields].delete(field)
@edit[:new][:fields].unshift(pulled)
end
end
@refresh_div = "column_lists"
@refresh_partial = "column_lists"
end
@selected = params[:selected_fields]
end
def move_cols_bottom
if !params[:selected_fields] || params[:selected_fields].length == 0 || params[:selected_fields][0] == ""
add_flash(_("No fields were selected to move bottom"), :error)
return
end
consecutive, first_idx, last_idx = selected_consecutive?
if !consecutive
add_flash(_("Select only one or consecutive fields to move to the bottom"), :error)
else
if last_idx < @edit[:new][:fields].length - 1
@edit[:new][:fields][first_idx..last_idx].each do |field|
pulled = @edit[:new][:fields].delete(field)
@edit[:new][:fields].push(pulled)
end
end
@refresh_div = "column_lists"
@refresh_partial = "column_lists"
end
@selected = params[:selected_fields]
end
def ab_button_name(button_hash = @edit[:new])
button_hash[:instance_name].blank? ? button_hash[:other_name] : button_hash[:instance_name]
end
def button_valid?(button_hash = @edit[:new])
add_flash(_("Button Text is required"), :error) if button_hash[:name].blank? || button_hash[:name].strip.blank?
if button_hash[:button_icon].blank?
add_flash(_("Button Icon must be selected"), :error)
end
add_flash(_("Button Hover Text is required"), :error) if button_hash[:description].blank?
add_flash(_("Starting Process is required"), :error) if ab_button_name(button_hash).blank?
add_flash(_("Request is required"), :error) if button_hash[:object_request].blank?
if button_hash[:visibility_typ] == "role" && button_hash[:roles].blank?
add_flash(_("At least one Role must be selected"), :error)
end
if button_hash[:open_url] == true && button_hash[:display_for] != 'single'
add_flash(_('URL can be opened only by buttons for a single entity'), :error)
end
if (!button_hash[:dialog_id].blank? && !button_hash[:dialog_id].to_i.zero?) && button_hash[:display_for] != 'single'
add_flash(_('Dialog can be opened only by buttons for a single entity'), :error)
end
validate_playbook_button(button_hash) if button_hash[:button_type] == "ansible_playbook"
!flash_errors?
end
def validate_playbook_button(button_hash)
add_flash(_("An Ansible Playbook must be selected"), :error) if button_hash[:service_template_id].blank?
if button_hash[:inventory_type] == 'manual' && button_hash[:hosts].blank?
add_flash(_("At least one host must be specified for manual mode"), :error)
end
end
# Set user record variables to new values
def button_set_record_vars(button)
button.name = @edit[:new][:name]
button.description = @edit[:new][:description]
button.applies_to_class = x_active_tree == :ab_tree ? @sb[:target_classes][@resolve[:target_class]] : "ServiceTemplate"
button.applies_to_id = x_active_tree == :ab_tree ? nil : @sb[:applies_to_id]
button.userid = session[:userid]
button.uri = @edit[:uri]
button[:options] = {}
button.disabled_text = @edit[:new][:disabled_text]
# button[:options][:target_attr_name] = @edit[:new][:target_attr_name]
button.uri_path, button.uri_attributes, button.uri_message = CustomButton.parse_uri(@edit[:uri])
button.uri_attributes["request"] = @edit[:new][:object_request]
button.options[:button_icon] = @edit[:new][:button_icon] unless @edit[:new][:button_icon].blank?
button.options[:button_color] = @edit[:new][:button_color] unless @edit[:new][:button_color].blank?
%i(button_type display open_url display_for submit_how).each do |key|
button[:options][key] = @edit[:new][key]
end
button.visibility ||= {}
if @edit[:new][:visibility_typ] == "role"
roles = []
@edit[:new][:roles].each do |r|
role = MiqUserRole.find_by_id(r)
roles.push(role.name) if role && r == role.id
end
button.visibility[:roles] = roles
else
button.visibility[:roles] = ["_ALL_"]
end
button_set_resource_action(button)
button_set_expressions_record(button)
button_set_playbook_record(button)
end
def button_set_expressions_record(button)
exp_remove_tokens(@edit[:new][:visibility_expression])
exp_remove_tokens(@edit[:new][:enablement_expression])
button.visibility_expression = @edit[:new][:visibility_expression]["???"] ? nil : MiqExpression.new(@edit[:new][:visibility_expression])
button.enablement_expression = @edit[:new][:enablement_expression]["???"] ? nil : MiqExpression.new(@edit[:new][:enablement_expression])
end
def field_expression_model
@custom_button.applies_to_class ||= (x_active_tree == :ab_tree ? @sb[:target_classes][@resolve[:target_class]] : "ServiceTemplate")
end
def button_set_expression_vars(field_expression, field_expression_table)
@edit[:new][field_expression] = @custom_button[field_expression].kind_of?(MiqExpression) ? @custom_button[field_expression].exp : nil
# Populate exp editor fields for the expression column
@edit[field_expression] ||= ApplicationController::Filter::Expression.new
@edit[field_expression][:expression] = [] # Store exps in an array
if @edit[:new][field_expression].blank?
@edit[field_expression][:expression] = {"???" => "???"} # Set as new exp element
@edit[:new][field_expression] = copy_hash(@edit[field_expression][:expression]) # Copy to new exp
else
@edit[field_expression][:expression] = copy_hash(@edit[:new][field_expression])
end
@edit[field_expression_table] = exp_build_table_or_nil(@edit[field_expression][:expression])
@expkey = field_expression # Set expression key to expression
@edit[field_expression].history.reset(@edit[field_expression][:expression])
@edit[field_expression][:exp_table] = exp_build_table(@edit[field_expression][:expression])
@edit[field_expression][:exp_model] = field_expression_model # Set model for the exp editor
end
def button_set_resource_action(button)
d = @edit[:new][:dialog_id].nil? ? nil : Dialog.find_by_id(@edit[:new][:dialog_id])
# if resource_Action is there update it else create new one
ra = button.resource_action
if ra
ra.dialog = d
ra.save
else
attrs = {:dialog => d}
button.resource_action.build(attrs)
end
end
def button_set_playbook_record(button)
if @edit[:new][:button_type] == 'ansible_playbook'
target = case @edit[:new][:inventory_type]
when "event_target"
'vmdb_object'
when "manual"
@edit[:new][:hosts]
when 'localhost'
'localhost'
end
attrs = {:service_template_name => ServiceTemplate.find(@edit[:new][:service_template_id]).name, :hosts => target}
button.uri_attributes.merge!(attrs)
end
end
def button_set_playbook_form_vars
@edit[:ansible_playbooks] = ServiceTemplateAnsiblePlaybook.order(:name).pluck(:name, :id) || []
service_template = ServiceTemplate.find_by(:name => @custom_button.uri_attributes[:service_template_name])
@edit[:new][:service_template_id] = service_template.try(:id)
if service_template
service_template.resource_actions.each do |ra|
d = Dialog.where(:id => ra.dialog_id).first
@edit[:new][:dialog_id] = d.id if d
end
end
@edit[:new][:inventory_type] = if @custom_button.uri_attributes[:hosts].blank?
'localhost'
else
case @custom_button.uri_attributes[:hosts]
when 'vmdb_object'
"event_target"
when 'localhost'
"localhost"
else
@edit[:new][:hosts] = @custom_button.uri_attributes[:hosts]
"manual"
end
end
end
# Set form variables for button add/edit
def button_set_form_vars
@sb[:buttons_node] = true
@edit = {}
if session[:resolve] && session[:resolve][:instance_name]
@resolve = session[:resolve]
else
build_resolve_screen
end
if @sb[:target_classes].nil?
@sb[:target_classes] = {}
CustomButton.button_classes.each { |db| @sb[:target_classes][ui_lookup(:model => db)] = db }
end
if x_active_tree == :sandt_tree
@resolve[:target_class] = @sb[:target_classes].invert["ServiceTemplate"]
elsif x_node.starts_with?("_xx-ab")
@resolve[:target_class] = @sb[:target_classes].invert[x_node.split('_')[1]]
else
@resolve[:target_class] = @sb[:target_classes].invert[x_node.split('-')[1] == "ub" ?
x_node.split('-')[2].split('_')[0] :
x_node.split('-')[1].split('_')[1]]
end
@record = @edit[:custom_button] = @custom_button
@edit[:instance_names] = @resolve[:instance_names]
@edit[:new] = {}
@edit[:current] = {}