Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert vm and template edit form #8072

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions app/controllers/miq_template_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "rexml/document"

class MiqTemplateController < ApplicationController
include VmCommon
include Mixins::GenericListMixin
Expand Down Expand Up @@ -47,7 +45,6 @@ def set_session_data
end

has_custom_buttons
feature_for_actions 'miq_template_edit', :edit_vm
feature_for_actions 'miq_template_timeline', :tl_chooser
feature_for_actions 'miq_template_perf', :perf_top_chart
end
1 change: 0 additions & 1 deletion app/controllers/vm_cloud_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ def breadcrumbs_options
menu_section :clo
feature_for_actions %w[instances_filter_accord images_filter_accord], *ADV_SEARCH_ACTIONS
feature_for_actions 'vm_show', :groups, :users, :patches
feature_for_actions ['instance_edit', 'image_edit'], :edit_vm
feature_for_actions ['instance_protect', 'image_protect'], :protect
feature_for_actions ['instance_timeline', 'image_timeline'], :tl_chooser
feature_for_actions ['instance_perf', 'image_perf'], :perf_top_chart
Expand Down
235 changes: 8 additions & 227 deletions app/controllers/vm_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def textual_summary_flash_list
private :textual_summary_flash_list
helper_method :textual_group_list
helper_method :textual_summary_flash_list
helper_method :parent_choices_with_no_parent_choice
helper_method :select_check?
helper_method :disable_check?
end
Expand Down Expand Up @@ -578,126 +577,23 @@ def remove_service

def edit
@record = find_record_with_rbac(VmOrTemplate, params[:id]) # Set the VM object
set_form_vars
build_edit_screen
session[:changed] = false

# reset @explorer if coming from explorer views
@edit ||= {}
@edit[:explorer] = true if params[:action] == "x_button" || session.fetch_path(:edit, :explorer)
@explorer = true if @edit[:explorer]

@active_tab = "edit"
@tab_id = @record.id.to_s
@tabs = [['edit', _('Information')]]
drop_breadcrumb(:name => _("Edit VM '%{name}'") % {:name => @record.name}, :url => "/vm/edit") unless @explorer

@refresh_partial = "vm_common/form"
end

# FIXME: these match toolbar button names/features
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)
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
flash_to_session(_("Edit of VM and Instance \"%{name}\" was cancelled by the user") % {:name => @record.name})
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.empty? ? -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
flash_to_session
javascript_redirect(previous_breadcrumb_url)
end
end
when "reset"
edit
flash_to_session(_("All changes have been reset"), :warning)
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]
Expand Down Expand Up @@ -827,22 +723,6 @@ def tree_select
replace_right_cell
end

def parent_choices(ids)
@parent_choices = {}
@parent_choices[Digest::MD5.hexdigest(ids.inspect)] ||= begin
ems_ids = VmOrTemplate.where(:id => ids).distinct.pluck(:ems_id).compact
parent_item_scope = Rbac.filtered(VmOrTemplate.where(:ems_id => ems_ids).where.not(:id => ids).order(:name))

parent_item_scope.pluck(:name, :location, :id).each_with_object({}) do |vm, memo|
memo[vm[0] + " -- #{vm[1]}"] = vm[2]
end
end
end

def parent_choices_with_no_parent_choice
{'"no parent"' => -1}.merge(parent_choices(@record.id))
end

# Return vm_cloud or vm_infra based on selected record
def vm_or_instance(record)
if record
Expand Down Expand Up @@ -1229,33 +1109,6 @@ def get_host_for_vm(vm)
end
end

# Set form variables for edit
def set_form_vars
@edit = {}
@edit[:vm_id] = @record.id
@edit[:new] = {}
@edit[:current] = {}
@edit[:key] = "vm_edit__#{@record.id || "new"}"
@edit[:explorer] = true if params[:action] == "x_button" || session.fetch_path(:edit, :explorer)

@edit[:current][:custom_1] = @edit[:new][:custom_1] = @record.custom_1.to_s
@edit[:current][:description] = @edit[:new][:description] = @record.description.to_s
@edit[:new][:parent] = @record.parents.empty? ? -1 : @record.parents.first.id # Set the currently selected parent

vms = @record.children # Get the child VMs
@edit[:new][:kids] = {}
vms.each { |vm| @edit[:new][:kids][vm.name + " -- #{vm.location}"] = vm.id } # Build a hash for the kids list box

@edit[:choices] = {}

# Build a hash for the VMs to choose from, only if they have no parent
@edit[:choices] = parent_choices([@record.id] + vms.pluck(:id))

@edit[:current][:parent] = @edit[:new][:parent]
@edit[:current][:kids] = @edit[:new][:kids].dup
session[:edit] = @edit
end

def action_type(type, amount)
case type
when "advanced_settings"
Expand Down Expand Up @@ -1377,8 +1230,7 @@ def set_right_cell_vars(options = {})
locals = options[:dialog_locals]
when "edit"
partial = "vm_common/form"
header = _("Editing %{vm_or_template} \"%{name}\"") % {:name => name, :vm_or_template => ui_lookup(:table => table)}
action = "edit_vm"
header = _("Editing %{vm_or_template} \"%{name}\"") % {:name => name, :vm_or_template => model_for_vm(@record).display_name}
when 'chargeback'
partial = @refresh_partial
header = _('Chargeback preview for "%{vm_name}"') % { :vm_name => name }
Expand Down Expand Up @@ -1486,77 +1338,6 @@ def set_right_cell_vars(options = {})
return partial, action, header, locals
end

def get_vm_child_selection
if params["right.x"] || params[:button] == "right"
if params[:kids_chosen].nil?
add_flash(_("No VMs were selected to move right"), :error)
else
kids = @edit[:new][:kids].invert
params[:kids_chosen].each do |kc|
if @edit[:new][:kids].value?(kc.to_i)
@edit[:choices][kids[kc.to_i]] = kc.to_i
@edit[:new][:kids].delete(kids[kc.to_i])
end
end
end
elsif params["left.x"] || params[:button] == "left"
if params[:choices_chosen].nil?
add_flash(_("No VMs were selected to move left"), :error)
else
kids = @edit[:choices].invert
params[:choices_chosen].each do |cc|
if @edit[:choices].value?(cc.to_i)
@edit[:new][:kids][kids[cc.to_i]] = cc.to_i
@edit[:choices].delete(kids[cc.to_i])
end
end
end
elsif params["allright.x"] || params[:button] == "allright"
if @edit[:new][:kids].empty?
add_flash(_("No child VMs to move right, no action taken"), :error)
else
@edit[:new][:kids].each do |key, value|
@edit[:choices][key] = value
end
@edit[:new][:kids].clear
end
end
end

# Get variables from edit form
def get_form_vars
@record = VmOrTemplate.find_by(:id => @edit[:vm_id])
copy_params_if_present(@edit[:new], params, %i[custom_1 description name])
@edit[:new][:parent] = params[:chosen_parent].to_i if params[:chosen_parent]
# if coming from explorer
get_vm_child_selection if %w[allright left right].include?(params[:button])
end

# Build the audit object when a record is saved, including all of the changed fields
def build_saved_vm_audit(vm)
msg = "[#{vm.name} -- #{vm.location}] Record saved ("
event = "vm_genealogy_change"
i = 0
@edit[:new].each_key do |k|
next if @edit[:new][k] == @edit[:current][k]
msg += ", " if i.positive?
i += 1
if k == :kids
# if @edit[:new][k].is_a?(Hash)
msg = msg + k.to_s + ":[" + @edit[:current][k].keys.join(",") + "] to [" + @edit[:new][k].keys.join(",") + "]"
elsif k == :parent
parent_choices_invert = parent_choices_with_no_parent_choice.invert
current_parent_choice = parent_choices_invert[@edit[:current][k]]
new_parent_choice = parent_choices_invert[@edit[:new][k]]
msg = "#{msg}#{k}:[#{current_parent_choice}] to [#{new_parent_choice}]"
else
msg = msg + k.to_s + ":[" + @edit[:current][k].to_s + "] to [" + @edit[:new][k].to_s + "]"
end
end
msg += ")"
{:event => event, :target_id => vm.id, :target_class => vm.class.base_class.name, :userid => session[:userid], :message => msg}
end

def update_buttons(locals)
locals[:continue_button] = locals[:submit_button] = false
locals[:continue_button] = true if @edit[:buttons].include?(:continue)
Expand Down
1 change: 0 additions & 1 deletion app/controllers/vm_infra_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def breadcrumbs_options
menu_section :inf
feature_for_actions %w[vms_filter_accord templates_filter_accord], *ADV_SEARCH_ACTIONS
feature_for_actions 'vm_show', :groups, :users, :patches
feature_for_actions ['vm_edit', 'miq_template_edit'], :edit_vm
feature_for_actions ['vm_protect', 'miq_template_protect'], :protect
feature_for_actions ['vm_timeline', 'miq_template_timeline'], :tl_chooser
feature_for_actions ['vm_perf', 'miq_template_perf'], :perf_top_chart
Expand Down
1 change: 0 additions & 1 deletion app/controllers/vm_or_template_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def breadcrumbs_options
menu_section :svc
has_custom_buttons
feature_for_actions ['vm_show', 'miq_template_show'], :groups, :users, :patches
feature_for_actions ['vm_edit', 'miq_template_edit', 'instance_edit', 'image_edit'], :edit_vm
feature_for_actions ['vm_protect', 'miq_template_protect'], :protect
feature_for_actions ['vm_perf', 'miq_template_perf', 'instance_perf', 'image_perf'], :perf_top_chart
feature_for_actions ['vm_timeline', 'miq_template_timeline', 'instance_timeline', 'image_timeline'], :tl_chooser
Expand Down
Loading