forked from ManageIQ/manageiq-ui-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuttons.rb
More file actions
1191 lines (1065 loc) · 47 KB
/
Copy pathbuttons.rb
File metadata and controls
1191 lines (1065 loc) · 47 KB
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
include CustomButtonHelper
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(@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
next if field_nodes.first != "cbg"
cs = CustomButtonSet.find(field_nodes.last)
cs.set_data[:group_index] = i + 1
cs.save!
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", :action => "group_reorder")
end
end
def group_reorder_field_changed
assert_privileges("ab_group_reorder")
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 << "miqScrollTop();" if @flash_array.present?
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
assert_privileges("ab_group_new")
group_create_update("create")
end
def group_update
assert_privileges("ab_group_edit")
group_create_update("update")
end
# Provider, Service, User, Group, Tenant, Cloud Tenant, Generic Object
# we need a "Provider" so adding an "ExtManagementSystem" to the list.
MODEL_WITH_OPEN_URL = %w[ExtManagementSystem Service User MiqGroup Tenant CloudTenant GenericObject Vm].freeze
# Types of visibility for the custom buttons
VISIBILITY_TYPES = {'role' => 'role', 'all' => 'all'}.freeze
ALL_ROLES = ["_ALL_", "all"].freeze
FIELDS_EMPTY = [[""], ["", ""]].freeze
def button_visibility_box_edit
typ_changed = params[:visibility_typ].present?
@edit[:new][:visibility_typ] = VISIBILITY_TYPES[params[:visibility_typ]] if typ_changed
visibility_typ = @edit[:new][:visibility_typ]
if visibility_typ.to_s == "role"
plural = visibility_typ.pluralize
key = plural.to_sym
prefix = "#{plural}_"
@edit[:new][key] = [] if typ_changed
params.each do |var, value|
next unless var.starts_with?(prefix)
name = var.split(prefix).last.to_i
if value == "1"
@edit[:new][key] |= [name] # union
elsif value.downcase == "null"
@edit[:new][key].delete(name)
end
end
else
@edit[:new][:roles] ||= []
@edit[:new][:roles] |= ["_ALL_"]
end
end
def automate_button_field_changed
assert_privileges(feature_by_action)
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 disabled_text button_type inventory_type])
@edit[:new][:disabled_open_url] = !(MODEL_WITH_OPEN_URL.include?(@resolve[:target_class]) && @edit[:new][:display_for] == 'single')
@edit[:new][:open_url] = false if @edit[:new][:disabled_open_url]
@edit[:new][:dialog_id] = nil if params[:display_for].present? && params[:display_for] != 'single'
@edit[:new][:dialog_id] = params[:dialog_id] == "" ? nil : params[:dialog_id] if params.key?("dialog_id")
button_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 %i[display_for instance_name other_name target_class button_type].any? { |k| params.key?(k) }
@sb[:active_tab] = params[:instance_name] ? "ab_advanced_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] = false
# This block checks if any of the fields have changed
# Broken into different branches for specific fields
# If no fields are changed it will not set @changed or session[:changed] to true
@edit[:new].each_key do |key|
if @edit[:new][key] != @edit[:current][key]
if @edit[:new][key].blank? && @edit[:current][key].blank? # check empty string / nil case
next
elsif @edit[:new][key] == @edit[:current][key].to_s # check string / integer case
next
elsif key == :roles # check role values
if (ALL_ROLES.include?(@edit[:new][key]) || @edit[:new][key].empty?) && (ALL_ROLES.include?(@edit[:current][key]) || @edit[:current][key].nil?) # if visibility is set to "To All"
next
elsif @edit[:new][key].collect(&:to_i).sort == @edit[:current][key].collect(&:to_i).sort # check if new roles array and current roles array are equal after sorting the ids
next
else # if new roles array and current roles array are not equal
@changed = session[:changed] = true
break
end
elsif key == :attrs # check attribute values
@edit[:new][key].each_with_index do |_item, index|
if FIELDS_EMPTY.include?(@edit[:new][:attrs][index]) && @edit[:current][:attrs][index].empty? # check if attribute and value field is empty
next
elsif @edit[:new][:attrs][index].empty? && @edit[:current][:attrs][index].empty? # check if attribute or value field is empty
next
else # if new attribute array and current attribute array are not equal
@changed = session[:changed] = true
break
end
end
elsif key == :visibility_typ # check visibility type
if @edit[:new][key] == "all" && @edit[:current][key].nil? # if visibility is set to "To All"
next
else # if new visibility type value and current visibility type value are not equal
@changed = session[:changed] = true
break
end
else # if new value and current value are not equal
@changed = session[:changed] = true
break
end
end
end
page << javascript_for_miq_button_visibility(@changed)
end
page << "miqSparkle(false);"
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
assert_privileges("ab_button_edit")
button_create_update("update")
end
def button_create
assert_privileges("ab_button_new")
button_create_update("create")
end
def group_form_valid
required = %i[name description button_icon]
required.none? do |field|
@edit[:new][field].blank?
end
end
def open_url_after_dialog
external_url = ExternalUrl.find_by(
:resource_id => params[:targetId],
:resource_type => params[:realTargetType],
:user => User.current_user
)
# FIXME: remove this fallback once the ':remote_console_url=' is removed from automate
external_url ||= SystemConsole.find_by(:vm_id => params[:targetId])
url = external_url.try(:url)
render :json => {:open_url => url}
end
private
def feature_by_action
features_in_action = %w[ab_button_new ab_button_edit]
@sb[:action] if features_in_action.include?(@sb[:action])
end
BASE_MODEL_EXPLORER_CLASSES = [MiqGroup, MiqTemplate, Tenant, User, Vm].freeze
def custom_button_done
external_url = ExternalUrl.find_by(
:resource_id => params[:id],
:resource_type => params[:base_cls],
:user => User.current_user
)
# FIXME: remove this fallback once the ':remote_console_url=' is removed from automate
external_url ||= SystemConsole.find_by(:vm_id => params[:id])
url = external_url.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&.key?(:submit_how) && button.options[:submit_how].to_s == 'all')
button.invoke(objs, 'UI')
else
objs.each { |obj| button.invoke(obj, 'UI') }
end
end
def sync_playbook_dialog(button)
service_template = ServiceTemplate.find_by(:name => button.uri_attributes[:service_template_name])
dialog_id = nil
service_template&.resource_actions&.each do |ra|
d = Dialog.where(:id => ra.dialog_id).first
dialog_id = d.id if d
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(params[:button_id])
cls = custom_button_class_model(button.applies_to_class)
@explorer = true if BASE_MODEL_EXPLORER_CLASSES.include?(cls)
ids ||= params[:id] unless relationship_table_screen? && @record.nil?
ids = find_checked_items if ids == 'LIST' || ids.nil?
if ids.blank?
render_flash(_("Error launching custom button: No item was selected."), :error)
return
end
objs = Rbac.filtered(cls.where(:id => ids))
obj = objs.first
if objs.empty?
render_flash(_("Error launching 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[:dialog_locals][:open_url] = button.options.present? && button.options.fetch_path(:open_url)
options.merge!(display_options) unless display_options.empty?
dialog_initialize(button.resource_action, options)
elsif button.options.present? && button.options.fetch_path(:open_url)
# not supported for objs: cannot do wait for task for multiple tasks
task_id = button.invoke_async(obj, 'UI')
initiate_wait_for_task(
:task_id => task_id,
:action => :custom_button_done,
:extra_params => { :base_cls => cls.base_class.to_s }
)
else
begin
custom_buttons_invoke(button, objs)
rescue StandardError => bang
add_flash(_("Error launching: \"%{task_description}\" %{error_message}") %
{:task_description => params[:desc], :error_message => bang.message}, :error)
else
add_flash(_("\"%{task_description}\" was launched") % {:task_description => params[:desc]})
end
javascript_flash
end
end
def load_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 typ == "update"
update_page_content("saved")
else
all_sets = CustomButtonSet.find_all_by_class_name(params[:applies_to_class])
all_sets.each_with_index do |group, i|
group.set_data[:group_index] = i + 1
group.save!
end
if x_active_tree == :sandt_tree
aset = CustomButtonSet.find_by(:id => params[:id])
# push new button at the end of button_order array
if aset
st = ServiceTemplate.find(@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
update_page_content("added")
end
end
def update_page_content(action)
add_flash("Button Group #{params[:name]} was #{action}")
@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])
end
def group_create_update(typ)
@edit = session[:edit]
@record = @custom_button_set = @edit[:custom_button_set_id] ? CustomButtonSet.find(@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)
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].presence || @edit[:new][:other_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"
if @switch_tab
render :update do |page|
page << javascript_prologue
page.replace("flash_msg_div", :partial => "layouts/flash_msg")
page << "miqScrollTop();" if @flash_array.present?
page << "document.querySelector(\"#ab_advanced_tab_tab > a\").click();"
end
else
javascript_flash
end
return
end
attrs = {}
@edit[:new][:attrs].each do |a|
attrs[a[0].to_sym] = a[1] if a[0].present?
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 >= 2
# if group is not unassigned group, add uri as a last member of the group
if x_active_tree == :ab_tree || nodes.length > 2
# find custombutton set in ab_tree or when adding button under a group
group_id = nodes[2].split('-').last
@aset = CustomButtonSet.find(group_id)
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 >= 2
# if group is not unassigned group, add uri as a last member of the group
@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 >= 2 && nodes[1].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 |error|
add_flash(_("Error during 'add': %{error_message}") %
{:error_message => @custom_button.errors.full_message(error.attribute, error.message)}, :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] if a[0].present?
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?
unless button_valid?
@breadcrumbs = []
drop_breadcrumb(:name => _("Edit of Button"), :url => "/miq_ae_customization/button_edit")
@lastaction = "automate_button"
@layout = "miq_ae_automate_button"
if @switch_tab
render :update do |page|
page << javascript_prologue
page.replace("flash_msg_div", :partial => "layouts/flash_msg")
page << "miqScrollTop();" if @flash_array.present?
page << "document.querySelector(\"#ab_advanced_tab_tab > a\").click();"
end
else
javascript_flash
end
return
end
button_set_record_vars(@custom_button)
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 |error|
add_flash(_("Error during 'edit': %{field_name} %{error_message}") %
{:field_name => error.attribute.to_s.capitalize, :error_message => error.message}, :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(@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
@sb[:buttons] = 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 if @custom_button_set[:name].present?
@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].try(:[], :button_order)
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 && !@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.custom_button_sets.blank? }
.sort_by(&:name)
.collect { |u| [u.name, u.id] }
@edit[:current] = copy_hash(@edit[:new])
session[:edit] = @edit
end
def move_cols_top
if params[:selected_fields].blank? || 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.positive?
@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].blank? || 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].presence || button_hash[:instance_name]
end
def button_valid?(button_hash = @edit[:new])
add_flash(_("Button Name 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 Description is required"), :error) if button_hash[:description].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].present? && !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"
if ab_button_name(button_hash).blank?
add_flash(_("Starting Process is required"), :error)
@switch_tab = true if @flash_array.length == 1
end
if button_hash[:object_request].blank?
add_flash(_("Request is required"), :error)
@switch_tab = true if @flash_array.length == 1
end
!flash_errors?
end
def validate_playbook_button(button_hash)
if button_hash[:service_template_id].blank? || button_hash[:service_template_id].zero?
add_flash(_("An Ansible Playbook must be selected"), :error)
end
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 ? @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.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] if @edit[:new][:button_icon].present?
button.options[:button_color] = @edit[:new][:button_color] if @edit[:new][:button_color].present?
%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"
button.visibility[:roles] = @edit[:new][:roles]
else
button.visibility[:roles] = ["_ALL_"]
end
button_set_resource_action(button)
if @edit[:new][:display_for] == "single"
button_set_expressions_record(button)
else
button.visibility_expression = button.enablement_expression = nil
end
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 ? @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(@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)
service_template&.resource_actions&.each do |ra|
d = Dialog.where(:id => ra.dialog_id).first
@edit[:new][:dialog_id] = d.id if d
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
@resolve[:target_class] = if x_active_tree == :sandt_tree
"ServiceTemplate"
elsif x_node.starts_with?("-ub-")
x_node.sub(/-ub-([^_]+)(_.*)?/, '\1')
else
x_node.sub(/xx-ab_([^_]+)_.*/, '\1')
end
@record = @edit[:custom_button] = @custom_button
@edit[:instance_names] = Array(@resolve[:instance_names])
@edit[:new] = {}
@edit[:current] = {}
@edit[:new][:attrs] ||= []
@edit[:rec_id] = @custom_button.try(:id)
if @custom_button.uri_attributes
instance_name = @custom_button.uri_object_name
if @edit[:instance_names].include?(instance_name)
@edit[:new][:instance_name] = instance_name
else
@edit[:new][:other_name] = instance_name
end
@edit[:new][:object_request] = @custom_button.uri_attributes["request"]
button_type = @custom_button.options.try(:[], :button_type) ? @custom_button.options[:button_type] : 'default'
default_attributes = %w[request]
default_attributes = %w[request service_template_name hosts] if button_type == 'ansible_playbook'
@custom_button.uri_attributes.each do |attr|
if attr[0] != "object_name" && !default_attributes.include?(attr[0].to_s)
@edit[:new][:attrs].push(attr) unless @edit[:new][:attrs].include?(attr)
end
end
end
(ApplicationController::AE_MAX_RESOLUTION_FIELDS - @edit[:new][:attrs].length).times { @edit[:new][:attrs].push([]) }
@edit[:new][:starting_object] ||= "SYSTEM/PROCESS"
@edit[:new][:instance_name] ||= "Request"
@edit[:new].update(
:target_class => @resolve[:target_class],
:name => @custom_button.name,
:description => @custom_button.description,
:button_icon => @custom_button.options.try(:[], :button_icon),