Skip to content

Commit

Permalink
Control Explorer split part ManageIQ#5
Browse files Browse the repository at this point in the history
In this commit, Alert Profiles accordion has been moved into it's own explorer, as part of Control explorer split. This is a pre-work for Control explorer de-explorization.
Moved some common methods to `policy_mixin`.

ManageIQ#6819
  • Loading branch information
h-kataria committed Aug 24, 2020
1 parent 78aabde commit dea857a
Show file tree
Hide file tree
Showing 15 changed files with 809 additions and 240 deletions.
539 changes: 539 additions & 0 deletions app/controllers/miq_alert_set_controller.rb

Large diffs are not rendered by default.

246 changes: 22 additions & 224 deletions app/controllers/miq_policy_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ def button
'alert_edit' => :alert_edit,
'alert_copy' => :alert_edit,
'alert_new' => :alert_edit,
'alert_profile_assign' => :alert_profile_assign,
'alert_profile_edit' => :alert_profile_edit,
'alert_profile_new' => :alert_profile_edit,
'miq_event_edit' => :miq_event_edit,
'policy_copy' => :policy_copy,
'policy_edit' => :policy_edit,
Expand Down Expand Up @@ -266,7 +263,7 @@ def search
case x_active_tree
when "profile", "action", "alert"
replace_right_cell(:nodetype => x_node)
when "policy", "alert_profile"
when "policy"
replace_right_cell(:nodetype => "xx")
end
end
Expand Down Expand Up @@ -372,13 +369,11 @@ def get_node_info(treenodeid, show_list = true)
policy_get_info(MiqPolicy.find(nodeid))
when "a", "ta", "fa" # Action or True/False Action
action_get_info(MiqAction.find(nodeid))
when "ap" # Alert Profile
alert_profile_get_info(MiqAlertSet.find(nodeid))
when "al" # Alert
alert_get_info(MiqAlert.find(nodeid))
end
@show_adv_search = (@nodetype == "xx" && !@folders) ||
(@nodetype == "root" && !%i[alert_profile_tree policy_tree].include?(x_active_tree))
(@nodetype == "root" && !%i[policy_tree].include?(x_active_tree))
{:view => @view, :pages => @pages}
end

Expand All @@ -389,8 +384,6 @@ def get_root_node_info
policy_get_all_folders
when :action_tree
action_get_all
when :alert_profile_tree
alert_profile_get_all_folders
when :alert_tree
alert_get_all
end
Expand All @@ -403,7 +396,7 @@ def replace_right_cell(options = {})
replace_trees = Array(replace_trees)
@explorer = true

trees = build_replaced_trees(replace_trees, %i[policy action alert_profile alert])
trees = build_replaced_trees(replace_trees, %i[policy action alert])

c_tb = build_toolbar(center_toolbar_filename)

Expand All @@ -420,8 +413,6 @@ def replace_right_cell(options = {})
self.x_node = @new_policy_node if @new_policy_node
when :action
self.x_node = @new_action_node if @new_action_node
when :alert_profile
self.x_node = @new_alert_profile_node if @new_alert_profile_node
when :alert
self.x_node = @new_alert_node if @new_alert_node
else
Expand All @@ -441,13 +432,12 @@ def replace_right_cell(options = {})
case x_active_tree
when :policy_tree then ['policy_folders', _('Policies')]
when :action_tree then ['action_list', _('Actions')]
when :alert_profile_tree then ['alert_profile_folders', _('Alert Profiles')]
when :alert_tree then ['alert_list', _('Alerts')]
end

presenter.update(:main_div, r[:partial => partial_name])
right_cell_text = _("All %{models}") % {:models => model}
right_cell_text += _(" (Names with \"%{search_text}\")") % {:search_text => @search_text} if @search_text.present? && %w[alert_profile_tree policy_tree].exclude?(x_active_tree.to_s)
right_cell_text += _(" (Names with \"%{search_text}\")") % {:search_text => @search_text} if @search_text.present? && %w[policy_tree].exclude?(x_active_tree.to_s)
when 'xx'
presenter.update(
:main_div,
Expand All @@ -466,9 +456,6 @@ def replace_right_cell(options = {})
_("%{typ} Policies") % {:typ => mode.capitalize}
end
r[:partial => 'policy_folders']
elsif @alert_profiles
right_cell_text = _("All %{typ} Alert Profiles") % {:typ => ui_lookup(:model => @sb[:folder].try(:camelize))}
r[:partial => 'alert_profile_list']
end
)
right_cell_text += _(" (Names with \"%{search_text}\")") % {:search_text => @search_text} if @search_text.present? && !@folders
Expand Down Expand Up @@ -505,16 +492,6 @@ def replace_right_cell(options = {})
msg = @edit ? _("Editing Action \"%{name}\"") : _("Action \"%{name}\"")
msg % {:name => @action.description}
end
when 'ap'
presenter.update(:main_div, r[:partial => 'alert_profile_details', :locals => {:read_only => true}])
right_cell_text = if @alert_profile.id.blank?
_("Adding a new Alert Profile")
elsif @edit
_("Editing %{model} Alert Profile \"%{name}\"") % {:name => @alert_profile.description,
:model => ui_lookup(:model => @edit[:new][:mode])}
else
_("Alert Profile \"%{name}\"") % {:name => @alert_profile.description}
end
when 'al'
presenter.update(:main_div, r[:partial => 'alert_details', :locals => {:read_only => true}])
right_cell_text = if @alert.id.blank?
Expand Down Expand Up @@ -564,205 +541,32 @@ def replace_right_cell(options = {})
render :json => presenter.for_render
end

def handle_selection_buttons_left(members, members_chosen, choices, _choices_chosen)
if params[members_chosen].nil?
add_flash(_("No %{members} were selected to move left") % {:members => members.to_s.split("_").first.titleize},
:error)
return
end

if @edit[:event_id]
# Handle Actions for an Event
params[members_chosen].each do |mc|
idx = nil
# Find the index of the new members array
@edit[:new][members].each_with_index { |mem, i| idx = mem[-1] == mc.to_i ? i : idx }
next if idx.nil?

desc = @edit[:new][members][idx][0].slice(4..-1) # Remove (x) prefix from the chosen item
@edit[choices][desc] = mc.to_i # Add item back into the choices hash
@edit[:new][members].delete_at(idx) # Remove item from the array
end
else
mems = @edit[:new][members].invert
params[members_chosen].each do |mc|
@edit[choices][mems[mc.to_i]] = mc.to_i
@edit[:new][members].delete(mems[mc.to_i])
end
end
end

def handle_selection_buttons_right(members, _members_chosen, choices, choices_chosen)
if params[choices_chosen].nil?
add_flash(_("No %{member} were selected to move right") %
{:member => members.to_s.split("_").first.titleize}, :error)
return
end

mems = @edit[choices].invert
if @edit[:event_id]
# Handle Actions for an Event
params[choices_chosen].each do |mc|
# Add selection to chosen members array, default to synch = true
@edit[:new][members].push(["(S) " + mems[mc.to_i], true, mc.to_i])
@edit[choices].delete(mems[mc.to_i]) # Remove from the choices hash
end
else
params[choices_chosen].each do |mc|
@edit[:new][members][mems[mc.to_i]] = mc.to_i
@edit[choices].delete(mems[mc.to_i])
end
end
end

def handle_selection_buttons_allleft(members, _members_chosen, choices, _choices_chosen)
if @edit[:new][members].empty?
add_flash(_("No %{member} were selected to move left") %
{:member => members.to_s.split("_").first.titleize}, :error)
return
end

if @edit[:event_id]
# Handle Actions for an Event
@edit[:new][members].each do |m|
# Put description/id of each chosen member back into choices hash
@edit[choices][m.first.slice(4..-1)] = m.last
end
else
@edit[:new][members].each do |key, value|
@edit[choices][key] = value
end
end
@edit[:new][members].clear
end

def handle_selection_buttons_sortout_selected(members_chosen)
if params[:button].starts_with?("true")
@true_selected = params[members_chosen][0].to_i
else
@false_selected = params[members_chosen][0].to_i
end
end

def handle_selection_buttons_up_down(members, members_chosen, _choices, _choices_chosen, up)
if params[members_chosen].nil? || params[members_chosen].length != 1
message = if up
_("Select only one or consecutive %{member} to move up")
else
_("Select only one or consecutive %{member} to move down")
end

add_flash(message % {:member => members.to_s.split("_").first.singularize.titleize}, :error)
return
end

handle_selection_buttons_sortout_selected(members_chosen)
idx = nil
mc = params[members_chosen][0]
# Find item index in new members array
@edit[:new][members].each_with_index { |mem, i| idx = mem[-1] == mc.to_i ? i : idx }

return if idx.nil?
return if up && idx.zero? # cannot go higher
return if !up && idx >= @edit[:new][members].length - 1 # canot go lower

pulled = @edit[:new][members].delete_at(idx)
delta = up ? -1 : 1
@edit[:new][members].insert(idx + delta, pulled)
end

def handle_selection_buttons_sync_async(members, members_chosen, _choices, _choices_chosen, sync)
if params[members_chosen].nil?
msg = if sync
_("No %{member} selected to set to Synchronous")
else
_("No %{member} selected to set to Asynchronous")
end
add_flash(msg % {:member => members.to_s.split("_").first.titleize}, :error)
return
end

handle_selection_buttons_sortout_selected(members_chosen)

params[members_chosen].each do |mc|
idx = nil
# Find the index in the new members array
@edit[:new][members].each_with_index { |mem, i| idx = mem[-1] == mc.to_i ? i : idx }
next if idx.nil?

letter = sync ? 'S' : 'A'
@edit[:new][members][idx][0] = "(#{letter}) " + @edit[:new][members][idx][0].slice(4..-1) # Change prefix to (A)
@edit[:new][members][idx][1] = sync # true for sync
end
end

# Handle the middle buttons on the add/edit forms
# pass in member list symbols (i.e. :policies)
def handle_selection_buttons(members,
members_chosen = :members_chosen,
choices = :choices,
choices_chosen = :choices_chosen)
if params[:button].ends_with?("_left")
handle_selection_buttons_left(members, members_chosen, choices, choices_chosen)
elsif params[:button].ends_with?("_right")
handle_selection_buttons_right(members, members_chosen, choices, choices_chosen)
elsif params[:button].ends_with?("_allleft")
handle_selection_buttons_allleft(members, members_chosen, choices, choices_chosen)
elsif params[:button].ends_with?("_up")
handle_selection_buttons_up_down(members, members_chosen, choices, choices_chosen, true)
elsif params[:button].ends_with?("_down")
handle_selection_buttons_up_down(members, members_chosen, choices, choices_chosen, false)
elsif params[:button].ends_with?("_sync")
handle_selection_buttons_sync_async(members, members_chosen, choices, choices_chosen, true)
elsif params[:button].ends_with?("_async")
handle_selection_buttons_sync_async(members, members_chosen, choices, choices_chosen, false)
end
end

def apply_search_filter(search_str, results)
if search_str.first == "*"
results.delete_if { |r| !r.description.downcase.ends_with?(search_str[1..-1].downcase) }
elsif search_str.last == "*"
results.delete_if { |r| !r.description.downcase.starts_with?(search_str[0..-2].downcase) }
else
results.delete_if { |r| !r.description.downcase.include?(search_str.downcase) }
end
end

# Get list of folder contents
def folder_get_info(folder_node)
nodetype, nodeid = folder_node.split("_")
@sb[:mode] = nil
@sb[:nodeid] = nil
@sb[:folder] = nodeid.nil? ? nodetype.split("-").last : nodeid
if x_active_tree == :policy_tree
if nodeid.nil? && %w[compliance control].include?(nodetype.split('-').last)
# level 1 - compliance & control
_, mode = nodetype.split('-')
@folders = UI_FOLDERS.collect do |model|
"#{model.name.titleize} #{mode.titleize}"
end
@right_cell_text = case mode
when 'compliance' then _('Compliance Policies')
when 'control' then _('Control Policies')
else _("%{typ} Policies") % {:typ => mode.titleize}
end
else
# level 2 - host, vm, etc. under compliance/control - OR deeper levels
@sb[:mode] = nodeid.split("-")[1]
@sb[:nodeid] = nodeid.split("-").last
@sb[:folder] = "#{nodeid.split("-")[1]}-#{nodeid.split("-")[2]}"
set_search_text
policy_get_all if folder_node.split("_").length <= 2
@right_cell_text = _("All %{typ} Policies") % {:typ => ui_lookup(:model => @sb[:nodeid].try(:camelize))}
@right_cell_div = "policy_list"
if nodeid.nil? && %w[compliance control].include?(nodetype.split('-').last)
# level 1 - compliance & control
_, mode = nodetype.split('-')
@folders = UI_FOLDERS.collect do |model|
"#{model.name.titleize} #{mode.titleize}"
end
elsif x_active_tree == :alert_profile_tree
@alert_profiles = MiqAlertSet.where(:mode => @sb[:folder]).sort_by { |as| as.description.downcase }
@right_cell_text = case mode
when 'compliance' then _('Compliance Policies')
when 'control' then _('Control Policies')
else _("%{typ} Policies") % {:typ => mode.titleize}
end
else
# level 2 - host, vm, etc. under compliance/control - OR deeper levels
@sb[:mode] = nodeid.split("-")[1]
@sb[:nodeid] = nodeid.split("-").last
@sb[:folder] = "#{nodeid.split("-")[1]}-#{nodeid.split("-")[2]}"
set_search_text
@alert_profiles = apply_search_filter(@search_text, @alert_profiles) if @search_text.present?
@right_cell_text = _("All %{typ} Alert Profiles") % {:typ => ui_lookup(:model => @sb[:folder].try(:camelize))}
@right_cell_div = "alert_profile_list"
policy_get_all if folder_node.split("_").length <= 2
@right_cell_text = _("All %{typ} Policies") % {:typ => ui_lookup(:model => @sb[:nodeid].try(:camelize))}
@right_cell_div = "policy_list"
end
end

Expand Down Expand Up @@ -871,12 +675,6 @@ def features
:role => "policy",
:role_any => true
},
{
:name => :alert_profile,
:title => _("Alert Profiles"),
:role => "alert_profile",
:role_any => true
},
{
:name => :alert,
:title => _("Alerts"),
Expand Down
Loading

0 comments on commit dea857a

Please sign in to comment.