forked from ManageIQ/manageiq-ui-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_common.rb
1794 lines (1649 loc) · 73.6 KB
/
vm_common.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 VmCommon
extend ActiveSupport::Concern
include ActionView::Helpers::JavaScriptHelper
include ChargebackPreview
def textual_group_list
[
%i(properties lifecycle relationships vmsafe normal_operating_ranges miq_custom_attributes ems_custom_attributes),
%i(compliance power_management security configuration datastore_allocation datastore_usage diagnostics tags)
]
end
included do
private :textual_group_list
helper_method :textual_group_list
end
# handle buttons pressed on the button bar
def button
@edit = session[:edit] # Restore @edit for adv search box
params[:page] = @current_page unless @current_page.nil? # Save current page for list refresh
@refresh_div = "main_div" # Default div for button.rjs to refresh
case params[:pressed]
when 'custom_button'
custom_buttons
return
when 'perf_reload'
perf_chart_chooser
# VM sub-screen is showing, so return
return if @flash_array.nil?
when 'perf_refresh'
perf_refresh_data
when 'remove_service'
remove_service
end
if @flash_array.nil? # if no button handler ran, show not implemented msg
add_flash(_("Button not yet implemented"), :error)
@refresh_partial = "layouts/flash_msg"
@refresh_div = "flash_msg_div"
else # Figure out what was showing to refresh it
if @lastaction == "show" && ["vmtree"].include?(@showtype)
@refresh_partial = @showtype
elsif @lastaction == "show" && ["config"].include?(@showtype)
@refresh_partial = @showtype
elsif @lastaction == "show_list"
# default to the gtl_type already set
else
@refresh_partial = "layouts/flash_msg"
@refresh_div = "flash_msg_div"
end
end
@vm = @record = identify_record(params[:id], VmOrTemplate) unless @lastaction == "show_list"
if !@flash_array.nil? && @single_delete
javascript_redirect :action => 'show_list', :flash_msg => @flash_array[0][:message] # redirect to build the retire screen
elsif params[:pressed].ends_with?("_edit")
if @redirect_controller
javascript_redirect :controller => @redirect_controller, :action => @refresh_partial, :id => @redirect_id, :org_controller => @org_controller
else
javascript_redirect :action => @refresh_partial, :id => @redirect_id
end
else
if @refresh_div == "main_div" && @lastaction == "show_list"
replace_gtl_main_div
else
if @refresh_div == "flash_msg_div"
javascript_flash(:spinner_off => true)
else
options
partial_replace(@refresh_div, "vm_common/#{@refresh_partial}")
end
end
end
end
# to reload currently displayed summary screen in explorer
def reload
@_params[:id] = if hide_vms && x_node.split('-')[1] != to_cid(params[:id]) && params[:id].present?
'v-' + to_cid(params[:id])
else
x_node
end
tree_select
end
def show_timeline
db = get_rec_cls
@display = "timeline"
session[:tl_record_id] = params[:id] if params[:id]
@record = find_by_id_filtered(db, from_cid(session[:tl_record_id]))
@timeline = @timeline_filter = true
@lastaction = "show_timeline"
tl_build_timeline # Create the timeline report
drop_breadcrumb(:name => _("Timelines"), :url => "/#{db}/show_timeline/#{@record.id}?refresh=n")
if @explorer
@refresh_partial = "layouts/tl_show"
if params[:refresh]
@sb[:action] = "timeline"
replace_right_cell
end
end
end
alias_method :image_timeline, :show_timeline
alias_method :instance_timeline, :show_timeline
alias_method :vm_timeline, :show_timeline
alias_method :miq_template_timeline, :show_timeline
def hide_vms
!User.current_user.settings.fetch_path(:display, :display_vms) # default value is false
end
def vm_selected
@vm.present?
end
def x_show
@vm = @record = identify_record(params[:id], VmOrTemplate)
generic_x_show
end
def download_summary_pdf
super do
@flash_array = []
@record = identify_record(params[:id], VmOrTemplate)
end
end
def show(id = nil)
@flash_array = [] if params[:display] && params[:display] != "snapshot_info"
@sb[:action] = params[:display]
return if perfmenu_click?
@display = params[:display] || "main" unless pagination_or_gtl_request?
@display = params[:vm_tree] if params[:vm_tree]
@lastaction = "show"
@showtype = "config"
@record = identify_record(id || params[:id], VmOrTemplate)
if @record.nil?
referrer = Rails.application.routes.recognize_path(request.referrer)
redirect_to :controller => referrer[:controller], :action => referrer[:action]
return
end
return if record_no_longer_exists?(@record)
@explorer = true if request.xml_http_request? # Ajax request means in explorer
unless @explorer
tree_node_id = TreeBuilder.build_node_id(@record)
session[:exp_parms] = {:display => @display, :refresh => params[:refresh], :id => tree_node_id}
controller_name = controller_for_vm(model_for_vm(@record))
# redirect user back to where they came from if they dont have access to any of vm explorers
# or redirect them to the one they have access to
case controller_name
when "vm_infra"
redirect_controller = role_allows?(:feature => "vandt_accord") || role_allows?(:feature => "vms_filter_accord") ?
"vm_infra" : nil
when "vm_cloud"
redirect_controller = role_allows?(:feature => "instances_accord") || role_allows?(:feature => "instances_filter_accord") ?
"vm_cloud" : nil
end
redirect_controller = role_allows?(:feature => "vms_instances_filter_accord") ? "vm_or_template" : nil unless redirect_controller
if redirect_controller
action = "explorer"
else
url = request.env['HTTP_REFERER'].split('/')
add_flash(_("User '%{username}' is not authorized to access '%{controller_name}'") %
{:username => current_userid, :controller_name => ui_lookup(:table => controller_name)}, :warning)
session[:flash_msgs] = @flash_array.dup
redirect_controller = url[3]
action = url[4]
end
redirect_to :controller => redirect_controller,
:action => action
return
end
if @record.class.base_model.to_s == "MiqTemplate"
rec_cls = "miq_template"
else
rec_cls = "vm"
end
@gtl_url = "/show"
if %w(main summary_only).include?(@display)
get_tagdata(@record)
drop_breadcrumb({:name => _("Virtual Machines"),
:url => "/#{rec_cls}/show_list?page=#{@current_page}&refresh=y"}, true)
drop_breadcrumb(:name => @record.name + _(" (Summary)"), :url => "/#{rec_cls}/show/#{@record.id}")
@showtype = "main"
@button_group = rec_cls
set_summary_pdf_data if @display == "summary_only"
elsif @display == "networks"
drop_breadcrumb(:name => @record.name + _(" (Networks)"),
:url => "/#{rec_cls}/show/#{@record.id}?display=#{@display}")
elsif @display == "os_info"
drop_breadcrumb(:name => @record.name + _(" (OS Information)"),
:url => "/#{rec_cls}/show/#{@record.id}?display=#{@display}")
elsif @display == "hv_info"
drop_breadcrumb(:name => @record.name + _(" (Container)"),
:url => "/#{rec_cls}/show/#{@record.id}?display=#{@display}")
elsif @display == "resources_info"
drop_breadcrumb(:name => @record.name + _(" (Resources)"),
:url => "/#{rec_cls}/show/#{@record.id}?display=#{@display}")
elsif @display == "snapshot_info"
drop_breadcrumb(:name => @record.name + _(" (Snapshots)"),
:url => "/#{rec_cls}/show/#{@record.id}?display=#{@display}")
session[:snap_selected] = nil if Snapshot.find_by(:id => session[:snap_selected]).nil?
@sb[@sb[:active_accord]] = TreeBuilder.build_node_id(@record)
@snapshot_tree = TreeBuilderSnapshots.new(:snapshot_tree, :snapshot, @sb, true, :root => @record)
@active = if @snapshot_tree.selected_node
snap_selected = Snapshot.find_by_id(from_cid(@snapshot_tree.selected_node.split('-').last))
session[:snap_selected] = snap_selected.id
snap_selected.current?
else
false
end
@button_group = "snapshot"
elsif @display == "devices"
drop_breadcrumb(:name => @record.name + _(" (Devices)"),
:url => "/#{rec_cls}/show/#{@record.id}?display=#{@display}")
elsif @display == "vmtree_info"
drop_breadcrumb({:name => @record.name, :url => "/#{rec_cls}/show/#{@record.id}"}, true)
drop_breadcrumb(:name => @record.name + _(" (Genealogy)"),
:url => "/#{rec_cls}/show/#{@record.id}?display=#{@display}")
if @record.parents.length > 1
add_flash(_("VM has too many parents."), :error)
javascript_flash(:spinner_off => true)
return
else
@genealogy_tree = TreeBuilderGenealogy.new(:genealogy_tree, :genealogy, @sb, true, @record)
session[:genealogy_tree_root_id] = @genealogy_tree.root_id
end
@button_group = "vmtree"
elsif @display == "compliance_history"
count = params[:count] ? params[:count].to_i : 10
@ch_tree = TreeBuilderComplianceHistory.new(:ch_tree, :ch, @sb, true, @record)
session[:ch_tree] = @ch_tree.tree_nodes
session[:tree_name] = "ch_tree"
session[:squash_open] = (count == 1)
drop_breadcrumb({:name => @record.name, :url => "/#{rec_cls}/show/#{@record.id}"}, true)
if count == 1
drop_breadcrumb(:name => @record.name + _(" (Latest Compliance Check)"),
:url => "/#{rec_cls}/show/#{@record.id}?display=#{@display}")
else
drop_breadcrumb(:name => @record.name + _(" (Compliance History - Last %{number} Checks)") % {:number => count},
:url => "/#{rec_cls}/show/#{@record.id}?display=#{@display}")
end
@showtype = @display
elsif @display == "performance"
@showtype = "performance"
drop_breadcrumb(:name => _("%{name} Capacity & Utilization") % {:name => @record.name},
:url => "/#{rec_cls}/show/#{@record.id}?display=#{@display}&refresh=n")
perf_gen_init_options # Initialize perf chart options, charts will be generated async
elsif @display == "disks"
@showtype = "disks"
disks
drop_breadcrumb(:name => _("%{name} (Disks)") % {:name => @record.name},
:url => "/#{rec_cls}/show/#{@record.id}?display=#{@display}")
end
set_config(@record)
get_host_for_vm(@record)
session[:tl_record_id] = @record.id
replace_gtl_main_div if pagination_request?
if @explorer
@refresh_partial = "layouts/performance"
replace_right_cell unless params[:display] == "performance"
end
end
def node_id(id)
id == 'root' ? session[:genealogy_tree_root_id] : from_cid(parse_nodetype_and_id(id).last)
end
def genealogy_tree_selected
base = node_id(params[:id])
@display = "vmtree_info"
javascript_redirect :action => "show", :id => base, :vm_tree => "vmtree_info"
end
def snap_pressed
session[:snap_selected] = from_cid(params[:id])
@snap_selected = Snapshot.find_by_id(session[:snap_selected])
@vm = @record = identify_record(x_node_right_cell.split('-').last, VmOrTemplate)
if @snap_selected.nil?
@display = "snapshot_info"
add_flash(_("Last selected Snapshot no longer exists"), :error)
end
@snapshot_tree = TreeBuilderSnapshots.new(:snapshot_tree,
:snapshot,
@sb,
true,
:root => @record,
:selected_node => session[:snap_selected])
@active = @snap_selected.current? if @snap_selected
@button_group = "snapshot"
@explorer = true
c_tb = build_toolbar("x_vm_center_tb")
render :update do |page|
page << javascript_prologue
page << "$('#toolbar').show();" if c_tb.present?
page << javascript_pf_toolbar_reload('center_tb', c_tb)
page.replace("flash_msg_div", :partial => "layouts/flash_msg")
page.replace("desc_content", :partial => "/vm_common/snapshots_desc",
:locals => {:selected => params[:id]})
end
end
def disks
end
def processes
show_association('processes', _('Running Processes'), 'processes', [:operating_system, :processes], OsProcess,
'processes')
end
def registry_items
show_association('registry_items', _('Registry Entries'), 'registry_items', :registry_items, RegistryItem)
end
def advanced_settings
show_association('advanced_settings', _('Advanced Settings'), 'advancedsetting', :advanced_settings,
AdvancedSetting)
end
def linux_initprocesses
show_association('linux_initprocesses', _('Init Processes'), 'linuxinitprocesses', :linux_initprocesses,
SystemService, 'linux_initprocesses')
end
def win32_services
show_association('win32_services', _('Win32 Services'), 'win32service', :win32_services, SystemService,
'win32_services')
end
def kernel_drivers
show_association('kernel_drivers', _('Kernel Drivers'), 'kerneldriver', :kernel_drivers, SystemService,
'kernel_drivers')
end
def filesystem_drivers
show_association('filesystem_drivers', _('File System Drivers'), 'filesystemdriver', :filesystem_drivers,
SystemService, 'filesystem_drivers')
end
def filesystems
show_association('filesystems', _('Files'), 'filesystems', :filesystems, Filesystem)
end
def security_groups
show_association('security_groups', _('Security Groups'), 'security_group', :security_groups, SecurityGroup)
end
def floating_ips
show_association('floating_ips', _('Floating IPs'), 'floating_ip', :floating_ips, FloatingIp)
end
def cloud_subnets
show_association('cloud_subnets', _('Subnets'), 'cloud_subnet', :cloud_subnets, CloudSubnet)
end
def cloud_networks
show_association('cloud_subnets', _('Networks'), 'cloud_subnet', :cloud_subnets, CloudNetwork)
end
def cloud_volumes
show_association('cloud_volumes', _('Cloud Volumes'), 'cloud_volume', :cloud_volumes, CloudVolume)
end
def network_routers
show_association('network_routers', _('Routers'), 'network_router', :network_routers, NetworkRouter)
end
def network_ports
show_association('network_ports', _('Ports'), 'network_port', :network_ports, NetworkPort)
end
def load_balancers
show_association('load_balancers', _('Load Balancers'), 'load_balancer', :load_balancers, LoadBalancer)
end
def snap
assert_privileges(params[:pressed])
@vm = @record = identify_record(params[:id], VmOrTemplate)
@name = @description = ""
@in_a_form = true
@button_group = "snap"
@show_snapshot_memory_checkbox = show_snapshot_memory_checkbox?(@vm)
drop_breadcrumb(:name => _("Snapshot VM '%{name}''") % {:name => @record.name},
:url => "/vm_common/snap",
:display => "snapshot_info")
if @explorer
@edit ||= {}
@edit[:explorer] = true
session[:changed] = true
@refresh_partial = "vm_common/snap"
end
end
alias_method :vm_snapshot_add, :snap
def render_missing_field(session, missing_field_name)
add_flash(_("%{missing_field_name} is required") %
{:missing_field_name => missing_field_name}, :error)
@in_a_form = true
drop_breadcrumb(:name => _("Snapshot VM '%{name}'") % {:name => @record.name},
:url => "/vm_common/snap")
if session[:edit] && session[:edit][:explorer]
@edit = session[:edit] # saving it to use in next transaction
javascript_flash(:spinner_off => true)
else
render :action => "snap"
end
end
def show_snapshot_memory_checkbox?(vm)
return true unless vm.respond_to?(:snapshotting_memory_allowed?)
vm.snapshotting_memory_allowed?
end
def snap_vm
@vm = @record = identify_record(params[:id], VmOrTemplate)
if params["cancel"] || params[:button] == "cancel"
flash = _("Snapshot of VM %{name} was cancelled by the user") % {:name => @record.name}
if session[:edit] && session[:edit][:explorer]
add_flash(flash)
@_params[:display] = "snapshot_info"
show
else
redirect_to :action => @lastaction, :id => @record.id, :flash_msg => flash
end
elsif params["create.x"] || params[:button] == "create"
@name = params[:name]
@description = params[:description]
if params[:name].blank? && !@record.try(:snapshot_name_optional?)
render_missing_field(session, "Name")
elsif params[:description].blank? && @record.try(:snapshot_description_required?)
render_missing_field(session, "Description")
else
flash_error = false
# audit = {:event=>"vm_genealogy_change", :target_id=>@record.id, :target_class=>@record.class.base_class.name, :userid => session[:userid]}
begin
# @record.create_snapshot(params[:name], params[:description], params[:snap_memory])
Vm.process_tasks(:ids => [@record.id],
:task => "create_snapshot",
:userid => session[:userid],
:name => params[:name],
:description => params[:description],
:memory => params[:snap_memory] == "1")
rescue => bang
puts bang.backtrace.join("\n")
flash = _("Error during 'Create Snapshot': %{message}") % {:message => bang.message}
flash_error = true
# AuditEvent.failure(audit.merge(:message=>"[#{@record.name} -- #{@record.location}] Update returned: #{bang}"))
else
flash = _("Create Snapshot for %{model} \"%{name}\" was started") % {:model => ui_lookup(:model => "Vm"), :name => @record.name}
# AuditEvent.success(build_saved_vm_audit(@record))
end
params[:id] = @record.id.to_s # reset id in params for show
# params[:display] = "snapshot_info"
if session[:edit] && session[:edit][:explorer]
add_flash(flash, flash_error ? :error : :success)
@_params[:display] = "snapshot_info"
show
else
redirect_to :action => @lastaction, :id => @record.id, :flash_msg => flash, :flash_error => flash_error, :display => "snapshot_info"
end
end
end
end
def policies
@vm = @record = identify_record(params[:id], VmOrTemplate)
@lastaction = "rsop"
@showtype = "policies"
drop_breadcrumb(:name => _("Policy Simulation Details for %{name}") % {:name => @record.name},
:url => "/vm/policies/#{@record.id}")
@polArr = @record.resolve_profiles(session[:policies].keys).sort_by { |p| p["description"] }
@policy_options = {}
@policy_options[:out_of_scope] = true
@policy_options[:passed] = true
@policy_options[:failed] = true
@policy_simulation_tree = TreeBuilderPolicySimulation.new(:policy_simulation_tree,
:policy_simulation,
@sb,
true,
:root => @polArr,
:root_name => @record.name,
:options => @policy_options)
@edit = session[:edit] if session[:edit]
if @edit && @edit[:explorer]
if session[:policies].empty?
render_flash(_("No policies were selected for Policy Simulation."), :error)
return
end
@in_a_form = true
replace_right_cell(:action => 'policy_sim')
else
render :template => 'vm/show'
end
end
def policy_show_options
if params[:passed] == "null" || params[:passed] == ""
@policy_options[:passed] = false
@policy_options[:failed] = true
elsif params[:failed] == "null" || params[:failed] == ""
@policy_options[:passed] = true
@policy_options[:failed] = false
elsif params[:failed] == "1"
@policy_options[:failed] = true
elsif params[:passed] == "1"
@policy_options[:passed] = true
end
@vm = @record = identify_record(params[:id], VmOrTemplate)
@policy_simulation_tree = TreeBuilderPolicySimulation.new(:policy_simulation_tree,
:policy_simulation,
@sb,
true,
:root => @polArr,
:root_name => @record.name,
:options => @policy_options)
replace_main_div({:partial => "vm_common/policies"}, {:flash => true})
end
# Show/Unshow out of scope items
def policy_options
@vm = @record = identify_record(params[:id], VmOrTemplate)
@policy_options ||= {}
@policy_options[:out_of_scope] = (params[:out_of_scope] == "1")
@policy_simulation_tree = TreeBuilderPolicySimulation.new(:policy_simulation_tree,
:policy_simulation,
@sb,
true,
:root => @polArr,
:root_name => @record.name,
:options => @policy_options)
replace_main_div({:partial => "vm_common/policies"}, {:flash => true})
end
# Set right_size selected db records
def right_size
@record = Vm.find_by_id(params[:id])
@lastaction = "right_size"
@rightsize = true
@in_a_form = true
if params[:button] == "back"
javascript_prologue( previous_breadcrumb_url)
end
if !@explorer && params[:button] != "back"
drop_breadcrumb(:name => _("Right Size VM '%{name}''") % {:name => @record.name}, :url => "/vm/right_size")
render :action => "show"
end
end
def evm_relationship
@record = find_by_id_filtered(VmOrTemplate, params[:id]) # Set the VM object
@edit = {}
@edit[:vm_id] = @record.id
@edit[:key] = "evm_relationship_edit__new"
@edit[:current] = {}
@edit[:new] = {}
evm_relationship_build_screen
@edit[:current] = copy_hash(@edit[:new])
session[:changed] = false
@in_a_form = true
if @explorer
@refresh_partial = "vm_common/evm_relationship"
@edit[:explorer] = true
end
end
alias_method :image_evm_relationship, :evm_relationship
alias_method :instance_evm_relationship, :evm_relationship
alias_method :vm_evm_relationship, :evm_relationship
alias_method :miq_template_evm_relationship, :evm_relationship
# Build the evm_relationship assignment screen
def evm_relationship_build_screen
@servers = {} # Users array for first chooser
MiqServer.all.each { |s| @servers["#{s.name} (#{s.id})"] = s.id.to_s }
@edit[:new][:server] = @record.miq_server ? @record.miq_server.id.to_s : nil # Set to first category, if not already set
end
def evm_relationship_field_changed
return unless load_edit("evm_relationship_edit__new")
evm_relationship_get_form_vars
changed = (@edit[:new] != @edit[:current])
render :update do |page|
page << javascript_prologue
page << javascript_for_miq_button_visibility(changed)
end
end
def evm_relationship_get_form_vars
@record = VmOrTemplate.find_by_id(@edit[:vm_id])
@edit[:new][:server] = params[:server_id] if params[:server_id]
end
def evm_relationship_update
return unless load_edit("evm_relationship_edit__new")
evm_relationship_get_form_vars
case params[:button]
when "cancel"
msg = _("Edit Management Engine Relationship was cancelled by the user")
if @edit[:explorer]
add_flash(msg)
@sb[:action] = nil
replace_right_cell
else
javascript_redirect :action => 'show', :id => @record.id, :flash_msg => msg
end
when "save"
svr = @edit[:new][:server] && @edit[:new][:server] != "" ? MiqServer.find(@edit[:new][:server]) : nil
@record.miq_server = svr
@record.save
msg = _("Management Engine Relationship saved")
if @edit[:explorer]
add_flash(msg)
@sb[:action] = nil
replace_right_cell
else
javascript_redirect :action => 'show', :id => @record.id, :flash_msg => msg
end
when "reset"
@in_a_form = true
if @edit[:explorer]
@explorer = true
evm_relationship
add_flash(_("All changes have been reset"), :warning)
replace_right_cell
else
javascript_redirect :action => 'evm_relationship', :id => @record.id, :flash_msg => _("All changes have been reset"), :flash_warning => true, :escape => true
end
end
end
def delete
@lastaction = "delete"
redirect_to :action => 'show_list', :layout => false
end
def destroy
find_by_id_filtered(VmOrTemplate, params[:id]).destroy
redirect_to :action => 'list'
end
def profile_build
@catinfo ||= {}
session[:vm].resolve_profiles(session[:policies].keys).each do |policy|
cat = policy["description"]
@catinfo[cat] = true unless @catinfo.key?(cat)
end
end
def profile_toggle
if params[:pressed] == "tag_cat_toggle"
profile_build
policy_escaped = j(params[:policy])
cat = params[:cat]
render :update do |page|
page << javascript_prologue
if @catinfo[cat]
@catinfo[cat] = false
page << javascript_show("cat_#{policy_escaped}_div")
page << "$('#cat_#{policy_escaped}_icon').prop('src', '#{ActionController::Base.helpers.image_path('tree/compress.png')}');"
else
@catinfo[cat] = true # Set squashed = true
page << javascript_hide("cat_#{policy_escaped}_div")
page << "$('#cat_#{policy_escaped}_icon').prop('src', '#{ActionController::Base.helpers.image_path('tree/expand.png')}');"
end
end
else
add_flash(_("Button not yet implemented"), :error)
javascript_flash(:spinner_off => true)
end
end
def add_to_service
@record = find_by_id_filtered(Vm, params[:id])
@svcs = {}
Service.all.each { |s| @svcs[s.name] = s.id }
drop_breadcrumb(:name => _("Add VM to a Service"), :url => "/vm/add_to_service")
@in_a_form = true
end
def add_vm_to_service
@record = find_by_id_filtered(Vm, params[:id])
if params["cancel.x"]
flash = _("Add VM \"%{name}\" to a Service was cancelled by the user") % {:name => @record.name}
redirect_to :action => @lastaction, :id => @record.id, :flash_msg => flash
else
chosen = params[:chosen_service].to_i
flash = _("%{model} \"%{name}\" successfully added to Service \"%{to_name}\"") % {:model => ui_lookup(:model => "Vm"), :name => @record.name, :to_name => Service.find(chosen).name}
begin
@record.add_to_vsc(Service.find(chosen).name)
rescue => bang
flash = _("Error during 'Add VM to service': %{message}") % {:message => bang}
end
redirect_to :action => @lastaction, :id => @record.id, :flash_msg => flash
end
end
def remove_service
assert_privileges(params[:pressed])
@record = find_by_id_filtered(Vm, params[:id])
begin
@vervice_name = Service.find_by_name(@record.location).name
@record.remove_from_vsc(@vervice_name)
rescue => bang
add_flash(_("Error during 'Remove VM from service': %{message}") % {:message => bang.message}, :error)
else
add_flash(_("VM successfully removed from service \"%{name}\"") % {:name => @vervice_name})
end
end
def edit
@record = find_by_id_filtered(VmOrTemplate, params[:id]) # Set the VM object
set_form_vars
build_edit_screen
session[:changed] = false
@active_tab = "edit"
@tab_id = @record.id.to_s
@tabs = [["edit", "Information"]]
@refresh_partial = "vm_common/form"
end
alias_method :image_edit, :edit
alias_method :instance_edit, :edit
alias_method :vm_edit, :edit
alias_method :miq_template_edit, :edit
def build_edit_screen
drop_breadcrumb(:name => _("Edit VM '%{name}''") % {:name => @record.name}, :url => "/vm/edit") unless @explorer
session[:edit] = @edit
@in_a_form = true
@active_tab = "edit"
@tab_id = @record.id.to_s
@tabs = [["edit", "Information"]]
end
# AJAX driven routine to check for changes in ANY field on the form
def form_field_changed
return unless load_edit("vm_edit__#{params[:id]}")
get_form_vars
changed = (@edit[:new] != @edit[:current])
render :update do |page|
page << javascript_prologue
page.replace_html("main_div",
:partial => "vm_common/form") if %w(allright left right).include?(params[:button])
page << javascript_for_miq_button_visibility(changed) if changed
page << "miqSparkle(false);"
end
end
def edit_vm
return unless load_edit("vm_edit__#{params[:id]}")
# reset @explorer if coming from explorer views
@explorer = true if @edit[:explorer]
get_form_vars
case params[:button]
when "cancel"
if @edit[:explorer]
add_flash(_("Edit of %{model} \"%{name}\" was cancelled by the user") % {:model => ui_lookup(:model => @record.class.base_model.name), :name => @record.name})
@record = @sb[:action] = nil
replace_right_cell
else
add_flash(_("Edit of %{model} \"%{name}\" was cancelled by the user") % {:model => ui_lookup(:model => "Vm"), :name => @record.name})
session[:flash_msgs] = @flash_array.dup
javascript_redirect previous_breadcrumb_url
end
when "save"
if @edit[:new][:parent] != -1 && @edit[:new][:kids].invert.include?(@edit[:new][:parent]) # Check if parent is a kid, if selected
add_flash(_("Parent VM can not be one of the child VMs"), :error)
@changed = session[:changed] = (@edit[:new] != @edit[:current])
build_edit_screen
if @edit[:explorer]
replace_right_cell
else
render :action => "edit"
end
else
current = @record.parents.length == 0 ? -1 : @record.parents.first.id # get current parent id
chosen = @edit[:new][:parent].to_i # get the chosen parent id
@record.custom_1 = @edit[:new][:custom_1]
@record.description = @edit[:new][:description] # add vm description
if current != chosen
@record.remove_parent(@record.parents.first) unless current == -1 # Remove existing parent, if there is one
@record.set_parent(VmOrTemplate.find(chosen)) unless chosen == -1 # Set new parent, if one was chosen
end
vms = @record.children # Get the VM's child VMs
kids = @edit[:new][:kids].invert # Get the VM ids from the kids list box
audit = {:event => "vm_genealogy_change", :target_id => @record.id, :target_class => @record.class.base_class.name, :userid => session[:userid]}
begin
@record.save!
vms.each { |v| @record.remove_child(v) unless kids.include?(v.id) } # Remove any VMs no longer in the kids list box
kids.each_key { |k| @record.set_child(VmOrTemplate.find(k)) } # Add all VMs in kids hash, dups will not be re-added
rescue => bang
add_flash(_("Error during '%{name} update': %{message}") % {:name => @record.class.base_model.name,
:message => bang.message}, :error)
AuditEvent.failure(audit.merge(:message => "[#{@record.name} -- #{@record.location}] Update returned: #{bang}"))
else
flash = _("%{model} \"%{name}\" was saved") % {:model => ui_lookup(:model => @record.class.base_model.name), :name => @record.name}
AuditEvent.success(build_saved_vm_audit(@record))
end
params[:id] = @record.id.to_s # reset id in params for show
@record = nil
add_flash(flash)
if @edit[:explorer]
@sb[:action] = nil
replace_right_cell
else
session[:flash_msgs] = @flash_array.dup
javascript_redirect previous_breadcrumb_url
end
end
when "reset"
edit
add_flash(_("All changes have been reset"), :warning)
session[:flash_msgs] = @flash_array.dup
get_vm_child_selection if params["right.x"] || params["left.x"] || params["allright.x"]
@changed = session[:changed] = false
build_edit_screen
if @edit[:explorer]
replace_right_cell
else
javascript_redirect :action => "edit", :controller => "vm", :id => params[:id]
end
else
@changed = session[:changed] = (@edit[:new] != @edit[:current])
build_edit_screen
if @edit[:explorer]
replace_right_cell
else
render :action => "edit"
end
end
end
def set_checked_items
session[:checked_items] = []
if params[:all_checked]
ids = params[:all_checked].split(',')
ids.each do |id|
id = node_id(id)
session[:checked_items].push(id) unless session[:checked_items].include?(id)
end
end
@lastaction = "set_checked_items"
head :ok
end
def scan_history
@vm = @record = identify_record(params[:id], VmOrTemplate)
@scan_history = ScanHistory.find_by_vm_or_template_id(@record.id)
@listicon = "scan_history"
@showtype = "scan_history"
@lastaction = "scan_history"
@gtl_url = "/scan_history"
@no_checkboxes = true
@showlinks = true
@view, @pages = get_view(ScanHistory, :parent => @record) # Get the records (into a view) and the paginator
@current_page = @pages[:current] unless @pages.nil? # save the current page number
if @scan_history.nil?
drop_breadcrumb(:name => @record.name + _(" (Analysis History)"), :url => "/vm/#{@record.id}")
else
drop_breadcrumb(:name => @record.name + _(" (Analysis History)"),
:url => "/vm/scan_history/#{@scan_history.vm_or_template_id}")
end
if pagination_request?
render :update do |page|
page << javascript_prologue
page.replace_html("gtl_div", :partial => "layouts/gtl")
page << "miqSparkle(false);" # Need to turn off sparkle in case original ajax element gets replaced
end
else
if @explorer || request.xml_http_request? # Is this an Ajax request?
@sb[:action] = params[:action]
@refresh_partial = "layouts/#{@showtype}"
replace_right_cell
else
render :action => 'show'
end
end
end
def scan_histories
@vm = @record = identify_record(params[:id], VmOrTemplate)
@explorer = true if request.xml_http_request? # Ajax request means in explorer
@scan_history = ScanHistory.find_by_vm_or_template_id(@record.id)
if @scan_history.nil?
redirect_to :action => "scan_history", :flash_msg => _("Error: Record no longer exists in the database"), :flash_error => true
return
end
@lastaction = "scan_histories"
@sb[:action] = params[:action]
if !params[:show].nil? || !params[:x_show].nil?
id = params[:show] ? params[:show] : params[:x_show]
@item = ScanHistory.find(from_cid(id))
drop_breadcrumb(:name => time_ago_in_words(@item.started_on.in_time_zone(Time.zone)).titleize, :url => "/vm/scan_history/#{@scan_history.vm_or_template_id}?show=#{@item.id}")
@view = get_db_view(ScanHistory) # Instantiate the MIQ Report view object
show_item
else
drop_breadcrumb({:name => time_ago_in_words(@scan_history.started_on.in_time_zone(Time.zone)).titleize, :url => "/vm/show/#{@scan_history.vm_or_template_id}"}, true)
@listicon = "scan_history"
show_details(ScanHistory)
end
end
def parent_folder_id(vm)
if vm.orphaned
"xx-orph"
elsif vm.archived
"xx-arch"
elsif vm.cloud && vm.template
TreeBuilder.build_node_cid(vm.ems_id, 'ExtManagementSystem')
elsif vm.cloud && vm.availability_zone_id.nil?
TreeBuilder.build_node_cid(vm.ems_id, 'ExtManagementSystem')
elsif vm.cloud
TreeBuilder.build_node_cid(vm.availability_zone_id, 'AvailabilityZone')
elsif (blue_folder = vm.parent_blue_folder) && !blue_folder.hidden
TreeBuilder.build_node_cid(blue_folder.id, 'EmsFolder')
elsif vm.ems_id # has no folder parent but is in the tree
if vm.parent_datacenter
TreeBuilder.build_node_cid(vm.parent_datacenter.id, 'Datacenter')
else
TreeBuilder.build_node_cid(vm.ems_id, 'ExtManagementSystem')
end
else
nil # no selection if VmOrTemplate has no parent
end
end
# Tree node selected in explorer
def tree_select
@explorer = true
@lastaction = "explorer"
@sb[:action] = nil
# Need to see if record is unauthorized if it's a VM node
@nodetype, id = parse_nodetype_and_id(params[:id])
@vm = @record = identify_record(id, VmOrTemplate) if ["Vm", "MiqTemplate"].include?(TreeBuilder.get_model_for_prefix(@nodetype)) && !@record
# Handle filtered tree nodes
if x_active_tree.to_s =~ /_filter_tree$/ && # FIXME: create some property on trees for this
!["Vm", "MiqTemplate"].include?(TreeBuilder.get_model_for_prefix(@nodetype))
search_id = @nodetype == "root" ? 0 : from_cid(id)
adv_search_build(vm_model_from_active_tree(x_active_tree))
session[:edit] = @edit # Set because next method will restore @edit from session
listnav_search_selected(search_id) unless params.key?(:search_text) # Clear or set the adv search filter
if @edit[:adv_search_applied] &&
MiqExpression.quick_search?(@edit[:adv_search_applied][:exp]) &&
%w(reload tree_select).include?(params[:action])
self.x_node = params[:id]
quick_search_show
return
end
end
unless @unauthorized
self.x_node = if vm_selected && hide_vms
parent_folder_id(@vm)
else
params[:id]
end
replace_right_cell
else
add_flash(_("User is not authorized to view %{model} \"%{name}\"") %
{:model => ui_lookup(:model => @record.class.base_model.to_s), :name => @record.name},
:error) unless flash_errors?
javascript_flash(:spinner_off => true, :activate_node => {:tree => x_active_tree.to_s, :node => x_node})
end
end
private
# Check for parent nodes missing from vandt tree and return them if any
def open_parent_nodes(record)
add_nodes = nil
existing_node = nil # Init var
if record.orphaned? || record.archived?
parents = [{:type => "x", :id => (record.orphaned ? "orph" : "arch")}]
else
if x_active_tree == :instances_tree
parents = record.kind_of?(ManageIQ::Providers::CloudManager::Vm) && record.availability_zone ? [record.availability_zone] : [record.ext_management_system]
else