Skip to content

Commit

Permalink
Merge pull request #7557 from h-kataria/split_policy_log
Browse files Browse the repository at this point in the history
Split policy log
  • Loading branch information
gtanzillo authored Jan 15, 2021
2 parents bb2e334 + 3f18a60 commit a0f069b
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 99 deletions.
6 changes: 0 additions & 6 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1614,9 +1614,6 @@ def get_global_session_data
# Set @edit key default for the expression editor to use
@expkey = session[:expkey] || :expression

# Get server hash, if it is in the session for supported controllers
@server_options = session[:server_options] if %w[configuration support].include?(controller_name)

# Get timelines hash, if it is in the session for the running controller
@tl_options = tl_session_data

Expand Down Expand Up @@ -1668,9 +1665,6 @@ def set_global_session_data
session[:miq_exists_mode] = @exists_mode unless @exists_mode.nil?
session[:last_trans_time] = Time.now

# Set server hash, if @server_options is present
session[:server_options] = @server_options

# Set timelines hash, if it is in the session for the running controller
set_tl_session_data

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ def session_reset

# Clear instance vars that end up in the session
@sb = @edit = @view = @settings = @lastaction = @perf_options = @assign = nil
@server_options = @pp_choices = @panels = @breadcrumbs = nil
@pp_choices = @panels = @breadcrumbs = nil
end

# Initialize session hash variables for the logged in user
Expand Down
44 changes: 0 additions & 44 deletions app/controllers/miq_policy_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ def index
def button
@edit = session[:edit] # Restore @edit for adv search box
@refresh_div = "main_div" # Default div for button.rjs to refresh
if params[:pressed] == "refresh_log"
refresh_log
return
end

unless @refresh_partial # if no button handler ran, show not implemented msg
add_flash(_("Button not yet implemented"), :error)
Expand Down Expand Up @@ -105,41 +101,6 @@ def search
end
end

def log
assert_privileges('policy_log')
@breadcrumbs = []
@log = $policy_log.contents(nil, 1000)
add_flash(_("Logs for this %{product} Server are not available for viewing") % {:product => Vmdb::Appliance.PRODUCT_NAME}, :warning) if @log.blank?
@lastaction = "policy_logs"
@layout = "miq_policy_logs"
@msg_title = "Policy"
@download_action = "fetch_log"
@server_options ||= {}
@server_options[:server_id] ||= MiqServer.my_server.id
@server = MiqServer.my_server
drop_breadcrumb(:name => _("Log"), :url => "/miq_ae_policy/log")
render :action => "show"
end

def refresh_log
assert_privileges('policy_log')
@log = $policy_log.contents(nil, 1000)
@server = MiqServer.my_server
add_flash(_("Logs for this %{product} Server are not available for viewing") % {:product => Vmdb::Appliance.PRODUCT_NAME}, :warning) if @log.blank?
replace_main_div(:partial => "layouts/log_viewer")
end

# Send the log in text format
def fetch_log
assert_privileges('policy_log')
disable_client_cache
send_data($policy_log.contents(nil, nil),
:filename => "policy.log")
AuditEvent.success(:userid => session[:userid],
:event => "download_policy_log",
:message => "Policy log downloaded")
end

private

# Get all info for the node about to be displayed
Expand Down Expand Up @@ -398,14 +359,12 @@ def get_session_data
@lastaction = session[:miq_policy_lastaction]
@display = session[:miq_policy_display]
@current_page = session[:miq_policy_current_page]
@server_options = session[:server_options] if session[:server_options]
end

def set_session_data
super
session[:layout] = @layout
session[:miq_policy_current_page] = @current_page
session[:server_options] = @server_options
end

def features
Expand All @@ -425,14 +384,11 @@ def breadcrumbs_options
{:title => _("Control")},
menu_breadcrumb,
].compact,
:not_tree => %w[log].include?(action_name),
:record_title => :description,
}
end

def menu_breadcrumb
return nil if %w[log].include?(action_name)

{:title => _('Explorer')}
end

Expand Down
90 changes: 90 additions & 0 deletions app/controllers/miq_policy_log_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
class MiqPolicyLogController < ApplicationController
before_action :check_privileges
before_action :get_session_data
after_action :cleanup_action
after_action :set_session_data

include Mixins::GenericSessionMixin
include Mixins::BreadcrumbsMixin

def index
assert_privileges('policy_log')
flash_to_session
assert_privileges('policy_log')
@breadcrumbs = []
@log = $policy_log.contents(nil, 1000)
add_flash(_("Logs for this %{product} Server are not available for viewing") % {:product => Vmdb::Appliance.PRODUCT_NAME}, :warning) if @log.blank?
@lastaction = "policy_logs"
@layout = "miq_policy_logs"
@msg_title = "Policy"
drop_breadcrumb(:name => _("Log"), :url => "/miq_ae_policy/log")
end

# handle buttons pressed on the button bar
def button
@edit = session[:edit] # Restore @edit for adv search box
@refresh_div = "main_div" # Default div for button.rjs to refresh
if params[:pressed] == "refresh_log"
refresh_log
return
end

unless @refresh_partial # 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"
end
end

def refresh_log
assert_privileges('policy_log')
@log = $policy_log.contents(nil, 1000)
@server = MiqServer.my_server
add_flash(_("Logs for this %{product} Server are not available for viewing") % {:product => Vmdb::Appliance.PRODUCT_NAME}, :warning) if @log.blank?
replace_main_div(:partial => "layouts/log_viewer")
end

# Send the log in text format
def fetch_log
assert_privileges('policy_log')
disable_client_cache
send_data($policy_log.contents(nil, nil),
:filename => "policy.log")
AuditEvent.success(:userid => session[:userid],
:event => "download_policy_log",
:message => "Policy log downloaded")
end

def self.table_name
@table_name = "log"
end

private

def get_session_data
@title = _("Log")
@layout = "miq_policy_log"
@lastaction = session[:miq_policy_log_lastaction]
end

def set_session_data
super
session[:layout] = @layout
session[:miq_policy_log_lastaction] = @lastaction
end

def breadcrumbs_options
{
:breadcrumbs => [
{:title => _("Control")},
menu_breadcrumb,
].compact,
}
end

def menu_breadcrumb
{:title => _('Log')}
end

menu_section :con
end
2 changes: 1 addition & 1 deletion app/presenters/menu/default_menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def control_menu_section
Menu::Item.new('miq_alert', N_('Alerts'), 'miq_alert', {:feature => 'miq_alert', :any => true}, '/miq_alert/explorer'),
Menu::Item.new('miq_policy_rsop', N_('Simulation'), 'policy_simulation', {:feature => 'policy_simulation'}, '/miq_policy_rsop'),
Menu::Item.new('miq_policy_export', N_('Import / Export'), 'policy_import_export', {:feature => 'policy_import_export'}, '/miq_policy/export'),
Menu::Item.new('miq_policy_logs', N_('Log'), 'policy_log', {:feature => 'policy_log'}, '/miq_policy/log')
Menu::Item.new('miq_policy_logs', N_('Log'), 'policy_log', {:feature => 'policy_log'}, '/miq_policy_log')
])
end

Expand Down
3 changes: 2 additions & 1 deletion app/views/layouts/_log_viewer.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- legend_text ||= _("Last 1000 lines from server %{server_name} [%{server_id}] in zone %{zone_name}") % {:server_name => @server.name, :server_id => @server_options[:server_id], :zone_name => @server.my_zone}
- server = MiqServer.my_server
- legend_text ||= _("Last 1000 lines from server %{server_name} [%{server_id}] in zone %{zone_name}") % {:server_name => server.name, :server_id => MiqServer.my_server.id, :zone_name => server.my_zone}
= render :partial => "layouts/flash_msg"
%h3= legend_text

Expand Down
File renamed without changes.
12 changes: 9 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2089,8 +2089,6 @@
:miq_policy => {
:get => %w(
explorer
fetch_log
log
),
:post => %w(
button
Expand All @@ -2102,13 +2100,21 @@
reload
tree_autoload
tree_select
wait_for_task
) +
adv_search_post +
exp_post +
x_post
},

:miq_policy_log => {
:get => %w(
fetch_log
),
:post => %w(
button
)
},

:miq_policy_rsop => {
:post => %w(
rsop
Expand Down
3 changes: 3 additions & 0 deletions spec/config/routes.pending.yml
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,9 @@ MiqPolicyExportController:
MiqPolicyRsopController:
- report_data
- wait_for_task
MiqPolicyLogController:
- report_data
- wait_for_task
MiqPolicySetController:
- form_field_changed
- index
Expand Down
31 changes: 0 additions & 31 deletions spec/controllers/miq_policy_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,13 @@
let(:lastaction) { 'lastaction' }
let(:display) { 'display' }
let(:current_page) { 'current_page' }
let(:server_options) { 'server options' }
let(:layout) { 'miq_policy' }

describe '#get_session_data' do
it "Sets variables correctly" do
allow(controller).to receive(:session).and_return(:miq_policy_lastaction => lastaction,
:miq_policy_display => display,
:miq_policy_current_page => current_page,
:server_options => server_options,
:layout => layout)
allow(controller).to receive(:alert_build_pulldowns).and_return(nil)
controller.send(:get_session_data)
Expand All @@ -147,7 +145,6 @@
expect(controller.instance_variable_get(:@lastaction)).to eq(lastaction)
expect(controller.instance_variable_get(:@display)).to eq(display)
expect(controller.instance_variable_get(:@current_page)).to eq(current_page)
expect(controller.instance_variable_get(:@server_options)).to eq(server_options)
end
end

Expand All @@ -157,38 +154,16 @@
controller.instance_variable_set(:@display, display)
controller.instance_variable_set(:@current_page, current_page)
controller.instance_variable_set(:@layout, layout)
controller.instance_variable_set(:@server_options, server_options)
controller.send(:set_session_data)

expect(controller.session[:miq_policy_lastaction]).to eq(lastaction)
expect(controller.session[:miq_policy_display]).to eq(display)
expect(controller.session[:miq_policy_current_page]).to eq(current_page)
expect(controller.session[:layout]).to eq(layout)
expect(controller.session[:server_options]).to eq(server_options)
end
end
end

# context 'removing conditions' do
# let(:condition) { FactoryBot.create(:condition) }
# let(:policy) { FactoryBot.create(:miq_policy, :name => "test_policy", :conditions => [condition]) }
#
# before do
# login_as FactoryBot.create(:user, :features => 'condition_remove')
# controller.params = {:policy_id => policy.id, :id => condition.id}
# controller.instance_variable_set(:@sb, {})
# allow(controller).to receive(:x_node).and_return("pp_pp-1r36_p-#{policy.id}_co-#{condition.id}")
# end
#
# it 'removes condition successfully' do
# expect(controller).to receive(:replace_right_cell)
# controller.send(:condition_remove)
# policy.reload
# expect(assigns(:flash_array).first[:message]).to include("has been removed from Policy")
# expect(policy.conditions).to eq([])
# end
# end

describe "breadcrumbs" do
before { EvmSpecHelper.local_miq_server }

Expand All @@ -197,11 +172,5 @@

expect(controller.data_for_breadcrumbs.pluck(:title)[1]).to eq("Explorer")
end

it "shows 'log' on log screen" do
get :log

expect(controller.data_for_breadcrumbs.pluck(:title)[1]).to eq("Log")
end
end
end
43 changes: 43 additions & 0 deletions spec/controllers/miq_policy_log_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
describe MiqPolicyLogController do
before do
stub_user(:features => :all)
end

context "GenericSessionMixin" do
let(:lastaction) { 'lastaction' }
let(:layout) { 'miq_policy_log' }

describe '#get_session_data' do
it "Sets variables correctly" do
allow(controller).to receive(:session).and_return(:miq_policy_log_lastaction => lastaction,
:layout => layout)
controller.send(:get_session_data)

expect(controller.instance_variable_get(:@title)).to eq("Log")
expect(controller.instance_variable_get(:@layout)).to eq(layout)
expect(controller.instance_variable_get(:@lastaction)).to eq(lastaction)
end
end

describe '#set_session_data' do
it "Sets session correctly" do
controller.instance_variable_set(:@lastaction, lastaction)
controller.instance_variable_set(:@layout, layout)
controller.send(:set_session_data)

expect(controller.session[:miq_policy_log_lastaction]).to eq(lastaction)
expect(controller.session[:layout]).to eq(layout)
end
end
end

describe "breadcrumbs" do
before { EvmSpecHelper.local_miq_server }

it "shows 'log' on log screen" do
get :index

expect(controller.data_for_breadcrumbs.pluck(:title)[1]).to eq("Log")
end
end
end
Loading

0 comments on commit a0f069b

Please sign in to comment.